wip events n stuff

This commit is contained in:
Paul Liverman III 2016-04-17 00:29:27 -07:00
parent 8e3622c0c4
commit 51bb8f581a
8 changed files with 110 additions and 70 deletions

View File

@ -1,5 +1,8 @@
local graphics local graphics, mouse
graphics = love.graphics do
local _obj_0 = love
graphics, mouse = _obj_0.graphics, _obj_0.mouse
end
local insert, remove local insert, remove
do do
local _obj_0 = table local _obj_0 = table
@ -141,6 +144,9 @@ do
setTitle = function(self, title) setTitle = function(self, title)
self.title:setText(title) self.title:setText(title)
return self return self
end,
getTitle = function(self)
return self.title:getText()
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
@ -203,35 +209,31 @@ do
end end
return false return false
end end
self.head.mousereleased = function(self, x, y, button)
if button == left then
self.selected = false
pop_ref.focused = false
return true
end
return false
end
else else
self.head.mx = 0 self.head.mx = 0
self.head.my = 0 self.head.my = 0
self.head.update = function(self) self.head.update = function(self)
return false local x, y = mouse.getPosition()
return self:setPosition(x - mx, y - my)
end end
self.head.mousepressed = function(self, x, y, button) self.head.mousepressed = function(self, x, y, button)
if button == left then if button == left then
self.selected = true self.selected = true
self.mx = x self.mx = x
self.my = y self.my = y
end
end
self.head.mousereleased = function(self, x, y, button)
if button == left then
self.selected = false
return true return true
end end
return false return false
end end
end end
self.head.mousereleased = function(self, x, y, button)
if button == left then
self.selected = false
pop_ref.focused = false
return true
end
return false
end
end, end,
__base = _base_0, __base = _base_0,
__name = "window", __name = "window",

View File

@ -163,7 +163,7 @@ pop.mousereleased = function(x, y, button)
do do
local element = pop.events[button] local element = pop.events[button]
if element then if element then
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) then if element.clicked and (not element.excludeDraw) then
do do
clickedHandled = element:clicked(x - element.x, y - element.y, button) clickedHandled = element:clicked(x - element.x, y - element.y, button)
if clickedHandled then if clickedHandled then
@ -171,7 +171,7 @@ pop.mousereleased = function(x, y, button)
end end
end end
end end
if element.mousereleased and (not element.excludeDraw) then if element.mousereleased then
do do
mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button) mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button)
if mousereleasedHandled then if mousereleasedHandled then
@ -185,14 +185,26 @@ pop.mousereleased = function(x, y, button)
end end
pop.keypressed = function(key) pop.keypressed = function(key)
print("keypressed", key) print("keypressed", key)
local element = pop.focused
if element and element.keypressed and (not element.excludeDraw) then
return element.keypressed(key)
end
return false return false
end end
pop.keyreleased = function(key) pop.keyreleased = function(key)
print("keyreleased", key) print("keyreleased", key)
local element = pop.focused
if element and element.keyreleased then
return element.keyreleased(key)
end
return false return false
end end
pop.textinput = function(text) pop.textinput = function(text)
print("textinput", text) print("textinput", text)
local element = pop.focused
if element and element.textinput and (not element.excludeDraw) then
return element.textinput(text)
end
return false return false
end end
pop.skin = function(element, skin, depth) pop.skin = function(element, skin, depth)

View File

