ui_element.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. -- root element class
  2. -- defines the element itself, usually invisible and is usefull for elements
  3. -- which are invisible but have their purpose (e.g. Timer element, or Collection element)
  4. Element = {}
  5. Element.__index = Element
  6. Element.active = true -- putting this to true will make UIManager to handle this object, like updating, drawing, etc...
  7. Element.drawable = false -- putting this to true will make UIManager to try to draw this element by calling its draw() method
  8. Element.input = true -- this will make UIManager to send input to this object by firing according methods
  9. Element.updateable = false -- this will make UIManager to try to invoke update(dt) method
  10. Element.name = "Element" -- this identifier should be unique to every element you create, so then you can find it without storing a reference variable
  11. Element.ident = "ui_element" -- this is a type identifier, for, say "make all buttons go invisible" or something
  12. function Element:new(name) -- element instancing
  13. local self = {}
  14. setmetatable(self,Element)
  15. if name ~= nil then self.name = name end -- if name is not specified, it will use its default name
  16. return self
  17. end
  18. -- all the methods are usually fired by uimanager
  19. function Element:getName() return self.name end
  20. function Element:oncreate() end
  21. function Element:keypressed(key,isrepeat) end
  22. function Element:keyreleased(key) end
  23. function Element:mousepressed(x,y,b) end
  24. function Element:mousereleased(x,y,b) end
  25. function Element:wheelmoved(x,y) end
  26. function Element:mousemoved(x,y,dx,dy) end
  27. function Element:onchangewindow(w,h) end
  28. function Element:textinput(t) end
  29. Container = {}
  30. Container.__index = Container
  31. Container.name = "Container"
  32. Container.ident = "ui_container"
  33. function Container:new(name)
  34. local self = setmetatable({},Container)
  35. self.name = name or self.name
  36. return self
  37. end
  38. setmetatable(Container,{__index = Element})
  39. function Container:addItem(item)
  40. table.insert(self.items,item)
  41. end
  42. function Container:getItem(name)
  43. local c = #self.items
  44. if c>0 then
  45. for i=1,c do
  46. if self.items[i]:getName() == item then return self.items[i]
  47. elseif self.items[i].items ~= nil then return self.items[i]:getItem(name) end
  48. end
  49. end
  50. return nil
  51. end
  52. function Container:deleteItem(name)
  53. local c = #self.items
  54. if c>0 then
  55. for k,v in pairs(self.items) do
  56. self.items[k] = nil
  57. end
  58. end
  59. end