window added (wip), other improvements/changes

This commit is contained in:
Fox 2016-03-30 14:01:15 -07:00
parent e2a14399bd
commit b5496c4aa4
11 changed files with 561 additions and 9 deletions

View File

@ -17,6 +17,24 @@ function love.load()
pop.skin(pop.text("Here's easier-to-code test text in the center!"):align("center", "center", true)) -- 'true' means align to pixel! 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) 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) b = pop.box(w, {0, 0, 0, 255}):setMargin(5):setSize(100, 100)
c:move(100)
w2 = pop.window(nil, "Window")
w2:move(100, 100)
w2:setWidth(500)
w2:move(-50, 80)
w2:setHeight(500)
w2:move(0, -175)
w2.child[2]:align("center")
--w2:align("center")
--w2:setAlignment("center"):align("center")
--w2.child[1]:setBackground {100, 100, 100, 255}
--w2.child[3]:setBackground {160, 140, 40, 255}
--TODO make rounding to nearest pixel DEFAULT BEHAVIOR
--TODO make debugdraw better
end end
function love.update(dt) function love.update(dt)
@ -26,6 +44,7 @@ end
function love.draw() function love.draw()
pop.draw() pop.draw()
--pop.debugDraw() --pop.debugDraw()
--w2:debugDraw()
end end
function love.mousepressed(x, y, button) function love.mousepressed(x, y, button)

View File

@ -105,6 +105,32 @@ do
getSize = function(self) getSize = function(self)
return self.w, self.h return self.w, self.h
end, end,
setWidth = function(self, w)
local _exp_0 = self.horizontal
if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2)
elseif "right" == _exp_0 then
self.x = self.x - (w - self.w)
end
self.w = w
return self
end,
getWidth = function(self)
return self.w
end,
setHeight = function(self, h)
local _exp_0 = self.vertical
if "center" == _exp_0 then
self.y = self.y - ((h - self.h) / 2)
elseif "bottom" == _exp_0 then
self.y = self.y - (h - self.h)
end
self.h = h
return self
end,
getHeight = function(self)
return self.h
end,
adjustSize = function(self, w, h) adjustSize = function(self, w, h)
local W, H = self:getSize() local W, H = self:getSize()
if w then if w then
@ -136,7 +162,7 @@ do
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_1 then
self.y = self.y + (self.parent.h - self.h - self.margin) self.y = self.y + (self.parent.h - self.h - self.margin)
end end
if toPixel then if toPixel or (toPixel == nil) then
self.x = floor(self.x) self.x = floor(self.x)
self.y = floor(self.y) self.y = floor(self.y)
end end
@ -179,8 +205,8 @@ do
self.x = 0 self.x = 0
self.y = 0 self.y = 0
end end
self.w = 10 self.w = 20
self.h = 10 self.h = 20
self.horizontal = "left" self.horizontal = "left"
self.vertical = "top" self.vertical = "top"
self.margin = 0 self.margin = 0

View File

@ -56,6 +56,14 @@ do
self.h = h self.h = h
return self return self
end, end,
setWidth = function(self)
self:setSize()
return self
end,
setHeight = function(self)
self:setSize()
return self
end,
setText = function(self, text) setText = function(self, text)
if text == nil then if text == nil then
text = "" text = ""

View File

