Closed cfagot closed 3 months ago
I added an example game in a new branch: https://github.com/cfagot/xilem/tree/space_survival. I intend to contribute that in a new pull request if/when this pull request is approved. It might be too large as an example (not huge but is about 2000 lines small). But I think it would be good because it shows off the capabilities of xilem/masonry/vello and helps establish a guide post for how apps like this can be made with xilem (hopefully not guiding in the wrong direction though :).
All the code is clean room (other than a few snippets I pulled form my own code) so no license issues. And no external dependencies either.
I haven't looked at this PR in-depth, so I might be missing things, but so far I'd be against merging it.
Overall the fact that this introduces new interfaces which closely follow the API of another crate and by default do nothing but delegate to that another identical method (eg MasonryState::submit_user_event
does nothing but call MasonryState::handle_user_event
) strikes me as a major code smell.
Similarly, this PR introduces like 5 different methods with the same name or similar name for window events, device events and user events. Having worked on projects with similar structures, I can tell you from experience the DX of maintaining these APIs in painful.
My recommendation would be to nix the AppToXilemInterface
trait and the AppToXilemInterface
and look for a way to give your app direct access to the Winit event loop and whatever else you need. Doing so means you might need to create your own composition root, eg you won't end up using Xilem
or AppDriver
and will have to create your own root types instead. I think that's fine: if you have a direct dependency on Xilem and Masonry, you're already creating a game engine anyway.
The purpose of this pull request is to allow masonry and xilem to share the event loop with the rest of the application but still essentially own and manage the event loop. There is also a small hack to allow widgets to always repaint, but that isn't meant to be included in the final pull request (left it in for information purposes).
Breakdown of changes:
Masonry
Xilem
let app = Xilem::new(app_state, app_logic).with_app_interface(Box::new(AppInterface::new()));
Example
In the following example, the application hooks into resumed/suspended to change the control flow of the event loop, block redraw events coming from the event loop, and uses about_to_wait to update game world and push a redraw request to xilem.
Note that GameState is an Arc<Mutex> because it needs to be shared between xilem app, widget view, and widget. If there is a better way to do that let me know.