ui_uielement.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. -- this is a definition of drawable UI element
  2. local l_gfx = love.graphics
  3. UIElement = {}
  4. UIElement.__index = UIElement
  5. UIElement.x = 0
  6. UIElement.y = 0
  7. UIElement.w = 32
  8. UIElement.h = 32
  9. UIElement.visible = true -- UI manager will not draw this element if false
  10. UIElement.active = true -- UI manager will not update, will not handle inputs for this element if false
  11. UIElement.drawable = true -- initial definition: elements created with this flag true are inserted into UIManager's drawing loop
  12. UIElement.input = true
  13. UIElement.updateable = false
  14. UIElement.colorFill = {128,128,128,128} -- default fill color, you can change it in definition or at runtime for specific instance
  15. UIElement.colorDisabledFill = {32,32,32,128}
  16. UIElement.colorLine = {192,192,192,128}
  17. UIElement.colorFont = {255,255,255,255}
  18. UIElement.colorHardFill = {64,64,64,255}
  19. UIElement.colorHighlight = {192,192,192,128}
  20. UIElement.ident = "ui_uielement"
  21. UIElement.name = "UIElement"
  22. UIElement.caption_xpad = 0 -- if element has caption in it, it will draw caption with shift text's position with these coordinates
  23. UIElement.caption_ypad = 0
  24. UIElement.blendMode = "alpha" -- can be used for element to change its blend mode while drawing
  25. UIElement.font = l_gfx:getFont()
  26. function UIElement:new(name)
  27. local self = {}
  28. setmetatable(self,UIElement)
  29. if name ~= nil then self.name = name end
  30. return self
  31. end
  32. setmetatable(UIElement,{__index = Element}) -- drawable UIElement inherits stuff from Element
  33. function UIElement:update(dt) end
  34. function UIElement:draw() end -- since its a drawable class, it should have its draw method %)
  35. function UIElement:mousepressed(x,y,b) if self:isMouseOver(x,y) then return self:click(b) end end
  36. function UIElement:mousereleased(x,y,b) if self:isMouseOver(x,y) then return self:unclick(b) end end
  37. function UIElement:mousemoved(x,y,dx,dy) if self:isMouseOver(x,y) then self:hover() end end
  38. function UIElement:hover() end -- currently doesnt have its uses, maybe one day
  39. function UIElement:click(b) end -- this function defines your element's behaviour when user clicks on it
  40. function UIElement:unclick(b) end -- ... and when releases a mouse button
  41. function UIElement:isMouseOver(x,y) -- checks whether or not mouse pointer touches element's box (from its x,y position up to its width and height)
  42. local mx,my = love.mouse:getPosition()
  43. x = x or mx
  44. y = y or my
  45. if x>=self.x and x<=self.x+self.w and y>=self.y and y<=self.y+self.h then
  46. return true
  47. else
  48. return false
  49. end
  50. end
  51. function UIElement:hide(act) self.visible = false self.active = act or false end -- hides an element and makes it inactive if argument is not specified
  52. function UIElement:show(act) self.visible = true self.active = act or true end -- shows an element and makes it active if argument is not specified
  53. function UIElement:setPosition(x,y) self.x = x or self.x self.y = y or self.y end -- sets element's position
  54. function UIElement:setSize(w,h) self.w = w or self.w self.h = h or self.h end -- sets element box size
  55. function UIElement:getPosition() return self.x,self.y end