Skip to content

Commit 4ae9f38

Browse files
brenodanyelstoneage-mtaDezash
authored
admin: Improve lists (#316)
* admin: improve lists * update min-client-version * lists: replace background image to button * fix spect-slap variable name * avoid executing the same function twice * Update [admin]/admin/client/gui/admin_main.lua Co-authored-by: Gabrielius Laurinavičius <[email protected]> * disable the propagate parameter in the doubleclick event Co-authored-by: Gabrielius Laurinavičius <[email protected]> * close list when click somewhere else * fix indentation Co-authored-by: Breno Danyel <[email protected]> Co-authored-by: Gabrielius Laurinavičius <[email protected]>
1 parent 0f4ef32 commit 4ae9f38

File tree

3 files changed

+1592
-1475
lines changed

3 files changed

+1592
-1475
lines changed

[admin]/admin/client/admin_gui.lua

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
**************************************]]
1010
_guiprotected = {}
1111

12+
local lists = {}
13+
1214
function guiCreateHeader ( x, y, w, h, text, relative, parent )
1315
local header = guiCreateLabel ( x, y, w, h, text, relative, parent )
1416
if ( header ) then
@@ -51,3 +53,136 @@ function guiCreateButton ( x, y, w, h, text, relative, parent, right )
5153
end
5254
return false
5355
end
56+
57+
function guiCreateList(x, y, w, h, tabHeight, header, relative, parent, right)
58+
local list = guiCreateButton(x, y, w, h, header, relative, parent, right)
59+
60+
local parentWidth, parentHeight = guiGetSize(parent, false)
61+
62+
local absoluteWidth = parentWidth * w
63+
local absoluteHeight = parentHeight * h
64+
65+
local dropDown = guiCreateStaticImage(absoluteWidth - 20, 0, 20, 20, "client\\images\\dropdown.png", false, list)
66+
guiSetProperty(dropDown, 'AlwaysOnTop', 'True')
67+
68+
addEventHandler('onClientGUIClick', dropDown, function()
69+
guiListSetVisible(list, true)
70+
end, false)
71+
72+
local bg = guiCreateButton(x, y, w, tabHeight, '', relative, parent)
73+
guiSetProperty(bg, 'AlwaysOnTop', 'True')
74+
guiSetVisible(bg, false)
75+
76+
local edit = guiCreateEdit(0, 0, absoluteWidth - 20, 20, '', false, bg)
77+
guiSetProperty(edit, 'AlwaysOnTop', 'True')
78+
79+
addEventHandler('onClientGUIChanged', edit, function()
80+
guiListLoadItems(list)
81+
end)
82+
83+
local searchIcon = guiCreateStaticImage(absoluteWidth - 40, 0, 20, 20, "client\\images\\search.png", false, edit)
84+
guiSetEnabled(searchIcon, false)
85+
guiSetProperty(searchIcon, 'AlwaysOnTop', 'True')
86+
87+
local close = guiCreateButton(absoluteWidth-20, 0, 20, 20, 'X', false, bg)
88+
guiSetProperty(close, 'AlwaysOnTop', 'True')
89+
guiSetAlpha(close, 1)
90+
91+
addEventHandler('onClientGUIClick', close, function()
92+
guiListSetVisible(list, false)
93+
end, false)
94+
95+
local gridlist = guiCreateGridList(0, 0, 1, 1, true, bg)
96+
97+
addEventHandler('onClientGUIDoubleClick', gridlist, function()
98+
local callback = lists[list].callback
99+
if (type(callback) == 'function') then
100+
local row = guiGridListGetSelectedItem(gridlist)
101+
local data = guiGridListGetItemData(gridlist, row, 1)
102+
local text = guiGridListGetItemText(gridlist, row, 1)
103+
if (row > -1) then
104+
callback(data, text)
105+
guiListSetVisible(list, false)
106+
end
107+
end
108+
end, false)
109+
110+
111+
lists[list] = {
112+
bg = bg,
113+
edit = edit,
114+
gridlist = gridlist,
115+
items = {},
116+
callback = function() end,
117+
}
118+
119+
return list
120+
end
121+
122+
function guiListSetVisible(list, state)
123+
if lists[list] then
124+
guiSetVisible(lists[list].bg, state)
125+
if state then
126+
guiFocus(lists[list].edit)
127+
end
128+
return true
129+
end
130+
return false
131+
end
132+
133+
function guiListSetItems(list, items)
134+
if lists[list] then
135+
lists[list].items = items
136+
guiListLoadItems(list)
137+
return true
138+
end
139+
return false
140+
end
141+
142+
function guiListSetCallBack(list, callback)
143+
if lists[list] then
144+
lists[list].callback = callback
145+
return true
146+
end
147+
return false
148+
end
149+
150+
function guiListSetColumns(list, columns)
151+
if lists[list] then
152+
for _, v in ipairs(columns) do
153+
guiGridListAddColumn(lists[list].gridlist, v.text, v.width)
154+
end
155+
return true
156+
end
157+
return false
158+
end
159+
160+
function guiListLoadItems(list)
161+
local listData = lists[list]
162+
if listData then
163+
local filter = guiGetText(listData.edit)
164+
guiGridListClear(listData.gridlist)
165+
for k, v in ipairs(listData.items) do
166+
if (v.text:lower():find(filter:lower())) then
167+
local row = guiGridListAddRow(listData.gridlist)
168+
guiGridListSetItemText(listData.gridlist, row, 1, tostring ( v.text ), false, false )
169+
guiGridListSetItemData(listData.gridlist, row, 1, tostring ( v.data ) )
170+
end
171+
end
172+
return true
173+
end
174+
return false
175+
end
176+
177+
addEventHandler('onClientGUIClick', guiRoot, function(button)
178+
if (button == 'left') then
179+
local parent = getElementParent(source)
180+
if parent then
181+
for list in pairs(lists) do
182+
if guiGetVisible(list) and (parent ~= list) and (getElementParent(parent) ~= getElementParent(list)) then
183+
guiListSetVisible(list, false)
184+
end
185+
end
186+
end
187+
end
188+
end)

0 commit comments

Comments
 (0)