PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/[admin]/admin2/client/widgets/admin_color.lua

https://gitlab.com/yasin3223/mtasa-resources
Lua | 260 lines | 221 code | 30 blank | 9 comment | 38 complexity | 29ecdfff2f5e693710d572db95566522 MD5 | raw file
  1. --[[**********************************
  2. *
  3. * Multi Theft Auto - Admin Panel
  4. *
  5. * client\widgets\admin_color.lua
  6. *
  7. * Original File by lil_Toady
  8. *
  9. **************************************]]
  10. aColor = {
  11. Form = nil,
  12. Color = {
  13. r = 0,
  14. g = 0,
  15. b = 0,
  16. h = 0,
  17. s = 0
  18. },
  19. Picking = false,
  20. Thread = nil
  21. }
  22. function aColor.Open ( x, y, r, g, b, relative, parent )
  23. local sx, sy = guiGetScreenSize ()
  24. if ( not x or not y ) then
  25. x, y = getCursorPosition ()
  26. x = sx * x
  27. y = sy * y
  28. else
  29. if ( relative ) then
  30. if ( parent ) then
  31. local px, py = guiGetSize ( parent, false )
  32. x = px * x
  33. x = py * y
  34. else
  35. x = sx * x
  36. y = sy * y
  37. end
  38. end
  39. if ( parent ) then
  40. while ( parent ~= nil ) do
  41. local px, py = guiGetPosition ( parent, false )
  42. x = px + x
  43. y = py + y
  44. parent = getElementParent ( parent )
  45. end
  46. end
  47. end
  48. x = x - 1
  49. y = y - 1
  50. if ( r and g and b ) then
  51. aColor.Color.r = math.floor ( r ) % 256
  52. aColor.Color.g = math.floor ( g ) % 256
  53. aColor.Color.b = math.floor ( b ) % 256
  54. else
  55. aColor.Color = { r = 255, g = 0, b = 0 }
  56. end
  57. aColor.Color.h, aColor.Color.s = aColor.rgb2hs ( aColor.Color.r, aColor.Color.g, aColor.Color.b )
  58. if ( not aColor.Form ) then
  59. aColor.Form = guiCreateStaticImage ( x, y, 210, 138, "client/images/black.png", false )
  60. guiSetAlpha ( aColor.Form, 0.6 )
  61. aColor.Palette = guiCreateStaticImage ( 5, 5, 128, 128, "client/images/palette.png", false, aColor.Form )
  62. guiCreateLabel ( 138, 37, 10, 20, "R:", false, aColor.Form )
  63. guiCreateLabel ( 138, 57, 10, 20, "G:", false, aColor.Form )
  64. guiCreateLabel ( 138, 77, 10, 20, "B:", false, aColor.Form )
  65. aColor.R = guiCreateEdit ( 155, 35, 50, 20, "", false, aColor.Form )
  66. aColor.G = guiCreateEdit ( 155, 55, 50, 20, "", false, aColor.Form )
  67. aColor.B = guiCreateEdit ( 155, 75, 50, 20, "", false, aColor.Form )
  68. aColor.Ok = guiCreateButton ( 155, 113, 50, 20, "ok", false, aColor.Form )
  69. guiSetProperty ( aColor.Form, "AlwaysOnTop", "true" )
  70. aRegister ( "Color", aColor.Form, aColor.Open, aColor.Close )
  71. end
  72. guiSetText ( aColor.R, tostring ( aColor.Color.r ) )
  73. guiSetText ( aColor.G, tostring ( aColor.Color.g ) )
  74. guiSetText ( aColor.B, tostring ( aColor.Color.b ) )
  75. aColor.Picking = false
  76. guiSetVisible ( aColor.Form, true )
  77. addEventHandler ( "onClientRender", getRootElement(), aColor.onRender )
  78. addEventHandler ( "onClientGUIChanged", aColor.Form, aColor.onChanged )
  79. addEventHandler ( "onClientGUIBlur", aColor.Form, aColor.onBlur )
  80. setTimer ( function () -- some hack for window not to get insta closed if opened in click handler
  81. if ( aColor.Form and guiGetVisible ( aColor.Form ) ) then
  82. guiBringToFront ( aColor.Form )
  83. addEventHandler ( "onClientClick", getRootElement(), aColor.onClick )
  84. end
  85. end, 50, 1 )
  86. aColor.Thread = sourceCoroutine
  87. coroutine.yield ()
  88. aColor.Thread = nil
  89. return aColor.Color.r, aColor.Color.g, aColor.Color.b
  90. end
  91. function aColor.Close ( destroy )
  92. guiSetInputEnabled ( false )
  93. if ( aColor.Form ) then
  94. removeEventHandler ( "onClientGUIBlur", aColor.Form, aColor.onBlur )
  95. removeEventHandler ( "onClientGUIChanged", aColor.Form, aColor.onChanged )
  96. removeEventHandler ( "onClientClick", getRootElement(), aColor.onClick )
  97. removeEventHandler ( "onClientRender", getRootElement(), aColor.onRender )
  98. if ( destroy ) then
  99. destroyElement ( aColor.Form )
  100. aColor.Form = nil
  101. else
  102. guiSetVisible ( aColor.Form, false )
  103. end
  104. if ( aColor.Thread ) then
  105. coroutine.resume ( aColor.Thread )
  106. end
  107. end
  108. end
  109. function aColor.onClick ( button, state, x, y )
  110. local px, py = guiGetPosition ( aColor.Form, false )
  111. if ( state == "up" ) then
  112. if ( aColor.Picking ) then
  113. aColor.Picking = false
  114. return
  115. end
  116. local sx, sy = guiGetSize ( aColor.Form, false )
  117. if ( x < px or x > px + sx ) or ( y < py or y > py + sy ) then
  118. aColor.Close ()
  119. return
  120. end
  121. end
  122. if ( button ~= "left" ) then
  123. return
  124. end
  125. if ( x >= px + 5 and x <= px + 133 ) and
  126. ( y >= py + 5 and y <= py + 133 ) then
  127. aColor.Picking = state == "down"
  128. end
  129. end
  130. function aColor.onRender ()
  131. if ( isConsoleActive() ) then
  132. return
  133. end
  134. local color = aColor.Color
  135. local x, y = guiGetPosition ( aColor.Form, false )
  136. x = x + 5
  137. y = y + 5
  138. if ( aColor.Picking ) then
  139. local sx, sy = guiGetScreenSize ()
  140. local cx, cy = getCursorPosition ()
  141. cx = sx * cx
  142. cy = sy * cy
  143. if ( cx < x ) then cx = x
  144. elseif ( cx > x + 127 ) then cx = x + 127 end
  145. if ( cy < y ) then cy = y
  146. elseif ( cy > y + 127 ) then cy = y + 127 end
  147. color.h, color.s = ( cx - x ) / 127, ( 127 - cy + y ) / 127
  148. color.r, color.g, color.b = aColor.hs2rgb ( color.h, color.s )
  149. guiSetText ( aColor.R, tostring ( color.r ) )
  150. guiSetText ( aColor.G, tostring ( color.g ) )
  151. guiSetText ( aColor.B, tostring ( color.b ) )
  152. end
  153. dxDrawLine ( x + 133, y + 10, x + 200, y + 10, tocolor ( color.r, color.g, color.b, 255 ), 20, true )
  154. x = x + color.h * 127
  155. y = y + ( 1 - color.s ) * 127
  156. local c = tocolor ( 0, 0, 0, 255 )
  157. dxDrawLine ( x - 7, y, x - 2, y, c, 2, true)
  158. dxDrawLine ( x + 2, y, x + 7, y, c, 2, true)
  159. dxDrawLine ( x, y - 7, x, y - 2, c, 2, true)
  160. dxDrawLine ( x, y + 2, x, y + 7, c, 2, true)
  161. end
  162. function aColor.onChanged ()
  163. local acc = { [aColor.R] = "r", [aColor.G] = "g", [aColor.B] = "b" }
  164. if ( acc[source] ) then
  165. local value = tonumber ( guiGetText ( source ) )
  166. if ( not value ) then
  167. if ( guiGetText ( source ) == "" ) then aColor.Color[acc[source]] = 0
  168. else guiSetText ( source, aColor.Color[acc[source]] ) end
  169. elseif ( value >= 0 and value <= 255 ) then
  170. aColor.Color[acc[source]] = value
  171. else
  172. guiSetText ( source, aColor.Color[acc[source]] )
  173. end
  174. aColor.Color.h, aColor.Color.s = aColor.rgb2hs ( aColor.Color.r, aColor.Color.g, aColor.Color.b )
  175. end
  176. end
  177. function aColor.onBlur ()
  178. local acc = { [aColor.R] = "r", [aColor.G] = "g", [aColor.B] = "b" }
  179. if ( acc[source] ) then
  180. if ( guiGetText ( source ) == "" ) then
  181. guiSetText ( source, "0" )
  182. end
  183. end
  184. end
  185. function aColor.hs2rgb ( h, s )
  186. local m2 = (0.5 + s) - (0.5 * s)
  187. local m1 = 1 - m2
  188. local r = aColor.hue2rgb(m1, m2, h + 1/3)
  189. local g = aColor.hue2rgb(m1, m2, h)
  190. local b = aColor.hue2rgb(m1, m2, h - 1/3)
  191. return math.floor ( r * 255 ), math.floor ( g * 255 ), math.floor ( b * 255 )
  192. end
  193. function aColor.hue2rgb ( m1, m2, h )
  194. if ( h < 0 ) then h = h + 1
  195. elseif ( h > 1 ) then h = h - 1 end
  196. if h*6 < 1 then
  197. return m1 + (m2 - m1) * h * 6
  198. elseif h*2 < 1 then
  199. return m2
  200. elseif h*3 < 2 then
  201. return m1 + (m2 - m1) * (2/3 - h) * 6
  202. else
  203. return m1
  204. end
  205. end
  206. function aColor.rgb2hs ( r, g, b )
  207. local max = math.max(r, g, b)
  208. local min = math.min(r, g, b)
  209. local l = (min + max) / 2
  210. local h = 0
  211. local s = 0
  212. if ( max ~= min ) then
  213. local d = max - min
  214. if l < 0.5 then
  215. s = d / (max + min)
  216. else
  217. s = d / (2 - max - min)
  218. end
  219. if max == r then
  220. h = (g - b) / d
  221. if g < b then h = h + 6 end
  222. elseif max == g then
  223. h = (b - r) / d + 2
  224. else
  225. h = (r - g) / d + 4
  226. end
  227. h = h / 6
  228. end
  229. return h, -s
  230. end