From 2442730dedfd4c2af37d11baba8d35779050fe6b Mon Sep 17 00:00:00 2001 From: Paul Liverman Date: Mon, 22 Feb 2016 19:55:05 -0800 Subject: [PATCH] started work on rewriting in moonscript --- src/pop/elements/box.moon | 52 ++++++++++++ src/pop/elements/element.moon | 152 ++++++++++++++++++++++++++++++++++ src/pop/init.moon | 86 +++++++++++++++++++ 3 files changed, 290 insertions(+) create mode 100644 src/pop/elements/box.moon create mode 100644 src/pop/elements/element.moon create mode 100644 src/pop/init.moon diff --git a/src/pop/elements/box.moon b/src/pop/elements/box.moon new file mode 100644 index 0000000..a70116f --- /dev/null +++ b/src/pop/elements/box.moon @@ -0,0 +1,52 @@ +import graphics from love +import sub, len from string + +path = sub ..., 1, len(...) - len "/box" +element = require "#{path}/element" + +class box extends element + new: (pop, parent, background=false) => + super pop, parent + + @background = background + + draw: => --NOTE these ifs might be wrong? test this! + if @background + if type(@background) == "table" + graphics.setColor @background + else + graphics.setColor @background + w, h = @background\getDimensions! + w = @w/w + h = @h/h + graphics.draw @background, @x, @y, 0, w, h + + return @ + + debugDraw: => + graphics.setLineWidth 0.5 + graphics.setColor 0, 0, 0, 100 + graphics.rectangle "fill", @x, @y, @w, @h + graphics.setColor 0, 0, 200, 200 + graphics.rectangle "line", @x, @y, @w, @h + graphics.setColor 200, 200, 255, 255 + graphics.print "b", @x, @y + + return @ + + setBackground: (background) => + @background = background + return @ + + getBackground: => + return @background + + setColor: (r, g, b, a=255) => + @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... diff --git a/src/pop/elements/element.moon b/src/pop/elements/element.moon new file mode 100644 index 0000000..806df58 --- /dev/null +++ b/src/pop/elements/element.moon @@ -0,0 +1,152 @@ +import graphics from love + +class element + new: (pop, parent) => + @parent = parent + @child = {} + + @x = parent.x or 0 + @y = parent.y or 0 + @w = 10 + @h = 10 + + @horizontal = "left" + @vertical = "top" + + debugDraw: => + graphics.setLineWidth 0.5 + graphics.setColor 0, 0, 0, 100 + graphics.rectangle "fill", @x, @y, @w, @h + graphics.setColor 0, 200, 0, 200 + graphics.rectangle "line", @x, @y, @w, @h + graphics.setColor 200, 255, 200, 255 + graphics.print "e", @x, @y + + return @ + + move: (x, y) => + @x += x + @y += y + + for i in @child do + if not @child[i].excludeMovement + @child[i]\move x, y + + return @ + + setPosition: (x, y) => + oldX = @x + oldY = @y + + switch @horizontal + when "left" + @x = x + when "center" + @x = x - @w/2 + when "right" + @x = x - @w + + switch @vertical + when "top" + @y = y + when "center" + @y = y - @h/2 + when "bottom" + @y = y - @h + + for i in @child + if not @child[i].excludeMovement + @child[i]\move x - oldX, y - oldY + + return @ + + getPosition: => + resultX = @x + resultY = @y + + switch @horizontal + when "center" + resultX += @w/2 + when "right" + resultX += @w + + switch @vertical + when "center" + resultY += @h/2 + when "bottom" + resultY += @h + + return resultX, resultY + + setSize: (w, h) => + if w + switch @horizontal + when "center" + @x -= (w - @w)/2 + when "right" + @x -= w - @w + @w = w + + if h + switch @vertical + when "center" + @y -= (h - @h)/2 + when "bottom" + @y -= h - @h + + @h = h + + return @ + + getSize: => + return @w, @h + + adjustSize: (w, h) => + W, H = @getSize! + + if w + W += w + if h + H += h + + @setSize W, H + + return @ + + align: (horizontal, vertical) => + @setAlignment horizontal, vertical + + @x = @parent.x + @y = @parent.y + + switch @horizontal + when "center" + @x += (@parent.w - @w)/2 + when "right" + @x += @parent.w - @w + + switch @vertical + when "center" + @y += (@parent.h - @h)/2 + when "bottom" + @y += @parent.h - @h + + return @ + + alignTo: (element, horizontal, vertical) => + realParent = @parent + @parent = element + + @align horizontal, vertical + + @parent = realParent + + return @ + + setAlignment: (horizontal, vertical) => + if horizontal + @horizontal = horizontal + if vertical + @vertical = vertical + + return @ diff --git a/src/pop/init.moon b/src/pop/init.moon new file mode 100644 index 0000000..80aaa56 --- /dev/null +++ b/src/pop/init.moon @@ -0,0 +1,86 @@ +import filesystem, graphics from love +import insert from table + +path = ... + +class Pop + new: => + @elements = {} + @window = {child: {}} --TODO eliminate this crap + --@focused = @window --TODO redo better + + elements = filesystem.getDirectoryItems "#{path}/elements" + for i in elements + name = elements[i]\sub 1, -5 + @elements[name] = require "#{path}/elements/#{name}" + print "loaded element: #{name}" + + if not @name + @name = (...) -> return @create(name, ...) + print "wrapped created: #{name}()" + + @window = @create("element")\setSize(graphics.getWidth!, graphics.getHeight!) + print "created window" + + create: (elementType, parent=@window, ...) => + newElement = @elements[elementType](@, parent, ...) + insert parent.child, newElement + return newElement + + update: (dt, element=@window) => + if not element.excludeUpdating + if element.update + element\update dt + + for i in element.child + @update dt, element.child[i] + + draw: (element=@window) => + if not element.excludeRendering + if element.draw + element\draw + + for i in element.child + @draw element.child[i] + + mousepressed: (button, x, y, element) => + error "Unimplemented." + + mousereleased: (button, x, y, element) => + error "Unimplemented." + + keypressed: (key) => + error "Unimplemented." + + keyreleased: (key) => + error "Unimplemented." + + textinput: (text) => + error "Unimplemented." + + skin: (element, skin, apply_to_children=false) => + if element.background + element.background = skin.background + if element.color + element.color = skin.color + if element.font + element.font = skin.font + + if not apply_to_children + for i in element.child + @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 + + for i in element.child + @debugDraw element.child[i]