Advertisement
PetrikDev

Untitled

Jul 9th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. --[[--------------------------------------------------
  2. GUI Editor
  3. client
  4. dx_slider.lua
  5.  
  6. creates a dx slider widget for use in the right click menus
  7. --]]--------------------------------------------------
  8.  
  9. DX_Slider = {}
  10. DX_Slider.__index = DX_Slider
  11. DX_Slider.instances = {}
  12.  
  13. function DX_Slider:create(x, y, width, height)
  14. local new = setmetatable(
  15. {
  16. x = x,
  17. y = y,
  18. width = width,
  19. height = height,
  20. minValue = 0,
  21. maxValue = 100,
  22. pointer = {
  23. value = 0,
  24. position = 0,
  25. width = 16,
  26. height = 16,
  27. dragging = false,
  28. },
  29. margin = 10,
  30. snapToBoundaries = false,
  31. drawBoundaries = false,
  32. enabled_ = true,
  33. visible_ = false,
  34. postGUI = true,
  35. },
  36. DX_Slider
  37. )
  38.  
  39. DX_Slider.instances[#DX_Slider.instances + 1] = new
  40.  
  41. return new
  42. end
  43.  
  44.  
  45. function DX_Slider:position(x, y)
  46. if x then
  47. self.x = x
  48. self.y = y
  49. else
  50. return self.x, self.y
  51. end
  52. end
  53.  
  54.  
  55. function DX_Slider:size(width, height)
  56. if width then
  57. self.width = width
  58. self.height = height
  59. else
  60. return self.width, self.height
  61. end
  62. end
  63.  
  64.  
  65. function DX_Slider:visible(visible)
  66. if visible ~= nil then
  67. self.visible_ = visible
  68.  
  69. if not visible then
  70. self.pointer.dragging = false
  71. end
  72. else
  73. return self.visible_
  74. end
  75. end
  76.  
  77.  
  78. function DX_Slider:value(value)
  79. if value then
  80. value = math.round(value)
  81. self.pointer.value = value
  82. self:updatePointerPosition(value)
  83. else
  84. return self.pointer.value
  85. end
  86. end
  87.  
  88.  
  89. function DX_Slider:enabled(value)
  90. if value ~= nil then
  91. self.enabled_ = value
  92. else
  93. return self.enabled_
  94. end
  95. end
  96.  
  97.  
  98. function DX_Slider:isMouseOnPointer(x, y)
  99. if x >= (self.x + self.pointer.position) and x <= (self.x + self.pointer.position + self.pointer.width) and
  100. y >= (self.y + (self.height / 2) - (self.pointer.height / 2)) and y <= (self.y + (self.height / 2) + (self.pointer.height / 2)) then
  101. return true
  102. end
  103.  
  104. return false
  105. end
  106.  
  107.  
  108. function DX_Slider:updatePointerPosition(value)
  109. local width = self.width - 20
  110. local position
  111. local newValue
  112.  
  113. if value then
  114. newValue = value
  115.  
  116. position = (value / self.maxValue) * width
  117. else
  118. local x = getCursorPosition(true)
  119.  
  120. position = x - self.x - 10
  121.  
  122. if position > self.width then
  123. position = self.width
  124. end
  125.  
  126. local percent = position / width
  127.  
  128. percent = math.min(math.max(percent, 0), 1)
  129.  
  130. newValue = math.floor(self.minValue + (percent * (self.maxValue - self.minValue)) + 0.5)
  131. end
  132.  
  133. self.pointer.value = math.min(math.max(newValue, self.minValue), self.maxValue)
  134.  
  135. position = math.min(math.max(position, 0), width)
  136.  
  137. local newPosition = position - (self.pointer.width / 2) + 10
  138.  
  139. if self.snapToBoundaries then
  140. newPosition = ((width / ((self.maxValue - self.minValue))) * ((self.pointer.value - self.minValue))) - (self.pointer.width / 2) + 10
  141. end
  142.  
  143. if self.pointer.position ~= newPosition then
  144. self.pointer.position = newPosition
  145.  
  146. if self.onChange and self:visible() then
  147. self.onChange(unpack(self.onChangeArgs or {}))
  148. end
  149. end
  150. end
  151.  
  152.  
  153. function DX_Slider:draw()
  154. if self:visible() then
  155. --dxDrawLine(self.x, self.y + (self.height / 2), self.x + self.width, self.y + (self.height / 2), tocolor(200, 200, 200, 255), 1, self.postGUI)
  156. dxDrawLine(self.x + 1, self.y + (self.height / 2), self.x + self.width - 2, self.y + (self.height / 2), tocolor(100, 100, 100, 100), 1, self.postGUI)
  157.  
  158. dxDrawLine(self.x + 1, self.y + (self.height / 2) - 1, self.x + self.width - 2, self.y + (self.height / 2) - 1, tocolor(unpack(gColours.primary)), 1, self.postGUI)
  159. dxDrawLine(self.x + 1, self.y + (self.height / 2) + 1, self.x + self.width - 2, self.y + (self.height / 2) + 1, tocolor(unpack(gColours.primary)), 1, self.postGUI)
  160.  
  161. dxDrawImage(self.x, self.y, self.height, self.height, "images/dx_elements/slider_end.png", 0, 0, 0, tocolor(255, 255, 255, 255), self.postGUI)
  162. dxDrawImage(self.x + self.width - self.height, self.y, self.height, self.height, "images/dx_elements/slider_end.png", 180, 0, 0, tocolor(255, 255, 255, 255), self.postGUI)
  163.  
  164. if self.drawBoundaries then
  165. for i = 0, (self.maxValue - self.minValue), 1 do
  166. local pos = (((self.width - 20) / ((self.maxValue - self.minValue) )) * (i)) - (4) + 10
  167. dxDrawImage(self.x + pos, self.y + 4, 8, 8, "images/dx_elements/radio_button.png", 0, 0, 0, tocolor(255, 255, 255, 255), self.postGUI)
  168. end
  169. end
  170.  
  171. if self.pointer.dragging then
  172. self:updatePointerPosition()
  173. end
  174.  
  175. dxDrawImage(self.x + self.pointer.position, self.y + (self.height / 2) - (self.pointer.height / 2), self.pointer.width, self.pointer.height, "images/dx_elements/slider_pointer.png", 0, 0, 0, tocolor(255, 255, 255, 255), self.postGUI)
  176. end
  177. end
  178.  
  179.  
  180. addEventHandler("onClientClick", root,
  181. function(button, state, absoluteX, absoluteY)
  182. if not gEnabled then
  183. return
  184. end
  185.  
  186. if button == "left" then
  187. if state == "down" then
  188. for _,slider in ipairs(DX_Slider.instances) do
  189. if slider:enabled() and slider:isMouseOnPointer(absoluteX, absoluteY) then
  190. slider.pointer.dragging = true
  191. end
  192. end
  193. elseif state == "up" then
  194. for _,slider in ipairs(DX_Slider.instances) do
  195. if slider.pointer.dragging then
  196. slider.pointer.dragging = false
  197. end
  198. end
  199. end
  200. end
  201. end
  202. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement