2016-02-23 03:55:05 +00:00
|
|
|
import filesystem, graphics from love
|
|
|
|
import insert from table
|
|
|
|
|
|
|
|
path = ...
|
|
|
|
|
|
|
|
class Pop
|
|
|
|
new: =>
|
|
|
|
@elements = {}
|
2016-02-25 00:25:54 +00:00
|
|
|
@window = {child: {}}
|
2016-02-23 03:55:05 +00:00
|
|
|
--@focused = @window --TODO redo better
|
|
|
|
|
|
|
|
elements = filesystem.getDirectoryItems "#{path}/elements"
|
2016-02-25 00:25:54 +00:00
|
|
|
for i = 1, #elements
|
2016-02-23 03:55:05 +00:00
|
|
|
name = elements[i]\sub 1, -5
|
|
|
|
@elements[name] = require "#{path}/elements/#{name}"
|
|
|
|
print "loaded element: #{name}"
|
|
|
|
|
2016-02-25 00:25:54 +00:00
|
|
|
if not @[name]
|
|
|
|
@[name] = (...) => return @create(name, ...)
|
|
|
|
print "wrapper created: #{name}()"
|
2016-02-23 03:55:05 +00:00
|
|
|
|
2016-02-25 00:25:54 +00:00
|
|
|
@window = @create("element", @window)\setSize(graphics.getWidth!, graphics.getHeight!)
|
2016-02-23 03:55:05 +00:00
|
|
|
print "created window"
|
2016-02-25 00:25:54 +00:00
|
|
|
--@window.parent = @window
|
|
|
|
--@window.parent = false
|
2016-02-23 03:55:05 +00:00
|
|
|
|
|
|
|
create: (elementType, parent=@window, ...) =>
|
2016-02-25 00:25:54 +00:00
|
|
|
newElement = @elements[elementType](parent, ...)
|
2016-02-23 03:55:05 +00:00
|
|
|
insert parent.child, newElement
|
|
|
|
return newElement
|
|
|
|
|
|
|
|
update: (dt, element=@window) =>
|
|
|
|
if not element.excludeUpdating
|
|
|
|
if element.update
|
|
|
|
element\update dt
|
|
|
|
|
2016-02-25 00:25:54 +00:00
|
|
|
for i = 1, #element.child
|
2016-02-23 03:55:05 +00:00
|
|
|
@update dt, element.child[i]
|
|
|
|
|
|
|
|
draw: (element=@window) =>
|
|
|
|
if not element.excludeRendering
|
|
|
|
if element.draw
|
|
|
|
element\draw
|
|
|
|
|
2016-02-25 00:25:54 +00:00
|
|
|
for i = 1, #element.child
|
2016-02-23 03:55:05 +00:00
|
|
|
@draw element.child[i]
|
|
|
|
|
2016-02-25 00:25:54 +00:00
|
|
|
mousepressed: (button, x, y, element=@window) =>
|
|
|
|
-- if within bounds of element, check its children
|
|
|
|
-- if not handled by child, check if it can handle it
|
|
|
|
-- abort loop 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 @mousepressed button, x, y, element.child[i]
|
|
|
|
return true
|
|
|
|
|
|
|
|
if element.mousepressed
|
|
|
|
return element\mousepressed button, x - element.x, y - element.y
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
|
|
|
|
--TODO rewrite for multiple return values, mousereleased is the first val, click is the second!
|
|
|
|
mousereleased: (button, x, y, element=@window) =>
|
|
|
|
-- same as mousepressed, except an additional click is fired
|
|
|
|
-- (TODO fix, so you have to start and end the click on the same element)
|
|
|
|
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 @mousereleased button, x, y, element.child[i]
|
|
|
|
return true
|
|
|
|
|
|
|
|
if element.mousereleased
|
|
|
|
return element\mousereleased button, x - element.x, y - element.y
|
|
|
|
else
|
|
|
|
return false
|
2016-02-23 03:55:05 +00:00
|
|
|
|
|
|
|
keypressed: (key) =>
|
|
|
|
error "Unimplemented."
|
|
|
|
|
|
|
|
keyreleased: (key) =>
|
|
|
|
error "Unimplemented."
|
|
|
|
|
|
|
|
textinput: (text) =>
|
|
|
|
error "Unimplemented."
|
|
|
|
|
2016-02-25 00:25:54 +00:00
|
|
|
skin: (element, skin, apply_to_children=true) =>
|
2016-02-23 16:11:09 +00:00
|
|
|
element.margin = skin.margin
|
|
|
|
|
2016-02-23 03:55:05 +00:00
|
|
|
if element.background
|
|
|
|
element.background = skin.background
|
|
|
|
if element.color
|
|
|
|
element.color = skin.color
|
|
|
|
if element.font
|
|
|
|
element.font = skin.font
|
|
|
|
|
2016-02-25 00:25:54 +00:00
|
|
|
if apply_to_children
|
|
|
|
for i = 1, #element.child
|
2016-02-23 03:55:05 +00:00
|
|
|
@skin element.child[i], skin
|
|
|
|
|
|
|
|
debugDraw: (element=@window) =>
|
|
|
|
if element.debugDraw
|
|
|
|
element\debugDraw!
|
|
|
|
else
|
|
|
|
graphics.setLineWidth 1
|
|
|
|
graphics.setColor 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
|
|
|
|
graphics.setColor 200, 200, 200, 255
|
|
|
|
graphics.print ".", element.x, element.y
|
|
|
|
|
2016-02-25 00:25:54 +00:00
|
|
|
for i = 1, #element.child
|
2016-02-23 03:55:05 +00:00
|
|
|
@debugDraw element.child[i]
|