/[admin]/admin2/client/admin_gui.lua

https://github.com/multitheftauto/mtasa-resources · Lua · 294 lines · 267 code · 18 blank · 9 comment · 44 complexity · 78084908fa1a39ce8dd46125b868da00 MD5 · raw file

  1. --[[**********************************
  2. *
  3. * Multi Theft Auto - Admin Panel
  4. *
  5. * client/admin_gui.lua
  6. *
  7. * Original File by lil_Toady
  8. *
  9. **************************************]]
  10. _guiprotected = {}
  11. function guiCreateHeader(x, y, w, h, text, relative, parent)
  12. local header = guiCreateLabel(x, y, w, h, text, relative, parent)
  13. if (header) then
  14. guiLabelSetColor(header, 255, 0, 0)
  15. guiSetFont(header, "default-bold-small")
  16. return header
  17. end
  18. return false
  19. end
  20. function guiCreateInnerImage(image, parent, above)
  21. local sx, sy = guiGetSize(parent, false)
  22. local img = nil
  23. if (above) then
  24. img = guiCreateStaticImage(0, 0, 0, 0, image, false, getElementParent(parent))
  25. local px, py = guiGetPosition(parent, false)
  26. guiSetPosition(img, px + sx - sy, py, false)
  27. else
  28. img = guiCreateStaticImage(0, 0, 0, 0, image, false, parent)
  29. guiSetPosition(img, sx - sy, 0, false)
  30. end
  31. guiSetSize(img, sy, sy, false)
  32. return img
  33. end
  34. local _guiCreateWindow = guiCreateWindow
  35. function guiCreateWindow(...)
  36. local window = _guiCreateWindow(...)
  37. if (window) then
  38. guiWindowSetSizable(window, false)
  39. return window
  40. end
  41. return nil
  42. end
  43. local _guiCreateTab = guiCreateTab
  44. function guiCreateTab(name, parent, right)
  45. local tab = _guiCreateTab(name, parent)
  46. if (tab) then
  47. if (right) then
  48. right = "general.tab_" .. right
  49. if (not hasPermissionTo(right)) then
  50. guiSetEnabled(tab, false)
  51. _guiprotected[right] = tab
  52. end
  53. end
  54. return tab
  55. end
  56. return false
  57. end
  58. local _guiCreateButton = guiCreateButton
  59. function guiCreateButton(x, y, w, h, text, relative, parent, right)
  60. local button = _guiCreateButton(x, y, w, h, text, relative, parent)
  61. if (button) then
  62. if (right) then
  63. right = "command." .. right
  64. if (not hasPermissionTo(right)) then
  65. guiSetEnabled(button, false)
  66. _guiprotected[right] = button
  67. end
  68. end
  69. guiSetFont(button, "default-bold-small")
  70. return button
  71. end
  72. return false
  73. end
  74. local _guiCreateCheckBox = guiCreateCheckBox
  75. function guiCreateCheckBox(x, y, w, h, text, checked, relative, parent, right)
  76. local check = _guiCreateCheckBox(x, y, w, h, text, checked, relative, parent)
  77. if (check) then
  78. if (right) then
  79. right = "command." .. right
  80. if (not hasPermissionTo(right)) then
  81. guiSetEnabled(check, false)
  82. _guiprotected[right] = check
  83. end
  84. end
  85. return check
  86. end
  87. return false
  88. end
  89. local guiColorPickers = {}
  90. function guiCreateColorPicker(x, y, w, h, r, g, b, relative, parent)
  91. local mask = guiCreateLabel(x, y, w, h, "", relative, parent)
  92. guiLabelSetHorizontalAlign(mask, "left", true)
  93. guiColorPickers[mask] = {r = r or 255, g = g or 0, b = b or 0}
  94. addEventHandler(
  95. "onClientGUIClick",
  96. mask,
  97. function(key, state)
  98. local info = guiColorPickers[source]
  99. if (key == "left" and state == "up" and info) then
  100. local x, y = guiGetAbsolutePosition(mask)
  101. local sx, sy = guiGetSize(mask, false)
  102. info.picking = true
  103. info.r, info.g, info.b = aColor.Open(x + sx, y - 5, info.r, info.g, info.b)
  104. info.picking = nil
  105. end
  106. end
  107. )
  108. addEventHandler(
  109. "onClientElementDestroy",
  110. mask,
  111. function()
  112. guiColorPickers[source] = nil
  113. end
  114. )
  115. end
  116. addEventHandler(
  117. "onClientRender",
  118. getRootElement(),
  119. function()
  120. if (isConsoleActive()) then
  121. return
  122. end
  123. for mask, info in pairs(guiColorPickers) do
  124. if (guiGetVisible(mask)) then
  125. if (info.picking) then
  126. info.r, info.g, info.b = aColor.Color.r, aColor.Color.g, aColor.Color.b
  127. end
  128. local x, y = guiGetAbsolutePosition(mask)
  129. local sx, sy = guiGetSize(mask, false)
  130. dxDrawLine(x, y + sy / 2, x + sx, y + sy / 2, tocolor(info.r, info.g, info.b, 255), sy, true)
  131. end
  132. end
  133. end
  134. )
  135. local guiBlendTable = {}
  136. function guiBlendElement(element, alpha, hide)
  137. local increment = (alpha - guiGetAlpha(element)) * 10
  138. guiBlendTable[element] = {inc = increment, hide = hide, target = alpha}
  139. end
  140. addEventHandler(
  141. "onClientRender",
  142. root,
  143. function()
  144. for element, v in pairs(guiBlendTable) do
  145. local a = guiGetAlpha(element) + v.inc / 40
  146. if (v.inc < 0 and a <= v.target) then
  147. a = v.target
  148. if (v.hide) then
  149. guiSetVisible(element, false)
  150. end
  151. guiBlendTable[element] = nil
  152. elseif (v.inc > 0 and a >= v.target) then
  153. a = v.target
  154. guiBlendTable[element] = nil
  155. end
  156. guiSetAlpha(element, a)
  157. end
  158. end
  159. )
  160. function guiCreateContextMenu(element)
  161. local menu = guiCreateStaticImage(0, 0, 100, 0, "client/images/black.png", false)
  162. guiSetVisible(menu, false)
  163. if (element) then
  164. guiSetContextMenu(element, menu)
  165. end
  166. return menu
  167. end
  168. function guiSetContextMenu(element, menu)
  169. addEventHandler(
  170. "onClientGUIClick",
  171. element,
  172. function(button)
  173. contextSource = source
  174. if (getElementType(source) == "gui-gridlist" and guiGridListGetSelectedItem(source) == -1) then
  175. return
  176. end
  177. if (button == "right") then
  178. local sx, sy = guiGetScreenSize()
  179. local x, y = getCursorPosition()
  180. x, y = sx * x, sy * y
  181. guiSetPosition(menu, x, y, false)
  182. guiSetVisible(menu, true)
  183. guiBringToFront(menu)
  184. setTimer(
  185. function()
  186. addEventHandler(
  187. "onClientClick",
  188. getRootElement(),
  189. function(button, state, x, y)
  190. local sx, sy = guiGetSize(menu, false)
  191. local px, py = guiGetPosition(menu, false)
  192. if (x < px or x > px + sx) or (y < py or y > py + sy) then
  193. guiSetVisible(menu, false)
  194. removeEventHandler("onClientClick", getRootElement(), debug.getinfo(1, "f").func)
  195. end
  196. end
  197. )
  198. end,
  199. 50,
  200. 1
  201. )
  202. end
  203. end,
  204. false
  205. )
  206. addEventHandler(
  207. "onClientGUIClick",
  208. menu,
  209. function(button)
  210. guiSetVisible(menu, false)
  211. end
  212. )
  213. end
  214. function guiContextMenuAddItem(element, text)
  215. local height = 16
  216. local sx, sy = guiGetSize(element, false)
  217. local n = #getElementChildren(element)
  218. local bg = guiCreateStaticImage(1, n * height + 1, 0, height, "client/images/black.png", false, element)
  219. local item = guiCreateLabel(0, 0, 0, height, " " .. text .. " ", false, bg)
  220. local extent = guiLabelGetTextExtent(item)
  221. local width = guiGetSize(element, false) - 2
  222. if (extent > width) then
  223. width = extent
  224. end
  225. guiSetSize(element, width + 2, (n + 1) * height + 2, false)
  226. guiSetSize(bg, width, height, false)
  227. guiSetSize(item, width, height, false)
  228. addEventHandler(
  229. "onClientMouseEnter",
  230. item,
  231. function()
  232. guiStaticImageLoadImage(getElementParent(source), "client/images/blue.png")
  233. end,
  234. false
  235. )
  236. addEventHandler(
  237. "onClientMouseLeave",
  238. item,
  239. function()
  240. guiStaticImageLoadImage(getElementParent(source), "client/images/black.png")
  241. end,
  242. false
  243. )
  244. return item
  245. end
  246. function guiCreateToolTip(element)
  247. end
  248. function guiGetAbsolutePosition(element)
  249. local x, y = guiGetPosition(element, false)
  250. local parent = getElementParent(element)
  251. while (parent ~= getResourceGUIElement()) do
  252. local px, py = guiGetPosition(parent, false)
  253. x = x + px
  254. y = y + py
  255. parent = getElementParent(parent)
  256. end
  257. return x, y
  258. end
  259. function guiHandleInput(element)
  260. addEventHandler(
  261. "onClientGUIFocus",
  262. element,
  263. function()
  264. guiSetInputEnabled(true)
  265. end,
  266. false
  267. )
  268. addEventHandler(
  269. "onClientGUIBlur",
  270. element,
  271. function()
  272. guiSetInputEnabled(false)
  273. end,
  274. false
  275. )
  276. end