Let me walk you through the button sample from the readme
Gooey is built around the GUI system that exists in Defold. The default system does not come with any predefined interaction components, only rendering configuration of boxes, texts and pies. To create a button without any libraries you would add a box node to the gui file and then inside the gui script, whenever input happens, check if the box was pressed.
function on_input(self, action_id, action)
if gui.pick_node(gui.get_node("box"), action.x, action.y) and action.released then
print("Box was taped")
end
end
What Gooey does, is to hide the underlying mechanics involved in doing this. As such you first have to setup a gui file with the right nodes, then feed these to Gooey functions in a gui_script.
local gooey = require "gooey.gooey"
local function update_button(button)
if button.pressed_now then
gui.play_flipbook(button.node, hash("button_pressed"))
elseif button.released_now then
gui.play_flipbook(button.node, hash("button_normal"))
elseif not button.pressed and button.over_now then
gui.play_flipbook(button.node, hash("button_over"))
elseif not button.pressed and button.out_now then
gui.play_flipbook(button.node, hash("button_normal"))
end
end
local function on_pressed(button)
print("pressed")
end
function on_input(self, action_id, action)
gooey.button("button/bg", action_id, action, on_pressed, update_button)
end
In the above example from the readme, we can see that gooey.button(...)
is the function that performs the same thing from the first example. What makes it a little confusing is the “button/bg” name of the node, along with the two extra functions passed in as arguments.
The “button/bg” relates to a template node named button. Inside the template node there is a box node called bg - short for background - which is then tested for input like the first example. Gooey comes these templates which saves you the trouble of having to create them yourself and figure out how the hierarchies of nodes should be for the more complex components. All you have to do is “add template” inside your gui file and select one of the Gooey ones.
The on_pressed function is called when the user presses the button, allowing you to react in some way.
The update_button(button) is called when something in the internal state has changed. In the first example there would have been many cases where the button behaved strange compared to what users expect. For example if the user presses down somewhere outside of the button and then moved the cursor/finger inside the button before releasing, by keeping an internal state this and many other small quirks can be handled gracefully. But to give the user some sort of feedback related to pressing down on the button, hovering inside or similar you can react to these internal changes in the update_button function and change the visual state of the button. The gooey buttons comes loaded with predefined flipbook animations representing different states. So all you have to do is tell the gui which one to play.
The button example is for sure the least complicated, but the workflow is similar to use all of them. Add a template component to the gui, feed the right nodes from the templates in to corresponding gooey function. And you can find code snippets for all of the different components in the readme.
I hope this helps you and others that have gotten stuck!