Skip to content
KaixoCode edited this page Oct 18, 2021 · 1 revision

A crucial aspect of a gui library is of course the window. To make this easier, there is a Gui helper class, where you can register windows, and it will take care of pretty much everything for you. The main window is the Frame, which has a TitleBar and a Panel.

Gui

The Gui helper class is really easy to use. It contains a collection of windows, and it will take care of updating/rendering etc, here is a short example how to register a window:

Gui _gui;

Frame& _frame = _gui.emplace<Frame>({
    .name = "Window"
});

_frame.Create(); // Must always create the window after initialization!

while (_gui.Loop());

Frame

A Frame is a window with a TitleBar and a Panel. The TitleBar displays the window name, a menubar, and the close/minimize/maximize buttons and the Panel fills the entire remaining area of the window. The title bar is fully customizable, you can adjust text color, size, and font, and you can change its background color. You can also adjust the color of the close/minimize/maximize buttons.

// Adjust text and color
_frame.titlebar.background = Color{ 26, 26, 26 };
_frame.titlebar.text.color = Color{ 255, 255, 255 };
_frame.titlebar.text.size = 12;
_frame.titlebar.font = "segoeui";

// Change color of the close button
_frame.titlebar.close.color.base = Color{ 26, 26, 26 };
_frame.titlebar.close.color.State(Pressed) = Color{ 170, 0, 0 };
_frame.titlebar.close.color.State(Hovering) = Color{ 255, 0, 0 };

Window

A Window class is OS specific, and must derive from the WindowBase. Its must use the WindowBase::WindowBase constructor, and it must override the WindowBase::Create method, in which it will create and initialize the OS specific window, using the specified settings. The WindowBase stores these settings in WindowBase::settings, however, not all these settings can be changed after WindowBase::Create has been called.

struct Settings
{
    std::string name = "Window";
    Dimensions dimensions{ 0, 0, 500, 500 };
    bool alwaysOnTop = false;
    bool transparentBuffer = false;
    int state = Show;
    bool resizeable = true;
    bool decorated = true;
    bool animations = true;
    bool hideOnClose = false;
};

The window class must also override the WindowBase::Loop method. The Gui will keep looping as long as all the registered windows return true from the WindowBase::Loop method, unless the setting hideOnClose was set to true, in which case the Gui will simply set the visibility state of the window to WindowState::Hide.

Since the Window extends Component, it also has component-like dimension access, which are public member variables. Because of this, the window class should keep its location and size in sync with these member variables. On top of this, the visibility state of a window is also stored as a component state, and thus can be accessed and changed using window.State(Visible). The Window implementation must keep its window state in sync with this state.

enum WindowState
{  
    Hide = 1, Show, Minimize, Maximize, Close
};

For example, the following code should hide the window:

window.State(Visible) = WindowState::Hide;

The Window class also contains a unique pointer to a GraphicsBase, which must be initialized inside WindowBase::Create to the correct graphics backend.

Clone this wiki locally