Skip to content
Giorgos Michaelides edited this page Jan 18, 2021 · 8 revisions

GuiHost

Gui controls can be grouped into an IGuiHost instance, subscribed to the GemGui's screen management and then render them.

The IGuiHost defines the following members

    event EventHandler<EventArgs> OnEnter;
    event EventHandler<EventArgs> OnExit;

    IEnumerable<AControl> Entries();

    AControl this[int id] { get; }

    ScreenState ScreenState { get; }

    ITransition Transition { get; set; }

    void EnterScreen();

    void ExitScreen();

    void HandleInput();

    void Update(GameTime gameTime);

    void Draw(SpriteBatch batch);

You can add a new IGuiHost with your controls

    void AddGuiHost(string guiHostId, params AControl[] controls)

    gui.AddGuiHost("MainScreen", newGameButton, optionsButton,exitButton)
    gui.AddGuiHost("OptionsScreen",volumeButton,backButton)

And then render / hide / disable swap them

   gui.Show("MyMainScreen")
   gui.Hide("OptionsScreen")
   gui.Swap("MyMainScreen","OptionsScreen")
   gui.Disable()

You can check if a host is active with

   bool IsShowing(string guiHostId);

Or access an IGuiHost instance with the gui's string index

   gui["MainScreen"].ScreenState;

Define the transition

When a GuiHost is entering or leaving the screen the ITransition manages the effect

interface ITransition
{
    event EventHandler<float> ProgressChanged;
    event EventHandler<TransitionDirection> TransitionStarted;
    event EventHandler<TransitionDirection> TransitionFinished;

    float Progress { get; }

    void Start(TransitionDirection direction);

    void Update(GameTime gameTime);
    void Draw(RenderTarget2D renderTarget, ScreenState state, SpriteBatch batch);
}

The host aggregates the controls when the ScreenState is Active and removes it when the ScreenState is Exit. The renderTarget represents the current host's screen. You can draw it according to your logic and the TransitionDirection.

The default ITransition implementation is

   TimedTransition(TimeSpan transitionTime, 
                   TransitionRenderAction transitionRenderAction)

which takes a transition time and the delegate TransitionRenderAction

   delegate void TransitionRenderAction(ScreenState state,
                                        float progress,
                                        RenderTarget2D renderTarget,
                                        SpriteBatch batch);

You can define your own transition logic by passing an ITransition instance to the IGuiHost

This effect makes the IGuiHost leave / enter the screen by accelerating and fading out

   gui["MainScreen"].Transition = 
      new TimedTransition(
           TimeSpan.FromSeconds(0.5),
           (state, progress, target, batch) =>
           batch.Draw(target, 
           (float)Math.Pow(progress - 1.0f, 2) * new Vector2(0, 300), 
           Color.White * progress));

For more advanced transitions or transformations, you can use the Gem.Gui.Animations namespace.

Render something in the background

If you want to render something in the gui's background you can use the EventHandler<SpriteBatch> DrawWith event

     gui.DrawWith += (sender, batch) => RenderBackground(batch);
Clone this wiki locally