@ -10,5 +10,14 @@ Note that any element using one of these excludes its children as well.
- `excludeUpdate` Excludes an element from being updated (by `pop.update()`). - `excludeUpdate` Excludes an element from being updated (by `pop.update()`).
- `excludeDraw` Excludes being rendered (by `pop.draw()`). - `excludeDraw` Excludes being rendered (by `pop.draw()`).
**Note**: `excludeDraw` also excludes an element from accepting events (it **Note**: `excludeDraw` also excludes an element from accepting the following
wouldn't make sense to have an invisible element capturing text input). events:
- `mousepressed`
- `clicked`
- `keypressed`
- `textinput`
The reason for this is that it wouldn't make sense for an invisible element to
be capturing input. However, some events are passed through in case an element
becomes invisible while processing input.

View File

@ -10,6 +10,6 @@ TODO: Write me.
- `pop.mousereleased()` Handling a click maybe should *not* check bounds. - `pop.mousereleased()` Handling a click maybe should *not* check bounds.
Handling a mouse release should maybe *not* check for `excludeDraw`. If an Handling a mouse release should maybe *not* check for `excludeDraw`. If an
element was already selected and then went invisible, I'm probably breaking element was already selected and then went invisible, I'm probably breaking
things worse by doing this. things worse by doing this. This has been changed.
[1]: ../Pop.md [1]: ../Pop.md

View File

@ -1,5 +1,8 @@
local graphics local graphics, mouse
graphics = love.graphics do
local _obj_0 = love
graphics, mouse = _obj_0.graphics, _obj_0.mouse
end
local insert, remove local insert, remove
do do
local _obj_0 = table local _obj_0 = table
@ -141,6 +144,9 @@ do
setTitle = function(self, title) setTitle = function(self, title)
self.title:setText(title) self.title:setText(title)
return self return self
end,
getTitle = function(self)
return self.title:getText()
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
@ -203,35 +209,31 @@ do
end end
return false return false
end end
self.head.mousereleased = function(self, x, y, button)
if button == left then
self.selected = false
pop_ref.focused = false
return true
end
return false
end
else else
self.head.mx = 0 self.head.mx = 0
self.head.my = 0 self.head.my = 0
self.head.update = function(self) self.head.update = function(self)
return false local x, y = mouse.getPosition()
return self:setPosition(x - mx, y - my)
end end
self.head.mousepressed = function(self, x, y, button) self.head.mousepressed = function(self, x, y, button)
if button == left then if button == left then
self.selected = true self.selected = true
self.mx = x self.mx = x
self.my = y self.my = y
end
end
self.head.mousereleased = function(self, x, y, button)
if button == left then
self.selected = false
return true return true
end end
return false return false
end end
end end
self.head.mousereleased = function(self, x, y, button)
if button == left then
self.selected = false
pop_ref.focused = false
return true
end
return false
end
end, end,
__base = _base_0, __base = _base_0,
__name = "window", __name = "window",

View File

@ -163,7 +163,7 @@ pop.mousereleased = function(x, y, button)
do do
local element = pop.events[button] local element = pop.events[button]
if element then if element then
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) then if element.clicked and (not element.excludeDraw) then
do do
clickedHandled = element:clicked(x - element.x, y - element.y, button) clickedHandled = element:clicked(x - element.x, y - element.y, button)
if clickedHandled then if clickedHandled then
@ -171,7 +171,7 @@ pop.mousereleased = function(x, y, button)
end end
end end
end end
if element.mousereleased and (not element.excludeDraw) then if element.mousereleased then
do do
mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button) mousereleasedHandled = element:mousereleased(x - element.x, y - element.y, button)
if mousereleasedHandled then if mousereleasedHandled then
@ -185,14 +185,26 @@ pop.mousereleased = function(x, y, button)
end end
pop.keypressed = function(key) pop.keypressed = function(key)
print("keypressed", key) print("keypressed", key)
local element = pop.focused
if element and element.keypressed and (not element.excludeDraw) then
return element.keypressed(key)
end
return false return false
end end
pop.keyreleased = function(key) pop.keyreleased = function(key)
print("keyreleased", key) print("keyreleased", key)
local element = pop.focused
if element and element.keyreleased then
return element.keyreleased(key)
end
return false return false
end end
pop.textinput = function(text) pop.textinput = function(text)
print("textinput", text) print("textinput", text)
local element = pop.focused
if element and element.textinput and (not element.excludeDraw) then
return element.textinput(text)
end
return false return false
end end
pop.skin = function(element, skin, depth) pop.skin = function(element, skin, depth)

View File

@ -1,4 +1,4 @@
import graphics from love import graphics, mouse from love
import insert, remove from table import insert, remove from table
import sub, len from string import sub, len from string
@ -64,33 +64,30 @@ class window extends element
return true return true
return false return false
@head.mousereleased = (x, y, button) =>
if button == left
@selected = false
pop_ref.focused = false -- clear our focus
return true
return false
else else
@head.mx = 0 -- local mouse coordinates when selected @head.mx = 0 -- local mouse coordinates when selected
@head.my = 0 @head.my = 0
@head.update = => @head.update = =>
--TODO write me! x, y = mouse.getPosition!
return false @setPosition x - mx, y - my
--return false -- why?
@head.mousepressed = (x, y, button) => @head.mousepressed = (x, y, button) =>
if button == left if button == left
@selected = true @selected = true
@mx = x @mx = x
@my = y @my = y
@head.mousereleased = (x, y, button) => -- this is actually the same for both versions...
if button == left
@selected = false
return true return true
return false return false
@head.mousereleased = (x, y, button) =>
if button == left
@selected = false
pop_ref.focused = false -- clear our focus
return true
return false
debugDraw: => debugDraw: =>
graphics.setLineWidth 0.5 graphics.setLineWidth 0.5
graphics.setColor 0, 0, 0, 100 graphics.setColor 0, 0, 0, 100
@ -128,18 +125,6 @@ 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
@ -221,3 +206,6 @@ class window extends element
setTitle: (title) => setTitle: (title) =>
@title\setText title @title\setText title
return @ return @
getTitle: =>
return @title\getText!

View File

@ -136,11 +136,11 @@ pop.mousereleased = (x, y, button) ->
mousereleasedHandled = false mousereleasedHandled = false
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
pop.events[button] = nil pop.events[button] = nil
if element.mousereleased and (not element.excludeDraw) if element.mousereleased
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
@ -148,15 +148,30 @@ pop.mousereleased = (x, y, button) ->
pop.keypressed = (key) -> pop.keypressed = (key) ->
print "keypressed", key print "keypressed", key
return false --TODO event handlers return if they have handled the event!
element = pop.focused
if element and element.keypressed and (not element.excludeDraw)
return element.keypressed key
return false
pop.keyreleased = (key) -> pop.keyreleased = (key) ->
print "keyreleased", key print "keyreleased", key
return false --TODO event handlers return if they have handled the event!
element = pop.focused
if element and element.keyreleased
return element.keyreleased key
return false
pop.textinput = (text) -> pop.textinput = (text) ->
print "textinput", text print "textinput", text
return false --TODO event handlers return if they have handled the event!
element = pop.focused
if element and element.textinput and (not element.excludeDraw)
return element.textinput text
return false
--TODO rewrite skin system to not rely on knowing internals of elements, --TODO rewrite skin system to not rely on knowing internals of elements,
-- instead call functions like setColor and setBackground -- instead call functions like setColor and setBackground