Clay for Defold native extension

I’ve always been in favor of immediate mode GUI and I’ve used Clay for lots of side-projects. It’s a renderer-agnostic UI layout framework. I’ve put together a native extension to have Clay layout elements which are then mapped to native Defold GUI nodes.

Instead of Clay’s heavily macro’d C API, there’s a Lua interface. You can have a look at the example project to see how this works.

Clay itself is a really fast layout engine, tens of thousands of elements in less than a millisecond. And by using Defold layers, you can do a lot of batched draw calls. The speed bottleneck is in parsing big, nested Lua tables, unfortunately.

This derpy example lays out 11,366 UI elements, but we only have 267 Defold GUI nodes, and with batching it’s all done in 11 drawcalls.

13 Likes

Nice I will give it a try! Being bad at UI, I like code only layout solutions :sweat_smile:

I was searching for clay UI examples and stepped on this article:

This has nothing to do with your Clay UI but is a nice article about every UI elements and how to use them, seems usefull.

Anyway, I’ll try your extension, I like the fact that is use the native Defold GUI for rendering, I wonder if we can mix it with DRUID UI to layout Druid widgets?

1 Like

I’m not up to speed with how Druid works. If it needs some additional data to be propagated from Lua into the associated GUI node, then you can look at how slice9 is propagated as userdata in the native extension.

I had a look at your code, and I think it would be a bad idea to integrate a druid widget inside a Clay element. Druid is just a framework that use true defold gui nodes, but if Druid modify things on nodes, Clay would not know that they changed and it will probably break its layout.
But I have a question: can we use the defold api to modify Clay-generated nodes after the end_layout() ? Can we get the node_id back from Clay and act on the node?
Like if clay was doing the initial layout then we act on the nodes with gui api?

Anyway, I think we can easily do the reverse: create a Druid widget that has a node which is the root of a Clay surface.
Like this, the tree hierarchy stay separated and in control of each engine.
Like we could have a Druid UI but for a specific menu use Clay to render a responsive flex layout in an area.

I will give it a try.

This would be cool as the thing that bothers me with Imgui is that it is rendered outside of the defold gui system, you cannot integrate it into an existing defold gui.

Feel free to play around with it! I think I should have stated a little bit more clearly what my intended outcome with this was :slight_smile:

Passing back the GUI nodes’ ids to Lua after end_layout is a bit tricky. It’s dealing with the two opposed UI paradigms in immediate and retained mode. The architecture I went for was to make Defold’s GUI nodes invicible. They will be added, removed, or reused on each update. I can’t guarantee a node persists, like in retained mode.

However, you can create multiple Clay surfaces, attached to different GUI root nodes, and call begin/end layout on them. Each surface comes with a little bit of overhead, but you can intentionally set a low max number of elements.

I’ve just updated this project to support the new rich-text formatting in Defold 1.13.2-beta

5 Likes

Nice! I have a question for you as I didnt find a way to do it:
How do you handle text INPUTs with Clay gui?
I mean a text input element that gather keyboard input.

1 Like

Good question. I havn’t had to do that before, but I can look at what the canonical way of doing that in “regular” Clay is, and see if there’s a neat analogue for it here.

2 Likes

Clay doesn’t define any concrete widgets like buttons, input fields, etc. It’s similar to Defold, just rectangles, text, and scroll containers, basically. Creating an input field would require a lot of the same work as Druid’s rich input widget. You’d have to animate the caret, text selection, interfacing with the platform’s input functionality, etc.

I don’t really need an input field right now, so I don’t want to take the time to write one. It’s honestly quite a bit involved hehe. But I’ve updated the repository with quite a few more examples of both a simple widget, the buttons, and how to do rich text formatting.

2 Likes

Thanks for the search! And thanks for the new examples, this looks great!

Yeah I was thinking it was the hard way to do text input.
But as text input implementation goes, I think we will need to have a stable gui node (i.e. same node existing throught all Clay updates) to keep interactive state and keyboards events etc. between frames stable…
But maybe not, I havent find time to deep dive into this (I am into the gamejam right now) and see how one can keep all stable through it