From a100f0f50c9918054d05aabc6c37dc9c98c5c7dc Mon Sep 17 00:00:00 2001 From: Paul Liverman Date: Mon, 22 Feb 2016 22:10:13 -0800 Subject: [PATCH] mostly done re-implementing things I think --- demo/conf.lua | 7 ++++ demo/main.lua | 74 +++++++++++++++++++++++++++++++++++++ src/pop/elements/text.moon | 75 ++++++++++++++++++++++++++++++++++++++ src/pop/skins/clear.moon | 7 ++++ todo.txt | 12 ++++++ 5 files changed, 175 insertions(+) create mode 100644 demo/conf.lua create mode 100644 demo/main.lua create mode 100644 src/pop/elements/text.moon create mode 100644 src/pop/skins/clear.moon create mode 100644 todo.txt diff --git a/demo/conf.lua b/demo/conf.lua new file mode 100644 index 0000000..b65f25e --- /dev/null +++ b/demo/conf.lua @@ -0,0 +1,7 @@ +function love.conf(t) + t.title = "Pop.Box Demo / Tests" + t.console = true + + t.window.width = 960 + t.window.height = 540 +end diff --git a/demo/main.lua b/demo/main.lua new file mode 100644 index 0000000..926071e --- /dev/null +++ b/demo/main.lua @@ -0,0 +1,74 @@ +local pop = require "pop" + +local lg = love.graphics + +local visualTestsShown = false +local testsRun = false +local debugDrawEnabled = false + +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) +end + +function love.update(dt) + pop.update(dt) +end + +function love.draw() + pop.draw() + + if debugDrawEnabled then + pop.debugDraw() + end +end + +function love.textinput(text) + --pop.textinput(text) +end + +function love.mousepressed(button, x, y) + --pop.mousepressed(button, x, y) +end + +function love.mousereleased(button, x, y) + --pop.mousereleased(button, x, y) +end + +function love.keypressed(key) + if key == "escape" 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 diff --git a/src/pop/elements/text.moon b/src/pop/elements/text.moon new file mode 100644 index 0000000..fe88329 --- /dev/null +++ b/src/pop/elements/text.moon @@ -0,0 +1,75 @@ +import graphics from love +import sub, len from string + +path = sub ..., 1, len(...) - len "/text" +element = require "#{path}/element" + +class text extends element + new: (pop, parent, text="", color={255,255,255,255}) => + super pop, parent + + @font = graphics.newFont 14 + @setText text + @color = color + + draw: => + graphics.setColor @color + graphics.setFont @font + graphics.print @text, @x, @y + + return @ + + debugDraw: => + graphics.setLineWidth 0.5 + graphics.setColor 0, 0, 0, 100 + graphics.rectangle "fill", @x, @y, @w, @h + graphics.setColor 200, 0, 0, 200 + graphics.rectangle "line", @x, @y, @w, @h + graphics.setColor 255, 200, 200, 255 + graphics.print "t", @x, @y + + return @ + + setSize: => + w = @font\getWidth @text + h = @font\getHeight * (select(2, @text:gsub("\n", "\n")) + 1) --hack to get height of multiple lines of text + + switch @horizontal + when "center" + @x -= (w - @w)/2 + when "right" + @x -= w - @w + + switch @vertical + when "center" + @y -= (h - @h)/2 + when "right" + @y -= h - @h + + @w = w + @h = h + + return @ + + setText: (text="") => + @text = text + @setSize! + return @ + + getText: => + return @text + + setFont: (font) => + @font = font + @setSize! + return @ + + getFont: => + return @font + + setColor: (r, g, b, a=255) => + @color = {r, g, b, a} + return @ + + getColor: => + return unpack @color diff --git a/src/pop/skins/clear.moon b/src/pop/skins/clear.moon new file mode 100644 index 0000000..b0ca85a --- /dev/null +++ b/src/pop/skins/clear.moon @@ -0,0 +1,7 @@ +import graphics from love + +return { + color: {255, 255, 255, 255} + background: false + font: graphics.newFont(13) +} diff --git a/todo.txt b/todo.txt new file mode 100644 index 0000000..a3de13f --- /dev/null +++ b/todo.txt @@ -0,0 +1,12 @@ +COPY README AND DOCS FROM MAIN BRANCH, BUT FIGURE OUT WHAT HAS CHANGED OR WHATEVER... + +local pop = require "pop" + +--[[ TODO +Test using different Drawables: Canvas, Image, Video +- Test without scaling, test with scaling up, test with scaling down. +- Test wtih scaling unevenly (more x / more y, one test for each!). +- Look at what can be done to them, and test doing other things. +- Test using various color tables. +- Test using false. +]]