How to write module to simplify GUI programming (SOLVED)

Sorry if the title is not very clear, in short I wanted to write myself a little script very quickly to simplify my life with GUIs, here is the script in question:

local Button = {
	pressed = false
}

Button.__index = Button

function Button:on_input(action, ...)

	if action.pressed and gui.pick_node(self.node, action.x, action.y) then

		gui.play_flipbook(self.node, self.btn_pressed)
		self.pressed = true

	elseif self.pressed then

		if action.released then

			self.pressed = false

			if gui.pick_node(self.node, action.x, action.y) then
				self.func_action(...)
			end

		else
			if not gui.pick_node(self.node, action.x, action.y) then
				gui.play_flipbook(self.node, self.btn_normal)
			else
				gui.play_flipbook(self.node, self.btn_pressed)
			end
		end

	end

end

return function (btn_node, btn_normal, btn_pressed, func_action)

	return setmetatable({
		node = btn_node,
		btn_normal = btn_normal,
		btn_pressed = btn_pressed,
		func_action = func_action
	}, Button)

end

But when I wanted to use it there is a small detail that I had forgotten, it is that the gui function table can only be called from a gui_script so did you another trick to avoid having to rewrite the same code in each script?

I tried passing gui as a parameter of Button:on_input in my code snippet but obviously it didn’t work (I didn’t feel smart for long ^^)

And happy new year to all by the way :partying_face:

Have you checked out the source of existing GUI libraries like Gooey? Seems they don’t have an issue calling the gui module directly from a .Lua file.

1 Like

Yeah, you can call gui functions in a module as long as you run the module from a GUI script. Take a look at Druid and Gooey for examples.

1 Like

I did indeed mix the brushes with the names, I had a script to manage the object which contains the GUIs named gui_menu.script and the scripts looking like title.gui_script, it’s stupid … I would think twice now when it’s late and I get stuck on a stupid detail like that…

Thanks to you if not for making me discover these modules!