@ -0,0 +1,155 @@
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("/window"))
local element = require(tostring(path) .. "/element")
local box = require(tostring(path) .. "/box")
local text = require(tostring(path) .. "/text")
local window
do
local _class_0
local _parent_0 = element
local _base_0 = {
debugDraw = function(self)
graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h)
graphics.setColor(200, 0, 200, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h)
graphics.setColor(255, 200, 255, 255)
graphics.print("w", self.x, self.y)
return self
end,
setSize = function(self, w, h)
local x = 0
local y = 0
if w then
local _exp_0 = self.horizontal
if "center" == _exp_0 then
x = x - ((w - self.w) / 2)
elseif "right" == _exp_0 then
x = x - (w - self.w)
end
self.head:setWidth(w)
self.window:setWidth(w)
self.w = w
self.x = self.x + x
end
if h then
h = h - self.head:getHeight()
local _exp_0 = self.vertical
if "center" == _exp_0 then
y = y - ((h - self.h) / 2)
elseif "right" == _exp_0 then
y = y - (h - self.h)
end
self.window:setHeight(h)
self.h = h + self.head:getHeight()
self.y = self.y + y
end
self.head:move(x, y)
self.title:move(x, y)
self.window:move(x, y)
return self
end,
setWidth = function(self, w)
local x = 0
local _exp_0 = self.horizontal
if "center" == _exp_0 then
x = x - ((w - self.w) / 2)
elseif "right" == _exp_0 then
x = x - (w - self.w)
end
self.head:setWidth(w)
self.window:setWidth(w)
self.w = w
self.x = self.x + x
self.head:move(x)
self.title:move(x)
self.window:move(x)
return self
end,
setHeight = function(self, h)
local y = 0
h = h - self.head:getHeight()
local _exp_0 = self.vertical
if "center" == _exp_0 then
y = y - ((h - self.h) / 2)
elseif "right" == _exp_0 then
y = y - (h - self.h)
end
self.window:setHeight(h)
self.h = h + self.head:getHeight()
self.y = self.y + y
self.head:move(x, y)
self.title:move(x, y)
self.window:move(x, y)
return self
end
}
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, parent, title, tBackground)
if title == nil then
title = "window"
end
if tBackground == nil then
tBackground = {
25,
180,
230,
255
}
end
_class_0.__parent.__init(self, parent)
self.head = box(self, tBackground)
self.title = text(self, title)
self.window = box(self, {
0,
0,
0,
255
})
local height = self.title:getHeight()
self.head:setSize(self.w, height)
self.window:move(nil, height)
self:setSize(100, 80)
self.child = {
self.head,
self.title,
self.window
}
end,
__base = _base_0,
__name = "window",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil then
local parent = rawget(cls, "__parent")
if parent then
return parent[name]
end
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
window = _class_0
return _class_0
end

View File

@ -105,6 +105,32 @@ do
getSize = function(self) getSize = function(self)
return self.w, self.h return self.w, self.h
end, end,
setWidth = function(self, w)
local _exp_0 = self.horizontal
if "center" == _exp_0 then
self.x = self.x - ((w - self.w) / 2)
elseif "right" == _exp_0 then
self.x = self.x - (w - self.w)
end
self.w = w
return self
end,
getWidth = function(self)
return self.w
end,
setHeight = function(self, h)
local _exp_0 = self.vertical
if "center" == _exp_0 then
self.y = self.y - ((h - self.h) / 2)
elseif "bottom" == _exp_0 then
self.y = self.y - (h - self.h)
end
self.h = h
return self
end,
getHeight = function(self)
return self.h
end,
adjustSize = function(self, w, h) adjustSize = function(self, w, h)
local W, H = self:getSize() local W, H = self:getSize()
if w then if w then
@ -136,7 +162,7 @@ do
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_1 then
self.y = self.y + (self.parent.h - self.h - self.margin) self.y = self.y + (self.parent.h - self.h - self.margin)
end end
if toPixel then if toPixel or (toPixel == nil) then
self.x = floor(self.x) self.x = floor(self.x)
self.y = floor(self.y) self.y = floor(self.y)
end end
@ -179,8 +205,8 @@ do
self.x = 0 self.x = 0
self.y = 0 self.y = 0
end end
self.w = 10 self.w = 20
self.h = 10 self.h = 20
self.horizontal = "left" self.horizontal = "left"
self.vertical = "top" self.vertical = "top"
self.margin = 0 self.margin = 0

View File

@ -56,6 +56,14 @@ do
self.h = h self.h = h
return self return self
end, end,
setWidth = function(self)
self:setSize()
return self
end,
setHeight = function(self)
self:setSize()
return self
end,
setText = function(self, text) setText = function(self, text)
if text == nil then if text == nil then
text = "" text = ""

155
lib/pop/elements/window.lua Normal file
View File

@ -0,0 +1,155 @@
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("/window"))
local element = require(tostring(path) .. "/element")
local box = require(tostring(path) .. "/box")
local text = require(tostring(path) .. "/text")
local window
do
local _class_0
local _parent_0 = element
local _base_0 = {
debugDraw = function(self)
graphics.setLineWidth(0.5)
graphics.setColor(0, 0, 0, 100)
graphics.rectangle("fill", self.x, self.y, self.w, self.h)
graphics.setColor(200, 0, 200, 200)
graphics.rectangle("line", self.x, self.y, self.w, self.h)
graphics.setColor(255, 200, 255, 255)
graphics.print("w", self.x, self.y)
return self
end,
setSize = function(self, w, h)
local x = 0
local y = 0
if w then
local _exp_0 = self.horizontal
if "center" == _exp_0 then
x = x - ((w - self.w) / 2)
elseif "right" == _exp_0 then
x = x - (w - self.w)
end
self.head:setWidth(w)
self.window:setWidth(w)
self.w = w
self.x = self.x + x
end
if h then
h = h - self.head:getHeight()
local _exp_0 = self.vertical
if "center" == _exp_0 then
y = y - ((h - self.h) / 2)
elseif "right" == _exp_0 then
y = y - (h - self.h)
end
self.window:setHeight(h)
self.h = h + self.head:getHeight()
self.y = self.y + y
end
self.head:move(x, y)
self.title:move(x, y)
self.window:move(x, y)
return self
end,
setWidth = function(self, w)
local x = 0
local _exp_0 = self.horizontal
if "center" == _exp_0 then
x = x - ((w - self.w) / 2)
elseif "right" == _exp_0 then
x = x - (w - self.w)
end
self.head:setWidth(w)
self.window:setWidth(w)
self.w = w
self.x = self.x + x
self.head:move(x)
self.title:move(x)
self.window:move(x)
return self
end,
setHeight = function(self, h)
local y = 0
h = h - self.head:getHeight()
local _exp_0 = self.vertical
if "center" == _exp_0 then
y = y - ((h - self.h) / 2)
elseif "right" == _exp_0 then
y = y - (h - self.h)
end
self.window:setHeight(h)
self.h = h + self.head:getHeight()
self.y = self.y + y
self.head:move(x, y)
self.title:move(x, y)
self.window:move(x, y)
return self
end
}
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, parent, title, tBackground)
if title == nil then
title = "window"
end
if tBackground == nil then
tBackground = {
25,
180,
230,
255
}
end
_class_0.__parent.__init(self, parent)
self.head = box(self, tBackground)
self.title = text(self, title)
self.window = box(self, {
0,
0,
0,
255
})
local height = self.title:getHeight()
self.head:setSize(self.w, height)
self.window:move(nil, height)
self:setSize(100, 80)
self.child = {
self.head,
self.title,
self.window
}
end,
__base = _base_0,
__name = "window",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil then
local parent = rawget(cls, "__parent")
if parent then
return parent[name]
end
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
window = _class_0
return _class_0
end

View File

@ -13,8 +13,8 @@ class element
@x = 0 @x = 0
@y = 0 @y = 0
@w = 10 @w = 20
@h = 10 @h = 20
@horizontal = "left" @horizontal = "left"
@vertical = "top" @vertical = "top"
@ -117,6 +117,34 @@ class element
getSize: => getSize: =>
return @w, @h return @w, @h
setWidth: (w) =>
switch @horizontal
when "center"
@x -= (w - @w)/2
when "right"
@x -= w - @w
@w = w
return @
getWidth: =>
return @w
setHeight: (h) =>
switch @vertical
when "center"
@y -= (h - @h)/2
when "bottom"
@y -= h - @h
@h = h
return @
getHeight: =>
return @h
adjustSize: (w, h) => adjustSize: (w, h) =>
W, H = @getSize! W, H = @getSize!
@ -152,7 +180,7 @@ class element
when "bottom" when "bottom"
@y += @parent.h - @h - @margin @y += @parent.h - @h - @margin
if toPixel if toPixel or (toPixel == nil)
@x = floor @x @x = floor @x
@y = floor @y @y = floor @y

View File

@ -59,6 +59,16 @@ class text extends element
return @ return @
-- cannot set width!
setWidth: =>
@setSize!
return @
-- cannot set height!
setHeight: =>
@setSize!
return @
setText: (text="") => setText: (text="") =>
@text = text @text = text
@setSize! @setSize!

View File

