mirror of
https://github.com/TangentFoxy/Pop.Box.git
synced 2024-12-15 12:44:20 +00:00
wip close buttons, titleoverflow controls, etc
This commit is contained in:
parent
a24bd88a17
commit
16b9d28973
@ -2,6 +2,7 @@ local lg = love.graphics
|
|||||||
local pop, inspect
|
local pop, inspect
|
||||||
|
|
||||||
local debugDraw = false
|
local debugDraw = false
|
||||||
|
local videoFile = lg.newVideo("test.ogv") -- so we can loop playback
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
print(love.getVersion())
|
print(love.getVersion())
|
||||||
@ -30,7 +31,6 @@ function love.load()
|
|||||||
--b:margin(2) -- testing streamlined_get_set extension
|
--b:margin(2) -- testing streamlined_get_set extension
|
||||||
b:fill() -- testing fill!
|
b:fill() -- testing fill!
|
||||||
|
|
||||||
---[[
|
|
||||||
w2 = pop.window(nil, "Window")
|
w2 = pop.window(nil, "Window")
|
||||||
w2:move(100, 100)
|
w2:move(100, 100)
|
||||||
w2:setWidth(500)
|
w2:setWidth(500)
|
||||||
@ -39,14 +39,15 @@ function love.load()
|
|||||||
w2:move(0, -175)
|
w2:move(0, -175)
|
||||||
w2.title:align("center")
|
w2.title:align("center")
|
||||||
w2:position(0, 0)
|
w2:position(0, 0)
|
||||||
--w2:setAlignment("right")
|
|
||||||
w2:size(200, 120):position(90, 70)
|
w2:size(200, 120):position(90, 70)
|
||||||
--w2:align("center")
|
w2:setClose(false)
|
||||||
--w2:setAlignment("center"):align("center")
|
local t2 = pop.text("Click here to toggle close\nbutton on this window."):setMargin(10):setColor(0,0,0)
|
||||||
|
t2.clicked = function()
|
||||||
--w2.child[1]:setBackground {100, 100, 100, 255}
|
print("CALLED") --NOTE not working!
|
||||||
--w2.child[3]:setBackground {160, 140, 40, 255}
|
w2:setClose(not w2:hasClose())
|
||||||
--]]
|
return true
|
||||||
|
end
|
||||||
|
w2:addChild(t2)
|
||||||
|
|
||||||
local test = lg.newImage("test.png")
|
local test = lg.newImage("test.png")
|
||||||
G = pop.element():align("right"):move(-2, 2)
|
G = pop.element():align("right"):move(-2, 2)
|
||||||
@ -58,14 +59,23 @@ function love.load()
|
|||||||
print(b.horizontal, b.vertical)
|
print(b.horizontal, b.vertical)
|
||||||
print(c.horizontal, c.vertical)
|
print(c.horizontal, c.vertical)
|
||||||
|
|
||||||
local window = pop.window():align("center", "center"):setTitle("Welcome!")
|
local window = pop.window():align("center", "center"):setTitle("Welcome! This title is far too big!")
|
||||||
--window:addChild(pop.text("Welcome to Pop.Box()!"))
|
--window:addChild(pop.text("Welcome to Pop.Box()!"))
|
||||||
|
|
||||||
|
pop.window():setClose(false):setClose(true)
|
||||||
|
|
||||||
|
local video = pop.box():align("right", "bottom"):setBackground(videoFile):setSize(320/2, 240/2):move(-20, -20)
|
||||||
|
videoFile:play()
|
||||||
|
|
||||||
--TODO make debugDraw better
|
--TODO make debugDraw better
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
pop.update(dt)
|
pop.update(dt)
|
||||||
|
|
||||||
|
if not videoFile:isPlaying() then
|
||||||
|
videoFile:rewind()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
|
@ -28,6 +28,7 @@ do
|
|||||||
end
|
end
|
||||||
insert(self.child, child)
|
insert(self.child, child)
|
||||||
child.parent = self
|
child.parent = self
|
||||||
|
child:align()
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
removeChild = function(self, child)
|
removeChild = function(self, child)
|
||||||
@ -41,7 +42,7 @@ do
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return error("Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it.")
|
return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it."
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
getChildren = function(self)
|
getChildren = function(self)
|
||||||
@ -240,7 +241,8 @@ do
|
|||||||
for k, v in ipairs(self.child) do
|
for k, v in ipairs(self.child) do
|
||||||
v:delete()
|
v:delete()
|
||||||
end
|
end
|
||||||
return self.parent:removeChild(self)
|
self.parent:removeChild(self)
|
||||||
|
self = nil
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
|
BIN
demo/pop/elements/img/close.png
Normal file
BIN
demo/pop/elements/img/close.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 355 B |
@ -17,6 +17,12 @@ local path = sub(..., 1, len(...) - len("/window"))
|
|||||||
local element = require(tostring(path) .. "/element")
|
local element = require(tostring(path) .. "/element")
|
||||||
local box = require(tostring(path) .. "/box")
|
local box = require(tostring(path) .. "/box")
|
||||||
local text = require(tostring(path) .. "/text")
|
local text = require(tostring(path) .. "/text")
|
||||||
|
local closeImage = {
|
||||||
|
100,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
80
|
||||||
|
}
|
||||||
local left = 1
|
local left = 1
|
||||||
local mousemoved_event = true
|
local mousemoved_event = true
|
||||||
do
|
do
|
||||||
@ -60,6 +66,14 @@ do
|
|||||||
local result = self.window:removeChild(child)
|
local result = self.window:removeChild(child)
|
||||||
if result == self.window then
|
if result == self.window then
|
||||||
return self
|
return self
|
||||||
|
elseif type(result) == "string" then
|
||||||
|
for k, v in ipairs(self.child) do
|
||||||
|
if v == child then
|
||||||
|
remove(self.child, k)
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return "Element \"" .. tostring(child) .. "\" is not a child of window \"" .. tostring(self) .. "\". Cannot remove it."
|
||||||
else
|
else
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
@ -85,11 +99,18 @@ do
|
|||||||
elseif "right" == _exp_0 then
|
elseif "right" == _exp_0 then
|
||||||
x = x - (w - self.w)
|
x = x - (w - self.w)
|
||||||
end
|
end
|
||||||
|
if self.close then
|
||||||
|
self.head:setWidth(w - self.head:getHeight())
|
||||||
|
else
|
||||||
self.head:setWidth(w)
|
self.head:setWidth(w)
|
||||||
|
end
|
||||||
self.window:setWidth(w)
|
self.window:setWidth(w)
|
||||||
self.w = w
|
self.w = w
|
||||||
self.x = self.x + x
|
self.x = self.x + x
|
||||||
self.title:align()
|
self.title:align()
|
||||||
|
if self.close then
|
||||||
|
self.close:align()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if h then
|
if h then
|
||||||
h = h - self.head:getHeight()
|
h = h - self.head:getHeight()
|
||||||
@ -115,11 +136,18 @@ do
|
|||||||
elseif "right" == _exp_0 then
|
elseif "right" == _exp_0 then
|
||||||
x = x - (w - self.w)
|
x = x - (w - self.w)
|
||||||
end
|
end
|
||||||
|
if self.close then
|
||||||
|
self.head:setWidth(w - self.head:getHeight())
|
||||||
|
else
|
||||||
self.head:setWidth(w)
|
self.head:setWidth(w)
|
||||||
|
end
|
||||||
self.window:setWidth(w)
|
self.window:setWidth(w)
|
||||||
self.w = w
|
self.w = w
|
||||||
self.x = self.x + x
|
self.x = self.x + x
|
||||||
self.title:align()
|
self.title:align()
|
||||||
|
if self.close then
|
||||||
|
self.close:align()
|
||||||
|
end
|
||||||
self.head:move(x)
|
self.head:move(x)
|
||||||
self.window:move(x)
|
self.window:move(x)
|
||||||
return self
|
return self
|
||||||
@ -143,10 +171,50 @@ do
|
|||||||
end,
|
end,
|
||||||
setTitle = function(self, title)
|
setTitle = function(self, title)
|
||||||
self.title:setText(title)
|
self.title:setText(title)
|
||||||
|
if self.titleOverflow == "trunicate" then
|
||||||
|
while self.title:getWidth() > self.head:getWidth() do
|
||||||
|
title = title:sub(1, -3)
|
||||||
|
self.title:setText(title .. "…")
|
||||||
|
end
|
||||||
|
elseif self.titleOverflow == "resize" then
|
||||||
|
if self.title:getWidth() > self.head:getWidth() then
|
||||||
|
self:setWidth(self.title:getWidth())
|
||||||
|
end
|
||||||
|
end
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
getTitle = function(self)
|
getTitle = function(self)
|
||||||
return self.title:getText()
|
return self.title:getText()
|
||||||
|
end,
|
||||||
|
setTitleOverflow = function(self, method)
|
||||||
|
self.titleOverflow = method
|
||||||
|
return self
|
||||||
|
end,
|
||||||
|
getTitleOverflow = function(self)
|
||||||
|
return self.titleOverflow
|
||||||
|
end,
|
||||||
|
setClose = function(self, enabled)
|
||||||
|
if enabled then
|
||||||
|
self.close = box(self, closeImage)
|
||||||
|
local height = self.head:getHeight()
|
||||||
|
self.close:align("right"):setSize(height, height)
|
||||||
|
self.head:setWidth(self.w - height)
|
||||||
|
self.title:align()
|
||||||
|
insert(self.child, self.close)
|
||||||
|
else
|
||||||
|
self.close:delete()
|
||||||
|
self.head:setWidth(self.w)
|
||||||
|
self.title:align()
|
||||||
|
self.close = false
|
||||||
|
end
|
||||||
|
return self
|
||||||
|
end,
|
||||||
|
hasClose = function(self)
|
||||||
|
if self.close then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
@ -184,15 +252,24 @@ do
|
|||||||
self.head = box(self, tBackground)
|
self.head = box(self, tBackground)
|
||||||
self.title = text(self, title, tColor)
|
self.title = text(self, title, tColor)
|
||||||
self.window = box(self, wBackground)
|
self.window = box(self, wBackground)
|
||||||
|
self.close = box(self, closeImage)
|
||||||
local height = self.title:getHeight()
|
local height = self.title:getHeight()
|
||||||
self.head:setSize(self.w, height)
|
self.head:setSize(self.w - height, height)
|
||||||
self.window:move(nil, height)
|
self.window:move(nil, height)
|
||||||
|
self.close:align("right"):setSize(height, height)
|
||||||
self:setSize(100, 80)
|
self:setSize(100, 80)
|
||||||
self.child = {
|
self.child = {
|
||||||
self.head,
|
self.head,
|
||||||
self.title,
|
self.title,
|
||||||
self.window
|
self.window,
|
||||||
|
self.close
|
||||||
}
|
}
|
||||||
|
self.titleOverflow = "trunicate"
|
||||||
|
self.close.clicked = function()
|
||||||
|
print("CLOSE WAS CLICKED")
|
||||||
|
self:delete()
|
||||||
|
return true
|
||||||
|
end
|
||||||
self.head.selected = false
|
self.head.selected = false
|
||||||
if mousemoved_event then
|
if mousemoved_event then
|
||||||
self.head.mousemoved = function(self, x, y, dx, dy)
|
self.head.mousemoved = function(self, x, y, dx, dy)
|
||||||
|
@ -156,12 +156,35 @@ pop.mousepressed = function(x, y, button, element)
|
|||||||
end
|
end
|
||||||
return handled
|
return handled
|
||||||
end
|
end
|
||||||
pop.mousereleased = function(x, y, button)
|
pop.mousereleased = function(x, y, button, element)
|
||||||
print("mousereleased", x, y, button)
|
|
||||||
local clickedHandled = false
|
local clickedHandled = false
|
||||||
local mousereleasedHandled = false
|
local mousereleasedHandled = false
|
||||||
|
if element then
|
||||||
|
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
||||||
|
if element.clicked and (not element.excludeDraw) then
|
||||||
|
clickedHandled = element:clicked(x - element.x, y - element.y, button)
|
||||||
|
end
|
||||||
|
if element.mousereleased then
|
||||||
|
mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button)
|
||||||
|
end
|
||||||
|
if clickedHandled then
|
||||||
|
pop.focused = element
|
||||||
|
end
|
||||||
|
if clickedHandled or mousereleasedHandled then
|
||||||
|
return clickedHandled, mousereleasedHandled
|
||||||
|
else
|
||||||
|
for i = 1, #element.child do
|
||||||
|
clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i])
|
||||||
|
if clickedHandled or mousereleasedHandled then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print("mousereleased", x, y, button)
|
||||||
do
|
do
|
||||||
local element = pop.events[button]
|
element = pop.events[button]
|
||||||
if element then
|
if element then
|
||||||
if element.clicked and (not element.excludeDraw) then
|
if element.clicked and (not element.excludeDraw) then
|
||||||
do
|
do
|
||||||
@ -181,6 +204,10 @@ pop.mousereleased = function(x, y, button)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if (not clickedHandled) and (not mousereleasedHandled) then
|
||||||
|
clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, pop.screen)
|
||||||
|
end
|
||||||
|
end
|
||||||
return clickedHandled, mousereleasedHandled
|
return clickedHandled, mousereleasedHandled
|
||||||
end
|
end
|
||||||
pop.keypressed = function(key)
|
pop.keypressed = function(key)
|
||||||
|
@ -28,6 +28,7 @@ do
|
|||||||
end
|
end
|
||||||
insert(self.child, child)
|
insert(self.child, child)
|
||||||
child.parent = self
|
child.parent = self
|
||||||
|
child:align()
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
removeChild = function(self, child)
|
removeChild = function(self, child)
|
||||||
@ -41,7 +42,7 @@ do
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return error("Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it.")
|
return "Element \"" .. tostring(child) .. "\" is not a child of element \"" .. tostring(self) .. "\". Cannot remove it."
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
getChildren = function(self)
|
getChildren = function(self)
|
||||||
@ -240,7 +241,8 @@ do
|
|||||||
for k, v in ipairs(self.child) do
|
for k, v in ipairs(self.child) do
|
||||||
v:delete()
|
v:delete()
|
||||||
end
|
end
|
||||||
return self.parent:removeChild(self)
|
self.parent:removeChild(self)
|
||||||
|
self = nil
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
|
BIN
lib/pop/elements/img/close.png
Normal file
BIN
lib/pop/elements/img/close.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 355 B |
@ -17,6 +17,12 @@ local path = sub(..., 1, len(...) - len("/window"))
|
|||||||
local element = require(tostring(path) .. "/element")
|
local element = require(tostring(path) .. "/element")
|
||||||
local box = require(tostring(path) .. "/box")
|
local box = require(tostring(path) .. "/box")
|
||||||
local text = require(tostring(path) .. "/text")
|
local text = require(tostring(path) .. "/text")
|
||||||
|
local closeImage = {
|
||||||
|
100,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
80
|
||||||
|
}
|
||||||
local left = 1
|
local left = 1
|
||||||
local mousemoved_event = true
|
local mousemoved_event = true
|
||||||
do
|
do
|
||||||
@ -60,6 +66,14 @@ do
|
|||||||
local result = self.window:removeChild(child)
|
local result = self.window:removeChild(child)
|
||||||
if result == self.window then
|
if result == self.window then
|
||||||
return self
|
return self
|
||||||
|
elseif type(result) == "string" then
|
||||||
|
for k, v in ipairs(self.child) do
|
||||||
|
if v == child then
|
||||||
|
remove(self.child, k)
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return "Element \"" .. tostring(child) .. "\" is not a child of window \"" .. tostring(self) .. "\". Cannot remove it."
|
||||||
else
|
else
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
@ -85,11 +99,18 @@ do
|
|||||||
elseif "right" == _exp_0 then
|
elseif "right" == _exp_0 then
|
||||||
x = x - (w - self.w)
|
x = x - (w - self.w)
|
||||||
end
|
end
|
||||||
|
if self.close then
|
||||||
|
self.head:setWidth(w - self.head:getHeight())
|
||||||
|
else
|
||||||
self.head:setWidth(w)
|
self.head:setWidth(w)
|
||||||
|
end
|
||||||
self.window:setWidth(w)
|
self.window:setWidth(w)
|
||||||
self.w = w
|
self.w = w
|
||||||
self.x = self.x + x
|
self.x = self.x + x
|
||||||
self.title:align()
|
self.title:align()
|
||||||
|
if self.close then
|
||||||
|
self.close:align()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if h then
|
if h then
|
||||||
h = h - self.head:getHeight()
|
h = h - self.head:getHeight()
|
||||||
@ -115,11 +136,18 @@ do
|
|||||||
elseif "right" == _exp_0 then
|
elseif "right" == _exp_0 then
|
||||||
x = x - (w - self.w)
|
x = x - (w - self.w)
|
||||||
end
|
end
|
||||||
|
if self.close then
|
||||||
|
self.head:setWidth(w - self.head:getHeight())
|
||||||
|
else
|
||||||
self.head:setWidth(w)
|
self.head:setWidth(w)
|
||||||
|
end
|
||||||
self.window:setWidth(w)
|
self.window:setWidth(w)
|
||||||
self.w = w
|
self.w = w
|
||||||
self.x = self.x + x
|
self.x = self.x + x
|
||||||
self.title:align()
|
self.title:align()
|
||||||
|
if self.close then
|
||||||
|
self.close:align()
|
||||||
|
end
|
||||||
self.head:move(x)
|
self.head:move(x)
|
||||||
self.window:move(x)
|
self.window:move(x)
|
||||||
return self
|
return self
|
||||||
@ -143,10 +171,50 @@ do
|
|||||||
end,
|
end,
|
||||||
setTitle = function(self, title)
|
setTitle = function(self, title)
|
||||||
self.title:setText(title)
|
self.title:setText(title)
|
||||||
|
if self.titleOverflow == "trunicate" then
|
||||||
|
while self.title:getWidth() > self.head:getWidth() do
|
||||||
|
title = title:sub(1, -3)
|
||||||
|
self.title:setText(title .. "…")
|
||||||
|
end
|
||||||
|
elseif self.titleOverflow == "resize" then
|
||||||
|
if self.title:getWidth() > self.head:getWidth() then
|
||||||
|
self:setWidth(self.title:getWidth())
|
||||||
|
end
|
||||||
|
end
|
||||||
return self
|
return self
|
||||||
end,
|
end,
|
||||||
getTitle = function(self)
|
getTitle = function(self)
|
||||||
return self.title:getText()
|
return self.title:getText()
|
||||||
|
end,
|
||||||
|
setTitleOverflow = function(self, method)
|
||||||
|
self.titleOverflow = method
|
||||||
|
return self
|
||||||
|
end,
|
||||||
|
getTitleOverflow = function(self)
|
||||||
|
return self.titleOverflow
|
||||||
|
end,
|
||||||
|
setClose = function(self, enabled)
|
||||||
|
if enabled then
|
||||||
|
self.close = box(self, closeImage)
|
||||||
|
local height = self.head:getHeight()
|
||||||
|
self.close:align("right"):setSize(height, height)
|
||||||
|
self.head:setWidth(self.w - height)
|
||||||
|
self.title:align()
|
||||||
|
insert(self.child, self.close)
|
||||||
|
else
|
||||||
|
self.close:delete()
|
||||||
|
self.head:setWidth(self.w)
|
||||||
|
self.title:align()
|
||||||
|
self.close = false
|
||||||
|
end
|
||||||
|
return self
|
||||||
|
end,
|
||||||
|
hasClose = function(self)
|
||||||
|
if self.close then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
@ -184,15 +252,24 @@ do
|
|||||||
self.head = box(self, tBackground)
|
self.head = box(self, tBackground)
|
||||||
self.title = text(self, title, tColor)
|
self.title = text(self, title, tColor)
|
||||||
self.window = box(self, wBackground)
|
self.window = box(self, wBackground)
|
||||||
|
self.close = box(self, closeImage)
|
||||||
local height = self.title:getHeight()
|
local height = self.title:getHeight()
|
||||||
self.head:setSize(self.w, height)
|
self.head:setSize(self.w - height, height)
|
||||||
self.window:move(nil, height)
|
self.window:move(nil, height)
|
||||||
|
self.close:align("right"):setSize(height, height)
|
||||||
self:setSize(100, 80)
|
self:setSize(100, 80)
|
||||||
self.child = {
|
self.child = {
|
||||||
self.head,
|
self.head,
|
||||||
self.title,
|
self.title,
|
||||||
self.window
|
self.window,
|
||||||
|
self.close
|
||||||
}
|
}
|
||||||
|
self.titleOverflow = "trunicate"
|
||||||
|
self.close.clicked = function()
|
||||||
|
print("CLOSE WAS CLICKED")
|
||||||
|
self:delete()
|
||||||
|
return true
|
||||||
|
end
|
||||||
self.head.selected = false
|
self.head.selected = false
|
||||||
if mousemoved_event then
|
if mousemoved_event then
|
||||||
self.head.mousemoved = function(self, x, y, dx, dy)
|
self.head.mousemoved = function(self, x, y, dx, dy)
|
||||||
|
@ -156,12 +156,35 @@ pop.mousepressed = function(x, y, button, element)
|
|||||||
end
|
end
|
||||||
return handled
|
return handled
|
||||||
end
|
end
|
||||||
pop.mousereleased = function(x, y, button)
|
pop.mousereleased = function(x, y, button, element)
|
||||||
print("mousereleased", x, y, button)
|
|
||||||
local clickedHandled = false
|
local clickedHandled = false
|
||||||
local mousereleasedHandled = false
|
local mousereleasedHandled = false
|
||||||
|
if element then
|
||||||
|
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
||||||
|
if element.clicked and (not element.excludeDraw) then
|
||||||
|
clickedHandled = element:clicked(x - element.x, y - element.y, button)
|
||||||
|
end
|
||||||
|
if element.mousereleased then
|
||||||
|
mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button)
|
||||||
|
end
|
||||||
|
if clickedHandled then
|
||||||
|
pop.focused = element
|
||||||
|
end
|
||||||
|
if clickedHandled or mousereleasedHandled then
|
||||||
|
return clickedHandled, mousereleasedHandled
|
||||||
|
else
|
||||||
|
for i = 1, #element.child do
|
||||||
|
clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, element.child[i])
|
||||||
|
if clickedHandled or mousereleasedHandled then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print("mousereleased", x, y, button)
|
||||||
do
|
do
|
||||||
local element = pop.events[button]
|
element = pop.events[button]
|
||||||
if element then
|
if element then
|
||||||
if element.clicked and (not element.excludeDraw) then
|
if element.clicked and (not element.excludeDraw) then
|
||||||
do
|
do
|
||||||
@ -181,6 +204,10 @@ pop.mousereleased = function(x, y, button)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if (not clickedHandled) and (not mousereleasedHandled) then
|
||||||
|
clickedHandled, mousereleasedHandled = pop.mousereleased(x, y, button, pop.screen)
|
||||||
|
end
|
||||||
|
end
|
||||||
return clickedHandled, mousereleasedHandled
|
return clickedHandled, mousereleasedHandled
|
||||||
end
|
end
|
||||||
pop.keypressed = function(key)
|
pop.keypressed = function(key)
|
||||||
|
@ -47,6 +47,8 @@ class element
|
|||||||
insert @child, child
|
insert @child, child
|
||||||
child.parent = @
|
child.parent = @
|
||||||
|
|
||||||
|
child\align! --NOTE not 100% sure if this is a good idea
|
||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
-- remove child by index and return it OR remove child by reference
|
-- remove child by index and return it OR remove child by reference
|
||||||
@ -60,7 +62,7 @@ class element
|
|||||||
if v == child
|
if v == child
|
||||||
remove @child, k
|
remove @child, k
|
||||||
return @
|
return @
|
||||||
error "Element \"#{child}\" is not a child of element \"#{@}\". Cannot remove it."
|
return "Element \"#{child}\" is not a child of element \"#{@}\". Cannot remove it."
|
||||||
|
|
||||||
getChildren: =>
|
getChildren: =>
|
||||||
return @child
|
return @child
|
||||||
@ -259,3 +261,4 @@ class element
|
|||||||
v\delete!
|
v\delete!
|
||||||
|
|
||||||
@parent\removeChild @
|
@parent\removeChild @
|
||||||
|
@ = nil
|
||||||
|
BIN
src/pop/elements/img/close.png
Normal file
BIN
src/pop/elements/img/close.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 355 B |
@ -7,6 +7,8 @@ element = require "#{path}/element"
|
|||||||
box = require "#{path}/box"
|
box = require "#{path}/box"
|
||||||
text = require "#{path}/text"
|
text = require "#{path}/text"
|
||||||
|
|
||||||
|
closeImage = {100, 0, 0, 80} --graphics.newImage "#{path}/img/close.png"
|
||||||
|
|
||||||
-- version compatibility
|
-- version compatibility
|
||||||
left = 1 -- what is the left mouse button?
|
left = 1 -- what is the left mouse button?
|
||||||
mousemoved_event = true -- is the mousemoved event available?
|
mousemoved_event = true -- is the mousemoved event available?
|
||||||
@ -35,19 +37,28 @@ class window extends element
|
|||||||
@head = box @, tBackground -- title box at top
|
@head = box @, tBackground -- title box at top
|
||||||
@title = text @, title, tColor -- text at top
|
@title = text @, title, tColor -- text at top
|
||||||
@window = box @, wBackground -- main window area
|
@window = box @, wBackground -- main window area
|
||||||
|
@close = box @, closeImage -- close button
|
||||||
|
|
||||||
-- correct placement / sizes of elements
|
-- correct placement / sizes of elements
|
||||||
height = @title\getHeight!
|
height = @title\getHeight!
|
||||||
@head\setSize @w, height
|
@head\setSize @w - height, height
|
||||||
@window\move nil, height
|
@window\move nil, height
|
||||||
|
@close\align("right")\setSize height, height
|
||||||
@setSize 100, 80
|
@setSize 100, 80
|
||||||
|
|
||||||
-- our child elements are still child elements
|
-- our child elements are still child elements
|
||||||
--TODO change title to be a child of head ?
|
--TODO change title to be a child of head ?
|
||||||
@child = {
|
@child = {
|
||||||
@head, @title, @window
|
@head, @title, @window, @close
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@titleOverflow = "trunicate" -- defaults to trunicating title to fit in window
|
||||||
|
|
||||||
|
@close.clicked = ->
|
||||||
|
print "CLOSE WAS CLICKED"
|
||||||
|
@delete!
|
||||||
|
return true
|
||||||
|
|
||||||
@head.selected = false -- whether or not the window title (and thus, the window) has been selected
|
@head.selected = false -- whether or not the window title (and thus, the window) has been selected
|
||||||
|
|
||||||
if mousemoved_event
|
if mousemoved_event
|
||||||
@ -108,6 +119,12 @@ class window extends element
|
|||||||
result = @window\removeChild child
|
result = @window\removeChild child
|
||||||
if result == @window
|
if result == @window
|
||||||
return @
|
return @
|
||||||
|
elseif type(result) == "string"
|
||||||
|
for k,v in ipairs @child
|
||||||
|
if v == child
|
||||||
|
remove @child, k
|
||||||
|
return @
|
||||||
|
return "Element \"#{child}\" is not a child of window \"#{@}\". Cannot remove it."
|
||||||
else
|
else
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -135,13 +152,20 @@ class window extends element
|
|||||||
when "right"
|
when "right"
|
||||||
x -= w - @w
|
x -= w - @w
|
||||||
|
|
||||||
|
if @close
|
||||||
|
@head\setWidth w - @head\getHeight!
|
||||||
|
else
|
||||||
@head\setWidth w
|
@head\setWidth w
|
||||||
|
|
||||||
@window\setWidth w
|
@window\setWidth w
|
||||||
@w = w
|
@w = w
|
||||||
@x += x
|
@x += x
|
||||||
|
|
||||||
@title\align!
|
@title\align!
|
||||||
|
|
||||||
|
if @close
|
||||||
|
@close\align!
|
||||||
|
|
||||||
if h
|
if h
|
||||||
h = h - @head\getHeight!
|
h = h - @head\getHeight!
|
||||||
switch @vertical
|
switch @vertical
|
||||||
@ -169,13 +193,20 @@ class window extends element
|
|||||||
when "right"
|
when "right"
|
||||||
x -= w - @w
|
x -= w - @w
|
||||||
|
|
||||||
|
if @close
|
||||||
|
@head\setWidth w - @head\getHeight!
|
||||||
|
else
|
||||||
@head\setWidth w
|
@head\setWidth w
|
||||||
|
|
||||||
@window\setWidth w
|
@window\setWidth w
|
||||||
@w = w
|
@w = w
|
||||||
@x += x
|
@x += x
|
||||||
|
|
||||||
@title\align!
|
@title\align!
|
||||||
|
|
||||||
|
if @close
|
||||||
|
@close\align!
|
||||||
|
|
||||||
@head\move x
|
@head\move x
|
||||||
--@title\move x
|
--@title\move x
|
||||||
@window\move x
|
@window\move x
|
||||||
@ -204,7 +235,47 @@ class window extends element
|
|||||||
|
|
||||||
setTitle: (title) =>
|
setTitle: (title) =>
|
||||||
@title\setText title
|
@title\setText title
|
||||||
|
|
||||||
|
if @titleOverflow == "trunicate"
|
||||||
|
while @title\getWidth! > @head\getWidth!
|
||||||
|
title = title\sub 1, -3
|
||||||
|
@title\setText title .. "…"
|
||||||
|
|
||||||
|
elseif @titleOverflow == "resize"
|
||||||
|
if @title\getWidth! > @head\getWidth!
|
||||||
|
@setWidth @title\getWidth!
|
||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
getTitle: =>
|
getTitle: =>
|
||||||
return @title\getText!
|
return @title\getText!
|
||||||
|
|
||||||
|
setTitleOverflow: (method) =>
|
||||||
|
@titleOverflow = method
|
||||||
|
|
||||||
|
return @
|
||||||
|
|
||||||
|
getTitleOverflow: =>
|
||||||
|
return @titleOverflow
|
||||||
|
|
||||||
|
setClose: (enabled) =>
|
||||||
|
if enabled
|
||||||
|
@close = box @, closeImage
|
||||||
|
height = @head\getHeight!
|
||||||
|
@close\align("right")\setSize height, height
|
||||||
|
@head\setWidth @w - height
|
||||||
|
@title\align! -- new might not be working?
|
||||||
|
insert @child, @close
|
||||||
|
else
|
||||||
|
@close\delete!
|
||||||
|
@head\setWidth @w
|
||||||
|
@title\align! -- new might not be working?
|
||||||
|
@close = false
|
||||||
|
|
||||||
|
return @
|
||||||
|
|
||||||
|
hasClose: =>
|
||||||
|
if @close
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
@ -129,12 +129,30 @@ pop.mousepressed = (x, y, button, element) ->
|
|||||||
|
|
||||||
return handled
|
return handled
|
||||||
|
|
||||||
pop.mousereleased = (x, y, button) ->
|
pop.mousereleased = (x, y, button, element) ->
|
||||||
print "mousereleased", x, y, button
|
|
||||||
|
|
||||||
clickedHandled = false
|
clickedHandled = false
|
||||||
mousereleasedHandled = false
|
mousereleasedHandled = false
|
||||||
|
|
||||||
|
if element
|
||||||
|
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h)
|
||||||
|
if element.clicked and (not element.excludeDraw)
|
||||||
|
clickedHandled = element\clicked x - element.x, y - element.y, button
|
||||||
|
if element.mousereleased
|
||||||
|
mousereleasedHandled = element\mousereleased x - element.x, y - element.y, button
|
||||||
|
|
||||||
|
if clickedHandled
|
||||||
|
pop.focused = element
|
||||||
|
|
||||||
|
if clickedHandled or mousereleasedHandled
|
||||||
|
return clickedHandled, mousereleasedHandled
|
||||||
|
else
|
||||||
|
for i = 1, #element.child
|
||||||
|
clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, element.child[i]
|
||||||
|
if clickedHandled or mousereleasedHandled
|
||||||
|
break
|
||||||
|
|
||||||
|
else
|
||||||
|
print "mousereleased", x, y, button
|
||||||
if element = pop.events[button]
|
if element = pop.events[button]
|
||||||
if element.clicked and (not element.excludeDraw) --and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h)
|
if element.clicked and (not element.excludeDraw) --and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h)
|
||||||
if clickedHandled = element\clicked x - element.x, y - element.y, button
|
if clickedHandled = element\clicked x - element.x, y - element.y, button
|
||||||
@ -144,6 +162,9 @@ pop.mousereleased = (x, y, button) ->
|
|||||||
if mousereleasedHandled = element\mousereleased x - element.x, y - element.y, button
|
if mousereleasedHandled = element\mousereleased x - element.x, y - element.y, button
|
||||||
pop.events[button] = nil
|
pop.events[button] = nil
|
||||||
|
|
||||||
|
if (not clickedHandled) and (not mousereleasedHandled)
|
||||||
|
clickedHandled, mousereleasedHandled = pop.mousereleased x, y, button, pop.screen
|
||||||
|
|
||||||
return clickedHandled, mousereleasedHandled
|
return clickedHandled, mousereleasedHandled
|
||||||
|
|
||||||
pop.keypressed = (key) ->
|
pop.keypressed = (key) ->
|
||||||
|
Loading…
Reference in New Issue
Block a user