Pop.Box/demo/pop/elements/window.lua

349 lines
9.9 KiB
Lua
Raw Normal View History

2016-04-17 07:29:27 +00:00
local graphics, mouse
do
local _obj_0 = love
graphics, mouse = _obj_0.graphics, _obj_0.mouse
end
2016-04-17 06:58:17 +00:00
local insert, remove
do
local _obj_0 = table
insert, remove = _obj_0.insert, _obj_0.remove
end
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")
2016-04-18 03:52:39 +00:00
local closeImage = graphics.newImage(tostring(path) .. "/img/close.png")
local left = 1
local mousemoved_event = true
do
local major, minor, revision = love.getVersion()
if (major == 0) and (minor == 10) and ((revision == 0) or (revision == 1)) then
left = 1
2016-04-03 07:22:49 +00:00
elseif (major == 0) and (minor == 9) then
left = "l"
if revision == 1 then
mousemoved_event = false
end
else
2016-04-17 06:58:17 +00:00
print("elements/window: unrecognized LOVE version: " .. tostring(major) .. "." .. tostring(minor) .. "." .. tostring(revision))
print(" assuming LOVE version > 0.10.1 (there may be bugs)")
end
end
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)
2016-05-01 08:55:50 +00:00
graphics.rectangle("fill", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(200, 0, 200, 200)
2016-05-01 08:55:50 +00:00
graphics.rectangle("line", self.data.x, self.data.y, self.data.w, self.data.h)
graphics.setColor(255, 200, 255, 255)
2016-05-01 08:55:50 +00:00
graphics.print("w", self.data.x, self.data.y)
return self
end,
addChild = function(self, child)
2016-05-01 08:55:50 +00:00
self.data.area:addChild(child)
return self
end,
2016-04-17 06:58:17 +00:00
removeChild = function(self, child)
2016-05-01 08:55:50 +00:00
local result = self.data.area:removeChild(child)
if result == self.data.area then
2016-04-17 06:58:17 +00:00
return self
elseif type(result) == "string" then
2016-05-01 08:55:50 +00:00
for k, v in ipairs(self.data.child) do
if v == child then
2016-05-01 08:55:50 +00:00
remove(self.data.child, k)
return self
end
end
return "Element \"" .. tostring(child) .. "\" is not a child of window \"" .. tostring(self) .. "\". Cannot remove it."
2016-04-17 06:58:17 +00:00
else
return result
end
end,
getChildren = function(self)
2016-05-01 08:55:50 +00:00
return self.data.area.child
end,
align = function(self, horizontal, vertical, toPixel)
_class_0.__parent.__base.align(self, horizontal, vertical, toPixel)
2016-05-01 08:55:50 +00:00
for i = 1, #self.data.child do
self.data.child[i]:align()
end
2016-05-01 08:55:50 +00:00
self.data.area:move(nil, self.data.head:getHeight())
return self
end,
setSize = function(self, w, h)
local x = 0
local y = 0
if w then
2016-05-01 08:55:50 +00:00
local _exp_0 = self.data.horizontal
if "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
x = x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then
2016-05-01 08:55:50 +00:00
x = x - (w - self.data.w)
end
2016-05-01 08:55:50 +00:00
if self.data.close then
self.data.head:setWidth(w - self.data.head:getHeight())
else
2016-05-01 08:55:50 +00:00
self.data.head:setWidth(w)
end
2016-05-01 08:55:50 +00:00
self.data.area:setWidth(w)
self.data.w = w
self.data.x = self.data.x + x
self.data.title:align()
if self.data.close then
self.data.close:align()
end
end
if h then
2016-05-01 08:55:50 +00:00
h = h - self.data.head:getHeight()
local _exp_0 = self.data.vertical
if "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
y = y - ((h - self.data.h) / 2)
elseif "right" == _exp_0 then
2016-05-01 08:55:50 +00:00
y = y - (h - self.data.h)
end
2016-05-01 08:55:50 +00:00
self.data.area:setHeight(h)
self.data.h = h + self.data.head:getHeight()
self.data.y = self.data.y + y
end
2016-05-01 08:55:50 +00:00
self.data.head:move(x, y)
self.data.area:move(x, y)
return self
end,
setWidth = function(self, w)
local x = 0
2016-05-01 08:55:50 +00:00
local _exp_0 = self.data.horizontal
if "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
x = x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then
2016-05-01 08:55:50 +00:00
x = x - (w - self.data.w)
end
2016-05-01 08:55:50 +00:00
if self.data.close then
self.data.head:setWidth(w - self.data.head:getHeight())
else
2016-05-01 08:55:50 +00:00
self.data.head:setWidth(w)
end
2016-05-01 08:55:50 +00:00
self.data.area:setWidth(w)
self.data.w = w
self.data.x = self.data.x + x
self.data.title:align()
if self.data.close then
self.data.close:align()
end
2016-05-01 08:55:50 +00:00
self.data.head:move(x)
self.data.area:move(x)
return self
end,
setHeight = function(self, h)
local y = 0
2016-05-01 08:55:50 +00:00
h = h - self.data.head:getHeight()
local _exp_0 = self.data.vertical
if "center" == _exp_0 then
2016-05-01 08:55:50 +00:00
y = y - ((h - self.data.h) / 2)
elseif "right" == _exp_0 then
2016-05-01 08:55:50 +00:00
y = y - (h - self.data.h)
end
2016-05-01 08:55:50 +00:00
self.data.area:setHeight(h)
self.data.h = h + self.data.head:getHeight()
self.data.y = self.data.y + y
self.data.head:move(nil, y)
self.data.title:move(nil, y)
self.data.area:move(nil, y)
return self
end,
setTitle = function(self, title)
2016-05-01 08:55:50 +00:00
self.data.title:setText(title)
if self.data.overflow == "trunicate" then
while self.data.title:getWidth() > self.data.head:getWidth() do
title = title:sub(1, -3)
2016-05-01 08:55:50 +00:00
self.data.title:setText(title .. "")
end
2016-05-01 08:55:50 +00:00
elseif self.data.overflow == "resize" then
if self.data.title:getWidth() > self.data.head:getWidth() then
self:setWidth(self.data.title:getWidth())
end
end
return self
2016-04-17 07:29:27 +00:00
end,
getTitle = function(self)
2016-05-01 08:55:50 +00:00
return self.data.title:getText()
end,
setTitleOverflow = function(self, method)
2016-05-01 08:55:50 +00:00
self.data.overflow = method
return self
end,
getTitleOverflow = function(self)
2016-05-01 08:55:50 +00:00
return self.data.overflow
end,
setClose = function(self, enabled)
if enabled then
2016-05-01 08:55:50 +00:00
self.data.close = box(self, closeImage)
self.data.close.clicked = function()
2016-04-18 03:52:39 +00:00
self:delete()
return true
end
2016-05-01 08:55:50 +00:00
local height = self.data.head:getHeight()
self.data.close:align("right"):setSize(height, height)
self.data.head:setWidth(self.data.w - height)
self.data.title:align()
insert(self.data.child, self.data.close)
else
2016-05-01 08:55:50 +00:00
self.data.close:delete()
self.data.head:setWidth(self.data.w)
self.data.title:align()
self.data.close = false
end
return self
end,
hasClose = function(self)
2016-05-01 08:55:50 +00:00
if self.data.close then
return true
else
return false
end
2016-04-20 23:12:38 +00:00
end,
delete = function(self)
_class_0.__parent.__base.delete(self)
2016-05-01 08:55:50 +00:00
self.data.head = nil
self.data.title = nil
self.data.area = nil
self.data.close = nil
end
}
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, parent, title, tBackground, tColor, wBackground)
if title == nil then
title = "window"
end
if tBackground == nil then
tBackground = {
25,
180,
230,
255
}
end
if tColor == nil then
tColor = {
255,
255,
255,
255
}
end
if wBackground == nil then
wBackground = {
200,
200,
210,
255
}
end
_class_0.__parent.__init(self, parent)
2016-05-01 08:55:50 +00:00
self.data.head = box(self, tBackground)
self.data.title = text(self.data.head, title, tColor)
self.data.area = box(self, wBackground)
self.data.close = box(self, closeImage)
local height = self.data.title:getHeight()
self.data.head:setSize(self.data.w - height, height)
self.data.area:move(nil, height)
self.data.close:align("right"):setSize(height, height)
self:setSize(100, 80)
2016-05-01 08:55:50 +00:00
self.data.child = {
self.data.head,
self.data.title,
self.data.area,
self.data.close
}
2016-05-01 08:55:50 +00:00
self.data.overflow = "trunicate"
self.data.area.mousepressed = function()
return true
end
2016-05-01 08:55:50 +00:00
self.data.area.clicked = function()
return true
end
2016-05-01 08:55:50 +00:00
self.data.close.clicked = function()
self:delete()
return true
end
2016-05-01 08:55:50 +00:00
self.data.head.data.selected = false
if mousemoved_event then
2016-05-01 08:55:50 +00:00
self.data.head.mousemoved = function(self, x, y, dx, dy)
if self.data.selected then
self.data.parent:move(dx, dy)
return true
end
return false
end
2016-05-01 08:55:50 +00:00
self.data.head.mousepressed = function(self, x, y, button)
if button == left then
2016-05-01 08:55:50 +00:00
self.data.selected = true
return true
end
return false
end
else
2016-05-01 08:55:50 +00:00
self.data.head.data.mx = 0
self.data.head.data.my = 0
self.data.head.update = function(self)
2016-04-17 07:29:27 +00:00
local x, y = mouse.getPosition()
2016-05-01 08:55:50 +00:00
return self:setPosition(x - self.data.mx, y - self.data.my)
end
2016-05-01 08:55:50 +00:00
self.data.head.mousepressed = function(self, x, y, button)
if button == left then
2016-05-01 08:55:50 +00:00
self.data.selected = true
self.data.mx = x
self.data.my = y
return true
end
return false
end
end
2016-05-01 08:55:50 +00:00
self.data.head.mousereleased = function(self, x, y, button)
2016-04-17 07:29:27 +00:00
if button == left then
2016-05-01 08:55:50 +00:00
self.data.selected = false
2016-04-17 07:29:27 +00:00
return true
end
return false
end
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