Difference between revisions of "Dialog Configurations"
(→DialogPositions.cpp) |
(→DialogPositions.xsd) |
||
Line 273: | Line 273: | ||
</pre> | </pre> | ||
− | ==== | + | ==== DialogConfigurations.xsd ==== |
<pre> | <pre> | ||
<?xml version="1.0"?> | <?xml version="1.0"?> | ||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> | <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> | ||
− | <xsd:element name=" | + | <xsd:element name="DialogConfiguration" type="DialogConfigurationType"/> |
− | <xsd:complexType name=" | + | <xsd:complexType name="DialogConfigurationType"> |
<xsd:sequence> | <xsd:sequence> | ||
− | <xsd:element name=" | + | <xsd:element name="Configuration" type="ConfigurationType" maxOccurs="unbounded"/> |
</xsd:sequence> | </xsd:sequence> | ||
</xsd:complexType> | </xsd:complexType> | ||
− | <xsd:complexType name=" | + | <xsd:complexType name="ConfigurationType"> |
− | <xsd:attribute name="Name" type="xsd:string" use="required"/> | + | <xsd:attribute name="Name" type="xsd:string" use="required"/> |
− | <xsd:attribute name="XPos" type="xsd:decimal" use="required"/> | + | <xsd:attribute name="XPos" type="xsd:decimal" use="required"/> |
− | <xsd:attribute name="YPos" type="xsd:decimal" use="required"/> | + | <xsd:attribute name="YPos" type="xsd:decimal" use="required"/> |
+ | <xsd:attribute name="Width" type="xsd:decimal" use="required"/> | ||
+ | <xsd:attribute name="Height" type="xsd:decimal" use="required"/> | ||
+ | <xsd:attribute name="Visible" type="xsd:boolean" use="required"/> | ||
+ | <xsd:attribute name="RolledUp" type="xsd:boolean" use="required"/> | ||
</xsd:complexType> | </xsd:complexType> | ||
</xsd:schema> | </xsd:schema> | ||
</pre> | </pre> |
Revision as of 00:33, 11 May 2006
Contents
Introduction
Dialogs (frame windows) can be moved around, resized, hidden, and rolled up while the application is running. However closing and then restarting the application will reset these settings to their default values, possibly those specified within a .layout file. This code saves the dialog position, size, visibility and whether the dialog is rolled up into an XML file. When the application restarts the contents of the XML file will be read and the dialog will be restored to its last state.
Sample_Demo7
This demo contains 3 dialogs. Let's store their configurations.
Edit Sample_Demo7.h:
- add #include "DialogConfigurations.h"
- add within the protected section: DialogConfigurations m_dialogConfigurations;
Edit Sample_Demo7.cpp to add the following at the end of the Demo7Sample::initialiseSample() function, between initDemoEventWiring(); and return true;:
m_dialogConfigurations.loadConfigurations("../datafiles/configs/Sample_Demo7.xml", "../datafiles/configs/DialogConfigurations.xsd"); m_dialogConfigurations.addDialog("Demo7/Window1"); m_dialogConfigurations.addDialog("Demo7/Window2"); m_dialogConfigurations.addDialog("Demo7/Window3");
Add the following to Demo7Sample::cleanupSample()
m_dialogConfigurations.saveConfigurations();
That's it! From now on the 3 windows within that demo will "remember" their previous configurations.
XML Position File
The call to loadPositions() takes two parameters: the XML file to load and the schema definition. If the XML file does not exist an exception will be thrown. This can be avoided by creating an empty XML file; the exception is thrown only when the file does not exist, not when it is empty. Then you can either manually write the XML contents yourself or use a little trick to have it written automatically.
Calling addDialog() and passing the window name as parameter will register that dialog to have its configuration written to the XML file. The next call to saveConfigurations() will write the configuration of that registered window such that the next time loadConfigurations() is called the window will be automatically registered.
Warning: the Demo7Sample::cleanupSample() is never called when exiting the demo, despite what the comments might say. A solution is presented on the message board.
Please discuss this snippet in the Storing Dialog Configurations thread on the message board.
Files
DialogConfigurations.h
#pragma once #include "CEGUI.h" #include "CEGUIXMLHandler.h" #include "CEGUIXMLParser.h" #include "CEGUIXMLAttributes.h" #include <map> class DialogConfigurations : public CEGUI::XMLHandler { public: DialogConfigurations(); void setDefaults(const CEGUI::Point& position, const CEGUI::Size& size, const bool& visible, const bool& rolledup); // Set default values bool loadConfigurations(const CEGUI::String& xmlFile, const CEGUI::String& schema); // Load the position of every stored dialog bool saveConfigurations(); // Store the position of every dialogs bool addDialog(const CEGUI::String& dialog); // Add a dialog to be monitored const CEGUI::Point& getSavedPosition(const CEGUI::String& dialog); // Return the position of the specified dialog const CEGUI::Size& getSavedSize(const CEGUI::String& dialog); // Return the size of the specified dialog const bool& getSavedVisible(const CEGUI::String& dialog); // Return the visible/hidden state of the specified dialog const bool& getSavedRolledup(const CEGUI::String& dialog); // Return the expansion state of the specified dialog private: void elementStart(const CEGUI::String& element, const CEGUI::XMLAttributes& attributes); static const CEGUI::String m_DialogConfigurationElement; // Tag name for dialog positions element static const CEGUI::String m_configurationElement; // Tag name for position elements static const char windowNameAttribute[]; // Attribute name that stores the name of the window static const char windowXPosAttribute[]; // Attribute name that stores the X position of the window static const char windowYPosAttribute[]; // Attribute name that stores the X position of the window static const char windowWidthAttribute[]; // Attribute name that stores the width of the window static const char windowHeightAttribute[]; // Attribute name that stores the height of the window static const char windowVisibleAttribute[]; // Attribute name that stores the visible state of the window static const char windowRolledupAttribute[]; // Attribute name that stores the rolledUp state of the window struct DialogConfig { CEGUI::Point position; CEGUI::Size size; bool visible; bool rolledup; }; std::map<CEGUI::String, DialogConfig> m_dialogConfigurations; // List of the dialog configurations CEGUI::String m_configFile; // File containing the positions CEGUI::Point m_defaultPosition; // Default position, used by getSavedPosition() CEGUI::Size m_defaultSize; // Default size, used by getSavedSize() bool m_defaultVisible; // Default visible/hidden, used by getSavedVisible() bool m_defaultRolledup; // Default rolled up/expanded, used by getSavedRolledup() };
DialogConfigurations.cpp
#include "DialogConfigurations.h" //Definition of XML elements and attributes const CEGUI::String DialogConfigurations::m_DialogConfigurationElement( "DialogConfigurations" ); const CEGUI::String DialogConfigurations::m_configurationElement( "Configuration" ); const char DialogConfigurations::windowNameAttribute[] = "Name"; const char DialogConfigurations::windowXPosAttribute[] = "XPos"; const char DialogConfigurations::windowYPosAttribute[] = "YPos"; const char DialogConfigurations::windowWidthAttribute[] = "Width"; const char DialogConfigurations::windowHeightAttribute[] = "Height"; const char DialogConfigurations::windowVisibleAttribute[] = "Visible"; const char DialogConfigurations::windowRolledupAttribute[] = "RolledUp"; DialogConfigurations::DialogConfigurations() { setDefaults(CEGUI::Point(0.0f, 0.0f), CEGUI::Size(0.2f, 0.2f), true, true); } void DialogConfigurations::setDefaults(const CEGUI::Point& position, const CEGUI::Size& size, const bool& visible, const bool& rolledup) { // Set default values // Should validate what's being passed as parameters m_defaultPosition = position; m_defaultSize = size; m_defaultVisible = visible; m_defaultRolledup = rolledup; } bool DialogConfigurations::loadConfigurations(const CEGUI::String& xmlFile, const CEGUI::String& schema) { // Load the config of every stored dialog assert(!xmlFile.empty() && "You must specify an xml file to loadConfigurations()"); m_configFile = xmlFile; CEGUI::System::getSingleton().getXMLParser()->parseXMLFile(*this, m_configFile, schema, ""); return true; } bool DialogConfigurations::saveConfigurations() { // Store the configuration of every registered dialog, either within // the loaded XML file or those manually registered via addDialog() assert(!m_configFile.empty() && "You must specify an xml file by calling loadConfigurations() before saveConfigurations()"); // Try to open the XML file in writing mode std::ofstream fileSave; fileSave.open(m_configFile.c_str(), std::ios::out) ; if( !fileSave.is_open() ) { CEGUI::Logger::getSingleton().logEvent("Could not write to dialog configuration file (" + m_configFile + "). Is it read-only?", CEGUI::Errors); return false; } // Write the header fileSave << "<?xml version=\"1.0\" ?>" << std::endl << "<" << m_DialogConfigurationElement << ">" << std::endl; // Write each dialog's position DialogConfig config; CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton(); std::map<CEGUI::String, DialogConfig>::iterator itConfig; CEGUI::Window* widget; for(itConfig = m_dialogConfigurations.begin(); itConfig != m_dialogConfigurations.end(); ++itConfig) { widget = winMgr.getWindow(itConfig->first); config.position = widget->getPosition(); config.size = widget->getSize(); config.visible = widget->isVisible(); if( widget->testClassName("FrameWindow") ) config.rolledup = static_cast<CEGUI::FrameWindow*>(widget)->isRolledup(); else config.rolledup = false; fileSave << " <" << m_configurationElement.c_str() << " " << windowNameAttribute << "=\"" << itConfig->first << "\" " << windowXPosAttribute << "=\"" << config.position.d_x << "\" " << windowYPosAttribute << "=\"" << config.position.d_y << "\" " << windowWidthAttribute << "=\"" << config.size.d_width << "\" " << windowHeightAttribute << "=\"" << config.size.d_height << "\" " << windowVisibleAttribute << "=\"" << config.visible << "\" " << windowRolledupAttribute << "=\"" << config.rolledup << "\" " << "/>" << std::endl; } // Write the footer fileSave << "</" << m_DialogConfigurationElement << ">" << std::endl; fileSave.close(); return true; } bool DialogConfigurations::addDialog(const CEGUI::String& dialog) { // Add a dialog to be monitored // This can be used to automatically write the contents of the initial XML file // rather than editing it by hand. The XML file passed to loadConfigurations() MUST // exist but it can be empty std::map<CEGUI::String, DialogConfig>::iterator itConfig; itConfig = m_dialogConfigurations.find(dialog); if(itConfig != m_dialogConfigurations.end()) return false; // This dialog is already added CEGUI::Window* widget = CEGUI::WindowManager::getSingleton().getWindow(dialog); DialogConfig config; config.position = widget->getPosition(); config.size = widget->getSize(); config.visible = widget->isVisible(); if( widget->testClassName("FrameWindow") ) config.rolledup = static_cast<CEGUI::FrameWindow*>(widget)->isRolledup(); else config.rolledup = m_defaultRolledup; // Not really needed, unless this window inherits from FrameWindow after being loaded m_dialogConfigurations[dialog] = config; return true; } const CEGUI::Point& DialogConfigurations::getSavedPosition(const CEGUI::String& dialog) { // Return the position of the specified dialog std::map<CEGUI::String, DialogConfig>::iterator itConfig; itConfig = m_dialogConfigurations.find(dialog); return itConfig == m_dialogConfigurations.end() ? m_defaultPosition : (*itConfig).second.position; } const CEGUI::Size& DialogConfigurations::getSavedSize(const CEGUI::String& dialog) { // Return the size of the specified dialog std::map<CEGUI::String, DialogConfig>::iterator itConfig; itConfig = m_dialogConfigurations.find(dialog); return itConfig == m_dialogConfigurations.end() ? m_defaultSize : (*itConfig).second.size; } const bool& DialogConfigurations::getSavedVisible(const CEGUI::String& dialog) { // Return the visible/hidden state of the specified dialog // Return the expansion state of the specified dialog std::map<CEGUI::String, DialogConfig>::iterator itConfig; itConfig = m_dialogConfigurations.find(dialog); return itConfig == m_dialogConfigurations.end() ? m_defaultVisible : (*itConfig).second.visible; } const bool& DialogConfigurations::getSavedRolledup(const CEGUI::String& dialog) { // Return the expansion state of the specified dialog std::map<CEGUI::String, DialogConfig>::iterator itConfig; itConfig = m_dialogConfigurations.find(dialog); return itConfig == m_dialogConfigurations.end() ? m_defaultRolledup : (*itConfig).second.rolledup; } void DialogConfigurations::elementStart(const CEGUI::String& element, const CEGUI::XMLAttributes& attributes) { // Parse the <Configuration> elements DialogConfig config; CEGUI::String name; CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton(); if (element == m_configurationElement) { name = attributes.getValueAsString(windowNameAttribute); config.position.d_x = attributes.getValueAsFloat(windowXPosAttribute); config.position.d_y = attributes.getValueAsFloat(windowYPosAttribute); config.size.d_width = attributes.getValueAsFloat(windowWidthAttribute); config.size.d_height = attributes.getValueAsFloat(windowHeightAttribute); config.visible = attributes.getValueAsBool(windowVisibleAttribute); config.rolledup = attributes.getValueAsBool(windowRolledupAttribute); // Prevent dialogs from opening beyond the screen limits if(config.position.d_x > 1.0f) config.position.d_x = 0.99f; if(config.position.d_y > 1.0f) config.position.d_y = 0.99f; // Set the configuration of the dialog CEGUI::Window* widget = winMgr.getWindow(name); widget->setPosition(config.position); widget->setSize(config.size); widget->setVisible(config.visible); if( winMgr.getWindow(name)->testClassName("FrameWindow") ) { CEGUI::FrameWindow* frameWindow = static_cast<CEGUI::FrameWindow*>(widget); if(frameWindow->isRolledup() != config.rolledup) frameWindow->toggleRollup(); } m_dialogConfigurations[name] = config; } }
DialogConfigurations.xsd
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:element name="DialogConfiguration" type="DialogConfigurationType"/> <xsd:complexType name="DialogConfigurationType"> <xsd:sequence> <xsd:element name="Configuration" type="ConfigurationType" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="ConfigurationType"> <xsd:attribute name="Name" type="xsd:string" use="required"/> <xsd:attribute name="XPos" type="xsd:decimal" use="required"/> <xsd:attribute name="YPos" type="xsd:decimal" use="required"/> <xsd:attribute name="Width" type="xsd:decimal" use="required"/> <xsd:attribute name="Height" type="xsd:decimal" use="required"/> <xsd:attribute name="Visible" type="xsd:boolean" use="required"/> <xsd:attribute name="RolledUp" type="xsd:boolean" use="required"/> </xsd:complexType> </xsd:schema>