Use of graphical user interfaces
A friendly way of interfacing with applications in a day and age where computers are basically a necessity inside every household is a must. The entire subject of GUIs could fill multiple books by itself, so for the sake of keeping this simple, we are only going to scratch the surface of what we have to work with:
class GUI_Manager : public StateDependent{
friend class GUI_Interface;
public:
...
bool AddInterface(const StateType& l_state,
const std::string& l_name);
bool AddInterface(const std::string& l_name);
GUI_Interface* GetInterface(const StateType& l_state,
const std::string& l_name);
GUI_Interface* GetInterface(const std::string& l_name);
bool RemoveInterface(const StateType& l_state,
const std::string& l_name);
bool RemoveInterface(const std::string& l_name);
bool LoadInterface(const StateType& l_state,
const std::string& l_interface, const std::string& l_name);
bool LoadInterface(const std::string& l_interface,
const std::string& l_name);
void ChangeState(const StateType& l_state);
void RemoveState(const StateType& l_state);
SharedContext* GetContext() const;
void DefocusAllInterfaces();
void HandleClick(EventDetails* l_details);
void HandleRelease(EventDetails* l_details);
void HandleTextEntered(EventDetails* l_details);
void AddEvent(GUI_Event l_event);
bool PollEvent(GUI_Event& l_event);
void Update(float l_dT);
void Render(sf::RenderWindow* l_wind);
template<class T>
void RegisterElement(const GUI_ElementType& l_id){ ... }
private:
...
};
Interface management, quite predictably, is also dependent on application states. The interfaces themselves are also assigned names, which is how they are loaded and stored. Mouse input, as well as text enter events, are both utilized in making the GUI system work, which is why this class actually uses the event manager and registers three call-backs with it. Not unlike other classes we have discussed, it also uses the factory method, in order to be able to dynamically create different types of elements that populate our interfaces.
Interfaces are described as groups of elements, like so:
Interface MainMenu MainMenu.style 0 0 Immovable NoTitle "Main menu" Element Label Title 100 0 MainMenuTitle.style "Main menu:" Element Label Play 0 32 MainMenuLabel.style "PLAY" Element Label Credits 0 68 MainMenuLabel.style "CREDITS" Element Label Quit 0 104 MainMenuLabel.style "EXIT"
Each element also supports styles for the three different states it can be in: neutral, hovered, and clicked. A single style file describes what an element would look like under all of these conditions:
State Neutral Size 300 32 BgColor 255 0 0 255 TextColor 255 255 255 255 TextSize 14 Font Main TextPadding 150 16 TextOriginCenter /State State Hover BgColor 255 100 0 255 /State State Clicked BgColor 255 150 0 255 /State
The Neutral
style serves as a base for the other two, which is why they only define attributes that are different from it. Using this model, interfaces of great complexity can be constructed and customized to do almost anything.