mirror of
https://github.com/edubart/otclient.git
synced 2025-11-30 23:26:51 +01:00
improve containers
This commit is contained in:
@@ -14,5 +14,7 @@ Module
|
||||
require 'uitabbar'
|
||||
require 'uipopupmenu'
|
||||
require 'uiwindow'
|
||||
require 'uiitem'
|
||||
require 'tooltip/tooltip'
|
||||
require 'messagebox/messagebox'
|
||||
require 'messagebox/messagebox'
|
||||
|
||||
|
||||
6
modules/core_widgets/uiitem.lua
Normal file
6
modules/core_widgets/uiitem.lua
Normal file
@@ -0,0 +1,6 @@
|
||||
function UIItem:onMouseRelease(mousePosition, mouseButton)
|
||||
local item = self:getItem()
|
||||
if not item or not self:containsPoint(mousePosition) then return false end
|
||||
return Game.processMouseAction(mousePosition, mouseButton, nil, item, item, nil, item)
|
||||
end
|
||||
|
||||
@@ -7,11 +7,12 @@ Module
|
||||
dependencies:
|
||||
- game_healthbar
|
||||
- game_inventory
|
||||
- game_skills
|
||||
//- game_skills
|
||||
- game_textmessage
|
||||
- game_viplist
|
||||
- game_console
|
||||
- game_outfit
|
||||
- game_containers
|
||||
|
||||
onLoad: |
|
||||
require 'game'
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
|
||||
function Thing:isInsideContainer()
|
||||
local pos = self:getPos()
|
||||
return (pos and pos.x == 65535 and pos.y >= 64)
|
||||
end
|
||||
|
||||
function Thing:getContainerId()
|
||||
local pos = self:getPos()
|
||||
if not pos then return 0 end
|
||||
return pos.y - 64
|
||||
end
|
||||
|
||||
-- public functions
|
||||
function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing, useThing, creatureThing, multiUseThing)
|
||||
local keyboardModifiers = g_window.getKeyboardModifiers()
|
||||
@@ -18,7 +29,7 @@ function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing,
|
||||
if mouseButton == MouseLeftButton and selectedThing then
|
||||
Game.useWith(Game.getSelectedThing(), useThing)
|
||||
Game.setSelectedThing(nil)
|
||||
-- restore cursor
|
||||
restoreCursor()
|
||||
return true
|
||||
elseif keyboardModifiers == KeyboardNoModifier and mouseButton == MouseRightButton then
|
||||
Game.createThingMenu(menuPosition, lookThing, useThing, creatureThing)
|
||||
@@ -28,7 +39,11 @@ function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing,
|
||||
return true
|
||||
elseif useThing and keyboardModifiers == KeyboardCtrlModifier and (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
|
||||
if useThing:isContainer() then
|
||||
print "open"
|
||||
if useThing:isInsideContainer() then
|
||||
Game.open(useThing, useThing:getContainerId())
|
||||
else
|
||||
Game.open(useThing, Containers.getFreeContainerId())
|
||||
end
|
||||
elseif useThing:isMultiUse() then
|
||||
Game.setSelectedThing(useThing)
|
||||
setTargetCursor()
|
||||
@@ -50,12 +65,16 @@ function Game.processMouseAction(menuPosition, mouseButton, autoWalk, lookThing,
|
||||
if multiUseThing:asCreature() then
|
||||
Game.attack(multiUseThing:asCreature())
|
||||
elseif multiUseThing:isContainer() then
|
||||
print "open"
|
||||
if multiUseThing:isInsideContainer() then
|
||||
Game.open(multiUseThing, multiUseThing:getContainerId())
|
||||
else
|
||||
Game.open(multiUseThing, Containers.getFreeContainerId())
|
||||
end
|
||||
elseif multiUseThing:isMultiUse() then
|
||||
Game.setSelectedThing(multiUseThing)
|
||||
setTargetCursor()
|
||||
else
|
||||
Game.use(useThing)
|
||||
Game.use(multiUseThing)
|
||||
end
|
||||
return true
|
||||
elseif lookThing and keyboardModifiers == KeyboardShiftModifier and (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
|
||||
@@ -81,15 +100,17 @@ function Game.createThingMenu(menuPosition, lookThing, useThing, creatureThing)
|
||||
menu:addOption('Look', function() Game.look(lookThing) end)
|
||||
end
|
||||
|
||||
-- Open or Use, depending if thing is a container
|
||||
if useThing then
|
||||
if useThing:isContainer() then
|
||||
-- check for open in new window
|
||||
menu:addOption('Open', function() print('open') end)
|
||||
if useThing:isInsideContainer() then
|
||||
menu:addOption('Open', function() Game.open(useThing, useThing:getContainerId()) end)
|
||||
menu:addOption('Open in new window', function() Game.open(useThing, Containers.getFreeContainerId()) end)
|
||||
else
|
||||
menu:addOption('Open', function() Game.open(useThing, Containers.getFreeContainerId()) end)
|
||||
end
|
||||
else
|
||||
if useThing:isMultiUse() then
|
||||
setTargetCursor()
|
||||
menu:addOption('Use with ...', function() Game.setSelectedThing(useThing) end)
|
||||
menu:addOption('Use with ...', function() Game.setSelectedThing(useThing) setTargetCursor() end)
|
||||
else
|
||||
menu:addOption('Use', function() Game.use(useThing) end)
|
||||
end
|
||||
@@ -179,3 +200,4 @@ function Game.createThingMenu(menuPosition, lookThing, useThing, creatureThing)
|
||||
|
||||
menu:display(menuPosition)
|
||||
end
|
||||
|
||||
|
||||
13
modules/game_containers/container.otui
Normal file
13
modules/game_containers/container.otui
Normal file
@@ -0,0 +1,13 @@
|
||||
MiniWindow
|
||||
size: 200 186
|
||||
padding-top: 30
|
||||
padding-left: 10
|
||||
|
||||
layout:
|
||||
type: grid
|
||||
cell-size: 32 32
|
||||
cell-spacing: 5
|
||||
num-columns: 4
|
||||
num-lines: 5
|
||||
|
||||
|
||||
79
modules/game_containers/containers.lua
Normal file
79
modules/game_containers/containers.lua
Normal file
@@ -0,0 +1,79 @@
|
||||
Containers = {}
|
||||
|
||||
-- private variables
|
||||
local m_containers = {}
|
||||
|
||||
-- public functions
|
||||
function Containers.clean()
|
||||
m_containers = {}
|
||||
end
|
||||
|
||||
function Containers.getFreeContainerId()
|
||||
for i=0,15 do
|
||||
if not m_containers[i] then
|
||||
return i
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- hooked events
|
||||
function Containers.onContainerOpen(containerId, itemId, name, capacity, hasParent)
|
||||
local container = m_containers[containerId]
|
||||
if container then
|
||||
Game.gameRightPanel:removeChild(container)
|
||||
end
|
||||
|
||||
container = displayUI('container.otui', { parent = Game.gameRightPanel })
|
||||
container:setText(name)
|
||||
|
||||
-- set icon, itemid
|
||||
-- closebutton
|
||||
-- resize
|
||||
if hasParent then
|
||||
-- parent button
|
||||
end
|
||||
|
||||
container.itemCount = 0
|
||||
container.capacity = capacity
|
||||
|
||||
for i=1,capacity do
|
||||
local item = UIItem.create()
|
||||
item:setStyle('Item')
|
||||
container:addChild(item)
|
||||
end
|
||||
|
||||
m_containers[containerId] = container
|
||||
print("opencid ".. containerId)
|
||||
end
|
||||
|
||||
function Containers.onContainerClose(containerId)
|
||||
print("closecid ".. containerId)
|
||||
local container = m_containers[containerId]
|
||||
if container then
|
||||
Game.gameRightPanel:removeChild(container)
|
||||
end
|
||||
m_containers[containerId] = nil
|
||||
end
|
||||
|
||||
function Containers.onContainerAddItem(containerId, item)
|
||||
print("addcid ".. containerId)
|
||||
local container = m_containers[containerId]
|
||||
if not container or not item or container.itemCount >= container.capacity then return end
|
||||
|
||||
-- maybe this has to be moved to client internal's files
|
||||
local pos = item:getPos()
|
||||
pos.z = container.itemCount
|
||||
item:setPos(pos)
|
||||
|
||||
local itemWidget = container:getChildByIndex(container.itemCount + 1)
|
||||
itemWidget:setItem(item)
|
||||
container.itemCount = container.itemCount + 1
|
||||
|
||||
end
|
||||
|
||||
connect(Game, { onLogin = Containers.clean,
|
||||
onLogout = Containers.clean,
|
||||
onContainerOpen = Containers.onContainerOpen,
|
||||
onContainerClose = Containers.onContainerClose,
|
||||
onContainerAddItem = Containers.onContainerAddItem })
|
||||
7
modules/game_containers/containers.otmod
Normal file
7
modules/game_containers/containers.otmod
Normal file
@@ -0,0 +1,7 @@
|
||||
Module
|
||||
name: game_containers
|
||||
description: Manage containers
|
||||
author: OTClient team
|
||||
website: https://github.com/edubart/otclient
|
||||
onLoad: |
|
||||
require 'containers'
|
||||
@@ -29,12 +29,6 @@ function Inventory.onSoulChange(soul)
|
||||
widget:setText("Soul:\n" .. soul)
|
||||
end
|
||||
|
||||
function Inventory.onInventoryItemMouseRelease(itemWidget, mousePosition, mouseButton)
|
||||
local item = itemWidget:getItem()
|
||||
if not item or not itemWidget:containsPoint(mousePosition) then return false end
|
||||
return Game.processMouseAction(mousePosition, mouseButton, nil, item, item, nil, item)
|
||||
end
|
||||
|
||||
connect(Game, { onLogin = Inventory.create,
|
||||
onLogout = Inventory.destroy,
|
||||
onInventoryChange = Inventory.onInventoryChange,
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
InvetoryItem < Item
|
||||
&onMouseRelease: Inventory.onInventoryItemMouseRelease
|
||||
|
||||
UIWindow
|
||||
width: 192
|
||||
height: 148
|
||||
@@ -9,34 +6,34 @@ UIWindow
|
||||
margin-right: 6
|
||||
move-policy: free updated
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// head
|
||||
id: slot1
|
||||
anchors.top: parent.top
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// armor
|
||||
id: slot4
|
||||
anchors.top: prev.bottom
|
||||
anchors.horizontalCenter: prev.horizontalCenter
|
||||
margin-top: 5
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// legs
|
||||
id: slot7
|
||||
anchors.top: prev.bottom
|
||||
anchors.horizontalCenter: prev.horizontalCenter
|
||||
margin-top: 5
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// feet
|
||||
id: slot8
|
||||
anchors.top: prev.bottom
|
||||
anchors.horizontalCenter: prev.horizontalCenter
|
||||
margin-top: 5
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// necklace
|
||||
id: slot2
|
||||
anchors.top: parent.top
|
||||
@@ -44,21 +41,21 @@ UIWindow
|
||||
margin-top: 10
|
||||
margin-right: 5
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// left
|
||||
id: slot6
|
||||
anchors.top: prev.bottom
|
||||
anchors.horizontalCenter: prev.horizontalCenter
|
||||
margin-top: 5
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// ring
|
||||
id: slot9
|
||||
anchors.top: prev.bottom
|
||||
anchors.horizontalCenter: prev.horizontalCenter
|
||||
margin-top: 5
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// backpack
|
||||
id: slot3
|
||||
anchors.top: parent.top
|
||||
@@ -66,14 +63,14 @@ UIWindow
|
||||
margin-top: 10
|
||||
margin-left: 5
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// right
|
||||
id: slot5
|
||||
anchors.top: prev.bottom
|
||||
anchors.horizontalCenter: prev.horizontalCenter
|
||||
margin-top: 5
|
||||
|
||||
InvetoryItem
|
||||
Item
|
||||
// ammo
|
||||
id: slot10
|
||||
anchors.top: prev.bottom
|
||||
|
||||
Reference in New Issue
Block a user