mirror of
https://github.com/TangentFoxy/Pop.Box.git
synced 2024-12-15 12:44:20 +00:00
re-organized, mousemove events, extensions
This commit is contained in:
parent
ba6d7e0f7a
commit
6c1df2ca55
5
build.sh
Executable file
5
build.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
cd src
|
||||||
|
moonc -t ../lib .
|
||||||
|
cd ..
|
||||||
|
cp -rf ./lib/pop/* ./demo/pop/
|
@ -23,9 +23,11 @@ function love.load()
|
|||||||
--]]
|
--]]
|
||||||
|
|
||||||
--c:move(100)
|
--c:move(100)
|
||||||
pop.box({255, 0, 0, 255}):position(50, 600) -- testing streamlined_get_set extension & optional parents
|
pop.box({255, 0, 0, 255}):position(50, 500) -- testing streamlined_get_set extension & optional parents
|
||||||
|
--b:margin(2) -- testing streamlined_get_set extension
|
||||||
|
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)
|
||||||
@ -67,6 +69,10 @@ function love.draw()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function love.mousemoved(x, y, dx, dy)
|
||||||
|
pop.mousemoved(x, y, dx, dy)
|
||||||
|
end
|
||||||
|
|
||||||
function love.mousepressed(x, y, button)
|
function love.mousepressed(x, y, button)
|
||||||
pop.mousepressed(x, y, button)
|
pop.mousepressed(x, y, button)
|
||||||
end
|
end
|
||||||
|
@ -196,6 +196,12 @@ do
|
|||||||
end,
|
end,
|
||||||
getMargin = function(self)
|
getMargin = function(self)
|
||||||
return self.margin
|
return self.margin
|
||||||
|
end,
|
||||||
|
fill = function(self)
|
||||||
|
self.x = self.parent.x + self.margin
|
||||||
|
self.y = self.parent.y + self.margin
|
||||||
|
self.w = self.parent.w - self.margin * 2
|
||||||
|
self.h = self.parent.h - self.margin * 2
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
|
@ -9,6 +9,23 @@ 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 left = 1
|
||||||
|
local move_event = true
|
||||||
|
do
|
||||||
|
local major, minor, revision = love.getVersion()
|
||||||
|
if (major == 0) and (minor == 10) and ((revision == 0) or (revision == 1)) then
|
||||||
|
left = 1
|
||||||
|
end
|
||||||
|
if (major == 0) and (minor == 9) then
|
||||||
|
left = "l"
|
||||||
|
if revision == 1 then
|
||||||
|
move_event = false
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print("elements/window: unrecognized LÖVE version: " .. tostring(major) .. "." .. tostring(minor) .. "." .. tostring(revision))
|
||||||
|
print(" assuming LÖVE version > 0.10.1 (there may be bugs)")
|
||||||
|
end
|
||||||
|
end
|
||||||
local window
|
local window
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
@ -94,7 +111,7 @@ do
|
|||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
setmetatable(_base_0, _parent_0.__base)
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
_class_0 = setmetatable({
|
_class_0 = setmetatable({
|
||||||
__init = function(self, parent, title, tBackground)
|
__init = function(self, parent, title, tBackground, tColor, wBackground)
|
||||||
if title == nil then
|
if title == nil then
|
||||||
title = "window"
|
title = "window"
|
||||||
end
|
end
|
||||||
@ -106,15 +123,26 @@ do
|
|||||||
255
|
255
|
||||||
}
|
}
|
||||||
end
|
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)
|
_class_0.__parent.__init(self, parent)
|
||||||
self.head = box(self, tBackground)
|
self.head = box(self, tBackground)
|
||||||
self.title = text(self, title)
|
self.title = text(self, title, tColor)
|
||||||
self.window = box(self, {
|
self.window = box(self, wBackground)
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
255
|
|
||||||
})
|
|
||||||
local height = self.title:getHeight()
|
local height = self.title:getHeight()
|
||||||
self.head:setSize(self.w, height)
|
self.head:setSize(self.w, height)
|
||||||
self.window:move(nil, height)
|
self.window:move(nil, height)
|
||||||
|
44
demo/pop/extensions/streamlined_get_set.lua
Normal file
44
demo/pop/extensions/streamlined_get_set.lua
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
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("/extensions/streamlined_get_set"))
|
||||||
|
local element = require(tostring(path) .. "/elements/element")
|
||||||
|
element.__base.position = function(self, x, y)
|
||||||
|
if x or y then
|
||||||
|
return self:setPosition(x, y)
|
||||||
|
else
|
||||||
|
return self:getPosition()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
element.__base.size = function(self, w, h)
|
||||||
|
if w or h then
|
||||||
|
return self:setSize(w, h)
|
||||||
|
else
|
||||||
|
return self:getSize()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
element.__base.width = function(self, w)
|
||||||
|
if w then
|
||||||
|
return self:setWidth(w)
|
||||||
|
else
|
||||||
|
return self:getWidth()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
element.__base.height = function(self, h)
|
||||||
|
if h then
|
||||||
|
return self:setHeight(h)
|
||||||
|
else
|
||||||
|
return self:getHeight()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
element.__base.margin = function(self, m)
|
||||||
|
if m then
|
||||||
|
return self:setMargin(m)
|
||||||
|
else
|
||||||
|
return self:getMargin()
|
||||||
|
end
|
||||||
|
end
|
@ -1,3 +1,6 @@
|
|||||||
|
if not (love.getVersion) then
|
||||||
|
error("Pop.Box only supports LÖVE versions >= 0.9.1")
|
||||||
|
end
|
||||||
local filesystem, graphics
|
local filesystem, graphics
|
||||||
do
|
do
|
||||||
local _obj_0 = love
|
local _obj_0 = love
|
||||||
@ -5,10 +8,13 @@ do
|
|||||||
end
|
end
|
||||||
local insert
|
local insert
|
||||||
insert = table.insert
|
insert = table.insert
|
||||||
|
local inheritsFromElement
|
||||||
|
inheritsFromElement = require(tostring(...) .. "/util").inheritsFromElement
|
||||||
local path = ...
|
local path = ...
|
||||||
local pop = { }
|
local pop = { }
|
||||||
pop.elements = { }
|
pop.elements = { }
|
||||||
pop.skins = { }
|
pop.skins = { }
|
||||||
|
pop.events = { }
|
||||||
pop.screen = false
|
pop.screen = false
|
||||||
pop.focused = false
|
pop.focused = false
|
||||||
pop.load = function()
|
pop.load = function()
|
||||||
@ -56,36 +62,38 @@ pop.load = function()
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
local extensions = filesystem.getDirectoryItems(tostring(path) .. "/extensions")
|
||||||
|
for i = 1, #extensions do
|
||||||
|
local _continue_0 = false
|
||||||
|
repeat
|
||||||
|
if not (extensions[i]:sub(-4) == ".lua") then
|
||||||
|
_continue_0 = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
local name = extensions[i]:sub(1, -5)
|
||||||
|
require(tostring(path) .. "/extensions/" .. tostring(name))
|
||||||
|
print("extension loaded: \"" .. tostring(name) .. "\"")
|
||||||
|
_continue_0 = true
|
||||||
|
until true
|
||||||
|
if not _continue_0 then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
|
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
|
||||||
return print("created \"pop.screen\"")
|
return print("created \"pop.screen\"")
|
||||||
end
|
end
|
||||||
local instanceOfElement
|
|
||||||
instanceOfElement = function(object)
|
|
||||||
if object and object.__class then
|
|
||||||
local cls = object.__class
|
|
||||||
if cls.__name == "element" then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
while cls.__parent do
|
|
||||||
cls = cls.__parent
|
|
||||||
if cls.__name == "element" then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
pop.create = function(element, parent, ...)
|
pop.create = function(element, parent, ...)
|
||||||
if parent == nil then
|
if parent == nil then
|
||||||
parent = pop.screen
|
parent = pop.screen
|
||||||
end
|
end
|
||||||
if instanceOfElement(parent) then
|
if inheritsFromElement(parent) then
|
||||||
element = pop.elements[element](parent, ...)
|
element = pop.elements[element](parent, ...)
|
||||||
|
insert(parent.child, element)
|
||||||
|
elseif parent == false then
|
||||||
|
element = pop.elements[element](false, ...)
|
||||||
else
|
else
|
||||||
element = pop.elements[element](pop.screen, parent, ...)
|
element = pop.elements[element](pop.screen, parent, ...)
|
||||||
end
|
insert(pop.screen.child, element)
|
||||||
if parent then
|
|
||||||
insert(parent.child, element)
|
|
||||||
end
|
end
|
||||||
return element
|
return element
|
||||||
end
|
end
|
||||||
@ -115,12 +123,16 @@ pop.draw = function(element)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
pop.mousepressed = function(x, y, button, element)
|
pop.mousemoved = function(self, x, y, dx, dy)
|
||||||
if element == nil then
|
if pop.focused and pop.focused.mousemoved then
|
||||||
element = pop.screen
|
return pop.focused:mousemoved(x, y, dx, dy)
|
||||||
end
|
end
|
||||||
if element == pop.screen then
|
return false
|
||||||
print("mousepressed", x, y, button, element)
|
end
|
||||||
|
pop.mousepressed = function(x, y, button, element)
|
||||||
|
if not (element) then
|
||||||
|
print("mousepressed", x, y, button)
|
||||||
|
element = pop.screen
|
||||||
end
|
end
|
||||||
local handled = false
|
local handled = false
|
||||||
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
||||||
@ -139,13 +151,37 @@ pop.mousepressed = function(x, y, button, element)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if handled then
|
||||||
|
pop.events[button] = element
|
||||||
|
end
|
||||||
return handled
|
return handled
|
||||||
end
|
end
|
||||||
pop.mousereleased = function(x, y, button, element)
|
pop.mousereleased = function(x, y, button)
|
||||||
if element == nil then
|
print("mousereleased", x, y, button)
|
||||||
element = pop.screen
|
local clickedHandled = false
|
||||||
|
local mousereleasedHandled = false
|
||||||
|
do
|
||||||
|
local element = pop.events[button]
|
||||||
|
if element then
|
||||||
|
if element.clicked and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
||||||
|
do
|
||||||
|
clickedHandled = element:clicked(x - element.x, y - element.y, button)
|
||||||
|
if clickedHandled then
|
||||||
|
pop.events[button] = nil
|
||||||
end
|
end
|
||||||
return false
|
end
|
||||||
|
end
|
||||||
|
if element.mousereleased then
|
||||||
|
do
|
||||||
|
mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button)
|
||||||
|
if mousereleasedHandled then
|
||||||
|
pop.events[button] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return clickedHandled, mousereleasedHandled
|
||||||
end
|
end
|
||||||
pop.keypressed = function(key)
|
pop.keypressed = function(key)
|
||||||
print("keypressed", key)
|
print("keypressed", key)
|
||||||
|
19
demo/pop/util.lua
Normal file
19
demo/pop/util.lua
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
local inheritsFromElement
|
||||||
|
inheritsFromElement = function(object)
|
||||||
|
if object and object.__class then
|
||||||
|
local cls = object.__class
|
||||||
|
if cls.__name == "element" then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
while cls.__parent do
|
||||||
|
cls = cls.__parent
|
||||||
|
if cls.__name == "element" then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
inheritsFromElement = inheritsFromElement
|
||||||
|
}
|
@ -196,6 +196,12 @@ do
|
|||||||
end,
|
end,
|
||||||
getMargin = function(self)
|
getMargin = function(self)
|
||||||
return self.margin
|
return self.margin
|
||||||
|
end,
|
||||||
|
fill = function(self)
|
||||||
|
self.x = self.parent.x + self.margin
|
||||||
|
self.y = self.parent.y + self.margin
|
||||||
|
self.w = self.parent.w - self.margin * 2
|
||||||
|
self.h = self.parent.h - self.margin * 2
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
|
@ -9,6 +9,23 @@ 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 left = 1
|
||||||
|
local move_event = true
|
||||||
|
do
|
||||||
|
local major, minor, revision = love.getVersion()
|
||||||
|
if (major == 0) and (minor == 10) and ((revision == 0) or (revision == 1)) then
|
||||||
|
left = 1
|
||||||
|
end
|
||||||
|
if (major == 0) and (minor == 9) then
|
||||||
|
left = "l"
|
||||||
|
if revision == 1 then
|
||||||
|
move_event = false
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print("elements/window: unrecognized LÖVE version: " .. tostring(major) .. "." .. tostring(minor) .. "." .. tostring(revision))
|
||||||
|
print(" assuming LÖVE version > 0.10.1 (there may be bugs)")
|
||||||
|
end
|
||||||
|
end
|
||||||
local window
|
local window
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
@ -94,7 +111,7 @@ do
|
|||||||
_base_0.__index = _base_0
|
_base_0.__index = _base_0
|
||||||
setmetatable(_base_0, _parent_0.__base)
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
_class_0 = setmetatable({
|
_class_0 = setmetatable({
|
||||||
__init = function(self, parent, title, tBackground)
|
__init = function(self, parent, title, tBackground, tColor, wBackground)
|
||||||
if title == nil then
|
if title == nil then
|
||||||
title = "window"
|
title = "window"
|
||||||
end
|
end
|
||||||
@ -106,15 +123,26 @@ do
|
|||||||
255
|
255
|
||||||
}
|
}
|
||||||
end
|
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)
|
_class_0.__parent.__init(self, parent)
|
||||||
self.head = box(self, tBackground)
|
self.head = box(self, tBackground)
|
||||||
self.title = text(self, title)
|
self.title = text(self, title, tColor)
|
||||||
self.window = box(self, {
|
self.window = box(self, wBackground)
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
255
|
|
||||||
})
|
|
||||||
local height = self.title:getHeight()
|
local height = self.title:getHeight()
|
||||||
self.head:setSize(self.w, height)
|
self.head:setSize(self.w, height)
|
||||||
self.window:move(nil, height)
|
self.window:move(nil, height)
|
||||||
|
44
lib/pop/extensions/streamlined_get_set.lua
Normal file
44
lib/pop/extensions/streamlined_get_set.lua
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
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("/extensions/streamlined_get_set"))
|
||||||
|
local element = require(tostring(path) .. "/elements/element")
|
||||||
|
element.__base.position = function(self, x, y)
|
||||||
|
if x or y then
|
||||||
|
return self:setPosition(x, y)
|
||||||
|
else
|
||||||
|
return self:getPosition()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
element.__base.size = function(self, w, h)
|
||||||
|
if w or h then
|
||||||
|
return self:setSize(w, h)
|
||||||
|
else
|
||||||
|
return self:getSize()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
element.__base.width = function(self, w)
|
||||||
|
if w then
|
||||||
|
return self:setWidth(w)
|
||||||
|
else
|
||||||
|
return self:getWidth()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
element.__base.height = function(self, h)
|
||||||
|
if h then
|
||||||
|
return self:setHeight(h)
|
||||||
|
else
|
||||||
|
return self:getHeight()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
element.__base.margin = function(self, m)
|
||||||
|
if m then
|
||||||
|
return self:setMargin(m)
|
||||||
|
else
|
||||||
|
return self:getMargin()
|
||||||
|
end
|
||||||
|
end
|
@ -1,3 +1,6 @@
|
|||||||
|
if not (love.getVersion) then
|
||||||
|
error("Pop.Box only supports LÖVE versions >= 0.9.1")
|
||||||
|
end
|
||||||
local filesystem, graphics
|
local filesystem, graphics
|
||||||
do
|
do
|
||||||
local _obj_0 = love
|
local _obj_0 = love
|
||||||
@ -5,10 +8,13 @@ do
|
|||||||
end
|
end
|
||||||
local insert
|
local insert
|
||||||
insert = table.insert
|
insert = table.insert
|
||||||
|
local inheritsFromElement
|
||||||
|
inheritsFromElement = require(tostring(...) .. "/util").inheritsFromElement
|
||||||
local path = ...
|
local path = ...
|
||||||
local pop = { }
|
local pop = { }
|
||||||
pop.elements = { }
|
pop.elements = { }
|
||||||
pop.skins = { }
|
pop.skins = { }
|
||||||
|
pop.events = { }
|
||||||
pop.screen = false
|
pop.screen = false
|
||||||
pop.focused = false
|
pop.focused = false
|
||||||
pop.load = function()
|
pop.load = function()
|
||||||
@ -56,36 +62,38 @@ pop.load = function()
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
local extensions = filesystem.getDirectoryItems(tostring(path) .. "/extensions")
|
||||||
|
for i = 1, #extensions do
|
||||||
|
local _continue_0 = false
|
||||||
|
repeat
|
||||||
|
if not (extensions[i]:sub(-4) == ".lua") then
|
||||||
|
_continue_0 = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
local name = extensions[i]:sub(1, -5)
|
||||||
|
require(tostring(path) .. "/extensions/" .. tostring(name))
|
||||||
|
print("extension loaded: \"" .. tostring(name) .. "\"")
|
||||||
|
_continue_0 = true
|
||||||
|
until true
|
||||||
|
if not _continue_0 then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
|
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
|
||||||
return print("created \"pop.screen\"")
|
return print("created \"pop.screen\"")
|
||||||
end
|
end
|
||||||
local instanceOfElement
|
|
||||||
instanceOfElement = function(object)
|
|
||||||
if object and object.__class then
|
|
||||||
local cls = object.__class
|
|
||||||
if cls.__name == "element" then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
while cls.__parent do
|
|
||||||
cls = cls.__parent
|
|
||||||
if cls.__name == "element" then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
pop.create = function(element, parent, ...)
|
pop.create = function(element, parent, ...)
|
||||||
if parent == nil then
|
if parent == nil then
|
||||||
parent = pop.screen
|
parent = pop.screen
|
||||||
end
|
end
|
||||||
if instanceOfElement(parent) then
|
if inheritsFromElement(parent) then
|
||||||
element = pop.elements[element](parent, ...)
|
element = pop.elements[element](parent, ...)
|
||||||
|
insert(parent.child, element)
|
||||||
|
elseif parent == false then
|
||||||
|
element = pop.elements[element](false, ...)
|
||||||
else
|
else
|
||||||
element = pop.elements[element](pop.screen, parent, ...)
|
element = pop.elements[element](pop.screen, parent, ...)
|
||||||
end
|
insert(pop.screen.child, element)
|
||||||
if parent then
|
|
||||||
insert(parent.child, element)
|
|
||||||
end
|
end
|
||||||
return element
|
return element
|
||||||
end
|
end
|
||||||
@ -115,12 +123,16 @@ pop.draw = function(element)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
pop.mousepressed = function(x, y, button, element)
|
pop.mousemoved = function(self, x, y, dx, dy)
|
||||||
if element == nil then
|
if pop.focused and pop.focused.mousemoved then
|
||||||
element = pop.screen
|
return pop.focused:mousemoved(x, y, dx, dy)
|
||||||
end
|
end
|
||||||
if element == pop.screen then
|
return false
|
||||||
print("mousepressed", x, y, button, element)
|
end
|
||||||
|
pop.mousepressed = function(x, y, button, element)
|
||||||
|
if not (element) then
|
||||||
|
print("mousepressed", x, y, button)
|
||||||
|
element = pop.screen
|
||||||
end
|
end
|
||||||
local handled = false
|
local handled = false
|
||||||
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
||||||
@ -139,13 +151,37 @@ pop.mousepressed = function(x, y, button, element)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if handled then
|
||||||
|
pop.events[button] = element
|
||||||
|
end
|
||||||
return handled
|
return handled
|
||||||
end
|
end
|
||||||
pop.mousereleased = function(x, y, button, element)
|
pop.mousereleased = function(x, y, button)
|
||||||
if element == nil then
|
print("mousereleased", x, y, button)
|
||||||
element = pop.screen
|
local clickedHandled = false
|
||||||
|
local mousereleasedHandled = false
|
||||||
|
do
|
||||||
|
local element = pop.events[button]
|
||||||
|
if element then
|
||||||
|
if element.clicked and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h) then
|
||||||
|
do
|
||||||
|
clickedHandled = element:clicked(x - element.x, y - element.y, button)
|
||||||
|
if clickedHandled then
|
||||||
|
pop.events[button] = nil
|
||||||
end
|
end
|
||||||
return false
|
end
|
||||||
|
end
|
||||||
|
if element.mousereleased then
|
||||||
|
do
|
||||||
|
mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button)
|
||||||
|
if mousereleasedHandled then
|
||||||
|
pop.events[button] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return clickedHandled, mousereleasedHandled
|
||||||
end
|
end
|
||||||
pop.keypressed = function(key)
|
pop.keypressed = function(key)
|
||||||
print("keypressed", key)
|
print("keypressed", key)
|
||||||
|
19
lib/pop/util.lua
Normal file
19
lib/pop/util.lua
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
local inheritsFromElement
|
||||||
|
inheritsFromElement = function(object)
|
||||||
|
if object and object.__class then
|
||||||
|
local cls = object.__class
|
||||||
|
if cls.__name == "element" then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
while cls.__parent do
|
||||||
|
cls = cls.__parent
|
||||||
|
if cls.__name == "element" then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
inheritsFromElement = inheritsFromElement
|
||||||
|
}
|
@ -223,3 +223,9 @@ class element
|
|||||||
|
|
||||||
getMargin: =>
|
getMargin: =>
|
||||||
return @margin
|
return @margin
|
||||||
|
|
||||||
|
fill: =>
|
||||||
|
@x = @parent.x + @margin
|
||||||
|
@y = @parent.y + @margin
|
||||||
|
@w = @parent.w - @margin*2
|
||||||
|
@h = @parent.h - @margin*2
|
||||||
|
@ -6,13 +6,29 @@ element = require "#{path}/element"
|
|||||||
box = require "#{path}/box"
|
box = require "#{path}/box"
|
||||||
text = require "#{path}/text"
|
text = require "#{path}/text"
|
||||||
|
|
||||||
|
-- version compatibility
|
||||||
|
left = 1 -- what is the left mouse button?
|
||||||
|
move_event = true -- is the mousemoved event available?
|
||||||
|
|
||||||
|
do
|
||||||
|
major, minor, revision = love.getVersion!
|
||||||
|
if (major == 0) and (minor == 10) and ((revision == 0) or (revision == 1))
|
||||||
|
left = 1 -- redundant, but whatever
|
||||||
|
if (major == 0) and (minor == 9)
|
||||||
|
left = "l"
|
||||||
|
if revision == 1
|
||||||
|
move_event = false
|
||||||
|
else
|
||||||
|
print "elements/window: unrecognized LÖVE version: #{major}.#{minor}.#{revision}"
|
||||||
|
print " assuming LÖVE version > 0.10.1 (there may be bugs)"
|
||||||
|
|
||||||
class window extends element
|
class window extends element
|
||||||
new: (parent, title="window", tBackground={25, 180, 230, 255}) =>
|
new: (parent, title="window", tBackground={25, 180, 230, 255}, tColor={255, 255, 255, 255}, wBackground={200, 200, 210, 255}) =>
|
||||||
super parent
|
super parent
|
||||||
|
|
||||||
@head = box @, tBackground -- title box at top
|
@head = box @, tBackground -- title box at top
|
||||||
@title = text @, title -- text at top
|
@title = text @, title, tColor -- text at top
|
||||||
@window = box @, {0,0,0,255} -- main window area
|
@window = box @, wBackground -- main window area
|
||||||
|
|
||||||
-- correct placement / sizes of elements
|
-- correct placement / sizes of elements
|
||||||
height = @title\getHeight!
|
height = @title\getHeight!
|
||||||
@ -25,6 +41,9 @@ class window extends element
|
|||||||
@head, @title, @window
|
@head, @title, @window
|
||||||
}
|
}
|
||||||
|
|
||||||
|
--@selected = false -- whether or not the window title (and thus, the window) has been selected
|
||||||
|
--NOTE all of these commented out, because I realized these event handlers should be attached to the title element
|
||||||
|
|
||||||
debugDraw: =>
|
debugDraw: =>
|
||||||
graphics.setLineWidth 0.5
|
graphics.setLineWidth 0.5
|
||||||
graphics.setColor 0, 0, 0, 100
|
graphics.setColor 0, 0, 0, 100
|
||||||
@ -36,6 +55,18 @@ class window extends element
|
|||||||
|
|
||||||
return @
|
return @
|
||||||
|
|
||||||
|
--update: =>
|
||||||
|
-- if selected, set position based on current mouse position relative to position it was when mousepressed
|
||||||
|
|
||||||
|
--mousemoved: (x, y, dx, dy) =>
|
||||||
|
-- if selected, set position based on new mouse position relative to position it was when mousepressed
|
||||||
|
|
||||||
|
--mousepressed: (x, y, button) =>
|
||||||
|
-- if button == "l" -> selected = true, mouse position saved
|
||||||
|
|
||||||
|
--mousereleased: (x, y, button) =>
|
||||||
|
-- if button == "l" -> set position based on position relative to when mousepressed, selected == false
|
||||||
|
|
||||||
setSize: (w, h) =>
|
setSize: (w, h) =>
|
||||||
x = 0
|
x = 0
|
||||||
y = 0
|
y = 0
|
||||||
|
@ -12,3 +12,38 @@ element.__base.position = (x, y) =>
|
|||||||
return @setPosition x, y
|
return @setPosition x, y
|
||||||
else
|
else
|
||||||
return @getPosition!
|
return @getPosition!
|
||||||
|
|
||||||
|
element.__base.size = (w, h) =>
|
||||||
|
if w or h
|
||||||
|
return @setSize w, h
|
||||||
|
else
|
||||||
|
return @getSize!
|
||||||
|
|
||||||
|
element.__base.width = (w) =>
|
||||||
|
if w
|
||||||
|
return @setWidth w
|
||||||
|
else
|
||||||
|
return @getWidth!
|
||||||
|
|
||||||
|
element.__base.height = (h) =>
|
||||||
|
if h
|
||||||
|
return @setHeight h
|
||||||
|
else
|
||||||
|
return @getHeight!
|
||||||
|
|
||||||
|
element.__base.margin = (m) =>
|
||||||
|
if m
|
||||||
|
return @setMargin m
|
||||||
|
else
|
||||||
|
return @getMargin!
|
||||||
|
|
||||||
|
--oldinit = element.__init
|
||||||
|
--
|
||||||
|
--element.__init = (...) ->
|
||||||
|
-- object = oldinit ...
|
||||||
|
-- value = object.margin
|
||||||
|
--
|
||||||
|
-- object.margin = setmetatable {:value}, {
|
||||||
|
-- __call: (...) ->
|
||||||
|
-- print ...
|
||||||
|
-- }
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
|
unless love.getVersion
|
||||||
|
error "Pop.Box only supports LÖVE versions >= 0.9.1"
|
||||||
|
|
||||||
import filesystem, graphics from love
|
import filesystem, graphics from love
|
||||||
import insert from table
|
import insert from table
|
||||||
|
import inheritsFromElement from require "#{...}/util"
|
||||||
|
|
||||||
path = ...
|
path = ...
|
||||||
|
|
||||||
@ -7,6 +11,7 @@ pop = {}
|
|||||||
|
|
||||||
pop.elements = {}
|
pop.elements = {}
|
||||||
pop.skins = {}
|
pop.skins = {}
|
||||||
|
pop.events = {}
|
||||||
|
|
||||||
pop.screen = false -- initialized in pop.load()
|
pop.screen = false -- initialized in pop.load()
|
||||||
pop.focused = false
|
pop.focused = false
|
||||||
@ -57,29 +62,19 @@ pop.load = ->
|
|||||||
name = extensions[i]\sub 1, -5
|
name = extensions[i]\sub 1, -5
|
||||||
require "#{path}/extensions/#{name}"
|
require "#{path}/extensions/#{name}"
|
||||||
|
|
||||||
|
print "extension 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)
|
||||||
pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!)
|
pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!)
|
||||||
print "created \"pop.screen\""
|
print "created \"pop.screen\""
|
||||||
|
|
||||||
instanceOfElement = (object) ->
|
|
||||||
if object and object.__class
|
|
||||||
cls = object.__class
|
|
||||||
|
|
||||||
if cls.__name == "element"
|
|
||||||
return true
|
|
||||||
|
|
||||||
while cls.__parent
|
|
||||||
cls = cls.__parent
|
|
||||||
if cls.__name == "element"
|
|
||||||
return true
|
|
||||||
|
|
||||||
return false
|
|
||||||
|
|
||||||
-- creates an element with specified parent (parent can be false or non-existent)
|
-- creates an element with specified parent (parent can be false or non-existent)
|
||||||
pop.create = (element, parent=pop.screen, ...) ->
|
pop.create = (element, parent=pop.screen, ...) ->
|
||||||
if instanceOfElement parent
|
if inheritsFromElement parent
|
||||||
element = pop.elements[element](parent, ...)
|
element = pop.elements[element](parent, ...)
|
||||||
insert parent.child, element
|
insert parent.child, element
|
||||||
|
elseif parent == false
|
||||||
|
element = pop.elements[element](false, ...)
|
||||||
else
|
else
|
||||||
element = pop.elements[element](pop.screen, parent, ...)
|
element = pop.elements[element](pop.screen, parent, ...)
|
||||||
insert pop.screen.child, element
|
insert pop.screen.child, element
|
||||||
@ -102,11 +97,19 @@ pop.draw = (element=pop.screen) ->
|
|||||||
for i = 1, #element.child
|
for i = 1, #element.child
|
||||||
pop.draw element.child[i]
|
pop.draw element.child[i]
|
||||||
|
|
||||||
pop.mousepressed = (x, y, button, element=pop.screen) ->
|
pop.mousemoved = (x, y, dx, dy) =>
|
||||||
if element == pop.screen
|
if pop.focused and pop.focused.mousemoved
|
||||||
print "mousepressed", x, y, button, element
|
return pop.focused\mousemoved x, y, dx, dy
|
||||||
|
|
||||||
|
return false
|
||||||
|
|
||||||
|
pop.mousepressed = (x, y, button, element) ->
|
||||||
|
unless element
|
||||||
|
print "mousepressed", x, y, button
|
||||||
|
element = pop.screen
|
||||||
|
|
||||||
handled = false
|
handled = false
|
||||||
|
|
||||||
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h)
|
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h)
|
||||||
if element.mousepressed
|
if element.mousepressed
|
||||||
handled = element\mousepressed x - element.x, y - element.y, button
|
handled = element\mousepressed x - element.x, y - element.y, button
|
||||||
@ -118,31 +121,27 @@ pop.mousepressed = (x, y, button, element=pop.screen) ->
|
|||||||
if handled
|
if handled
|
||||||
pop.focused = element.child[i]
|
pop.focused = element.child[i]
|
||||||
break
|
break
|
||||||
|
if handled
|
||||||
|
pop.events[button] = element
|
||||||
|
|
||||||
return handled
|
return handled
|
||||||
|
|
||||||
pop.mousereleased = (x, y, button, element=pop.screen) ->
|
pop.mousereleased = (x, y, button) ->
|
||||||
--[[
|
print "mousereleased", x, y, button
|
||||||
--if element == pop.screen
|
|
||||||
-- print "mousereleased", x, y, button, element
|
|
||||||
|
|
||||||
--clickHandled = false
|
clickedHandled = false
|
||||||
--mouseReleaseHandled = false
|
mousereleasedHandled = false
|
||||||
|
|
||||||
--if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h)
|
if element = pop.events[button]
|
||||||
-- if element.mousereleased
|
if element.clicked and (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h)
|
||||||
-- mouseReleaseHandled = element\mousereleased x - element.x, y - element.y, button
|
if clickedHandled = element\clicked x - element.x, y - element.y, button
|
||||||
-- unless mouseReleaseHandled
|
pop.events[button] = nil
|
||||||
-- for i = 1, #element.child
|
|
||||||
-- clickHandled, mouseReleaseHandled = pop.mousereleased x, y, button, element.child[i]
|
|
||||||
-- if mouseReleaseHandled
|
|
||||||
-- break
|
|
||||||
|
|
||||||
--if element == pop.focused
|
if element.mousereleased
|
||||||
|
if mousereleasedHandled = element\mousereleased x - element.x, y - element.y, button
|
||||||
|
pop.events[button] = nil
|
||||||
|
|
||||||
--return mouseReleaseHandled
|
return clickedHandled, mousereleasedHandled
|
||||||
--]]
|
|
||||||
|
|
||||||
return false -- ugh this sucks
|
|
||||||
|
|
||||||
pop.keypressed = (key) ->
|
pop.keypressed = (key) ->
|
||||||
print "keypressed", key
|
print "keypressed", key
|
||||||
|
17
src/pop/util.moon
Normal file
17
src/pop/util.moon
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
inheritsFromElement = (object) ->
|
||||||
|
if object and object.__class
|
||||||
|
cls = object.__class
|
||||||
|
|
||||||
|
if cls.__name == "element"
|
||||||
|
return true
|
||||||
|
|
||||||
|
while cls.__parent
|
||||||
|
cls = cls.__parent
|
||||||
|
if cls.__name == "element"
|
||||||
|
return true
|
||||||
|
|
||||||
|
return false
|
||||||
|
|
||||||
|
return {
|
||||||
|
:inheritsFromElement
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user