mirror of
https://github.com/TangentFoxy/Pop.Box.git
synced 2024-12-15 12:44:20 +00:00
complete rewrite mostly from scratch...mostly the same
This commit is contained in:
parent
a5e3714af8
commit
6571320310
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
lua51.dll
|
||||
moon*.exe
|
||||
moonscript.dll
|
@ -1,4 +1,5 @@
|
||||
@ECHO OFF
|
||||
REM Assumes you have Windows binaries in the project's root directory!
|
||||
cd src
|
||||
"%cd%\..\moonc.exe" -t ../lib .
|
||||
cd ..
|
||||
|
@ -1,5 +1,5 @@
|
||||
function love.conf(t)
|
||||
t.title = "Pop.Box Demo / Tests"
|
||||
t.title = "Pop.Box demo/tests"
|
||||
t.console = true
|
||||
|
||||
t.window.width = 960
|
||||
|
@ -1,17 +1,22 @@
|
||||
local pop = require "pop"
|
||||
|
||||
local lg = love.graphics
|
||||
|
||||
local visualTestsShown = false
|
||||
local testsRun = false
|
||||
local debugDrawEnabled = false
|
||||
local pop
|
||||
|
||||
function love.load()
|
||||
pop.text(nil, "Press \"s\" to show objects for visual testing/demo.\nPress \"t\" to run tests.\nPress \"d\" to toggle debug draw."):move(2, 2)
|
||||
--TODO correct the fact that the size is wrong here! (height doesn't take into account \n)
|
||||
--NOTE width? Is width calculated correctly when \n's exist? TEST THIS (also test tabs)
|
||||
pop.text(nil, "This is a test\ncollection of strings to see how width is determined.\nLooks like it takes width of widest line!"):move(30, 120)
|
||||
pop.element():align("right", "bottom"):setSize(25, 25):move(-5, -5)
|
||||
pop = require "pop"
|
||||
local c = pop.box():align("center", "center"):setSize(300, 300)
|
||||
pop.box(c, {255, 0, 0, 255}):setSize(100, 50)
|
||||
pop.box(c, {0, 255, 0, 255}):align("center"):setSize(100, 100)
|
||||
pop.box(c, {0, 0, 255, 255}):align("right"):setSize(50, 100)
|
||||
pop.box(c, {100, 100, 100, 255}):align("center", "center"):setSize(500, 100)
|
||||
pop.box(c):align("center"):setSize(50, 500):move(0, -100)
|
||||
pop.box(c, {255, 255, 0, 255}):align(false, "bottom"):setSize(100, 100)
|
||||
pop.box(c, {255, 150, 0, 255}):align("center", "bottom"):setSize(100, 50)
|
||||
pop.box(c, {0, 255, 255}):align("right", "bottom"):setSize(50, 100):move(-50)
|
||||
pop.text(nil, "Here's some test text\n(with newlines)\nin the top left corner!")
|
||||
pop.text(nil, "Here's some test text in the bottom right corner!"):align("right", "bottom")
|
||||
pop.skin(pop.text("Here's easier-to-code test text in the center!"):align("center", "center", true)) -- 'true' means align to pixel!
|
||||
w = pop.box(nil, {255, 255, 255, 255}):align(false, "bottom"):setSize(150, 150)
|
||||
b = pop.box(w, {0, 0, 0, 255}):setMargin(5):setSize(100, 100)
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
@ -20,55 +25,29 @@ end
|
||||
|
||||
function love.draw()
|
||||
pop.draw()
|
||||
|
||||
if debugDrawEnabled then
|
||||
pop.debugDraw()
|
||||
end
|
||||
--pop.debugDraw()
|
||||
end
|
||||
|
||||
function love.textinput(text)
|
||||
pop.textinput(text)
|
||||
function love.mousepressed(x, y, button)
|
||||
pop.mousepressed(x, y, button)
|
||||
end
|
||||
|
||||
function love.mousepressed(button, x, y)
|
||||
pop.mousepressed(button, x, y)
|
||||
end
|
||||
|
||||
function love.mousereleased(button, x, y)
|
||||
pop.mousereleased(button, x, y)
|
||||
function love.mousereleased(x, y, button)
|
||||
pop.mousereleased(x, y, button)
|
||||
end
|
||||
|
||||
function love.keypressed(key)
|
||||
if key == "escape" then
|
||||
local handled = pop.keypressed(key)
|
||||
|
||||
if (key == "escape") and (not handled) then
|
||||
love.event.quit()
|
||||
else
|
||||
if (key == "s") and (not visualTestsShown) then
|
||||
-- old visual tests
|
||||
local align = pop.box():align("center", "center"):setSize(200, 200)
|
||||
pop.box(align):align("left", "top"):setSize(75, 10):setColor(255, 0, 255, 255)
|
||||
pop.box(align):align("center", "top"):setColor(100, 100, 100)
|
||||
pop.box(align, {0, 255, 0, 255}):setSize(20, 5):align("right", "top")
|
||||
pop.box(align):align("left", "center"):setColor(0, 0, 255)
|
||||
pop.box(align):align("center", "center"):setSize(90, 90):setColor(255, 255, 255)
|
||||
pop.box(align):align("right", "center"):setColor(255, 0, 0)
|
||||
pop.box(align):align("left", "bottom"):setColor(0, 255, 0):setSize(nil, 40)
|
||||
pop.box(align):align("center", "bottom"):setColor(255, 255, 0)
|
||||
pop.box(align):align("right", "bottom"):setColor(0, 255, 255):setSize(40, 40)
|
||||
--pop.box(nil, {255, 0, 0, 255}):align("left", "top"):setSize(50, 50) --TODO adjust z-height of elements
|
||||
pop.text(nil, "Hello World!"):align("center"):setText("Hey, I've been modified!")--:move(0, 18)
|
||||
pop.text(nil, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}\\|:;\"',./<>?`~"):align("center", "bottom")
|
||||
|
||||
visualTestsShown = true
|
||||
elseif (key == "t") and (not testsRun) then
|
||||
require "test"
|
||||
elseif key == "d" then
|
||||
debugDrawEnabled = not debugDrawEnabled
|
||||
end
|
||||
|
||||
pop.keypressed(key)
|
||||
end
|
||||
end
|
||||
|
||||
function love.keyreleased(key)
|
||||
pop.keyreleased(key)
|
||||
end
|
||||
|
||||
function love.textinput(text)
|
||||
pop.textinput(text)
|
||||
end
|
||||
|
@ -16,8 +16,8 @@ do
|
||||
if self.background then
|
||||
if type(self.background) == "table" then
|
||||
graphics.setColor(self.background)
|
||||
graphics.rectangle("fill", self.x, self.y, self.w, self.h)
|
||||
else
|
||||
graphics.setColor(self.background)
|
||||
local w, h = self.background:getDimensions()
|
||||
w = self.w / w
|
||||
h = self.h / h
|
||||
@ -47,12 +47,16 @@ do
|
||||
if a == nil then
|
||||
a = 255
|
||||
end
|
||||
if type(r) == "table" then
|
||||
self.background = r
|
||||
else
|
||||
self.background = {
|
||||
r,
|
||||
g,
|
||||
b,
|
||||
a
|
||||
}
|
||||
end
|
||||
return self
|
||||
end,
|
||||
getColor = function(self)
|
||||
|
@ -1,5 +1,7 @@
|
||||
local graphics
|
||||
graphics = love.graphics
|
||||
local floor
|
||||
floor = math.floor
|
||||
local element
|
||||
do
|
||||
local _class_0
|
||||
@ -15,10 +17,14 @@ do
|
||||
return self
|
||||
end,
|
||||
move = function(self, x, y)
|
||||
if x then
|
||||
self.x = self.x + x
|
||||
end
|
||||
if y then
|
||||
self.y = self.y + y
|
||||
end
|
||||
for i = 1, #self.child do
|
||||
if not self.child[i].excludeMovement then
|
||||
if not (self.child[i].excludeMovement) then
|
||||
self.child[i]:move(x, y)
|
||||
end
|
||||
end
|
||||
@ -27,6 +33,7 @@ do
|
||||
setPosition = function(self, x, y)
|
||||
local oldX = self.x
|
||||
local oldY = self.y
|
||||
if x then
|
||||
local _exp_0 = self.horizontal
|
||||
if "left" == _exp_0 then
|
||||
self.x = x
|
||||
@ -35,16 +42,23 @@ do
|
||||
elseif "right" == _exp_0 then
|
||||
self.x = x - self.w
|
||||
end
|
||||
local _exp_1 = self.vertical
|
||||
if "top" == _exp_1 then
|
||||
else
|
||||
x = oldX
|
||||
end
|
||||
if y then
|
||||
local _exp_0 = self.vertical
|
||||
if "top" == _exp_0 then
|
||||
self.y = y
|
||||
elseif "center" == _exp_1 then
|
||||
elseif "center" == _exp_0 then
|
||||
self.y = y - self.h / 2
|
||||
elseif "bottom" == _exp_1 then
|
||||
elseif "bottom" == _exp_0 then
|
||||
self.y = y - self.h
|
||||
end
|
||||
else
|
||||
y = oldY
|
||||
end
|
||||
for i = 1, #self.child do
|
||||
if not self.child[i].excludeMovement then
|
||||
if not (self.child[i].excludeMovement) then
|
||||
self.child[i]:move(x - oldX, y - oldY)
|
||||
end
|
||||
end
|
||||
@ -57,7 +71,7 @@ do
|
||||
if "center" == _exp_0 then
|
||||
resultX = resultX + (self.w / 2)
|
||||
elseif "right" == _exp_0 then
|
||||
resultX = resultX + self.w
|
||||
resultY = resultY + self.w
|
||||
end
|
||||
local _exp_1 = self.vertical
|
||||
if "center" == _exp_1 then
|
||||
@ -102,7 +116,7 @@ do
|
||||
self:setSize(W, H)
|
||||
return self
|
||||
end,
|
||||
align = function(self, horizontal, vertical)
|
||||
align = function(self, horizontal, vertical, toPixel)
|
||||
self:setAlignment(horizontal, vertical)
|
||||
self.x = self.parent.x
|
||||
self.y = self.parent.y
|
||||
@ -122,13 +136,17 @@ do
|
||||
elseif "bottom" == _exp_1 then
|
||||
self.y = self.y + (self.parent.h - self.h - self.margin)
|
||||
end
|
||||
if toPixel then
|
||||
self.x = floor(self.x)
|
||||
self.y = floor(self.y)
|
||||
end
|
||||
return self
|
||||
end,
|
||||
alignTo = function(self, element, horizontal, vertical)
|
||||
local realParent = self.parent
|
||||
local parent = self.parent
|
||||
self.parent = element
|
||||
self:align(horizontal, vertical)
|
||||
self.parent = realParent
|
||||
self.parent = parent
|
||||
return self
|
||||
end,
|
||||
setAlignment = function(self, horizontal, vertical)
|
||||
@ -139,6 +157,14 @@ do
|
||||
self.vertical = vertical
|
||||
end
|
||||
return self
|
||||
end,
|
||||
setMargin = function(self, margin)
|
||||
self.margin = margin
|
||||
self:align()
|
||||
return self
|
||||
end,
|
||||
getMargin = function(self)
|
||||
return self.margin
|
||||
end
|
||||
}
|
||||
_base_0.__index = _base_0
|
||||
@ -146,8 +172,13 @@ do
|
||||
__init = function(self, pop, parent)
|
||||
self.parent = parent
|
||||
self.child = { }
|
||||
if parent then
|
||||
self.x = parent.x or 0
|
||||
self.y = parent.y or 0
|
||||
else
|
||||
self.x = 0
|
||||
self.y = 0
|
||||
end
|
||||
self.w = 10
|
||||
self.h = 10
|
||||
self.horizontal = "left"
|
||||
|
@ -5,13 +5,22 @@ do
|
||||
local _obj_0 = string
|
||||
sub, len = _obj_0.sub, _obj_0.len
|
||||
end
|
||||
local path = sub(..., 1, len(...) - len("/text"))
|
||||
local path = sub(..., 1, len(...) - len("/box"))
|
||||
local element = require(tostring(path) .. "/element")
|
||||
local text
|
||||
do
|
||||
local _class_0
|
||||
local _parent_0 = element
|
||||
local _base_0 = {
|
||||
wrap = function(pop)
|
||||
return function(parent, ...)
|
||||
if type(parent) == "string" then
|
||||
return pop.create("text", nil, parent, ...)
|
||||
else
|
||||
return pop.create("text", parent, ...)
|
||||
end
|
||||
end
|
||||
end,
|
||||
draw = function(self)
|
||||
graphics.setColor(self.color)
|
||||
graphics.setFont(self.font)
|
||||
@ -40,7 +49,7 @@ do
|
||||
local _exp_1 = self.vertical
|
||||
if "center" == _exp_1 then
|
||||
self.y = self.y - ((h - self.h) / 2)
|
||||
elseif "right" == _exp_1 then
|
||||
elseif "bottom" == _exp_1 then
|
||||
self.y = self.y - (h - self.h - self.margin)
|
||||
end
|
||||
self.w = w
|
||||
@ -70,12 +79,16 @@ do
|
||||
if a == nil then
|
||||
a = 255
|
||||
end
|
||||
if type(r) == "table" then
|
||||
self.color = r
|
||||
else
|
||||
self.color = {
|
||||
r,
|
||||
g,
|
||||
b,
|
||||
a
|
||||
}
|
||||
end
|
||||
return self
|
||||
end,
|
||||
getColor = function(self)
|
||||
|
@ -8,38 +8,71 @@ insert = table.insert
|
||||
local path = ...
|
||||
local pop = { }
|
||||
pop.elements = { }
|
||||
pop.window = {
|
||||
child = { }
|
||||
}
|
||||
pop.skins = { }
|
||||
pop.screen = false
|
||||
pop.load = function()
|
||||
local elements = filesystem.getDirectoryItems(tostring(path) .. "/elements")
|
||||
for i = 1, #elements do
|
||||
local _continue_0 = false
|
||||
repeat
|
||||
if not (elements[i]:sub(-4) == ".lua") then
|
||||
_continue_0 = true
|
||||
break
|
||||
end
|
||||
local name = elements[i]:sub(1, -5)
|
||||
pop.elements[name] = require(tostring(path) .. "/elements/" .. tostring(name))
|
||||
print("loaded element: " .. tostring(name))
|
||||
if not pop[name] then
|
||||
print("element loaded: \"" .. tostring(name) .. "\"")
|
||||
if not (pop[name]) then
|
||||
if pop.elements[name].wrap then
|
||||
pop[name] = pop.elements[name].wrap(pop)
|
||||
else
|
||||
pop[name] = function(...)
|
||||
return pop.create(name, ...)
|
||||
end
|
||||
print("wrapper created: " .. tostring(name) .. "()")
|
||||
end
|
||||
print("wrapper created: \"pop." .. tostring(name) .. "()\"")
|
||||
end
|
||||
_continue_0 = true
|
||||
until true
|
||||
if not _continue_0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
pop.window = pop.create("element"):setSize(graphics.getWidth(), graphics.getHeight())
|
||||
return print("created window")
|
||||
local skins = filesystem.getDirectoryItems(tostring(path) .. "/skins")
|
||||
for i = 1, #skins do
|
||||
local _continue_0 = false
|
||||
repeat
|
||||
if not (skins[i]:sub(-4) == ".lua") then
|
||||
_continue_0 = true
|
||||
break
|
||||
end
|
||||
pop.create = function(elementType, parent, ...)
|
||||
local name = skins[i]:sub(1, -5)
|
||||
pop.skins[name] = require(tostring(path) .. "/skins/" .. tostring(name))
|
||||
print("skin loaded: \"" .. tostring(name) .. "\"")
|
||||
_continue_0 = true
|
||||
until true
|
||||
if not _continue_0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
|
||||
return print("created \"pop.screen\"")
|
||||
end
|
||||
pop.create = function(element, parent, ...)
|
||||
if parent == nil then
|
||||
parent = pop.window
|
||||
parent = pop.screen
|
||||
end
|
||||
local newElement = pop.elements[elementType](parent, ...)
|
||||
insert(parent.child, newElement)
|
||||
return newElement
|
||||
element = pop.elements[element](pop, parent, ...)
|
||||
if parent then
|
||||
insert(parent.child, element)
|
||||
end
|
||||
return element
|
||||
end
|
||||
pop.update = function(dt, element)
|
||||
if element == nil then
|
||||
element = pop.window
|
||||
element = pop.screen
|
||||
end
|
||||
if not element.excludeUpdating then
|
||||
if not (element.excludeUpdate) then
|
||||
if element.update then
|
||||
element:update(dt)
|
||||
end
|
||||
@ -50,86 +83,80 @@ pop.update = function(dt, element)
|
||||
end
|
||||
pop.draw = function(element)
|
||||
if element == nil then
|
||||
element = pop.window
|
||||
element = pop.screen
|
||||
end
|
||||
if not element.excludeRendering then
|
||||
if not (element.excludeDraw) then
|
||||
if element.draw then
|
||||
local _
|
||||
do
|
||||
local _base_0 = element
|
||||
local _fn_0 = _base_0.draw
|
||||
_ = function(...)
|
||||
return _fn_0(_base_0, ...)
|
||||
end
|
||||
end
|
||||
element:draw()
|
||||
end
|
||||
for i = 1, #element.child do
|
||||
pop.draw(element.child)
|
||||
pop.draw(element.child[i])
|
||||
end
|
||||
end
|
||||
end
|
||||
pop.mousepressed = function(button, x, y, element)
|
||||
pop.mousepressed = function(x, y, button, element)
|
||||
if element == nil then
|
||||
element = pop.window
|
||||
element = pop.screen
|
||||
end
|
||||
if (x >= element.x) and (x <= (element.x + element.w)) then
|
||||
if (y >= element.y) and (y <= (element.y + element.h)) then
|
||||
for i = 1, #element.child do
|
||||
if pop.mousepressed(button, x, y, element.child[i]) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
if element.mousepressed then
|
||||
return element:mousepressed(button, x - element.x, y - element.y)
|
||||
else
|
||||
print("mousepressed", x, y, button, element)
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
pop.mousereleased = function(button, x, y, element)
|
||||
pop.mousereleased = function(x, y, button, element)
|
||||
if element == nil then
|
||||
element = pop.window
|
||||
element = pop.screen
|
||||
end
|
||||
print("mousereleased", x, y, button, element)
|
||||
return false
|
||||
end
|
||||
pop.keypressed = function(key)
|
||||
return print("pop.keypressed() is unimplemented.")
|
||||
print("keypressed", key)
|
||||
return false
|
||||
end
|
||||
pop.keyreleased = function(key)
|
||||
return print("pop.keyreleased() is unimplemented.")
|
||||
print("keyreleased", key)
|
||||
return false
|
||||
end
|
||||
pop.textinput = function(text)
|
||||
return print("pop.textinput() is unimplemented.")
|
||||
print("textinput", text)
|
||||
return false
|
||||
end
|
||||
pop.skin = function(element, skin, apply_to_children)
|
||||
if apply_to_children == nil then
|
||||
apply_to_children = true
|
||||
pop.skin = function(element, skin, depth)
|
||||
if element == nil then
|
||||
element = pop.screen
|
||||
end
|
||||
element.margin = skin.margin
|
||||
if element.background then
|
||||
if skin == nil then
|
||||
skin = pop.skins.default
|
||||
end
|
||||
if element.background and skin.background then
|
||||
element.background = skin.background
|
||||
end
|
||||
if element.color then
|
||||
if element.color and skin.color then
|
||||
element.color = skin.color
|
||||
end
|
||||
if element.font then
|
||||
if element.font and skin.font then
|
||||
element.font = skin.font
|
||||
end
|
||||
if apply_to_children then
|
||||
if not (depth or (depth == 0)) then
|
||||
if depth == tonumber(depth) then
|
||||
for i = 1, #element.child do
|
||||
pop.skin(element.child[i], skin)
|
||||
pop.skin(element.child[i], skin, depth - 1)
|
||||
end
|
||||
else
|
||||
for i = 1, #element.child do
|
||||
pop.skin(element.child[i], skin, false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
pop.debugDraw = function(element)
|
||||
if element == nil then
|
||||
element = pop.window
|
||||
element = pop.screen
|
||||
end
|
||||
if element.debugDraw then
|
||||
element:debugDraw()
|
||||
else
|
||||
graphics.setLineWidth(1)
|
||||
graphics.setColor(0, 0, 0, 100)
|
||||
graphics.setLineColor(0, 0, 0, 100)
|
||||
graphics.rectangle("fill", element.x, element.y, element.w, element.h)
|
||||
graphics.setColor(150, 150, 150, 150)
|
||||
graphics.rectangle("line", element.x, element.y, element.w, element.h)
|
||||
|
@ -1,13 +0,0 @@
|
||||
local graphics
|
||||
graphics = love.graphics
|
||||
return {
|
||||
color = {
|
||||
255,
|
||||
255,
|
||||
255,
|
||||
255
|
||||
},
|
||||
background = false,
|
||||
font = graphics.newFont(13),
|
||||
margin = 2
|
||||
}
|
16
demo/pop/skins/default.lua
Normal file
16
demo/pop/skins/default.lua
Normal file
@ -0,0 +1,16 @@
|
||||
local lg = love.graphics
|
||||
return {
|
||||
background = {
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
220
|
||||
},
|
||||
color = {
|
||||
255,
|
||||
255,
|
||||
255,
|
||||
250
|
||||
},
|
||||
font = lg.newFont(14)
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
local graphics
|
||||
graphics = love.graphics
|
||||
return {
|
||||
color = {
|
||||
255,
|
||||
255,
|
||||
255,
|
||||
255
|
||||
},
|
||||
background = false,
|
||||
font = graphics.newFont(13),
|
||||
margin = 2
|
||||
}
|
@ -10,12 +10,12 @@ class box extends element
|
||||
|
||||
@background = background
|
||||
|
||||
draw: => --NOTE these ifs might be wrong? test this!
|
||||
draw: =>
|
||||
if @background
|
||||
if type(@background) == "table"
|
||||
graphics.setColor @background
|
||||
graphics.rectangle "fill", @x, @y, @w, @h
|
||||
else
|
||||
graphics.setColor @background
|
||||
w, h = @background\getDimensions!
|
||||
w = @w / w
|
||||
h = @h / h
|
||||
@ -42,11 +42,15 @@ class box extends element
|
||||
return @background
|
||||
|
||||
setColor: (r, g, b, a=255) =>
|
||||
if type(r) == "table"
|
||||
@background = r
|
||||
else
|
||||
@background = {r, g, b, a}
|
||||
|
||||
return @
|
||||
|
||||
getColor: =>
|
||||
if type(@background) == "table"
|
||||
return unpack @background
|
||||
else
|
||||
error "This box doesn't have a color." --might be bad design...
|
||||
error "This box doesn't have a color." --might be a bad idea
|
||||
|
@ -1,12 +1,18 @@
|
||||
import graphics from love
|
||||
import floor from math
|
||||
|
||||
class element
|
||||
new: (pop, parent) =>
|
||||
@parent = parent
|
||||
@child = {}
|
||||
|
||||
if parent
|
||||
@x = parent.x or 0
|
||||
@y = parent.y or 0
|
||||
else
|
||||
@x = 0
|
||||
@y = 0
|
||||
|
||||
@w = 10
|
||||
@h = 10
|
||||
|
||||
@ -26,11 +32,13 @@ class element
|
||||
return @
|
||||
|
||||
move: (x, y) =>
|
||||
@x += x
|
||||
@y += y
|
||||
if x
|
||||
@x = @x + x
|
||||
if y
|
||||
@y = @y + y
|
||||
|
||||
for i = 1, #@child
|
||||
if not @child[i].excludeMovement
|
||||
unless @child[i].excludeMovement
|
||||
@child[i]\move x, y
|
||||
|
||||
return @
|
||||
@ -39,6 +47,7 @@ class element
|
||||
oldX = @x
|
||||
oldY = @y
|
||||
|
||||
if x
|
||||
switch @horizontal
|
||||
when "left"
|
||||
@x = x
|
||||
@ -46,7 +55,10 @@ class element
|
||||
@x = x - @w/2
|
||||
when "right"
|
||||
@x = x - @w
|
||||
else
|
||||
x = oldX
|
||||
|
||||
if y
|
||||
switch @vertical
|
||||
when "top"
|
||||
@y = y
|
||||
@ -54,9 +66,11 @@ class element
|
||||
@y = y - @h/2
|
||||
when "bottom"
|
||||
@y = y - @h
|
||||
else
|
||||
y = oldY
|
||||
|
||||
for i = 1, #@child
|
||||
if not @child[i].excludeMovement
|
||||
unless @child[i].excludeMovement
|
||||
@child[i]\move x - oldX, y - oldY
|
||||
|
||||
return @
|
||||
@ -69,7 +83,7 @@ class element
|
||||
when "center"
|
||||
resultX += @w/2
|
||||
when "right"
|
||||
resultX += @w
|
||||
resultY += @w
|
||||
|
||||
switch @vertical
|
||||
when "center"
|
||||
@ -115,7 +129,8 @@ class element
|
||||
|
||||
return @
|
||||
|
||||
align: (horizontal, vertical) =>
|
||||
--TODO note that align requires a parent!
|
||||
align: (horizontal, vertical, toPixel) =>
|
||||
@setAlignment horizontal, vertical
|
||||
|
||||
@x = @parent.x
|
||||
@ -137,15 +152,19 @@ class element
|
||||
when "bottom"
|
||||
@y += @parent.h - @h - @margin
|
||||
|
||||
if toPixel
|
||||
@x = floor @x
|
||||
@y = floor @y
|
||||
|
||||
return @
|
||||
|
||||
alignTo: (element, horizontal, vertical) =>
|
||||
realParent = @parent
|
||||
parent = @parent
|
||||
@parent = element
|
||||
|
||||
@align horizontal, vertical
|
||||
|
||||
@parent = realParent
|
||||
@parent = parent
|
||||
|
||||
return @
|
||||
|
||||
@ -156,3 +175,11 @@ class element
|
||||
@vertical = vertical
|
||||
|
||||
return @
|
||||
|
||||
setMargin: (margin) =>
|
||||
@margin = margin
|
||||
@align!
|
||||
return @
|
||||
|
||||
getMargin: =>
|
||||
return @margin
|
||||
|
@ -1,10 +1,17 @@
|
||||
import graphics from love
|
||||
import sub, len from string
|
||||
|
||||
path = sub ..., 1, len(...) - len "/text"
|
||||
path = sub ..., 1, len(...) - len "/box"
|
||||
element = require "#{path}/element"
|
||||
|
||||
class text extends element
|
||||
wrap: (pop) ->
|
||||
return (parent, ...) ->
|
||||
if type(parent) == "string"
|
||||
return pop.create("text", nil, parent, ...)
|
||||
else
|
||||
return pop.create("text", parent, ...)
|
||||
|
||||
new: (pop, parent, text="", color={255,255,255,255}) =>
|
||||
super pop, parent
|
||||
|
||||
@ -30,9 +37,10 @@ class text extends element
|
||||
|
||||
return @
|
||||
|
||||
-- unlike most elements, you cannot set a size for text elements
|
||||
setSize: =>
|
||||
w = @font\getWidth @text
|
||||
h = @font\getHeight! * (select(2, @text\gsub("\n", "\n")) + 1) --hack to get height of multiple lines of text
|
||||
h = @font\getHeight! * (select(2, @text\gsub("\n", "\n")) + 1) --hack to get height of multiple lines
|
||||
|
||||
switch @horizontal
|
||||
when "center"
|
||||
@ -43,7 +51,7 @@ class text extends element
|
||||
switch @vertical
|
||||
when "center"
|
||||
@y -= (h - @h)/2
|
||||
when "right"
|
||||
when "bottom"
|
||||
@y -= h - @h - @margin
|
||||
|
||||
@w = w
|
||||
@ -68,7 +76,11 @@ class text extends element
|
||||
return @font
|
||||
|
||||
setColor: (r, g, b, a=255) =>
|
||||
if type(r) == "table"
|
||||
@color = r
|
||||
else
|
||||
@color = {r, g, b, a}
|
||||
|
||||
return @
|
||||
|
||||
getColor: =>
|
||||
|
@ -6,94 +6,115 @@ path = ...
|
||||
pop = {}
|
||||
|
||||
pop.elements = {}
|
||||
pop.window = {child: {}} --placeholder to allow window creation without specialized code
|
||||
--pop.focused = false
|
||||
pop.skins = {}
|
||||
|
||||
pop.screen = false -- initialized in pop.load()
|
||||
--pop.focused ?
|
||||
|
||||
-- loads elements and skins, creates pop.screen (intended to only be called once at the beginning)
|
||||
pop.load = ->
|
||||
elements = filesystem.getDirectoryItems "#{path}/elements"
|
||||
for i = 1, #elements
|
||||
-- only attempt to load lua files
|
||||
unless elements[i]\sub(-4) == ".lua"
|
||||
continue
|
||||
|
||||
-- load into pop.elements table
|
||||
name = elements[i]\sub 1, -5
|
||||
pop.elements[name] = require "#{path}/elements/#{name}"
|
||||
print "loaded element: #{name}"
|
||||
print "element loaded: \"#{name}\""
|
||||
|
||||
if not pop[name]
|
||||
pop[name] = (...) -> return pop.create(name, ...)
|
||||
print "wrapper created: #{name}()"
|
||||
-- create pop.element() wrapper if possible
|
||||
unless pop[name]
|
||||
if pop.elements[name].wrap
|
||||
pop[name] = pop.elements[name].wrap pop
|
||||
else
|
||||
pop[name] = (...) ->
|
||||
return pop.create(name, ...)
|
||||
|
||||
pop.window = pop.create("element")\setSize(graphics.getWidth!, graphics.getHeight!)
|
||||
--pop.window.parent = pop.window --may be dangerous? infinite loop looking for the window?
|
||||
--pop.window.parent = false --may be dangerous? attempting to index a boolean?
|
||||
print "created window"
|
||||
print "wrapper created: \"pop.#{name}()\""
|
||||
|
||||
pop.create = (elementType, parent=pop.window, ...) ->
|
||||
newElement = pop.elements[elementType](parent, ...)
|
||||
insert parent.child, newElement
|
||||
return newElement
|
||||
-- works just like above, except no wrappers
|
||||
skins = filesystem.getDirectoryItems "#{path}/skins"
|
||||
for i = 1, #skins
|
||||
unless skins[i]\sub(-4) == ".lua"
|
||||
continue
|
||||
name = skins[i]\sub 1, -5
|
||||
pop.skins[name] = require "#{path}/skins/#{name}"
|
||||
print "skin loaded: \"#{name}\""
|
||||
|
||||
pop.update = (dt, element=pop.window) ->
|
||||
if not element.excludeUpdating
|
||||
-- main window (called screen because there will be a window element class)
|
||||
pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!)
|
||||
print "created \"pop.screen\""
|
||||
|
||||
-- creates an element with specified parent (parent can be false)
|
||||
pop.create = (element, parent=pop.screen, ...) ->
|
||||
element = pop.elements[element](pop, parent, ...)
|
||||
|
||||
if parent
|
||||
insert parent.child, element
|
||||
|
||||
return element
|
||||
|
||||
pop.update = (dt, element=pop.screen) ->
|
||||
unless element.excludeUpdate
|
||||
if element.update
|
||||
element\update dt
|
||||
|
||||
for i = 1, #element.child
|
||||
pop.update dt, element.child[i]
|
||||
|
||||
pop.draw = (element=pop.window) ->
|
||||
if not element.excludeRendering
|
||||
pop.draw = (element=pop.screen) ->
|
||||
unless element.excludeDraw
|
||||
if element.draw
|
||||
element\draw
|
||||
|
||||
element\draw!
|
||||
for i = 1, #element.child
|
||||
pop.draw element.child
|
||||
pop.draw element.child[i]
|
||||
|
||||
pop.mousepressed = (button, x, y, element=pop.window) ->
|
||||
-- if within bounds, check children
|
||||
-- if not handled, check if we can handle it
|
||||
-- abort with success if handled
|
||||
if (x >= element.x) and (x <= (element.x + element.w))
|
||||
if (y >= element.y) and (y <= (element.y + element.h))
|
||||
for i = 1, #element.child
|
||||
if pop.mousepressed button, x, y, element.child[i]
|
||||
return true
|
||||
pop.mousepressed = (x, y, button, element=pop.screen) ->
|
||||
print "mousepressed", x, y, button, element
|
||||
return false --TODO event handlers return if they have handled the event!
|
||||
|
||||
if element.mousepressed
|
||||
return element\mousepressed button, x - element.x, y - element.y
|
||||
else
|
||||
return false
|
||||
|
||||
--TODO multiple return values, mousereleased first, click second
|
||||
pop.mousereleased = (button, x, y, element=pop.window) ->
|
||||
-- same as mousepressed, except a click can be fired as well
|
||||
pop.mousereleased = (x, y, button, element=pop.screen) ->
|
||||
print "mousereleased", x, y, button, element
|
||||
return false --TODO event handlers return if they have handled the event!
|
||||
|
||||
pop.keypressed = (key) ->
|
||||
print "pop.keypressed() is unimplemented."
|
||||
print "keypressed", key
|
||||
return false --TODO event handlers return if they have handled the event!
|
||||
|
||||
pop.keyreleased = (key) ->
|
||||
print "pop.keyreleased() is unimplemented."
|
||||
print "keyreleased", key
|
||||
return false --TODO event handlers return if they have handled the event!
|
||||
|
||||
pop.textinput = (text) ->
|
||||
print "pop.textinput() is unimplemented."
|
||||
print "textinput", text
|
||||
return false --TODO event handlers return if they have handled the event!
|
||||
|
||||
pop.skin = (element, skin, apply_to_children=true) ->
|
||||
element.margin = skin.margin
|
||||
|
||||
if element.background
|
||||
-- skins an element (and its children unless depth == true or 0)
|
||||
-- depth can be an integer for how many levels to go down when skinning
|
||||
-- defaults to pop.screen and the default skin
|
||||
pop.skin = (element=pop.screen, skin=pop.skins.default, depth) ->
|
||||
if element.background and skin.background
|
||||
element.background = skin.background
|
||||
if element.color
|
||||
if element.color and skin.color
|
||||
element.color = skin.color
|
||||
if element.font
|
||||
if element.font and skin.font
|
||||
element.font = skin.font
|
||||
|
||||
if apply_to_children
|
||||
unless depth or (depth == 0)
|
||||
if depth == tonumber depth
|
||||
for i = 1, #element.child
|
||||
pop.skin element.child[i], skin
|
||||
pop.skin element.child[i], skin, depth - 1
|
||||
else
|
||||
for i = 1, #element.child
|
||||
pop.skin element.child[i], skin, false
|
||||
|
||||
pop.debugDraw = (element=pop.window) ->
|
||||
pop.debugDraw = (element=pop.screen) ->
|
||||
if element.debugDraw
|
||||
element\debugDraw!
|
||||
else
|
||||
graphics.setLineWidth 1
|
||||
graphics.setColor 0, 0, 0, 100
|
||||
graphics.setLineColor 0, 0, 0, 100
|
||||
graphics.rectangle "fill", element.x, element.y, element.w, element.h
|
||||
graphics.setColor 150, 150, 150, 150
|
||||
graphics.rectangle "line", element.x, element.y, element.w, element.h
|
||||
|
@ -1,8 +0,0 @@
|
||||
import graphics from love
|
||||
|
||||
return {
|
||||
color: {255, 255, 255, 255}
|
||||
background: false
|
||||
font: graphics.newFont 13
|
||||
margin: 2
|
||||
}
|
7
src/pop/skins/default.moon
Normal file
7
src/pop/skins/default.moon
Normal file
@ -0,0 +1,7 @@
|
||||
import graphics from love
|
||||
|
||||
return {
|
||||
background: {0, 0, 0, 220}
|
||||
color: {255, 255, 255, 250}
|
||||
font: graphics.newFont 14
|
||||
}
|
Loading…
Reference in New Issue
Block a user