diff --git a/elements/box.lua b/elements/box.lua new file mode 100644 index 0000000..21eb50d --- /dev/null +++ b/elements/box.lua @@ -0,0 +1,110 @@ +local graphics +graphics = love.graphics +local element = require(tostring((...):sub(1, -4)) .. "/element") +local box +do + local _class_0 + local _parent_0 = element + local _base_0 = { + draw = function(self) + if "table" == type(self.data.background) then + graphics.setColor(self.data.background) + graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h) + else + local w, h = self.data.background:getDimensions() + w = self.data.w / w + h = self.data.h / h + graphics.setColor(255, 255, 255, 255) + graphics.draw(self.data.background, self.data.x, self.data.y, 0, w, h) + end + return self + end, + setBackground = function(self, background) + if background then + self.data.background = background + else + error("Background must be a table representing a color, or a drawable object.") + end + return self + end, + getBackground = function(self) + return self.data.background + end, + setColor = function(self, r, g, b, a) + if a == nil then + a = 255 + end + if "table" == type(r) then + self.data.background = r + else + self.data.background = { + r, + g, + b, + a + } + end + return self + end, + getColor = function(self) + if "table" == type(self.data.background) then + return unpack(self.data.background) + else + return 255, 255, 255, 255 + end + end + } + _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, parent, data, background) + if data == nil then + data = { } + end + if background == nil then + background = { + 255, + 255, + 255, + 255 + } + end + self.parent, self.data = parent, data + if #self.data == 4 then + background = self.data + self.data = nil + end + _class_0.__parent.__init(self, self.parent, self.data) + self.data.type = "box" + if not (self.data.background) then + self.data.background = background + end + end, + __base = _base_0, + __name = "box", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + box = _class_0 + return _class_0 +end diff --git a/elements/box.moon b/elements/box.moon new file mode 100644 index 0000000..fde452c --- /dev/null +++ b/elements/box.moon @@ -0,0 +1,60 @@ +--- A generic box, drawn with specified color or an image. +--- @classmod box +--- @copyright Paul Liverman III (2017) +--- @license The MIT License (MIT) +--- @todo Make 9-slices available! +--- @todo Correct documentation on all elements + +import graphics from love + +element = require "#{(...)\sub 1, -4}/element" + +class box extends element + --- Constructor expects nothing, or a data table describing it. + new: (@parent, @data={}, background={255, 255, 255, 255}) => + -- assume a data object with four values is actually the background + if #@data == 4 + background = @data + @data = nil + + super @parent, @data + + @data.type = "box" + @data.background = background unless @data.background + + draw: => + if "table" == type @data.background + graphics.setColor @data.background + graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h + else + w, h = @data.background\getDimensions! + w = @data.w / w + h = @data.h / h + graphics.setColor 255, 255, 255, 255 + graphics.draw @data.background, @data.x, @data.y, 0, w, h + + return @ + + setBackground: (background) => + if background + @data.background = background + else + error "Background must be a table representing a color, or a drawable object." + return @ + + getBackground: => + return @data.background + + setColor: (r, g, b, a=255) => + if "table" == type r + @data.background = r + else + @data.background = {r, g, b, a} + + return @ + + getColor: => + if "table" == type @data.background + return unpack @data.background + else + return 255, 255, 255, 255 -- if it is drawable, it is drawn with a white color diff --git a/elements/element.lua b/elements/element.lua index 0b5685c..72a9772 100644 --- a/elements/element.lua +++ b/elements/element.lua @@ -162,6 +162,9 @@ do if not (self.data.child) then self.data.child = { } end + if not (self.data.type) then + self.data.type = "element" + end if not (self.data.x) then self.data.x = 0 end @@ -183,9 +186,6 @@ do if self.data.hoverable == nil then self.data.hoverable = true end - if not (self.data.type) then - self.data.type = "element" - end if (self.data.align == nil) and self.parent then self.data.align = true end @@ -196,7 +196,6 @@ do self.data.horizontal = "left" end self.child = { } - return self:align() end, __base = _base_0, __name = "element" diff --git a/elements/element.moon b/elements/element.moon index 3d16ba1..e75677a 100644 --- a/elements/element.moon +++ b/elements/element.moon @@ -17,22 +17,23 @@ class element @data.parent = false unless @data.parent @data.child = {} unless @data.child + @data.type = "element" unless @data.type + @data.x = 0 unless @data.x @data.y = 0 unless @data.y @data.w = 0 unless @data.w @data.h = 0 unless @data.h + @data.update = true if @data.update == nil @data.draw = true if @data.draw == nil @data.hoverable = true if @data.hoverable == nil - @data.type = "element" unless @data.type + @data.align = true if (@data.align == nil) and @parent @data.vertical = "top" unless @data.vertical @data.horizontal = "left" unless @data.horizontal @child = {} - @align! - --- @todo doc me align: (horizontal, vertical, toPixel=true) => unless @data.align return @ diff --git a/init.moon b/init.moon index 30bfb14..a4341b3 100644 --- a/init.moon +++ b/init.moon @@ -57,6 +57,7 @@ if major == 0 and minor == 9 button_4: "x1" button_5: "x2" + -- note: these should not be used mouse_wheel_down: "wd" mouse_wheel_up: "wu" }