bracket-lib icon indicating copy to clipboard operation
bracket-lib copied to clipboard

Is there a guide for modal interface?

Open singalen opened this issue 3 years ago • 3 comments

I've started to create a modal windows - inventory, dialogue - and I'm creating a separate game state for each. I guess this is not the best design, because even with a simple UI there will be a lot of modal windows. Is there an official perspective - how to best handle a modal windows? Maybe a recommended UI library?

singalen avatar Jun 22 '21 06:06 singalen

I haven't written a guide for that. There's a primitive modal window (no inputs) in my upcoming 7-day roguelike tutorial - in this section. That covers putting up a basic message box that triggers a game state change. It's not very flexible, but it's all I needed for that particular game.

There isn't really a good bracket-lib friendly UI library to help with that yet. I've actually started on one, but haven't had much time between the book release and the day job. I've been using egui and imgui in a few projects that take direct control over the render pipeline - they work really well, but the mismatch between text-mode and the UI library would be pretty jarring.

For handling modal states, you can improve on the enum system a bit with a state stack. Basically, you define a trait to be implemented by game states rather than a big enum. For example (I haven't tested this code):

pub trait GameMode {
    fn on_tick() -> StateTransition;
}

StateTransition defines the places to which a state can go. You could also have the state handle it, but that gets messy fast - especially with the borrow checker.

Then you have a state manager that holds a structure of Box<dyn GameMode> - probably a deque for quick push/pop. When the game starts, you'd push a starting menu state onto the stack. Begin play, push a playing state. Pop up a window? Push that state. Then each tick runs on_tick of the current top-most stack entry, and responds to any transition requests (they could be as simple as "close window" or complex as you need). If a state exits - closing the window - you can pop the old state off of the stack and be right where you were before. A lot of the time, you'll find the trait offers things like "on_init" and "on_exit" for any tearup/cleanup that needs to be done on transition.

Hope that helps!

thebracket avatar Jun 23 '21 00:06 thebracket

Got it, thank you very much. Indeed, I also didn’t find an immediate mode UI that can draw into console, they prefer to have a surface. Implementing a state stack didn’t occur to me for some reason. It sounds like the rift thing to do. I’ll do it, thanks!

singalen avatar Jun 23 '21 03:06 singalen

@thebracket this is offtopic, but since you mentioned egui/imgui, can you share an example on integrating it with bracket lib? I would imagine it being helpful in debug build to display egui controls to control game state directly.

ytaras avatar Sep 17 '21 07:09 ytaras