added mouse event handling

This commit is contained in:
Fox 2016-01-25 16:13:05 -08:00
parent 7406a1ee34
commit acc404153c
4 changed files with 79 additions and 8 deletions

View File

@ -15,6 +15,9 @@ extensible, allowing for complex interfaces to be built in it.
- Supports custom elements/skins, make your own and move them into the - Supports custom elements/skins, make your own and move them into the
appropriate directories for them to be automatically loaded. appropriate directories for them to be automatically loaded.
**Note**: Skins are currently a work-in-progress, and not automatically loaded.
See the documentation linked to below.
## Usage ## Usage
```lua ```lua

View File

@ -29,6 +29,21 @@ All elements have the following standard methods:
- `setAlignment(horizontal, vertical)` - Sets alignment *values* to this, but - `setAlignment(horizontal, vertical)` - Sets alignment *values* to this, but
does not move the element. Same `horizontal`/`vertical` strings as `align()` does not move the element. Same `horizontal`/`vertical` strings as `align()`
Events:
If any of these is set as a function, they will be called when the appropriate
event happens.
- `mousepressed(button, x, y)` - Called the moment a mouse click starts, uses
the same values for `button` as [love.mousepressed][4], but the `x`/`y` are
relative to the element.
- `mousereleased(button, x, y)` - Called the moment a mouse click stops, uses
the same values for `button` as [love.mousepressed][4], but the `x`/`y` are
relative to the element.
- `clicked(button, x, y)` - Called when a mouse click stops if it started on the
same element. Uses the same values for `button` as [love.mousepressed][4], but
the `x`/`y` are relative to the element.
**Note**! Calls to `align()`, `alignTo()`, and `setAlignment()` change what **Note**! Calls to `align()`, `alignTo()`, and `setAlignment()` change what
positions will be returned, and how positioning and resizing will work. Run the positions will be returned, and how positioning and resizing will work. Run the
demo to see how these affect things. demo to see how these affect things.
@ -98,3 +113,4 @@ not be rendered.
[1]: ./Skins.md [1]: ./Skins.md
[2]: https://love2d.org/wiki/Font [2]: https://love2d.org/wiki/Font
[3]: ./Drawables.md [3]: ./Drawables.md
[4]: https://love2d.org/wiki/love.mousepressed

View File

@ -16,6 +16,8 @@ elements at once.
- `pop.window` is the top level element. It essentially represents the game - `pop.window` is the top level element. It essentially represents the game
window. window.
- `pop.focused` holds a reference to the last clicked on element (that handled
the click using `element:mousepressed()` (see [Elements.md][1])).
- `pop.create(element, parent, ...)` is how elements are actually created, - `pop.create(element, parent, ...)` is how elements are actually created,
`element` is a string naming the desired element. There are wrappers around `element` is a string naming the desired element. There are wrappers around
any element that doesn't conflict with a key in the `pop` module so that you any element that doesn't conflict with a key in the `pop` module so that you
@ -36,10 +38,10 @@ elements at once.
- `pop.textinput(text)` is used to grab text input for any focused element that - `pop.textinput(text)` is used to grab text input for any focused element that
can accept it. can accept it.
- `pop.mousepressed(button, x, y)` is used to detect and handle when an element - `pop.mousepressed(button, x, y)` is used to detect and handle when an element
is clicked on. (Not actually used yet.) is clicked on. See [Elements.md][1].
- `pop.mousereleased(button, x, y)` is used to detect and handle when a mouse
button is released over an element. See [Elements.md][1].
- `pop.mousereleased(button, x, y)` is not used yet, but probably will be used
in the future.
- `pop.keypressed(key)` is not used yet, but probably will be used in the - `pop.keypressed(key)` is not used yet, but probably will be used in the
future. future.
- `pop.keyreleased(key)` is also not used yet, but probably will be used in the - `pop.keyreleased(key)` is also not used yet, but probably will be used in the

View File

@ -6,7 +6,7 @@ local pop = {}
pop.elementClasses = {} pop.elementClasses = {}
--pop.elements = {} --pop.elements = {}
pop.window = {child = {}} --top level element, defined in pop.load() pop.window = {child = {}} --top level element, defined in pop.load()
--TODO we need a "focused" element for textinput or whatever pop.focused = pop.window --defaults to top level
function pop.load() function pop.load()
-- load element classes -- load element classes
@ -72,12 +72,62 @@ function pop.draw(element)
end end
end end
function pop.mousepressed(button, x, y) function pop.mousepressed(button, x, y, element)
--TODO find element, if it has a callback, call with button and LOCAL x/y if not element then
element = pop.window
if (x < element.x) or (y < element.y) or
(x > (element.x + element.w)) or (y > (element.y + element.h)) then
return
end
end
local handled = false
for i=1,#element.child do
if (x >= element.x) and (y >= element.y) and
(x <= (element.x + element.w)) and (y <= (element.y + element.h)) then
handled = pop.mousepressed(button, x, y, element.child[i])
end
end
if (not handled) and element.mousepressed then
element:mousepressed(button, x - element.x, y - element.y)
pop.focused = element
end
return handled
end end
function pop.mousereleased(button, x, y) function pop.mousereleased(button, x, y, element)
--TODO find element, if it has a callback, call with button and LOCAL x/y if not element then
element = pop.window
if (x < element.x) or (y < element.y) or
(x > (element.x + element.w)) or (y > (element.y + element.h)) then
return
end
end
local handled = false
for i=1,#element.child do
if (x >= element.x) and (y >= element.y) and
(x <= (element.x + element.w)) and (y <= (element.y + element.h)) then
handled = pop.mousereleased(button, x, y, element.child[i])
end
end
if not handled then
if element.mousereleased then
element:mousereleased(button, x - element.x, y - element.y)
handled = true
end
if element.clicked then
element:clicked(button, x - element.x, y - element.y)
handled = true
end
end
return handled
end end
function pop.keypressed(key) function pop.keypressed(key)