Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("class")
- require("engine")
- -- GUIElement class
- engine.GUIElement = newClass(engine.Entity)
- -- GUIElement methods
- function engine.GUIElement:setHandler(handler, ...)
- self.handler = handler
- self.handleArgs = ...
- end
- -- Protected methods
- function engine.GUIElement:handle(...)
- self:action(...)
- if self.handler then self.handler(self, self.handleArgs) end
- end
- -- Virtual methods
- function engine.GUIElement:action(...)
- end
- -- Container class
- engine.Container = newClass(engine.GUIElement)
- -- Container methods
- function engine.Container:setup(width, height)
- self:setSize(width or 100, height or 100)
- self:setBackgroundColor({200, 200, 200})
- self:setBorderColor()
- self.borderThickness = 2
- self.hasBorder = true
- self.hasBackground = true
- self.selectable = true
- end
- function engine.Container:setBorderColor(color)
- self.borderColor = color or {50, 50, 50}
- end
- function engine.Container:draw()
- if self.hasBorder then
- love.graphics.setLineWidth(self.borderThickness)
- love.graphics.setColor(self.borderColor[1], self.borderColor[2], self.borderColor[3])
- local a = self.borderThickness / 2
- love.graphics.polygon("line", a, a, self.width - a, a, self.width - a, self.height - a, a, self.height - a)
- end
- end
- -- Label class
- engine.Label = newClass(engine.GUIElement)
- -- Label methods
- function engine.Label:setup(text)
- self:setSize(1, 1)
- self:setTextColor()
- self:setFont()
- self:setText(text)
- self.hasShadow = false
- self.shadowOffsetX = -2
- self.shadowOffsetY = 2
- self.shadowAlpha = 127
- end
- function engine.Label:setText(text)
- self.text = text
- self:updateSize()
- end
- function engine.Label:setTextColor(color)
- self.textColor = color or {50, 50, 50}
- end
- function engine.Label:setFont(font)
- self.font = font or love.graphics.newFont(12)
- if self.text then self:updateSize() end
- end
- function engine.Label:preDraw()
- love.graphics.setFont(self.font)
- if self.hasShadow then
- love.graphics.setColor(0, 0, 0, self.shadowAlpha)
- love.graphics.print(self.text, self.shadowOffsetX, self.shadowOffsetY)
- end
- love.graphics.setColor(self.textColor[1], self.textColor[2], self.textColor[3])
- love.graphics.print(self.text, 0, 0)
- end
- -- Private methods
- function engine.Label:updateSize()
- self:setSize(self.font:getWidth(self.text), self.font:getHeight())
- end
- -- Button class
- engine.Button = newClass(engine.Label)
- function engine.Button:setup(text, handler, ...)
- self:setHandler(handler, ...)
- self.borderThickness = 2
- self:getSuperClass():setup(text)
- self:setBackgroundColor({200, 200, 200})
- self:setBorderColor()
- self.hasBackground = true
- self.selectable = true
- end
- engine.Button.setBorderColor = engine.Container.setBorderColor
- function engine.Button:focus(f)
- if f then
- self:setBackgroundColor({150, 150, 255})
- else
- self:setBackgroundColor({200, 200, 200})
- end
- self:refresh()
- end
- function engine.Button:mousepressed(x, y, button)
- self:refresh()
- end
- engine.Button.mousereleased = engine.Button.mousepressed
- function engine.Button:mouseclicked(x, y, button)
- self:handle()
- end
- function engine.Button:draw()
- love.graphics.setBlendMode("alpha")
- love.graphics.setLineWidth(self.borderThickness)
- love.graphics.setColor(self.borderColor[1], self.borderColor[2], self.borderColor[3])
- local a = self.borderThickness / 2
- love.graphics.polygon("line", a, a, self.width - a, a, self.width - a, self.height - a, a, self.height - a)
- if love.mouse.isDown("l") and self.selected then
- love.graphics.setBlendMode("multiplicative")
- love.graphics.setColor(0, 0, 0, 127)
- love.graphics.polygon("fill", 0, 0, self.width, 0, self.width, self.height, 0, self.height)
- end
- end
- function engine.Button:preDraw()
- love.graphics.setColor(self.textColor[1], self.textColor[2], self.textColor[3])
- love.graphics.setFont(self.font)
- love.graphics.print(self.text, self.borderThickness * 2, self.borderThickness * 2)
- end
- -- Private methods
- function engine.Button:updateSize()
- self:setSize(self.font:getWidth(self.text) + self.borderThickness * 4, self.font:getHeight() + self.borderThickness * 4)
- end
- -- TextField class
- engine.TextField = newClass(engine.Button)
- function engine.TextField:setup(text, width, height, handler, ...)
- self:setHandler(handler, ...)
- self.borderThickness = 2
- self:getSuperClass():getClass():setup(text)
- self:setBackgroundColor({200, 200, 200})
- self:setBorderColor()
- self.hasBackground = true
- self.selectable = true
- self:setSize(width or 125, height or 25)
- end
- function engine.TextField:keypressed(key, unicode)
- if key == "return" then
- self:handle()
- elseif key == "backspace" then
- self:setText(string.sub(self.text, 1, #self.text - 1))
- elseif unicode > 31 and unicode < 127 then
- self:setText(self.text .. string.char(unicode))
- end
- self:refresh()
- end
- function engine.TextField:mouseclicked(x, y, button)
- end
- -- Private methods
- function engine.TextField:updateSize()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement