re-organized, mousemove events, extensions

This commit is contained in:
Paul Liverman III 2016-04-01 20:45:43 -07:00
parent ba6d7e0f7a
commit 6c1df2ca55
17 changed files with 479 additions and 114 deletions

5
build.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
cd src
moonc -t ../lib .
cd ..
cp -rf ./lib/pop/* ./demo/pop/

View File

@ -23,9 +23,11 @@ function love.load()
--]]
--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:move(100, 100)
w2:setWidth(500)
@ -67,6 +69,10 @@ function love.draw()
end
end
function love.mousemoved(x, y, dx, dy)
pop.mousemoved(x, y, dx, dy)
end
function love.mousepressed(x, y, button)
pop.mousepressed(x, y, button)
end

View File

@ -196,6 +196,12 @@ do
end,
getMargin = function(self)
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
}
_base_0.__index = _base_0

View File

@ -9,6 +9,23 @@ 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 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
do
local _class_0
@ -94,7 +111,7 @@ do
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, parent, title, tBackground)
__init = function(self, parent, title, tBackground, tColor, wBackground)
if title == nil then
title = "window"
end
@ -106,15 +123,26 @@ do
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)
self.head = box(self, tBackground)
self.title = text(self, title)
self.window = box(self, {
0,
0,
0,
255
})
self.title = text(self, title, tColor)
self.window = box(self, wBackground)
local height = self.title:getHeight()
self.head:setSize(self.w, height)
self.window:move(nil, height)

View 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

View File

@ -1,3 +1,6 @@
if not (love.getVersion) then
error("Pop.Box only supports LÖVE versions >= 0.9.1")
end
local filesystem, graphics
do
local _obj_0 = love
@ -5,10 +8,13 @@ do
end
local insert
insert = table.insert
local inheritsFromElement
inheritsFromElement = require(tostring(...) .. "/util").inheritsFromElement
local path = ...
local pop = { }
pop.elements = { }
pop.skins = { }
pop.events = { }
pop.screen = false
pop.focused = false
pop.load = function()
@ -56,36 +62,38 @@ pop.load = function()
break
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())
return print("created \"pop.screen\"")
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, ...)
if parent == nil then
parent = pop.screen
end
if instanceOfElement(parent) then
if inheritsFromElement(parent) then
element = pop.elements[element](parent, ...)
insert(parent.child, element)
elseif parent == false then
element = pop.elements[element](false, ...)
else
element = pop.elements[element](pop.screen, parent, ...)
end
if parent then
insert(parent.child, element)
insert(pop.screen.child, element)
end
return element
end
@ -115,12 +123,16 @@ pop.draw = function(element)
end
end
end
pop.mousepressed = function(x, y, button, element)
if element == nil then
element = pop.screen
pop.mousemoved = function(self, x, y, dx, dy)
if pop.focused and pop.focused.mousemoved then
return pop.focused:mousemoved(x, y, dx, dy)
end
if element == pop.screen then
print("mousepressed", x, y, button, element)
return false
end
pop.mousepressed = function(x, y, button, element)
if not (element) then
print("mousepressed", x, y, button)
element = pop.screen
end
local handled = false
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
if handled then
pop.events[button] = element
end
return handled
end
pop.mousereleased = function(x, y, button, element)
if element == nil then
element = pop.screen
pop.mousereleased = function(x, y, button)
print("mousereleased", x, y, button)
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
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
pop.keypressed = function(key)
print("keypressed", key)

19
demo/pop/util.lua Normal file
View 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
}

View File

@ -196,6 +196,12 @@ do
end,
getMargin = function(self)
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
}
_base_0.__index = _base_0

View File

@ -9,6 +9,23 @@ 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 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
do
local _class_0
@ -94,7 +111,7 @@ do
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, parent, title, tBackground)
__init = function(self, parent, title, tBackground, tColor, wBackground)
if title == nil then
title = "window"
end
@ -106,15 +123,26 @@ do
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)
self.head = box(self, tBackground)
self.title = text(self, title)
self.window = box(self, {
0,
0,
0,
255
})
self.title = text(self, title, tColor)
self.window = box(self, wBackground)
local height = self.title:getHeight()
self.head:setSize(self.w, height)
self.window:move(nil, height)

View 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

View File

@ -1,3 +1,6 @@
if not (love.getVersion) then
error("Pop.Box only supports LÖVE versions >= 0.9.1")
end
local filesystem, graphics
do
local _obj_0 = love
@ -5,10 +8,13 @@ do
end
local insert
insert = table.insert
local inheritsFromElement
inheritsFromElement = require(tostring(...) .. "/util").inheritsFromElement
local path = ...
local pop = { }
pop.elements = { }
pop.skins = { }
pop.events = { }
pop.screen = false
pop.focused = false
pop.load = function()
@ -56,36 +62,38 @@ pop.load = function()
break
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())
return print("created \"pop.screen\"")
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, ...)
if parent == nil then
parent = pop.screen
end
if instanceOfElement(parent) then
if inheritsFromElement(parent) then
element = pop.elements[element](parent, ...)
insert(parent.child, element)
elseif parent == false then
element = pop.elements[element](false, ...)
else
element = pop.elements[element](pop.screen, parent, ...)
end
if parent then
insert(parent.child, element)
insert(pop.screen.child, element)
end
return element
end
@ -115,12 +123,16 @@ pop.draw = function(element)
end
end
end
pop.mousepressed = function(x, y, button, element)
if element == nil then
element = pop.screen
pop.mousemoved = function(self, x, y, dx, dy)
if pop.focused and pop.focused.mousemoved then
return pop.focused:mousemoved(x, y, dx, dy)
end
if element == pop.screen then
print("mousepressed", x, y, button, element)
return false
end
pop.mousepressed = function(x, y, button, element)
if not (element) then
print("mousepressed", x, y, button)
element = pop.screen
end
local handled = false
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
if handled then
pop.events[button] = element
end
return handled
end
pop.mousereleased = function(x, y, button, element)
if element == nil then
element = pop.screen
pop.mousereleased = function(x, y, button)
print("mousereleased", x, y, button)
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
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
pop.keypressed = function(key)
print("keypressed", key)

19
lib/pop/util.lua Normal file
View 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
}

View File

@ -223,3 +223,9 @@ class element
getMargin: =>
return @margin
fill: =>
@x = @parent.x + @margin
@y = @parent.y + @margin
@w = @parent.w - @margin*2
@h = @parent.h - @margin*2

View File

@ -6,13 +6,29 @@ element = require "#{path}/element"
box = require "#{path}/box"
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
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
@head = box @, tBackground -- title box at top
@title = text @, title -- text at top
@window = box @, {0,0,0,255} -- main window area
@title = text @, title, tColor -- text at top
@window = box @, wBackground -- main window area
-- correct placement / sizes of elements
height = @title\getHeight!
@ -25,6 +41,9 @@ class window extends element
@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: =>
graphics.setLineWidth 0.5
graphics.setColor 0, 0, 0, 100
@ -36,6 +55,18 @@ class window extends element
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) =>
x = 0
y = 0

View File

@ -12,3 +12,38 @@ element.__base.position = (x, y) =>
return @setPosition x, y
else
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 ...
-- }

View File

@ -1,5 +1,9 @@
unless love.getVersion
error "Pop.Box only supports LÖVE versions >= 0.9.1"
import filesystem, graphics from love
import insert from table
import inheritsFromElement from require "#{...}/util"
path = ...
@ -7,6 +11,7 @@ pop = {}
pop.elements = {}
pop.skins = {}
pop.events = {}
pop.screen = false -- initialized in pop.load()
pop.focused = false
@ -57,29 +62,19 @@ pop.load = ->
name = extensions[i]\sub 1, -5
require "#{path}/extensions/#{name}"
print "extension loaded: \"#{name}\""
-- main window (called screen because there will be a window element class)
pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!)
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)
pop.create = (element, parent=pop.screen, ...) ->
if instanceOfElement parent
if inheritsFromElement parent
element = pop.elements[element](parent, ...)
insert parent.child, element
elseif parent == false
element = pop.elements[element](false, ...)
else
element = pop.elements[element](pop.screen, parent, ...)
insert pop.screen.child, element
@ -102,11 +97,19 @@ pop.draw = (element=pop.screen) ->
for i = 1, #element.child
pop.draw element.child[i]
pop.mousepressed = (x, y, button, element=pop.screen) ->
if element == pop.screen
print "mousepressed", x, y, button, element
pop.mousemoved = (x, y, dx, dy) =>
if pop.focused and pop.focused.mousemoved
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
if (x >= element.x) and (x <= element.x + element.w) and (y >= element.y) and (y <= element.y + element.h)
if element.mousepressed
handled = element\mousepressed x - element.x, y - element.y, button
@ -118,31 +121,27 @@ pop.mousepressed = (x, y, button, element=pop.screen) ->
if handled
pop.focused = element.child[i]
break
if handled
pop.events[button] = element
return handled
pop.mousereleased = (x, y, button, element=pop.screen) ->
--[[
--if element == pop.screen
-- print "mousereleased", x, y, button, element
pop.mousereleased = (x, y, button) ->
print "mousereleased", x, y, button
--clickHandled = false
--mouseReleaseHandled = false
clickedHandled = 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.mousereleased
-- mouseReleaseHandled = element\mousereleased x - element.x, y - element.y, button
-- unless mouseReleaseHandled
-- for i = 1, #element.child
-- clickHandled, mouseReleaseHandled = pop.mousereleased x, y, button, element.child[i]
-- if mouseReleaseHandled
-- break
if element = pop.events[button]
if element.clicked 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
pop.events[button] = nil
--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 false -- ugh this sucks
return clickedHandled, mousereleasedHandled
pop.keypressed = (key) ->
print "keypressed", key

17
src/pop/util.moon Normal file
View 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
}