mirror of
https://github.com/OTCv8/otclientv8.git
synced 2025-10-22 23:45:55 +02:00
Version 2.1 - imbuements, wrap/unwrap, 4 byte header, packet compression and other features
This commit is contained in:
81
modules/game_bot/functions/ui_elements.lua
Normal file
81
modules/game_bot/functions/ui_elements.lua
Normal file
@@ -0,0 +1,81 @@
|
||||
local context = G.botContext
|
||||
if type(context.UI) ~= "table" then
|
||||
context.UI = {}
|
||||
end
|
||||
local UI = context.UI
|
||||
|
||||
UI.Config = function(parent)
|
||||
return UI.createWidget("BotConfig", parent)
|
||||
end
|
||||
|
||||
-- call :setItems(table) to set items, call :getItems() to get them
|
||||
-- unique if true, won't allow duplicates
|
||||
-- callback (can be nil) gets table with new item list, eg: {{id=2160, count=1}, {id=268, count=100}, {id=269, count=20}}
|
||||
UI.Container = function(callback, unique, parent)
|
||||
local widget = UI.createWidget("BotContainer", parent)
|
||||
local oldItems = {}
|
||||
|
||||
local updateItems = function()
|
||||
local items = widget:getItems()
|
||||
widget:setItems(items)
|
||||
|
||||
-- callback part
|
||||
if not callback then return end
|
||||
local somethingNew = false
|
||||
for i, item in ipairs(items) do
|
||||
if type(oldItems[i]) ~= "table" then
|
||||
somethingNew = true
|
||||
break
|
||||
end
|
||||
if oldItems[i].id ~= item.id or oldItems[i].count ~= item.count then
|
||||
somethingNew = true
|
||||
break
|
||||
end
|
||||
end
|
||||
if somethingNew then
|
||||
oldItems = items
|
||||
callback(items)
|
||||
end
|
||||
end
|
||||
|
||||
widget.setItems = function(self, items)
|
||||
if type(self) == 'table' then
|
||||
items = self
|
||||
end
|
||||
local itemsToShow = math.max(10, #items + 2)
|
||||
if itemsToShow % 5 ~= 0 then
|
||||
itemsToShow = itemsToShow + 5 - itemsToShow % 5
|
||||
end
|
||||
widget.items:destroyChildren()
|
||||
for i = 1, itemsToShow do
|
||||
local widget = g_ui.createWidget("BotItem", widget.items)
|
||||
if type(items[i]) == 'number' then
|
||||
items[i] = {id=items[i], count=1}
|
||||
end
|
||||
if type(items[i]) == 'table' then
|
||||
widget:setItem(Item.create(items[i].id, items[i].count))
|
||||
end
|
||||
end
|
||||
oldItems = items
|
||||
for i, child in ipairs(widget.items:getChildren()) do
|
||||
child.onItemChange = updateItems
|
||||
end
|
||||
end
|
||||
|
||||
widget.getItems = function()
|
||||
local items = {}
|
||||
local duplicates = {}
|
||||
for i, child in ipairs(widget.items:getChildren()) do
|
||||
if child:getItemId() >= 100 then
|
||||
if not duplicates[child:getItemId()] or not unique then
|
||||
table.insert(items, {id=child:getItemId(), count=child:getItemCount()})
|
||||
duplicates[child:getItemId()] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
return items
|
||||
end
|
||||
|
||||
return widget
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user