diff --git a/config.ld b/config.ld index 01c7a66..97489bd 100644 --- a/config.ld +++ b/config.ld @@ -1,7 +1,10 @@ file = { "init.moon", + "elements/box.moon", "elements/element.moon", + "elements/text.moon", "elements/window.moon", + "extensions/streamlined_get_set.moon", "main.moon", "util.moon", "Element.luadoc", diff --git a/elements/element.lua b/elements/element.lua index 72a9772..b54d458 100644 --- a/elements/element.lua +++ b/elements/element.lua @@ -22,16 +22,20 @@ do self.data.x = self.parent.data.x self.data.y = self.parent.data.y local _exp_0 = self.data.horizontal - if "center" == _exp_0 then + if "left" == _exp_0 then + self.data.x = self.data.x + self.data.padding + elseif "center" == _exp_0 then self.data.x = self.data.x + ((self.parent.data.w - self.data.w) / 2) elseif "right" == _exp_0 then - self.data.x = self.data.x + (self.parent.data.w - self.data.w) + self.data.x = self.data.x + (self.parent.data.w - self.data.w - self.data.padding) end local _exp_1 = self.data.vertical - if "center" == _exp_1 then + if "top" == _exp_1 then + self.data.y = self.data.y + self.data.padding + elseif "center" == _exp_1 then self.data.y = self.data.y + ((self.parent.data.h - self.data.h) / 2) elseif "bottom" == _exp_1 then - self.data.y = self.data.y + (self.parent.data.h - self.data.h) + self.data.y = self.data.y + (self.parent.data.h - self.data.h - self.data.padding) end if toPixel then self.data.x = floor(self.data.x) @@ -46,21 +50,21 @@ do local dx, dy = self.data.x, self.data.y if x then self.data.x = x + local _exp_0 = self.data.horizontal + if "center" == _exp_0 then + self.data.x = self.data.x - (self.data.w / 2) + elseif "right" == _exp_0 then + self.data.x = self.data.x - self.data.w + end end if y then self.data.y = y - end - local _exp_0 = self.data.horizontal - if "center" == _exp_0 then - self.data.x = self.data.x - (self.data.w / 2) - elseif "right" == _exp_0 then - self.data.x = self.data.x - self.data.w - end - local _exp_1 = self.data.vertical - if "center" == _exp_1 then - self.data.y = self.data.y - (self.data.h / 2) - elseif "bottom" == _exp_1 then - self.data.y = self.data.y - self.data.h + local _exp_0 = self.data.vertical + if "center" == _exp_0 then + self.data.y = self.data.y - (self.data.h / 2) + elseif "bottom" == _exp_0 then + self.data.y = self.data.y - self.data.h + end end if toPixel then self.data.x = floor(self.data.x) @@ -76,7 +80,20 @@ do return self end, getPosition = function(self) - return self.data.x, self.data.y + local x, y = self.data.x, self.data.y + local _exp_0 = self.data.horizontal + if "center" == _exp_0 then + x = x + (self.data.w / 2) + elseif "right" == _exp_0 then + y = y + self.data.w + end + local _exp_1 = self.data.vertical + if "center" == _exp_1 then + y = y + (self.data.h / 2) + elseif "bottom" == _exp_1 then + y = y + self.data.h + end + return x, y end, setSize = function(self, w, h) if w then @@ -93,6 +110,7 @@ do end, setWidth = function(self, w) self.data.w = w + self:align() return self end, getWidth = function(self) @@ -100,11 +118,23 @@ do end, setHeight = function(self, h) self.data.h = h + self:align() return self end, getHeight = function(self) return self.data.h end, + adjustSize = function(self, w, h) + local W, H = self:getSize() + if w then + W = W + w + end + if h then + H = H + h + end + self:setSize(W, H) + return self + end, move = function(self, x, y) if x == nil then x = 0 @@ -112,15 +142,23 @@ do if y == nil then y = 0 end + self.data.x = self.data.x + x + self.data.y = self.data.y + y local _list_0 = self.child for _index_0 = 1, #_list_0 do local child = _list_0[_index_0] child:move(x, y) end - self.data.x = self.data.x + x - self.data.y = self.data.y + y return self end, + setPadding = function(self, padding) + self.data.padding = padding + self:align() + return self + end, + getPadding = function(self) + return self.data.padding + end, delete = function(self) for i = #self.child, 1, -1 do self.child[i]:delete() @@ -166,10 +204,18 @@ do self.data.type = "element" end if not (self.data.x) then - self.data.x = 0 + if self.parent then + self.data.x = self.parent.data.x + else + self.data.x = 0 + end end if not (self.data.y) then - self.data.y = 0 + if self.parent then + self.data.y = self.parent.data.y + else + self.data.y = 0 + end end if not (self.data.w) then self.data.w = 0 @@ -195,6 +241,9 @@ do if not (self.data.horizontal) then self.data.horizontal = "left" end + if not (self.data.padding) then + self.data.padding = 0 + end self.child = { } end, __base = _base_0, diff --git a/elements/element.moon b/elements/element.moon index e75677a..d5e5404 100644 --- a/elements/element.moon +++ b/elements/element.moon @@ -19,18 +19,28 @@ class element @data.child = {} unless @data.child @data.type = "element" unless @data.type - @data.x = 0 unless @data.x - @data.y = 0 unless @data.y + unless @data.x + if @parent + @data.x = @parent.data.x + else + @data.x = 0 + unless @data.y + if @parent + @data.y = @parent.data.y + else + @data.y = 0 @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.static = false if @data.static == nil @data.align = true if (@data.align == nil) and @parent @data.vertical = "top" unless @data.vertical @data.horizontal = "left" unless @data.horizontal + @data.padding = 0 unless @data.padding @child = {} @@ -45,16 +55,20 @@ class element @data.y = @parent.data.y switch @data.horizontal + when "left" + @data.x += @data.padding when "center" @data.x += (@parent.data.w - @data.w) / 2 when "right" - @data.x += @parent.data.w - @data.w + @data.x += @parent.data.w - @data.w - @data.padding switch @data.vertical + when "top" + @data.y += @data.padding when "center" @data.y += (@parent.data.h - @data.h) / 2 when "bottom" - @data.y += @parent.data.h - @data.h + @data.y += @parent.data.h - @data.h - @data.padding if toPixel @data.x = floor @data.x @@ -67,23 +81,20 @@ class element dx, dy = @data.x, @data.y if x - --dx = x - @data.x @data.x = x + switch @data.horizontal + when "center" + @data.x -= @data.w / 2 + when "right" + @data.x -= @data.w + if y - --dy = y - @data.y @data.y = y - - switch @data.horizontal - when "center" - @data.x -= @data.w / 2 - when "right" - @data.x -= @data.w - - switch @data.vertical - when "center" - @data.y -= @data.h / 2 - when "bottom" - @data.y -= @data.h + switch @data.vertical + when "center" + @data.y -= @data.h / 2 + when "bottom" + @data.y -= @data.h if toPixel @data.x = floor @data.x @@ -98,9 +109,22 @@ class element return @ --- @todo doc me - --- @todo rewrite me to return value based on alignment instead of just x/y getPosition: => - return @data.x, @data.y + x, y = @data.x, @data.y + + switch @data.horizontal + when "center" + x += @data.w / 2 + when "right" + y += @data.w + + switch @data.vertical + when "center" + y += @data.h / 2 + when "bottom" + y += @data.h + + return x, y --- Sets an element's width/height. Fixes alignment if needed. --- @tparam integer w[opt] Width. @@ -127,6 +151,7 @@ class element --- @treturn element self setWidth: (w) => @data.w = w + @align! return @ --- Returns an element's width. @@ -139,6 +164,7 @@ class element --- @treturn element self setHeight: (h) => @data.h = h + @align! return @ --- Returns an element's height. @@ -146,16 +172,40 @@ class element getHeight: => return @data.h + --- @todo doc me + adjustSize: (w, h) => + W, H = @getSize! + + if w + W += w + if h + H += h + + @setSize W, H + + return @ + --- Moves an element by specified x/y. --- @treturn element self move: (x=0, y=0) => - for child in *@child - child\move x, y + --if @data.static return @ @data.x += x @data.y += y + + for child in *@child + child\move x, y + return @ + setPadding: (padding) => + @data.padding = padding + @align! + return @ + + getPadding: => + return @data.padding + --- Deletes references to this element and then deletes it. delete: => for i=#@child, 1, -1 diff --git a/extensions/streamlined_get_set.lua b/extensions/streamlined_get_set.lua new file mode 100644 index 0000000..32bd558 --- /dev/null +++ b/extensions/streamlined_get_set.lua @@ -0,0 +1,39 @@ +local graphics +graphics = love.graphics +local sub, len +do + local _obj_0 = string + sub, len = _obj_0.sub, _obj_0.len +end +local path = sub(..., 1, len(...) - len("/extensions/streamlined_get_set")) +local element = require(tostring(path) .. "/elements/element") +local box = require(tostring(path) .. "/elements/box") +local text = require(tostring(path) .. "/elements/text") +element.__base.position = function(self, x, y) + if x or y then + return self:setPosition(x, y) + else + return self:getPosition() + end +end +element.__base.size = function(self, w, h) + if w or h then + return self:setSize(w, h) + else + return self:getSize() + end +end +element.__base.width = function(self, w) + if w then + return self:setWidth(w) + else + return self:getWidth() + end +end +elements.__base.height = function(self, h) + if h then + return self:setHeight(h) + else + return self:getHeight() + end +end diff --git a/extensions/streamlined_get_set.moon b/extensions/streamlined_get_set.moon new file mode 100644 index 0000000..d40bc67 --- /dev/null +++ b/extensions/streamlined_get_set.moon @@ -0,0 +1,37 @@ +--- @todo doc me (and add me to config.ld) +-- Adds methods to elements using a single function for get and set operations. +-- ex: instead of getWidth() and setWidth(value), width() and width(value) + +import graphics from love +import sub, len from string + +path = sub ..., 1, len(...) - len "/extensions/streamlined_get_set" +element = require "#{path}/elements/element" +box = require "#{path}/elements/box" +text = require "#{path}/elements/text" + +element.__base.position = (x, y) => + if x or y + return @setPosition x, y + else + return @getPosition! + +element.__base.size = (w, h) => + if w or h + return @setSize w, h + else + return @getSize! + +element.__base.width = (w) => + if w + return @setWidth w + else + return @getWidth! + +elements.__base.height = (h) => + if h + return @setHeight h + else + return @getHeight! + +--- @todo continue copying from old version... (and add new things or whatever) diff --git a/extensions/utility.lua b/extensions/utility.lua new file mode 100644 index 0000000..4a3506f --- /dev/null +++ b/extensions/utility.lua @@ -0,0 +1,15 @@ +local graphics +graphics = love.graphics +local sub, len +do + local _obj_0 = string + sub, len = _obj_0.sub, _obj_0.len +end +local path = sub(..., 1, len(...) - len("/extensions/streamlined_get_set")) +local element = require(tostring(path) .. "/elements/element") +element.__base.fill = function(self) + self.data.x = self.parent.data.x + self.data.padding + self.data.y = self.parent.data.y + self.data.padding + self.data.w = self.parent.data.w - self.data.padding * 2 + self.data.h = self.parent.data.h - self.data.padding * 2 +end diff --git a/extensions/utility.moon b/extensions/utility.moon new file mode 100644 index 0000000..566e279 --- /dev/null +++ b/extensions/utility.moon @@ -0,0 +1,17 @@ +--- @todo doc me +--- Functions I am not certain should be part of base classes, but nevertheless +--- may be useful. + +import graphics from love +import sub, len from string + +path = sub ..., 1, len(...) - len "/extensions/streamlined_get_set" +element = require "#{path}/elements/element" +--box = require "#{path}/elements/box" +--text = require "#{path}/elements/text" + +element.__base.fill = => + @data.x = @parent.data.x + @data.padding + @data.y = @parent.data.y + @data.padding + @data.w = @parent.data.w - @data.padding*2 + @data.h = @parent.data.h - @data.padding*2 diff --git a/main.lua b/main.lua index 4acc4b1..04505b2 100644 --- a/main.lua +++ b/main.lua @@ -1,10 +1,144 @@ local pop = require("") -pop.text("Hello World!"):align("center", "center") +local debug = false +love.load = function() + pop.text("Hello World!"):align("center", "center") + local centerBox = pop.box({ + w = 200, + h = 200 + }, { + 255, + 255, + 0, + 120 + }):align("center", "center") + pop.box(centerBox, { + w = 10, + h = 20 + }):align("left", "top") + pop.box(centerBox, { + w = 30, + h = 30 + }):align("center", "top") + pop.box(centerBox, { + w = 5, + h = 40 + }):align("left", "center") + pop.box(centerBox, { + w = 50, + h = 50 + }):align("right", "center") + pop.box(centerBox):align("left", "bottom"):setSize(5, 5) + pop.box(centerBox, { + w = 25, + h = 10 + }):align("center", "bottom") + pop.text(centerBox, "Align me!"):align("right", "top") + pop.window(centerBox):align("right", "bottom") + pop.box(centerBox, { + padding = 5, + w = 10, + h = 20, + background = { + 0, + 0, + 255, + 100 + } + }):align("left", "top") + pop.box(centerBox, { + padding = 5, + w = 30, + h = 30, + background = { + 0, + 0, + 255, + 100 + } + }):align("center", "top") + pop.box(centerBox, { + padding = 5, + w = 5, + h = 40, + background = { + 0, + 0, + 255, + 100 + } + }):align("left", "center") + pop.box(centerBox, { + padding = 5, + w = 50, + h = 50, + background = { + 0, + 0, + 255, + 100 + } + }):align("right", "center") + pop.text(centerBox, { + padding = 5, + color = { + 0, + 0, + 255, + 100 + } + }, "Text!"):align("left", "bottom") + pop.box(centerBox, { + padding = 5, + w = 25, + h = 10, + background = { + 0, + 0, + 255, + 100 + } + }):align("center", "bottom") + pop.text(centerBox, { + padding = 5, + color = { + 0, + 0, + 255, + 100 + } + }, "Align me!"):align("right", "top") + return pop.window(centerBox, { + padding = 5, + titleColor = { + 0, + 0, + 0, + 150 + }, + titleBackground = { + 0, + 0, + 255, + 100 + }, + windowBackground = { + 200, + 200, + 255, + 100 + } + }):align("right", "bottom") +end love.draw = function() - return pop.draw() + pop.draw() + if debug then + return pop.debugDraw() + end end love.keypressed = function(key) if key == "escape" then return love.event.quit() + elseif key == "d" then + debug = not debug end end diff --git a/main.moon b/main.moon index 2b0cf74..b6b37f6 100644 --- a/main.moon +++ b/main.moon @@ -3,18 +3,42 @@ --- @license The MIT License (MIT) pop = require "" +debug = false -pop.text("Hello World!")\align "center", "center" +love.load = -> + pop.text("Hello World!")\align "center", "center" + + -- alignment testing + centerBox = pop.box({w: 200, h: 200}, {255, 255, 0, 120})\align "center", "center" + pop.box(centerBox, {w: 10, h: 20})\align "left", "top" + pop.box(centerBox, {w: 30, h: 30})\align "center", "top" + pop.box(centerBox, {w: 5, h: 40})\align "left", "center" + pop.box(centerBox, {w: 50, h: 50})\align "right", "center" + pop.box(centerBox)\align("left", "bottom")\setSize 5, 5 + pop.box(centerBox, {w: 25, h: 10})\align "center", "bottom" + pop.text(centerBox, "Align me!")\align "right", "top" + pop.window(centerBox)\align "right", "bottom" + + pop.box(centerBox, {padding: 5, w: 10, h: 20, background: {0, 0, 255, 100}})\align "left", "top" + pop.box(centerBox, {padding: 5, w: 30, h: 30, background: {0, 0, 255, 100}})\align "center", "top" + pop.box(centerBox, {padding: 5, w: 5, h: 40, background: {0, 0, 255, 100}})\align "left", "center" + pop.box(centerBox, {padding: 5, w: 50, h: 50, background: {0, 0, 255, 100}})\align "right", "center" + pop.text(centerBox, {padding: 5, color: {0, 0, 255, 100}}, "Text!")\align("left", "bottom")--\setSize 5, 5 + pop.box(centerBox, {padding: 5, w: 25, h: 10, background: {0, 0, 255, 100}})\align "center", "bottom" + pop.text(centerBox, {padding: 5, color: {0, 0, 255, 100}}, "Align me!")\align "right", "top" + pop.window(centerBox, {padding: 5, titleColor: {0, 0, 0, 150}, titleBackground: {0, 0, 255, 100}, windowBackground: {200, 200, 255, 100}})\align "right", "bottom" --- @todo finish writing callbacks! love.draw = -> pop.draw! - --pop.debugDraw! + pop.debugDraw! if debug love.keypressed = (key) -> if key == "escape" love.event.quit! + elseif key == "d" + debug = not debug