AmbientRun / Ambient

The multiplayer game engine
https://ambient.run
Apache License 2.0
3.8k stars 124 forks source link

Global UI input dispatch / UI resource sharing #602

Open philpax opened 1 year ago

philpax commented 1 year ago

At present, each WASM module handles its own input from the WindowKeyboardInput message. This works fine when there's only one module handling UI, but this is unlikely to be the case once we have multiple embers running at the same time.

The current behaviour has its oddities:

Fixing this will likely require us to consider how we generally "share" resources between embers. The ECS and the messages work well for gameplay, but how do we handle more abstract resources like "visual hierarchy" and "module to accept input"?

The easiest solution would be to hardcode some mechanisms for this into the runtime, so that UI is especially privileged (functions for manipulating the focus stack, requesting a depth to render at for the UI, etc) but it's likely similar problems will occur in other parts of the ecosystem.

See also #620, which was previously contained in this issue.

FredrikNoren commented 1 year ago

Those two issues are quite different. I think focus root can easily be fixed by adding a resource component which all FocusRoot read/writes from (and listens to changes to). The input dispatch is trickier.

philpax commented 1 year ago

My thinking behind putting them together was that they're symptoms of the same problem: sharing global resources where there's no clear owner or ordering.

Using this scenario as an example:

image

C is a menu bar and A and B are windows. Each of these come from separate modules.

The three issues are:

I'll split off the first issue because that should be a much simpler fix, but the other two I'm not really sure how you'd fix them in a "distributed" environment. It seems like you'd need some kind of central broker or to implement some kind of consensus algorithm...