@ -0,0 +1,111 @@
import graphics from love
import sub, len from string
path = sub ..., 1, len(...) - len "/window"
element = require "#{path}/element"
box = require "#{path}/box"
text = require "#{path}/text"
class window extends element
new: (parent, title="window", tBackground={25, 180, 230, 255}) =>
super parent
@head = box @, tBackground -- title box at top
@title = text @, title -- text at top
@window = box @, {0,0,0,255} -- main window area
-- correct placement / sizes of elements
height = @title\getHeight!
@head\setSize @w, height
@window\move nil, height
@setSize 100, 80
-- our child elements are still child elements
@child = {
@head, @title, @window
}
debugDraw: =>
graphics.setLineWidth 0.5
graphics.setColor 0, 0, 0, 100
graphics.rectangle "fill", @x, @y, @w, @h
graphics.setColor 200, 0, 200, 200
graphics.rectangle "line", @x, @y, @w, @h
graphics.setColor 255, 200, 255, 255
graphics.print "w", @x, @y
return @
setSize: (w, h) =>
x = 0
y = 0
if w
switch @horizontal
when "center"
x -= (w - @w)/2
when "right"
x -= w - @w
@head\setWidth w
@window\setWidth w
@w = w
@x += x
if h
h = h - @head\getHeight!
switch @vertical
when "center"
y -= (h - @h)/2
when "right"
y -= h - @h
@window\setHeight h
@h = h + @head\getHeight!
@y += y
@head\move x, y
@title\move x, y
@window\move x, y
return @
setWidth: (w) =>
x = 0
switch @horizontal
when "center"
x -= (w - @w)/2
when "right"
x -= w - @w
@head\setWidth w
@window\setWidth w
@w = w
@x += x
@head\move x
@title\move x
@window\move x
return @
setHeight: (h) =>
y = 0
h = h - @head\getHeight!
switch @vertical
when "center"
y -= (h - @h)/2
when "right"
y -= h - @h
@window\setHeight h
@h = h + @head\getHeight!
@y += y
@head\move x, y
@title\move x, y
@window\move x, y
return @

View File

@ -14,6 +14,7 @@ pop.focused = false
-- loads elements and skins, creates pop.screen (intended to only be called once at the beginning) -- loads elements and skins, creates pop.screen (intended to only be called once at the beginning)
pop.load = -> pop.load = ->
elements = filesystem.getDirectoryItems "#{path}/elements" elements = filesystem.getDirectoryItems "#{path}/elements"
for i = 1, #elements for i = 1, #elements
-- only attempt to load lua files -- only attempt to load lua files
unless elements[i]\sub(-4) == ".lua" unless elements[i]\sub(-4) == ".lua"
@ -36,11 +37,14 @@ pop.load = ->
-- works just like above, except no wrappers -- works just like above, except no wrappers
skins = filesystem.getDirectoryItems "#{path}/skins" skins = filesystem.getDirectoryItems "#{path}/skins"
for i = 1, #skins for i = 1, #skins
unless skins[i]\sub(-4) == ".lua" unless skins[i]\sub(-4) == ".lua"
continue continue
name = skins[i]\sub 1, -5 name = skins[i]\sub 1, -5
pop.skins[name] = require "#{path}/skins/#{name}" pop.skins[name] = require "#{path}/skins/#{name}"
print "skin loaded: \"#{name}\"" print "skin loaded: \"#{name}\""
-- main window (called screen because there will be a window element class) -- main window (called screen because there will be a window element class)
@ -57,6 +61,7 @@ pop.create = (element, parent=pop.screen, ...) ->
return element return element
pop.update = (dt, element=pop.screen) -> pop.update = (dt, element=pop.screen) ->
--pop.screen\update dt
unless element.excludeUpdate unless element.excludeUpdate
if element.update if element.update
element\update dt element\update dt
@ -64,6 +69,7 @@ pop.update = (dt, element=pop.screen) ->
pop.update dt, element.child[i] pop.update dt, element.child[i]
pop.draw = (element=pop.screen) -> pop.draw = (element=pop.screen) ->
--pop.screen\draw!
unless element.excludeDraw unless element.excludeDraw
if element.draw if element.draw
element\draw! element\draw!