Defold Wrap Library

I guess someone might like this library. I write it to avoid the feeling of asking gui to do a lot. Now just act like a node can do everything by itself :grinning_face_with_smiling_eyes:

Instead of

local node = gui.get_node("my_node")
gui.set_alpha(node, 0.5)
gui.animate(node, "color.w", 1, gui.EASING_LINEAR, 2)
gui.set_enabled(node, true)

Write this

local node = wrap.node("my_node")
node:set_alpha(0.5):animate("color.w", 1, gui.EASING_LINEAR, 2):set_enabled(true)

You can also get a list of wrapped nodes

local w = wrap.node({ "my_node", title = "text_title", button = gui.get_node("button") })
w.my_node:set_alpha(0.5)
w.title:set_text("Admin")
w.button:set_size(vmath.vector3(100, 50, 0))

Currently, it has all methods working for GUI nodes, besides, it supports some convenient methods you may need it sometimes, like

add_position(...),
get_position_x()
get_position_y()
get_position_z()
set_position_x(...)
set_position_y(...)
set_position_z(...)
add_position_x(...)
add_position_y(...)
add_position_z(...)
...

Check it out: baochungit/defold-wrap (github.com)

15 Likes

That is pretty neat! Thanks for sharing!

3 Likes

Aa! Amazing! I make similar wrappers almost all the time for many things, because OOP :sweat_smile: it is especially useful for nodes that are many times not dynamic objects. You made it look nice and convenient :heart:

4 Likes

Thanks for sharing this.

1 Like

Defold Wrap has just expanded a bit!

It now can be used to create a GUI Applet (yes, applet is just what I call it).

Its structure is kind of this:
Applet – Scene – Objects (each object contains nodes and sub-objects)

Actually, I’m not good at explaining, so you can get it via this example code:

local applet = wrap.gui_applet("root", {
	init = function(scene)
		scene:new_object(Text, {
			position = vmath.vector3(0, 100, 0),
			text = "Demo of single scene."
		})
		scene.box = scene:new_object(Box, {
			position = vmath.vector3(0, -30, 0),
			size = vmath.vector3(50),
			color = "#ff3dd3"
		})
		scene.direction = 1
	end,
	update = function(scene, dt)
		local box_root_node = scene.box.root
		box_root_node:add_position_x(80 * dt * scene.direction)
		if math.abs(box_root_node:get_position_x()) > 90 then
			scene.direction = -scene.direction
		end
	end,
	final = function(scene)
	end
})

The above code is a demo for single scene applet. If you intend to have an applet with many scenes, you can do as following:

local applet = wrap.gui_applet("root")
applet:register_scene("loading", SceneLoading)
applet:register_scene("lobby", SceneLobby)
applet:show_scene("loading")

(For more information, please refer the example in the github repository.)

Let’s talk about the pros and cons of it.

Pros:

  • All your stuff (scenes, objects) will just belong to one script, so you can avoid the issue of different context when using callbacks.
  • Working with your nodes by Wrap way (more convenient)

Cons:

  • You almost can’t get the benefit of GUI editor. (I’m finding a way for using .gui file, will update later)
  • Your GUI nodes amount could be a lot (consider to increase Max Nodes value to avoid the limitation)

What to do

  • Add scene transitions
  • Find a way to use .gui file
  • Your suggestions?
1 Like