Advertisement
MegaLoler

Love Game Engine GUI Elements

Apr 8th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. require("class")
  2. require("engine")
  3.  
  4. -- GUIElement class
  5.  
  6. engine.GUIElement = newClass(engine.Entity)
  7.  
  8. -- GUIElement methods
  9.  
  10. function engine.GUIElement:setHandler(handler, ...)
  11.     self.handler = handler
  12.     self.handleArgs = ...
  13. end
  14.  
  15. -- Protected methods
  16.  
  17. function engine.GUIElement:handle(...)
  18.     self:action(...)
  19.     if self.handler then self.handler(self, self.handleArgs) end
  20. end
  21.  
  22. -- Virtual methods
  23.  
  24. function engine.GUIElement:action(...)
  25. end
  26.  
  27. -- Container class
  28.  
  29. engine.Container = newClass(engine.GUIElement)
  30.  
  31. -- Container methods
  32.  
  33. function engine.Container:setup(width, height)
  34.     self:setSize(width or 100, height or 100)
  35.     self:setBackgroundColor({200, 200, 200})
  36.     self:setBorderColor()
  37.     self.borderThickness = 2
  38.     self.hasBackground = true
  39.     self.selectable = true
  40. end
  41.  
  42. function engine.Container:setBorderColor(color)
  43.     self.borderColor = color or {50, 50, 50}
  44. end
  45.  
  46. function engine.Container:draw()
  47.     love.graphics.setLineWidth(self.borderThickness)
  48.     love.graphics.setColor(self.borderColor[1], self.borderColor[2], self.borderColor[3])
  49.     local a = self.borderThickness / 2
  50.     love.graphics.polygon("line", a, a, self.width - a, a, self.width - a, self.height - a, a, self.height - a)
  51. end
  52.  
  53. -- Label class
  54.  
  55. engine.Label = newClass(engine.GUIElement)
  56.  
  57. -- Label methods
  58.  
  59. function engine.Label:setup(text)
  60.     self:setSize(1, 1)
  61.     self:setTextColor()
  62.     self:setFont()
  63.     self:setText(text)
  64. end
  65.  
  66. function engine.Label:setText(text)
  67.     self.text = text
  68.     self:updateSize()
  69. end
  70.  
  71. function engine.Label:setTextColor(color)
  72.     self.textColor = color or {50, 50, 50}
  73. end
  74.  
  75. function engine.Label:setFont(font)
  76.     self.font = font or love.graphics.newFont(12)
  77.     if self.text then self:updateSize() end
  78. end
  79.  
  80. function engine.Label:preDraw()
  81.     love.graphics.setColor(self.textColor[1], self.textColor[2], self.textColor[3])
  82.     love.graphics.setFont(self.font)
  83.     love.graphics.print(self.text, 0, 0)
  84. end
  85.  
  86. -- Private methods
  87.  
  88. function engine.Label:updateSize()
  89.     self:setSize(self.font:getWidth(self.text), self.font:getHeight())
  90. end
  91.  
  92. -- Button class
  93.  
  94. engine.Button = newClass(engine.Label)
  95.  
  96. function engine.Button:setup(text, handler, ...)
  97.     self:setHandler(handler, ...)
  98.     self.borderThickness = 2
  99.     self:getSuperClass():setup(text)
  100.     self:setBackgroundColor({200, 200, 200})
  101.     self:setBorderColor()
  102.     self.hasBackground = true
  103.     self.selectable = true
  104. end
  105.  
  106. engine.Button.setBorderColor = engine.Container.setBorderColor
  107.  
  108. function engine.Button:focus(f)
  109.     if f then
  110.         self:setBackgroundColor({150, 150, 255})
  111.     else
  112.         self:setBackgroundColor({200, 200, 200})
  113.     end
  114.     self:refresh()
  115. end
  116.  
  117. function engine.Button:mousepressed(x, y, button)
  118.     self:refresh()
  119. end
  120.  
  121. engine.Button.mousereleased = engine.Button.mousepressed
  122.  
  123. function engine.Button:mouseclicked(x, y, button)
  124.     self:handle()
  125. end
  126.  
  127. function engine.Button:draw()
  128.     love.graphics.setBlendMode("alpha")
  129.     love.graphics.setLineWidth(self.borderThickness)
  130.     love.graphics.setColor(self.borderColor[1], self.borderColor[2], self.borderColor[3])
  131.     local a = self.borderThickness / 2
  132.     love.graphics.polygon("line", a, a, self.width - a, a, self.width - a, self.height - a, a, self.height - a)
  133.     if love.mouse.isDown("l") and self.selected then
  134.         love.graphics.setBlendMode("multiplicative")
  135.         love.graphics.setColor(0, 0, 0, 127)
  136.         love.graphics.polygon("fill", 0, 0, self.width, 0, self.width, self.height, 0, self.height)
  137.     end
  138. end
  139.  
  140. function engine.Label:preDraw()
  141.     love.graphics.setColor(self.textColor[1], self.textColor[2], self.textColor[3])
  142.     love.graphics.setFont(self.font)
  143.     love.graphics.print(self.text, self.borderThickness * 2, self.borderThickness * 2)
  144. end
  145.  
  146. -- Private methods
  147.  
  148. function engine.Button:updateSize()
  149.     self:setSize(self.font:getWidth(self.text) + self.borderThickness * 4, self.font:getHeight() + self.borderThickness * 4)
  150. end
  151.  
  152. -- TextField class
  153.  
  154. engine.TextField = newClass(engine.Button)
  155.  
  156. function engine.TextField:setup(text, width, height, handler, ...)
  157.     self:setHandler(handler, ...)
  158.     self.borderThickness = 2
  159.     self:getSuperClass():getClass():setup(text)
  160.     self:setBackgroundColor({200, 200, 200})
  161.     self:setBorderColor()
  162.     self.hasBackground = true
  163.     self.selectable = true
  164.     self:setSize(width or 125, height or 25)
  165. end
  166.  
  167. function engine.TextField:keypressed(key, unicode)
  168.     if key == "return" then
  169.         self:handle()
  170.     elseif key == "backspace" then
  171.         self:setText(string.sub(self.text, 1, #self.text - 1))
  172.     elseif unicode > 31 and unicode < 127 then
  173.         self:setText(self.text .. string.char(unicode))
  174.     end
  175.     self:refresh()
  176. end
  177.  
  178. function engine.Button:mouseclicked(x, y, button)
  179. end
  180.  
  181. -- Private methods
  182.  
  183. function engine.TextField:updateSize()
  184. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement