-
Notifications
You must be signed in to change notification settings - Fork 22
UI Window management
This sample also includes a way how to manage different UI windows and their lifecycles. Window can be everything in the application - settings view (graphics, audio, etc.), scoreboard, new game settings etc. To manage all that WindowManager
was created under the UI
directory. UI
directory also contains BaseWindow.h
all the created windows should inherit this class. BaseWindow
application contains basic methods which will create main UIElement which would contain all the sub-elements for particular windows.
Let's assume that you would like to create a new window - SettingsWindow
. You would create new subdirectory in the UI
directory, then you would create 2 files - SettingsWindow.h
and SettingsWindow.cpp
.
SettingsWindow.h
#include <Urho3D/Urho3DAll.h>
#include "../BaseWindow.h"
class SettingsWindow : public BaseWindow
{
URHO3D_OBJECT(SettingsWindow, BaseWindow);
public:
SettingsWindow(Context* context);
virtual ~SettingsWindow();
virtual void Init();
protected:
virtual void Create();
private:
void SubscribeToEvents();
};
SettingsWindow.cpp
#include <Urho3D/Urho3DAll.h>
#include "SettingsWindow.h"
#include "../../MyEvents.h"
/// Construct.
SettingsWindow::SettingsWindow(Context* context) :
BaseWindow(context, IntVector2(300, 300))
{
Init();
}
SettingsWindow::~SettingsWindow()
{
}
void SettingsWindow::Init()
{
Create();
SubscribeToEvents();
}
void SettingsWindow::Create()
{
UI* ui = GetSubsystem<UI>();
Text* text = _base->CreateChild<Text>();
text->SetText("Close me");
text->SetStyleAuto();
text->SetAlignment(HA_CENTER, VA_CENTER);
}
void SettingsWindow::SubscribeToEvents()
{
}
When creating new window BaseWindow
class excepts 2 parameters for the constructor
BaseWindow(Context* context, IntVector2 size = IntVector2(300, 200)) :
Second parameter size
is the size of the window that will be created.
When that's done, you should register this Object, you can do this by adding the following line in the UI/WindowManager.cpp
void WindowManager::RegisterAllFactories()
{
// Register classes
context_->RegisterFactory<SettingsWindow>(); // <--------------
}
If everything worked as expected, you can now trigger event to open/close this window
Opening window
C++ and AngelScript
VariantMap data;
data["Name"] = "SettingsWindow";
SendEvent("OpenWindow", data);
Closing window
C++ and AngelScript
VariantMap data;
data["Name"] = "SettingsWindow";
SendEvent("CloseWindow", data);
Closing all active windows
C++ and AngelScript
SendEvent("CloseAllWindows");
When the window is actually closed via the WindowManager, it will send out WindowClosed
event with Name
as a string parameter. You could listen to this event to change your game state accordingly. To see a live example, check the in-game level: https://github.com/ArnisLielturks/Urho3D-Empty-Project/blob/master/Source/Levels/Level.cpp which listens to pause menu close event.
C++ and AngelScript
void Level::HandleWindowClosed(StringHash eventType, VariantMap& eventData)
{
String windowName = eventData["Name"].GetString();
}
These events will be called by WindowManager
and it should take care of the rest.