Difference between revisions of "Dialog System"
m (moved DialogSystem to Dialog System) |
|||
(14 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | The default CEGUI implementation stores the currently specified values of the widgets such that closing and then reopening a window will redisplay those values. | + | {{VersionBadge|0.5}} {{VersionBadge|0.6}} |
+ | |||
+ | The default CEGUI implementation stores the currently specified values of the widgets such that closing and then reopening a window will redisplay those values. Presented here is a different approach, to allow values to be saved or cancelled. | ||
==== Explanation ==== | ==== Explanation ==== | ||
− | The DemoSample class is the "main" application. | + | The DemoSample class is the "main" application. It initializes CEGUI and loads a simple layout. It then initialises instances of the SimpleWindow and the SimpleDialog classes. These contain the specific code to handle the behavior of the Simple Window and the Simple Dialog. In turn they rely on the behaviors define within the DialogSystem class to activate higher level features. |
− | The | + | The DialogSystem class implements the behavior of the 'Ok', 'Cancel', and 'Apply' buttons that we've become accustomed to in the world of Windows. The DialogSystem::DialogSystemEvents enumaration defines the high level events supported by the DialogSystem class: |
open: display the window and its data | open: display the window and its data | ||
ok: save the data and close the window | ok: save the data and close the window | ||
Line 18: | Line 20: | ||
doClose(): hide the window | doClose(): hide the window | ||
− | The SimpleWindow class will manage everything that has to do with the Simple Window. | + | The SimpleWindow class will manage everything that has to do with the Simple Window. The initWindow() function initialises the DialogSystem by specifying the handle of the window/dialog (the name specified within the layout file) and whether it should initially be visible or not. Then it instructs the DialogSystem to handle four events as well as which action to initiate in response to these events: |
1) Pressing the "Toolbar_btnSimpleWindow" button opens the SimpleWindow. | 1) Pressing the "Toolbar_btnSimpleWindow" button opens the SimpleWindow. | ||
2) Pressing the "SimpleWindow_btnClose" button performs an 'Ok', saving the data and closing the window. | 2) Pressing the "SimpleWindow_btnClose" button performs an 'Ok', saving the data and closing the window. | ||
Line 24: | Line 26: | ||
4) Pressing the 'escape' key closes the dialog without saving any modifications (cancel). | 4) Pressing the 'escape' key closes the dialog without saving any modifications (cancel). | ||
− | The SimpleWindow class then overrides the doLoad() virtual function to place the contents of a variable (dataString) into its associated Editbox. | + | The SimpleWindow class then overrides the doLoad() virtual function to place the contents of a variable (dataString) into its associated Editbox. It also overrides the doSave() virtual function to place the contents of the Editbox back into the variable. |
− | The SimpleDialog class replicates many of what the SimpleWindow performed. | + | The SimpleDialog class replicates many of what the SimpleWindow performed. The first difference is that it specifies the handle of a parent window. This signals the DialogSystem class that when the Simple Dialog is opened it should disable its parent, making the Simple Dialog modal; inputs to the parent are blocked while this dialog is opened. |
− | The second difference is the addition of an apply button. | + | The second difference is the addition of an apply button. This apply button requires that two events be specified. The event of clicking on the apply button "SimpleDialog_btnApply" is bound to the high level event of DialogSystem::apply, which will call upon doSave() to move the widget data into the variables. Text changes within the edit box "SimpleDialog_edtValue" are also bound to the high level event of DialogSystem::modified, which will enable the apply button whenever the contents of the edit box are modified. If there were additional widgets within the window then their 'modified' events would also need to be specified. |
− | CEGUI has more widgets than an Editbox. | + | CEGUI has more widgets than an Editbox. These can easily be incorporated within the logic presented here. |
+ | Please post comments or questions on the message board thread for the [http://www.cegui.org.uk/phpBB2/viewtopic.php?p=7966#7966 Dialog System] | ||
− | ==== | + | ==== DialogSystemDemo.h ==== |
− | < | + | <source lang="cpp"> |
− | #ifndef | + | #ifndef _DialogSystemDemo_h_ |
− | #define | + | #define _DialogSystemDemo_h_ |
#include "CEGuiSample.h" | #include "CEGuiSample.h" | ||
#include "CEGUI.h" | #include "CEGUI.h" | ||
#include "DemoUtils.h" | #include "DemoUtils.h" | ||
− | #include " | + | #include "DialogSystem.h" |
− | class SimpleWindow : public | + | class SimpleWindow : public DialogSystem |
{ | { | ||
public: | public: | ||
Line 52: | Line 55: | ||
// Initialise the windowing system | // Initialise the windowing system | ||
− | + | DialogSystem::initialise("winSimpleWindow", // The handle of this window | |
− | + | false); // Initially invisible | |
− | + | // A modeless window does not have a parent | |
// Subscribe to widget events | // Subscribe to widget events | ||
// Note that the "close" button is set to behave as an "ok" button | // Note that the "close" button is set to behave as an "ok" button | ||
− | + | DialogSystem::bindEvent( "Toolbar_btnSimpleWindow", PushButton::EventClicked, DialogSystem::open); | |
− | + | DialogSystem::bindEvent( "SimpleWindow_btnClose", PushButton::EventClicked, DialogSystem::ok); | |
// Subscribe to window events | // Subscribe to window events | ||
− | + | DialogSystem::bindEvent("winSimpleWindow", FrameWindow::EventCloseClicked, DialogSystem::cancel); // The 'X' button was clicked | |
− | + | DialogSystem::bindEvent("winSimpleWindow", FrameWindow::EventKeyDown, DialogSystem::escape); // The 'escape' key was pressed | |
} | } | ||
protected: | protected: | ||
Line 70: | Line 73: | ||
// Handle the load action by placing data into widgets | // Handle the load action by placing data into widgets | ||
CEGUI::WindowManager::getSingleton().getWindow("SimpleWindow_edtValue")->setText(dataString); | CEGUI::WindowManager::getSingleton().getWindow("SimpleWindow_edtValue")->setText(dataString); | ||
− | return | + | return DialogSystem::doLoad(); |
} | } | ||
bool doSave() | bool doSave() | ||
Line 76: | Line 79: | ||
// Handle the save action by moving widget data into variables | // Handle the save action by moving widget data into variables | ||
dataString = CEGUI::WindowManager::getSingleton().getWindow("SimpleWindow_edtValue")->getText(); | dataString = CEGUI::WindowManager::getSingleton().getWindow("SimpleWindow_edtValue")->getText(); | ||
− | return | + | return DialogSystem::doSave(); |
} | } | ||
private: | private: | ||
Line 84: | Line 87: | ||
////////////////////////////////////////////// | ////////////////////////////////////////////// | ||
− | class SimpleDialog : public | + | class SimpleDialog : public DialogSystem |
{ | { | ||
public: | public: | ||
Line 94: | Line 97: | ||
// Initialise the windowing system | // Initialise the windowing system | ||
− | + | DialogSystem::initialise("dlgSimpleDialog", // The handle of this window | |
− | + | false, // Initially invisible | |
− | + | "winToolbar"); // The handle of its parent, making it a modal dialog | |
// Subscribe to widget events | // Subscribe to widget events | ||
− | + | DialogSystem::bindEvent( "Toolbar_btnSimpleDialog", PushButton::EventClicked, DialogSystem::open); | |
− | + | DialogSystem::bindEvent( "SimpleDialog_btnOk", PushButton::EventClicked, DialogSystem::ok); | |
− | + | DialogSystem::bindEvent( "SimpleDialog_btnCancel", PushButton::EventClicked, DialogSystem::cancel); | |
− | + | DialogSystem::bindEvent( "SimpleDialog_btnApply", PushButton::EventClicked, DialogSystem::apply); | |
// These events trigger a 'modified' event, activating the 'apply' button | // These events trigger a 'modified' event, activating the 'apply' button | ||
− | + | DialogSystem::bindEvent( "SimpleDialog_edtValue", Editbox::EventTextChanged, DialogSystem::modified); | |
// Subscribe to window events | // Subscribe to window events | ||
// Pressing the 'X' button will behave as a cancel | // Pressing the 'X' button will behave as a cancel | ||
− | + | DialogSystem::bindEvent( "dlgSimpleDialog", FrameWindow::EventCloseClicked, DialogSystem::cancel); // The 'X' button was clicked | |
− | + | DialogSystem::bindEvent( "dlgSimpleDialog", FrameWindow::EventKeyDown, DialogSystem::escape); // The 'escape' key was pressed | |
} | } | ||
protected: | protected: | ||
Line 117: | Line 120: | ||
// Handle the load action by placing data into widgets | // Handle the load action by placing data into widgets | ||
CEGUI::WindowManager::getSingleton().getWindow("SimpleDialog_edtValue")->setText(dataString); | CEGUI::WindowManager::getSingleton().getWindow("SimpleDialog_edtValue")->setText(dataString); | ||
− | return | + | return DialogSystem::doLoad(); |
} | } | ||
bool doSave() | bool doSave() | ||
Line 123: | Line 126: | ||
// Handle the save action by moving widget data into variables | // Handle the save action by moving widget data into variables | ||
dataString = CEGUI::WindowManager::getSingleton().getWindow("SimpleDialog_edtValue")->getText(); | dataString = CEGUI::WindowManager::getSingleton().getWindow("SimpleDialog_edtValue")->getText(); | ||
− | return | + | return DialogSystem::doSave(); |
} | } | ||
private: | private: | ||
Line 139: | Line 142: | ||
try | try | ||
{ | { | ||
− | + | // Retrieve the window manager | |
+ | WindowManager& winMgr = WindowManager::getSingleton(); | ||
+ | |||
+ | // Load the TaharezLook scheme and set up the default mouse cursor and font | ||
+ | SchemeManager::getSingleton().loadScheme("TaharezLook.scheme"); | ||
+ | System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow"); | ||
+ | if(!FontManager::getSingleton().isFontPresent("Commonwealth-10")) | ||
+ | FontManager::getSingleton().createFont("Commonwealth-10.font"); | ||
+ | |||
+ | // Set the GUI Sheet | ||
+ | Window* sheet = winMgr.createWindow("DefaultWindow", "root_wnd"); | ||
+ | System::getSingleton().setGUISheet(sheet); | ||
+ | |||
+ | // Load a layout | ||
+ | Window* guiLayout = winMgr.loadWindowLayout("DialogSystem.layout"); | ||
+ | sheet->addChildWindow(guiLayout); | ||
+ | |||
// Initialise the Simple Window and the Simple Dialog | // Initialise the Simple Window and the Simple Dialog | ||
Line 161: | Line 180: | ||
}; | }; | ||
− | #endif // | + | #endif // _DialogSystemDemo_h_ |
− | </ | + | </source> |
− | + | ==== DialogSystem.h ==== | |
− | ==== | + | <source lang="cpp"> |
− | < | + | #ifndef _DialogSystem_h_ |
− | #ifndef | + | #define _DialogSystem_h_ |
− | #define | + | |
#include "CEGUI.h" | #include "CEGUI.h" | ||
#include "vector" | #include "vector" | ||
− | class | + | class DialogSystem |
{ | { | ||
public: | public: | ||
− | + | DialogSystem(); | |
− | enum | + | enum DialogSystemEvents { open, ok, cancel, escape, apply, modified }; |
bool isModified(); // Return whether data within the window is modified | bool isModified(); // Return whether data within the window is modified | ||
Line 186: | Line 204: | ||
virtual bool doClose(); // Close the window | virtual bool doClose(); // Close the window | ||
void initialise(const CEGUI::String& window, bool visible, const CEGUI::String& parent = ""); // Initialise the window system | void initialise(const CEGUI::String& window, bool visible, const CEGUI::String& parent = ""); // Initialise the window system | ||
− | void bindEvent(const CEGUI::String& widget, const CEGUI::String& event, | + | void bindEvent(const CEGUI::String& widget, const CEGUI::String& event, DialogSystem::DialogSystemEvents action); // Subscribe to window events |
private: | private: | ||
// Events | // Events | ||
Line 200: | Line 218: | ||
CEGUI::String m_apply; // Handle of the apply button | CEGUI::String m_apply; // Handle of the apply button | ||
bool m_modal; // Whether the window is modal | bool m_modal; // Whether the window is modal | ||
− | |||
}; | }; | ||
− | #endif // | + | #endif // _DialogSystem_h_ |
− | </ | + | </source> |
− | + | ||
− | ==== | + | ==== DialogSystem.cpp ==== |
− | < | + | <source lang="cpp"> |
− | #include " | + | #include "DialogSystem.h" |
#include "assert.h" | #include "assert.h" | ||
− | + | DialogSystem::DialogSystem() | |
{ | { | ||
m_modal = false; | m_modal = false; | ||
} | } | ||
− | bool | + | bool DialogSystem::isModified() |
{ | { | ||
// Return whether data within the window is modified | // Return whether data within the window is modified | ||
Line 224: | Line 240: | ||
} | } | ||
− | bool | + | bool DialogSystem::doOpen() |
{ | { | ||
// Open the window | // Open the window | ||
Line 242: | Line 258: | ||
} | } | ||
− | bool | + | bool DialogSystem::doLoad() |
{ | { | ||
// Populate the window widgets with data | // Populate the window widgets with data | ||
Line 254: | Line 270: | ||
} | } | ||
− | bool | + | bool DialogSystem::doSave() |
{ | { | ||
// Update the data with the inputs from the widgets | // Update the data with the inputs from the widgets | ||
Line 265: | Line 281: | ||
} | } | ||
− | bool | + | bool DialogSystem::doClose() |
{ | { | ||
// Close the window | // Close the window | ||
Line 281: | Line 297: | ||
} | } | ||
− | void | + | void DialogSystem::initialise(const CEGUI::String& window, bool visible, const CEGUI::String& parent) |
{ | { | ||
// Initialise the window system | // Initialise the window system | ||
Line 292: | Line 308: | ||
− | void | + | void DialogSystem::bindEvent(const CEGUI::String& widget, const CEGUI::String& widgetEvent, DialogSystemEvents action) |
{ | { | ||
// Subscribe to events | // Subscribe to events | ||
Line 299: | Line 315: | ||
{ | { | ||
case open: | case open: | ||
− | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(& | + | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onOpen, this)); |
break; | break; | ||
case ok: | case ok: | ||
− | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(& | + | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onOk, this)); |
break; | break; | ||
case cancel: | case cancel: | ||
− | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(& | + | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onCancel, this)); |
break; | break; | ||
case escape: | case escape: | ||
− | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(& | + | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onEscape, this)); |
break; | break; | ||
case apply: | case apply: | ||
− | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(& | + | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onApply, this)); |
m_apply = widget; | m_apply = widget; | ||
break; | break; | ||
case modified: | case modified: | ||
if(!m_apply.empty()) | if(!m_apply.empty()) | ||
− | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(& | + | widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onModified, this)); |
break; | break; | ||
} | } | ||
} | } | ||
− | bool | + | bool DialogSystem::onOpen(const CEGUI::EventArgs& e) |
{ | { | ||
// Open the window | // Open the window | ||
Line 327: | Line 343: | ||
} | } | ||
− | bool | + | bool DialogSystem::onOk(const CEGUI::EventArgs& e) |
{ | { | ||
// The 'ok' button was pressed | // The 'ok' button was pressed | ||
Line 334: | Line 350: | ||
} | } | ||
− | bool | + | bool DialogSystem::onCancel(const CEGUI::EventArgs& e) |
{ | { | ||
// The 'cancel' button was pressed | // The 'cancel' button was pressed | ||
Line 341: | Line 357: | ||
} | } | ||
− | bool | + | bool DialogSystem::onEscape(const CEGUI::EventArgs& e) |
{ | { | ||
// The 'escape' key was pressed | // The 'escape' key was pressed | ||
Line 356: | Line 372: | ||
} | } | ||
− | bool | + | bool DialogSystem::onApply(const CEGUI::EventArgs& e) |
{ | { | ||
// The 'apply' button was pressed | // The 'apply' button was pressed | ||
Line 363: | Line 379: | ||
} | } | ||
− | bool | + | bool DialogSystem::onModified(const CEGUI::EventArgs& e) |
{ | { | ||
// A widget within the window was modified | // A widget within the window was modified | ||
Line 370: | Line 386: | ||
return true; | return true; | ||
} | } | ||
− | </ | + | </source> |
− | ==== | + | ==== DialogSystem.layout ==== |
− | < | + | <source lang="xml"> |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | <GUILayout > | ||
+ | <Window Type="DefaultWindow" Name="Root" > | ||
+ | <Property Name="InheritsAlpha" Value="False" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0,0},{0,0},{1,0},{1,0}}" /> | ||
+ | <Window Type="TaharezLook/FrameWindow" Name="winToolbar" > | ||
+ | <Property Name="Text" Value="Toolbar" /> | ||
+ | <Property Name="AlwaysOnTop" Value="True" /> | ||
+ | <Property Name="TitlebarFont" Value="Commonwealth-10" /> | ||
+ | <Property Name="CaptionColour" Value="00FFFFFF" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="TitlebarEnabled" Value="False" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.24875,0},{0.010001,0},{0.568749,0},{0.081667,0}}" /> | ||
+ | <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseTarget" /> | ||
+ | <Property Name="CloseButtonEnabled" Value="False" /> | ||
+ | <Window Type="TaharezLook/Button" Name="Toolbar_btnSimpleWindow" > | ||
+ | <Property Name="Text" Value="Simple Window" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.065103,0},{0.227595,0},{0.475554,0},{0.686899,0}}" /> | ||
+ | <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> | ||
+ | </Window> | ||
+ | <Window Type="TaharezLook/Button" Name="Toolbar_btnSimpleDialog" > | ||
+ | <Property Name="Text" Value="Simple Dialog" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.538253,0},{0.227595,0},{0.948703,0},{0.686899,0}}" /> | ||
+ | <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> | ||
+ | </Window> | ||
+ | </Window> | ||
+ | <Window Type="TaharezLook/FrameWindow" Name="winSimpleWindow" > | ||
+ | <Property Name="Text" Value="Simple Window" /> | ||
+ | <Property Name="TitlebarFont" Value="Commonwealth-10" /> | ||
+ | <Property Name="CaptionColour" Value="00FFFFFF" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="TitlebarEnabled" Value="True" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.005,0},{0.168333,0},{0.42,0},{0.511666,0}}" /> | ||
+ | <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseTarget" /> | ||
+ | <Window Type="TaharezLook/Button" Name="SimpleWindow_btnClose" > | ||
+ | <Property Name="Text" Value="Close" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.688554,0},{0.804855,0},{0.938554,0},{0.943204,0}}" /> | ||
+ | <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> | ||
+ | </Window> | ||
+ | <Window Type="TaharezLook/StaticText" Name="SimpleWindow_lblValue" > | ||
+ | <Property Name="Font" Value="Commonwealth-10" /> | ||
+ | <Property Name="Text" Value="Value:" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.455591,0},{0.430746,0},{0.606068,0},{0.569095,0}}" /> | ||
+ | </Window> | ||
+ | <Window Type="TaharezLook/Editbox" Name="SimpleWindow_edtValue" > | ||
+ | <Property Name="Font" Value="Commonwealth-10" /> | ||
+ | <Property Name="MaxTextLength" Value="1073741823" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.618241,0},{0.430746,0},{0.970525,0},{0.569095,0}}" /> | ||
+ | </Window> | ||
+ | </Window> | ||
+ | <Window Type="TaharezLook/FrameWindow" Name="dlgSimpleDialog" > | ||
+ | <Property Name="Text" Value="Simple Dialog" /> | ||
+ | <Property Name="TitlebarFont" Value="Commonwealth-10" /> | ||
+ | <Property Name="CaptionColour" Value="00FFFFFF" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="TitlebarEnabled" Value="True" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.43625,0},{0.165,0},{0.916249,0},{0.508333,0}}" /> | ||
+ | <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseTarget" /> | ||
+ | <Window Type="TaharezLook/Button" Name="SimpleDialog_btnOk" > | ||
+ | <Property Name="Text" Value="Ok" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.173754,0},{0.804855,0},{0.423754,0},{0.943204,0}}" /> | ||
+ | <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> | ||
+ | </Window> | ||
+ | <Window Type="TaharezLook/Button" Name="SimpleDialog_btnCancel" > | ||
+ | <Property Name="Text" Value="Cancel" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.451828,0},{0.804855,0},{0.701828,0},{0.943204,0}}" /> | ||
+ | <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> | ||
+ | </Window> | ||
+ | <Window Type="TaharezLook/Button" Name="SimpleDialog_btnApply" > | ||
+ | <Property Name="Text" Value="Apply" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.73123,0},{0.804855,0},{0.98123,0},{0.943204,0}}" /> | ||
+ | <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> | ||
+ | </Window> | ||
+ | <Window Type="TaharezLook/Editbox" Name="SimpleDialog_edtValue" > | ||
+ | <Property Name="Font" Value="Commonwealth-10" /> | ||
+ | <Property Name="MaxTextLength" Value="1073741823" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.626093,0},{0.418446,0},{0.971449,0},{0.556795,0}}" /> | ||
+ | </Window> | ||
+ | <Window Type="TaharezLook/StaticText" Name="SimpleDialog_lblValue" > | ||
+ | <Property Name="Text" Value="Value:" /> | ||
+ | <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> | ||
+ | <Property Name="UnifiedAreaRect" Value="{{0.482291,0},{0.418446,0},{0.617709,0},{0.556795,0}}" /> | ||
+ | </Window> | ||
+ | </Window> | ||
+ | </Window> | ||
+ | </GUILayout> | ||
+ | </source> | ||
==== main.cpp ==== | ==== main.cpp ==== | ||
− | < | + | <source lang="cpp"> |
#if defined( __WIN32__ ) || defined( _WIN32 ) | #if defined( __WIN32__ ) || defined( _WIN32 ) | ||
#define WIN32_LEAN_AND_MEAN | #define WIN32_LEAN_AND_MEAN | ||
Line 513: | Line 498: | ||
#endif | #endif | ||
− | #include " | + | #include "DialogSystemDemo.h" |
Line 525: | Line 510: | ||
return app.run(); | return app.run(); | ||
} | } | ||
− | </ | + | </source> |
+ | |||
+ | [[Category:HowTo]] |
Latest revision as of 15:39, 28 February 2011
Written for CEGUI 0.5
Works with versions 0.5.x (obsolete)
Written for CEGUI 0.6
Works with versions 0.6.x (obsolete)
The default CEGUI implementation stores the currently specified values of the widgets such that closing and then reopening a window will redisplay those values. Presented here is a different approach, to allow values to be saved or cancelled.
Contents
Explanation
The DemoSample class is the "main" application. It initializes CEGUI and loads a simple layout. It then initialises instances of the SimpleWindow and the SimpleDialog classes. These contain the specific code to handle the behavior of the Simple Window and the Simple Dialog. In turn they rely on the behaviors define within the DialogSystem class to activate higher level features.
The DialogSystem class implements the behavior of the 'Ok', 'Cancel', and 'Apply' buttons that we've become accustomed to in the world of Windows. The DialogSystem::DialogSystemEvents enumaration defines the high level events supported by the DialogSystem class:
open: display the window and its data ok: save the data and close the window cancel: close the window without saving the data escape: close the window without saving the data apply: save the data without closing the window modified: enable the 'apply' button
These high level events accomplish their jobs through the use of four basic actions:
doOpen(): display the window doLoad(): copy the value of variables into their associated widgets doSave(): copy the value of the widgets into their associated variables doClose(): hide the window
The SimpleWindow class will manage everything that has to do with the Simple Window. The initWindow() function initialises the DialogSystem by specifying the handle of the window/dialog (the name specified within the layout file) and whether it should initially be visible or not. Then it instructs the DialogSystem to handle four events as well as which action to initiate in response to these events: 1) Pressing the "Toolbar_btnSimpleWindow" button opens the SimpleWindow. 2) Pressing the "SimpleWindow_btnClose" button performs an 'Ok', saving the data and closing the window. 3) Clicking on the 'X' button closes the dialog without saving any modifications (cancel). 4) Pressing the 'escape' key closes the dialog without saving any modifications (cancel).
The SimpleWindow class then overrides the doLoad() virtual function to place the contents of a variable (dataString) into its associated Editbox. It also overrides the doSave() virtual function to place the contents of the Editbox back into the variable.
The SimpleDialog class replicates many of what the SimpleWindow performed. The first difference is that it specifies the handle of a parent window. This signals the DialogSystem class that when the Simple Dialog is opened it should disable its parent, making the Simple Dialog modal; inputs to the parent are blocked while this dialog is opened.
The second difference is the addition of an apply button. This apply button requires that two events be specified. The event of clicking on the apply button "SimpleDialog_btnApply" is bound to the high level event of DialogSystem::apply, which will call upon doSave() to move the widget data into the variables. Text changes within the edit box "SimpleDialog_edtValue" are also bound to the high level event of DialogSystem::modified, which will enable the apply button whenever the contents of the edit box are modified. If there were additional widgets within the window then their 'modified' events would also need to be specified.
CEGUI has more widgets than an Editbox. These can easily be incorporated within the logic presented here.
Please post comments or questions on the message board thread for the Dialog System
DialogSystemDemo.h
#ifndef _DialogSystemDemo_h_ #define _DialogSystemDemo_h_ #include "CEGuiSample.h" #include "CEGUI.h" #include "DemoUtils.h" #include "DialogSystem.h" class SimpleWindow : public DialogSystem { public: void initWindow() { // Initialise the window using namespace CEGUI; // Initialise the windowing system DialogSystem::initialise("winSimpleWindow", // The handle of this window false); // Initially invisible // A modeless window does not have a parent // Subscribe to widget events // Note that the "close" button is set to behave as an "ok" button DialogSystem::bindEvent( "Toolbar_btnSimpleWindow", PushButton::EventClicked, DialogSystem::open); DialogSystem::bindEvent( "SimpleWindow_btnClose", PushButton::EventClicked, DialogSystem::ok); // Subscribe to window events DialogSystem::bindEvent("winSimpleWindow", FrameWindow::EventCloseClicked, DialogSystem::cancel); // The 'X' button was clicked DialogSystem::bindEvent("winSimpleWindow", FrameWindow::EventKeyDown, DialogSystem::escape); // The 'escape' key was pressed } protected: bool doLoad() { // Handle the load action by placing data into widgets CEGUI::WindowManager::getSingleton().getWindow("SimpleWindow_edtValue")->setText(dataString); return DialogSystem::doLoad(); } bool doSave() { // Handle the save action by moving widget data into variables dataString = CEGUI::WindowManager::getSingleton().getWindow("SimpleWindow_edtValue")->getText(); return DialogSystem::doSave(); } private: CEGUI::String dataString; // Variable associated with the Editbox }; ////////////////////////////////////////////// class SimpleDialog : public DialogSystem { public: void initWindow() { // Initialise the dialog using namespace CEGUI; WindowManager& winMgr = WindowManager::getSingleton(); // Initialise the windowing system DialogSystem::initialise("dlgSimpleDialog", // The handle of this window false, // Initially invisible "winToolbar"); // The handle of its parent, making it a modal dialog // Subscribe to widget events DialogSystem::bindEvent( "Toolbar_btnSimpleDialog", PushButton::EventClicked, DialogSystem::open); DialogSystem::bindEvent( "SimpleDialog_btnOk", PushButton::EventClicked, DialogSystem::ok); DialogSystem::bindEvent( "SimpleDialog_btnCancel", PushButton::EventClicked, DialogSystem::cancel); DialogSystem::bindEvent( "SimpleDialog_btnApply", PushButton::EventClicked, DialogSystem::apply); // These events trigger a 'modified' event, activating the 'apply' button DialogSystem::bindEvent( "SimpleDialog_edtValue", Editbox::EventTextChanged, DialogSystem::modified); // Subscribe to window events // Pressing the 'X' button will behave as a cancel DialogSystem::bindEvent( "dlgSimpleDialog", FrameWindow::EventCloseClicked, DialogSystem::cancel); // The 'X' button was clicked DialogSystem::bindEvent( "dlgSimpleDialog", FrameWindow::EventKeyDown, DialogSystem::escape); // The 'escape' key was pressed } protected: bool doLoad() { // Handle the load action by placing data into widgets CEGUI::WindowManager::getSingleton().getWindow("SimpleDialog_edtValue")->setText(dataString); return DialogSystem::doLoad(); } bool doSave() { // Handle the save action by moving widget data into variables dataString = CEGUI::WindowManager::getSingleton().getWindow("SimpleDialog_edtValue")->getText(); return DialogSystem::doSave(); } private: CEGUI::String dataString; }; ////////////////////////////////////////// class DemoSample : public CEGuiSample { public: bool initialiseSample() { using namespace CEGUI; try { // Retrieve the window manager WindowManager& winMgr = WindowManager::getSingleton(); // Load the TaharezLook scheme and set up the default mouse cursor and font SchemeManager::getSingleton().loadScheme("TaharezLook.scheme"); System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow"); if(!FontManager::getSingleton().isFontPresent("Commonwealth-10")) FontManager::getSingleton().createFont("Commonwealth-10.font"); // Set the GUI Sheet Window* sheet = winMgr.createWindow("DefaultWindow", "root_wnd"); System::getSingleton().setGUISheet(sheet); // Load a layout Window* guiLayout = winMgr.loadWindowLayout("DialogSystem.layout"); sheet->addChildWindow(guiLayout); // Initialise the Simple Window and the Simple Dialog simpleWindow.initWindow(); simpleDialog.initWindow(); } catch(Exception &e) { ErrorMessage(e.getMessage().c_str(), "Error initializing the demo"); } return true; } void cleanupSample(void) { } private: SimpleWindow simpleWindow; SimpleDialog simpleDialog; }; #endif // _DialogSystemDemo_h_
DialogSystem.h
#ifndef _DialogSystem_h_ #define _DialogSystem_h_ #include "CEGUI.h" #include "vector" class DialogSystem { public: DialogSystem(); enum DialogSystemEvents { open, ok, cancel, escape, apply, modified }; bool isModified(); // Return whether data within the window is modified // Actions virtual bool doOpen(); // Open the window virtual bool doLoad(); // Assign the data into the widgets virtual bool doSave(); // Assign the widgets into the data virtual bool doClose(); // Close the window void initialise(const CEGUI::String& window, bool visible, const CEGUI::String& parent = ""); // Initialise the window system void bindEvent(const CEGUI::String& widget, const CEGUI::String& event, DialogSystem::DialogSystemEvents action); // Subscribe to window events private: // Events bool onOpen(const CEGUI::EventArgs& e); // Open the window bool onOk(const CEGUI::EventArgs& e); // Save the data and close the window bool onCancel(const CEGUI::EventArgs& e); // Close the window bool onEscape(const CEGUI::EventArgs& e); // Close the window bool onApply(const CEGUI::EventArgs& e); // Save the data bool onModified(const CEGUI::EventArgs& e); // A widget in the window has been modified CEGUI::String m_parent; // Handle to the parent CEGUI::String m_window; // Handle to the window CEGUI::String m_apply; // Handle of the apply button bool m_modal; // Whether the window is modal }; #endif // _DialogSystem_h_
DialogSystem.cpp
#include "DialogSystem.h" #include "assert.h" DialogSystem::DialogSystem() { m_modal = false; } bool DialogSystem::isModified() { // Return whether data within the window is modified assert(!m_apply.empty() && "The isModified() function requires that you specify an \"Apply\" button"); return !CEGUI::WindowManager::getSingleton().getWindow(m_apply)->isDisabled(); } bool DialogSystem::doOpen() { // Open the window assert(!m_window.empty() && "You have forgotten to call initialise()"); if(m_modal) { // Displaying a modal window disables its parent assert(!m_parent.empty() && "The value of m_modal or m_parent has become corrupted"); CEGUI::WindowManager::getSingleton().getWindow(m_parent)->setEnabled(false); } // Display the window CEGUI::WindowManager::getSingleton().getWindow(m_window)->setVisible(true); // Load the data into the widgets return doLoad(); } bool DialogSystem::doLoad() { // Populate the window widgets with data // Note that this can also be used to simulate an undo for every widget // present in the window // Disable the apply button since there are no modifications if(!m_apply.empty()) CEGUI::WindowManager::getSingleton().getWindow(m_apply)->setEnabled(false); return true; } bool DialogSystem::doSave() { // Update the data with the inputs from the widgets // Disable the apply button since there are no modifications if(!m_apply.empty()) CEGUI::WindowManager::getSingleton().getWindow(m_apply)->setEnabled(false); return true; } bool DialogSystem::doClose() { // Close the window assert(!m_window.empty() && "You have forgotten to call initialise()"); if(m_modal) { // Closing a modal window enables its parent assert(!m_parent.empty() && "The value of m_modal or m_parent has become corrupted"); CEGUI::WindowManager::getSingleton().getWindow(m_parent)->setEnabled(true); } CEGUI::WindowManager::getSingleton().getWindow(m_window)->setVisible(false); return true; } void DialogSystem::initialise(const CEGUI::String& window, bool visible, const CEGUI::String& parent) { // Initialise the window system // Specifying a parent makes this window modal m_window = window; CEGUI::WindowManager::getSingleton().getWindow(m_window)->setVisible(visible); m_parent = parent; m_modal = !m_parent.empty(); } void DialogSystem::bindEvent(const CEGUI::String& widget, const CEGUI::String& widgetEvent, DialogSystemEvents action) { // Subscribe to events CEGUI::Window* widgetHandle = CEGUI::WindowManager::getSingleton().getWindow(widget); switch(action) { case open: widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onOpen, this)); break; case ok: widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onOk, this)); break; case cancel: widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onCancel, this)); break; case escape: widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onEscape, this)); break; case apply: widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onApply, this)); m_apply = widget; break; case modified: if(!m_apply.empty()) widgetHandle->subscribeEvent(widgetEvent, CEGUI::Event::Subscriber(&DialogSystem::onModified, this)); break; } } bool DialogSystem::onOpen(const CEGUI::EventArgs& e) { // Open the window return doOpen(); } bool DialogSystem::onOk(const CEGUI::EventArgs& e) { // The 'ok' button was pressed // Respond by saving the data and closing the window return doSave() && doClose(); } bool DialogSystem::onCancel(const CEGUI::EventArgs& e) { // The 'cancel' button was pressed // Respond by closing the window without saving the data return doClose(); } bool DialogSystem::onEscape(const CEGUI::EventArgs& e) { // The 'escape' key was pressed // Respond by closing the dialog without saving the data // Note that Win32AppHelper::doDirectInputEvents() intercepts this key // This means that the escape key will NOT reach here const CEGUI::KeyEventArgs& keyArgs = static_cast<const CEGUI::KeyEventArgs&>(e); if(keyArgs.scancode == CEGUI::Key::Escape) { return doClose(); } else return false; } bool DialogSystem::onApply(const CEGUI::EventArgs& e) { // The 'apply' button was pressed // Respond by saving the data without closing the window return doSave(); } bool DialogSystem::onModified(const CEGUI::EventArgs& e) { // A widget within the window was modified // Respond by enabling the 'apply' button CEGUI::WindowManager::getSingleton().getWindow(m_apply)->setEnabled(true); return true; }
DialogSystem.layout
<?xml version="1.0" encoding="UTF-8"?> <GUILayout > <Window Type="DefaultWindow" Name="Root" > <Property Name="InheritsAlpha" Value="False" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0,0},{0,0},{1,0},{1,0}}" /> <Window Type="TaharezLook/FrameWindow" Name="winToolbar" > <Property Name="Text" Value="Toolbar" /> <Property Name="AlwaysOnTop" Value="True" /> <Property Name="TitlebarFont" Value="Commonwealth-10" /> <Property Name="CaptionColour" Value="00FFFFFF" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="TitlebarEnabled" Value="False" /> <Property Name="UnifiedAreaRect" Value="{{0.24875,0},{0.010001,0},{0.568749,0},{0.081667,0}}" /> <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseTarget" /> <Property Name="CloseButtonEnabled" Value="False" /> <Window Type="TaharezLook/Button" Name="Toolbar_btnSimpleWindow" > <Property Name="Text" Value="Simple Window" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.065103,0},{0.227595,0},{0.475554,0},{0.686899,0}}" /> <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> </Window> <Window Type="TaharezLook/Button" Name="Toolbar_btnSimpleDialog" > <Property Name="Text" Value="Simple Dialog" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.538253,0},{0.227595,0},{0.948703,0},{0.686899,0}}" /> <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> </Window> </Window> <Window Type="TaharezLook/FrameWindow" Name="winSimpleWindow" > <Property Name="Text" Value="Simple Window" /> <Property Name="TitlebarFont" Value="Commonwealth-10" /> <Property Name="CaptionColour" Value="00FFFFFF" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="TitlebarEnabled" Value="True" /> <Property Name="UnifiedAreaRect" Value="{{0.005,0},{0.168333,0},{0.42,0},{0.511666,0}}" /> <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseTarget" /> <Window Type="TaharezLook/Button" Name="SimpleWindow_btnClose" > <Property Name="Text" Value="Close" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.688554,0},{0.804855,0},{0.938554,0},{0.943204,0}}" /> <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> </Window> <Window Type="TaharezLook/StaticText" Name="SimpleWindow_lblValue" > <Property Name="Font" Value="Commonwealth-10" /> <Property Name="Text" Value="Value:" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.455591,0},{0.430746,0},{0.606068,0},{0.569095,0}}" /> </Window> <Window Type="TaharezLook/Editbox" Name="SimpleWindow_edtValue" > <Property Name="Font" Value="Commonwealth-10" /> <Property Name="MaxTextLength" Value="1073741823" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.618241,0},{0.430746,0},{0.970525,0},{0.569095,0}}" /> </Window> </Window> <Window Type="TaharezLook/FrameWindow" Name="dlgSimpleDialog" > <Property Name="Text" Value="Simple Dialog" /> <Property Name="TitlebarFont" Value="Commonwealth-10" /> <Property Name="CaptionColour" Value="00FFFFFF" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="TitlebarEnabled" Value="True" /> <Property Name="UnifiedAreaRect" Value="{{0.43625,0},{0.165,0},{0.916249,0},{0.508333,0}}" /> <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseTarget" /> <Window Type="TaharezLook/Button" Name="SimpleDialog_btnOk" > <Property Name="Text" Value="Ok" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.173754,0},{0.804855,0},{0.423754,0},{0.943204,0}}" /> <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> </Window> <Window Type="TaharezLook/Button" Name="SimpleDialog_btnCancel" > <Property Name="Text" Value="Cancel" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.451828,0},{0.804855,0},{0.701828,0},{0.943204,0}}" /> <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> </Window> <Window Type="TaharezLook/Button" Name="SimpleDialog_btnApply" > <Property Name="Text" Value="Apply" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.73123,0},{0.804855,0},{0.98123,0},{0.943204,0}}" /> <Property Name="MouseCursorImage" Value="set:TaharezLook image:MouseArrow" /> </Window> <Window Type="TaharezLook/Editbox" Name="SimpleDialog_edtValue" > <Property Name="Font" Value="Commonwealth-10" /> <Property Name="MaxTextLength" Value="1073741823" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.626093,0},{0.418446,0},{0.971449,0},{0.556795,0}}" /> </Window> <Window Type="TaharezLook/StaticText" Name="SimpleDialog_lblValue" > <Property Name="Text" Value="Value:" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.482291,0},{0.418446,0},{0.617709,0},{0.556795,0}}" /> </Window> </Window> </Window> </GUILayout>
main.cpp
#if defined( __WIN32__ ) || defined( _WIN32 ) #define WIN32_LEAN_AND_MEAN #define NOMINMAX #include "windows.h" #endif #include "DialogSystemDemo.h" #if defined( __WIN32__ ) || defined( _WIN32 ) int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) #else int main(int argc, char *argv[]) #endif { DemoSample app; return app.run(); }