local otui = [[
ListItem < UIWidget
background-color: alpha
text-offset: 3 1
focusable: true
height: 16
font: verdana-11px-rounded
text-align: left
$focus:
background-color: #00000055
Button
id: remove
!text: tr('X')
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
width: 14
height: 14
margin-right: 15
text-align: center
text-offset: 0 1
tooltip: Remove item from the list.
]]
g_ui.importStyleFromString(otui)
function AddPlayerList(_id, bttn_name, height, color)
local id = _id
if GENERAL_SETTINGS[id] == nil then
GENERAL_SETTINGS[id] = {}
end
local player_list = GENERAL_SETTINGS[id]
local ui = UIsetupUI([[
Panel
id: ]] .. id .. '\n' .. [[
margin: 3
padding: 3
height: ]] .. height .. '\n' .. [[
background-color: ]] .. color .. '\n' .. [[
TextList
id: list
height: ]] .. height - 48 .. '\n' .. [[
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
vertical-scrollbar: scrollbar
VerticalScrollBar
id: scrollbar
anchors.top: list.top
anchors.bottom: list.bottom
anchors.right: list.right
step: 14
pixels-scroll: true
BotTextEdit
id: name
anchors.top: list.bottom
margin-top: 3
anchors.left: parent.left
anchors.right: parent.right
BotButton
id: add
text: ]] .. bttn_name .. '\n' .. [[
anchors.top: prev.bottom
margin-top: 3
anchors.left: parent.left
anchors.right: parent.right
font: verdana-11px-rounded
]] )
local ui_list = ui.list
local ui_name = ui.name
local ui_add = ui.add
local rootWidget = g_ui.getRootWidget()
local moving_item = false
local function addListItem(item_name)
local name = item_name
local label = g_ui.createWidget("ListItem", ui_list)
label:setId('ListItem' .. name)
label:setText(name)
label.remove.onClick = function()
table.remove(player_list, table.find(player_list, name))
label:destroy()
CONFIG:save(CONFIG_GENERAL)
end
label.onMousePress = function(widget, mousePos, mouseButton)
if not moving_item and mouseButton == 1 then
local child = rootWidget:recursiveGetChildByPos(mousePos)
if child and widget == child then
--print('press', widget:getText())
moving_item = true
end
end
end
label.onMouseRelease = function(widget, mousePos, mouseButton)
if moving_item and mouseButton == 1 then
local start_item = ui_list:getFocusedChild()
local end_item = rootWidget:recursiveGetChildByPos(mousePos)
if start_item and end_item and start_item ~= end_item and start_item == widget and
start_item:getId():find('ListItem') and end_item:getId():find('ListItem') then
local start_index = ui_list:getChildIndex(start_item)
local end_index = ui_list:getChildIndex(end_item)
ui_list:moveChildToIndex(start_item, end_index)
ui_list:ensureChildVisible(start_item)
ui_list:moveChildToIndex(end_item, start_index)
ui_list:ensureChildVisible(end_item)
player_list[start_index] = end_item:getText()
player_list[end_index] = start_item:getText()
CONFIG:save(CONFIG_GENERAL)
end
moving_item = false
end
end
CONFIG:save(CONFIG_GENERAL)
end
for _, item_name in ipairs(player_list) do
addListItem(item_name)
end
ui_add.onClick = function()
local names = string.split(ui_name:getText(), ",")
if #names == 0 then
print("MKBot[PlayerList]: Name is missing!")
return
end
for i = 1, #names do
local name = names[i]:trim()
if name:len() == 0 then
print("MKBot[PlayerList]: Name is missing!")
else
if not table.find(player_list, name) then
table.insert(player_list, name)
addListItem(name)
ui_name:setText("")
else
print("MKBot[PlayerList]: Player " .. name .. " is already added!")
ui_name:setText("")
end
end
end
end
end