STUFFS EVERYWHERE

This commit is contained in:
Paul Liverman III 2017-04-30 14:13:56 -07:00
parent 44ab484fec
commit a2db191adf
9 changed files with 415 additions and 47 deletions

View File

@ -1,7 +1,10 @@
file = { file = {
"init.moon", "init.moon",
"elements/box.moon",
"elements/element.moon", "elements/element.moon",
"elements/text.moon",
"elements/window.moon", "elements/window.moon",
"extensions/streamlined_get_set.moon",
"main.moon", "main.moon",
"util.moon", "util.moon",
"Element.luadoc", "Element.luadoc",

View File

@ -22,16 +22,20 @@ do
self.data.x = self.parent.data.x self.data.x = self.parent.data.x
self.data.y = self.parent.data.y self.data.y = self.parent.data.y
local _exp_0 = self.data.horizontal 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) self.data.x = self.data.x + ((self.parent.data.w - self.data.w) / 2)
elseif "right" == _exp_0 then 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 end
local _exp_1 = self.data.vertical 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) self.data.y = self.data.y + ((self.parent.data.h - self.data.h) / 2)
elseif "bottom" == _exp_1 then 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 end
if toPixel then if toPixel then
self.data.x = floor(self.data.x) self.data.x = floor(self.data.x)
@ -46,22 +50,22 @@ do
local dx, dy = self.data.x, self.data.y local dx, dy = self.data.x, self.data.y
if x then if x then
self.data.x = x self.data.x = x
end
if y then
self.data.y = y
end
local _exp_0 = self.data.horizontal local _exp_0 = self.data.horizontal
if "center" == _exp_0 then if "center" == _exp_0 then
self.data.x = self.data.x - (self.data.w / 2) self.data.x = self.data.x - (self.data.w / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
self.data.x = self.data.x - self.data.w self.data.x = self.data.x - self.data.w
end end
local _exp_1 = self.data.vertical end
if "center" == _exp_1 then if y then
self.data.y = y
local _exp_0 = self.data.vertical
if "center" == _exp_0 then
self.data.y = self.data.y - (self.data.h / 2) self.data.y = self.data.y - (self.data.h / 2)
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_0 then
self.data.y = self.data.y - self.data.h self.data.y = self.data.y - self.data.h
end end
end
if toPixel then if toPixel then
self.data.x = floor(self.data.x) self.data.x = floor(self.data.x)
self.data.y = floor(self.data.y) self.data.y = floor(self.data.y)
@ -76,7 +80,20 @@ do
return self return self
end, end,
getPosition = function(self) 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, end,
setSize = function(self, w, h) setSize = function(self, w, h)
if w then if w then
@ -93,6 +110,7 @@ do
end, end,
setWidth = function(self, w) setWidth = function(self, w)
self.data.w = w self.data.w = w
self:align()
return self return self
end, end,
getWidth = function(self) getWidth = function(self)
@ -100,11 +118,23 @@ do
end, end,
setHeight = function(self, h) setHeight = function(self, h)
self.data.h = h self.data.h = h
self:align()
return self return self
end, end,
getHeight = function(self) getHeight = function(self)
return self.data.h return self.data.h
end, 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) move = function(self, x, y)
if x == nil then if x == nil then
x = 0 x = 0
@ -112,15 +142,23 @@ do
if y == nil then if y == nil then
y = 0 y = 0
end end
self.data.x = self.data.x + x
self.data.y = self.data.y + y
local _list_0 = self.child local _list_0 = self.child
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
local child = _list_0[_index_0] local child = _list_0[_index_0]
child:move(x, y) child:move(x, y)
end end
self.data.x = self.data.x + x
self.data.y = self.data.y + y
return self return self
end, 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) delete = function(self)
for i = #self.child, 1, -1 do for i = #self.child, 1, -1 do
self.child[i]:delete() self.child[i]:delete()
@ -166,11 +204,19 @@ do
self.data.type = "element" self.data.type = "element"
end end
if not (self.data.x) then if not (self.data.x) then
if self.parent then
self.data.x = self.parent.data.x
else
self.data.x = 0 self.data.x = 0
end end
end
if not (self.data.y) then if not (self.data.y) then
if self.parent then
self.data.y = self.parent.data.y
else
self.data.y = 0 self.data.y = 0
end end
end
if not (self.data.w) then if not (self.data.w) then
self.data.w = 0 self.data.w = 0
end end
@ -195,6 +241,9 @@ do
if not (self.data.horizontal) then if not (self.data.horizontal) then
self.data.horizontal = "left" self.data.horizontal = "left"
end end
if not (self.data.padding) then
self.data.padding = 0
end
self.child = { } self.child = { }
end, end,
__base = _base_0, __base = _base_0,

View File

