Andidy / engine-v2

1 stars 0 forks source link

Create Uniform Gui Drawing Functionality #55

Closed Andidy closed 2 years ago

Andidy commented 2 years ago

Currently individual instances of GUI elements need to be stored in order to be called. Some grouping can be done based on type of control (Example: all checkboxes could be stored together). However this makes it difficult to automatically call all the functions needed for a GUI to be drawn for a specific scene.

My understanding of the best way to improve the current GUI system is to create an abstract base class for all GUI elements to inherit from. The ABC will only contain a method virtual void Draw() = 0;. The purpose of the Draw function is to provide a uniform interface for drawing the GUI elements and updating their state. Example: GuiButton() in raylib returns a bool if the button was pressed. This bool is stored in a member variable pressed of the helper struct gui::Button. The Draw() function would look like the following:

void Button::Draw() {
  pressed = GuiButton(bounds, text.c_str());
}

This way the expected usage of:

if (GuiButton(bounds, text) {
  // perform button functionality
}

is preserved and would look like:

button.Draw();
if (button.pressed) {
  // perform button functionality
}

It also allows moving the execution of the button's functionality to a later part of the code if that is desired. This change will allow placing all GUI elements into one collection per Scene which will make management and automation easier, since most GUI elements change little of the game's state.