ui_radiobutton.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. local l_gfx = love.graphics
  2. -- RadioButton component is the one that can create groups of switches with only one switched on, usage:
  3. --[[
  4. local rb1 = RadioButton:new("RB1")
  5. local rb2 = RadioButton:new("RB2")
  6. local rb3 = RadioButton:new("RB3")
  7. local rbgroup = {rb1,rb2,rb3}
  8. for i=1,3 do
  9. rbgroup[i]:setGroup(rbgroup)
  10. end
  11. -- clicking on one radiobutton will make it checked, and every other in this group unchecked
  12. ]]
  13. RadioButton = {}
  14. RadioButton.__index = RadioButton
  15. RadioButton.ident = "ui_radiobutton"
  16. RadioButton.name = "RadioButton"
  17. RadioButton.caption = "RadioButton"
  18. RadioButton.w = 16
  19. RadioButton.h = 16
  20. RadioButton.buttonStyle = false -- if true, it will be drawn as a button
  21. function RadioButton:new(name)
  22. local self = {}
  23. setmetatable(self,RadioButton)
  24. self.group = {self} -- group array this radiobutton belongs to
  25. self.checked = false
  26. if name ~= nil then self.name = name end
  27. return self
  28. end
  29. setmetatable(RadioButton,{__index = UIElement})
  30. function RadioButton:mousepressed(x,y,b)
  31. if self:isMouseOver(x,y) then
  32. if b == 1 then
  33. local c = #self.group
  34. if c>0 then
  35. for i=1,c do
  36. if self.group[i].name ~= self.name then
  37. self.group[i].checked = false
  38. end
  39. end
  40. end
  41. self.checked = true
  42. end
  43. self:click(b)
  44. end
  45. end
  46. -- this sets radiobutton group
  47. function RadioButton:setGroup(group)
  48. self.group = group or {self}
  49. end
  50. -- and this gives an index of a checked radiobutton in this group
  51. function RadioButton:getGroupIndex()
  52. local c = #self.group
  53. if c>0 then
  54. for i=1,c do
  55. if self.group[i].checked == true then return i end
  56. end
  57. end
  58. end
  59. function RadioButton:draw()
  60. local cr,cg,cb,ca = love.graphics.getColor()
  61. if self.buttonStyle == true then
  62. if self.checked == true then
  63. l_gfx.setColor(self.colorHighlight)
  64. else
  65. l_gfx.setColor(self.colorFill)
  66. end
  67. if self.active == false then
  68. l_gfx.setColor(self.colorDisabledFill)
  69. end
  70. l_gfx.rectangle("fill",self.x,self.y,self.w,self.h)
  71. if self.active == true then
  72. l_gfx.setColor(self.colorFont)
  73. else
  74. l_gfx.setColor(self.colorFill)
  75. end
  76. l_gfx.printf(self.caption,self.x,self.y+(self.h/2-7),self.w,"center")
  77. else
  78. l_gfx.setColor(self.colorHighlight)
  79. l_gfx.circle("line",self.x+8,self.y+8,8,16)
  80. if self.checked == true then
  81. l_gfx.setColor(self.colorHighlight)
  82. l_gfx.circle("fill",self.x+8,self.y+8,6,12)
  83. end
  84. if self.active == true then
  85. l_gfx.setColor(self.colorFont)
  86. else
  87. l_gfx.setColor(self.colorFill)
  88. end
  89. l_gfx.print(self.caption,self.x+20,self.y+2)
  90. end
  91. l_gfx.setColor(cr,cg,cb,ca)
  92. end
  93. -- this element is specifically for particle editor
  94. -- its buttons are colored and are highlighted with a frame upon checking
  95. RadioColorPicker = {}
  96. RadioColorPicker.__index = RadioColorPicker
  97. RadioColorPicker.ident = "ui_radiocolorpicker"
  98. RadioColorPicker.name = "RadioColorPicker"
  99. RadioColorPicker.caption = ""
  100. RadioColorPicker.colorHighlight = {192,192,192,192}
  101. function RadioColorPicker:new(name)
  102. local self = {}
  103. setmetatable(self,RadioColorPicker)
  104. self.group = {self}
  105. self.checked = false
  106. self.color = {255,255,255,255}
  107. if name ~= nil then self.name = name end
  108. return self
  109. end
  110. setmetatable(RadioColorPicker,{__index = RadioButton})
  111. function RadioColorPicker:draw()
  112. local cr,cg,cb,ca = love.graphics.getColor()
  113. if self.checked == true then
  114. l_gfx.setColor(self.colorHighlight)
  115. else
  116. l_gfx.setColor(self.colorFill)
  117. end
  118. if self.active == false then
  119. l_gfx.setColor(self.colorDisabledFill)
  120. end
  121. l_gfx.rectangle("line",self.x,self.y,self.w,self.h)
  122. if self.active == true then
  123. l_gfx.setColor(self.color)
  124. else
  125. l_gfx.setColor(self.colorDisabledFill)
  126. end
  127. l_gfx.rectangle("fill",self.x+1,self.y+1,self.w-2,self.h-2)
  128. l_gfx.setColor(cr,cg,cb,ca)
  129. end
  130. function RadioColorPicker:setColor(r,g,b,a)
  131. self.color = {r or 255,g or 255,b or 255,a or 255}
  132. end