@ -19,18 +19,28 @@ class element
@data.child = {} unless @data.child @data.child = {} unless @data.child
@data.type = "element" unless @data.type @data.type = "element" unless @data.type
@data.x = 0 unless @data.x unless @data.x
@data.y = 0 unless @data.y 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.w = 0 unless @data.w
@data.h = 0 unless @data.h @data.h = 0 unless @data.h
@data.update = true if @data.update == nil @data.update = true if @data.update == nil
@data.draw = true if @data.draw == nil @data.draw = true if @data.draw == nil
@data.hoverable = true if @data.hoverable == 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.align = true if (@data.align == nil) and @parent
@data.vertical = "top" unless @data.vertical @data.vertical = "top" unless @data.vertical
@data.horizontal = "left" unless @data.horizontal @data.horizontal = "left" unless @data.horizontal
@data.padding = 0 unless @data.padding
@child = {} @child = {}
@ -45,16 +55,20 @@ class element
@data.y = @parent.data.y @data.y = @parent.data.y
switch @data.horizontal switch @data.horizontal
when "left"
@data.x += @data.padding
when "center" when "center"
@data.x += (@parent.data.w - @data.w) / 2 @data.x += (@parent.data.w - @data.w) / 2
when "right" when "right"
@data.x += @parent.data.w - @data.w @data.x += @parent.data.w - @data.w - @data.padding
switch @data.vertical switch @data.vertical
when "top"
@data.y += @data.padding
when "center" when "center"
@data.y += (@parent.data.h - @data.h) / 2 @data.y += (@parent.data.h - @data.h) / 2
when "bottom" when "bottom"
@data.y += @parent.data.h - @data.h @data.y += @parent.data.h - @data.h - @data.padding
if toPixel if toPixel
@data.x = floor @data.x @data.x = floor @data.x
@ -67,18 +81,15 @@ class element
dx, dy = @data.x, @data.y dx, dy = @data.x, @data.y
if x if x
--dx = x - @data.x
@data.x = x @data.x = x
if y
--dy = y - @data.y
@data.y = y
switch @data.horizontal switch @data.horizontal
when "center" when "center"
@data.x -= @data.w / 2 @data.x -= @data.w / 2
when "right" when "right"
@data.x -= @data.w @data.x -= @data.w
if y
@data.y = y
switch @data.vertical switch @data.vertical
when "center" when "center"
@data.y -= @data.h / 2 @data.y -= @data.h / 2
@ -98,9 +109,22 @@ class element
return @ return @
--- @todo doc me --- @todo doc me
--- @todo rewrite me to return value based on alignment instead of just x/y
getPosition: => 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. --- Sets an element's width/height. Fixes alignment if needed.
--- @tparam integer w[opt] Width. --- @tparam integer w[opt] Width.
@ -127,6 +151,7 @@ class element
--- @treturn element self --- @treturn element self
setWidth: (w) => setWidth: (w) =>
@data.w = w @data.w = w
@align!
return @ return @
--- Returns an element's width. --- Returns an element's width.
@ -139,6 +164,7 @@ class element
--- @treturn element self --- @treturn element self
setHeight: (h) => setHeight: (h) =>
@data.h = h @data.h = h
@align!
return @ return @
--- Returns an element's height. --- Returns an element's height.
@ -146,16 +172,40 @@ class element
getHeight: => getHeight: =>
return @data.h 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. --- Moves an element by specified x/y.
--- @treturn element self --- @treturn element self
move: (x=0, y=0) => move: (x=0, y=0) =>
for child in *@child --if @data.static return @
child\move x, y
@data.x += x @data.x += x
@data.y += y @data.y += y
for child in *@child
child\move x, y
return @ return @
setPadding: (padding) =>
@data.padding = padding
@align!
return @
getPadding: =>
return @data.padding
--- Deletes references to this element and then deletes it. --- Deletes references to this element and then deletes it.
delete: => delete: =>
for i=#@child, 1, -1 for i=#@child, 1, -1

View File

@ -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

View File

@ -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)

15
extensions/utility.lua Normal file
View File

@ -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

17
extensions/utility.moon Normal file
View File

@ -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

136
main.lua
View File

@ -1,10 +1,144 @@
local pop = require("") local pop = require("")
local debug = false
love.load = function()
pop.text("Hello World!"):align("center", "center") 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() love.draw = function()
return pop.draw() pop.draw()
if debug then
return pop.debugDraw()
end
end end
love.keypressed = function(key) love.keypressed = function(key)
if key == "escape" then if key == "escape" then
return love.event.quit() return love.event.quit()
elseif key == "d" then
debug = not debug
end end
end end

View File

@ -3,18 +3,42 @@
--- @license The MIT License (MIT) --- @license The MIT License (MIT)
pop = require "" pop = require ""
debug = false
love.load = ->
pop.text("Hello World!")\align "center", "center" 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! --- @todo finish writing callbacks!
love.draw = -> love.draw = ->
pop.draw! pop.draw!
--pop.debugDraw! pop.debugDraw! if debug
love.keypressed = (key) -> love.keypressed = (key) ->
if key == "escape" if key == "escape"
love.event.quit! love.event.quit!
elseif key == "d"
debug = not debug