diff --git a/README.md b/README.md index dfd6f04..823246a 100644 --- a/README.md +++ b/README.md @@ -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 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 ```lua diff --git a/docs/Elements.md b/docs/Elements.md index a6ae83c..6ac16b0 100644 --- a/docs/Elements.md +++ b/docs/Elements.md @@ -29,6 +29,21 @@ All elements have the following standard methods: - `setAlignment(horizontal, vertical)` - Sets alignment *values* to this, but 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 positions will be returned, and how positioning and resizing will work. Run the demo to see how these affect things. @@ -98,3 +113,4 @@ not be rendered. [1]: ./Skins.md [2]: https://love2d.org/wiki/Font [3]: ./Drawables.md +[4]: https://love2d.org/wiki/love.mousepressed diff --git a/docs/Pop.md b/docs/Pop.md index 9dbfd17..7163a45 100644 --- a/docs/Pop.md +++ b/docs/Pop.md @@ -16,6 +16,8 @@ elements at once. - `pop.window` is the top level element. It essentially represents the game 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, `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 @@ -36,10 +38,10 @@ elements at once. - `pop.textinput(text)` is used to grab text input for any focused element that can accept it. - `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 future. - `pop.keyreleased(key)` is also not used yet, but probably will be used in the diff --git a/pop/init.lua b/pop/init.lua index 2a4bd52..0a9793f 100644 --- a/pop/init.lua +++ b/pop/init.lua @@ -6,7 +6,7 @@ local pop = {} pop.elementClasses = {} --pop.elements = {} 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() -- load element classes @@ -72,12 +72,62 @@ function pop.draw(element) end end -function pop.mousepressed(button, x, y) - --TODO find element, if it has a callback, call with button and LOCAL x/y +function pop.mousepressed(button, x, y, element) + 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 -function pop.mousereleased(button, x, y) - --TODO find element, if it has a callback, call with button and LOCAL x/y +function pop.mousereleased(button, x, y, element) + 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 function pop.keypressed(key)