Difference between revisions of "Dialog Configurations"
m (moved DialogConfigurations to Dialog Configurations) |
|
(No difference)
|
Latest revision as of 16:16, 4 March 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)
Contents
Introduction
Dialogs (frame windows) can be moved around, resized, hidden, and rolled up while the application is running. MultiColumnLists can have their columns rearranged, resized, and a column can sort its content in ascending or descending order. However closing and then restarting the application will reset these settings to their default values, those specified within a .layout file if one was use, or those specified in code.
This clases saves these attributes into an XML file. When the application restarts the contents of the XML file will be read and the dialogs and MultiColumnLists will be restored to their last states.
Please discuss this snippet in the Storing Dialog Configurations thread on the message board.
Incorporating into Sample_Demo7
This demo contains 3 dialogs. Let's store their configurations.
Add DialogConfigurations.cpp to the Sample_Demo7 project.
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.
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.
Configuration Attributes
The XML configuration file is loaded by a call to loadConfigurations(). It silently ignores errors within that XML file. The worst that will happen is that some dialogs and MultiColumnLists will revert to their initial values.
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.
The second parameter of addDialog() allows you to specify which attributes to monitor. By default every attribute is monitored. However you can specify to monitor one or many of the following, separated by spaces: Position, Visible, and RolledUp.
The addMultiColumnList() function behaves in a similar manner, allowing you to specify which MultiColumnLists and which of their attributes to monitor. Again the default is to monitor every attribute but you can also specify one or many of the following: ColumnSequence, ColumnWidths, SortedColumn, and AscendingSort.
Files
DialogConfigurations.h
#ifndef _DialogConfigurations_h_ #define _DialogConfigurations_h_ #include "CEGUI.h" #include "CEGUIXMLHandler.h" #include "CEGUIXMLParser.h" #include "CEGUIXMLAttributes.h" #include <map> class DialogConfigurations : public CEGUI::XMLHandler { public: // Constructor. // Sets various default values. DialogConfigurations(); // Load the configuration of every stored dialog and multi column list. // By default, also apply these configurations. bool loadConfigurations(const CEGUI::String& xmlFile, const CEGUI::String& schema = "", const bool& applyConfigurations = true); // Store the configuration of every dialog and multi column list. bool saveConfigurations(); // Set default configurations for dialogs. void setDialogDefaults(const CEGUI::URect& position, const bool& visible, const bool& rolledup); // Specify which dialog attributes are monitored by default. void setDefaultDialogAttributesMonitored(const CEGUI::String& attributesMonitored); // Add a dialog to be monitored. // Optionally specify which attributes this specific dialog monitors. bool addDialog(const CEGUI::String& widget, const CEGUI::String& attributesMonitored = ""); // Return the position of the specified dialog. const CEGUI::URect& getDialogSavedPosition(const CEGUI::String& dialog); // Return the visible/hidden state of the specified dialog. const bool& getDialogSavedVisible(const CEGUI::String& dialog); // Return the expansion state of the specified dialog. const bool& getDialogSavedRolledup(const CEGUI::String& dialog); // Apply the saved configuration to the specified dialog. bool applyDialogConfiguration(const CEGUI::String& dialog = ""); // Set default configurations for multi column lists. void setMultiColumnListDefaults(const std::vector<CEGUI::String>& columnSequence, const CEGUI::uint& sortedColumn, const bool& sortAscending, const std::vector<CEGUI::String>& columnWidths); // Specify which dialog attributes are monitored by default. void setDefaultMultiColumnListAttributesMonitored(const CEGUI::String& attributesMonitored); // Add a MultiColumnList to be monitored. bool addMultiColumnList(const CEGUI::String& widget, const CEGUI::String& attributesMonitored = ""); // Apply the saved configuration to the specified multi column list. bool applyMultiColumnListConfiguration(const CEGUI::String& multiColumnList = ""); private: // Whether a particular attribute is monitored. bool isMonitoringAttribute(const std::vector<CEGUI::String>& list, const CEGUI::String& attribute); // Performs the actual load every stored configuration. void elementStart(const CEGUI::String& element, const CEGUI::XMLAttributes& xmlAttributes); // Tag name for dialog positions element. static const CEGUI::String m_DialogConfigurationElement; // Tag name for position elements. static const CEGUI::String m_configurationElementDialog; // Attribute name that stores the name of the window. static const char windowNameAttribute[]; // Attribute name that stores the position of the window. static const char windowPosAttribute[]; // Attribute name that stores the visible state of the window. static const char windowVisibleAttribute[]; // Attribute name that stores the rolledUp state of the window. static const char windowRolledupAttribute[]; // Tag name of MultiColumnList elements. static const CEGUI::String m_configurationElementMultiColumnList; // Attribute name that stores the sequence of the columns. static const char mclColumnSequenceAttribute[]; // Attribute name that stores which column is sorted. static const char mclSortedColumnAttribute[]; // Attribute name that stores the sort order direction. static const char mclAscendingSortAttribute[]; // Attribute name that stores the widths of the columns. static const char DialogConfigurations::mclColumnWidthAttribute[]; // Separator for the column sequences. static const char DialogConfigurations::mclColumnSequenceSeparator; // Separator for the column widths. static const char DialogConfigurations::mclColumnWidthSeparator; // Special attribute name to monitor every attribute. static const char monitoredAttributeAll[]; // Special attribute name to monitor no attribute. static const char monitoredAttributeNone[]; // The value of one dialog configuration. struct DialogConfig { CEGUI::URect position; bool visible; bool rolledup; }; // List of the dialog configurations. std::map<CEGUI::String, DialogConfig> m_dialogConfigurations; // Default position. CEGUI::URect m_defaultPosition; // Default visible/hidden. bool m_defaultVisible; // Default rolled up/expanded. bool m_defaultRolledup; // Specify which attributes to monitor for this dialog. void _setDialogAttributesMonitored(const CEGUI::String& dialog, const CEGUI::String& attributesMonitored); // List of the attributes monitored per dialog. std::map<CEGUI::String, std::vector<CEGUI::String>> m_attributesMonitoredDialog; // The value of one multi column list configuration. struct MultiColumnListConfig { std::vector<CEGUI::String> columnSequence; CEGUI::uint sortedColumn; bool sortAscending; std::vector<CEGUI::String> columnWidths; }; // List of the multi column list configurations. std::map<CEGUI::String, MultiColumnListConfig> m_multiColumnListConfigurations; // Default column sequence. std::vector<CEGUI::String> m_defaultColumnSequence; // Default sorted column. CEGUI::uint m_defaultSortedColumn; // Default sort order. bool m_defaultSortAscending; // Default column widths. std::vector<CEGUI::String> m_defaultColumnWidths; // Specify which attributes to monitor for this multi column list. void _setMultiColumnListAttributesMonitored(const CEGUI::String& multiColumnList, const CEGUI::String& attributesMonitored); // List of the attributes monitored per MultiColumnList std::map<CEGUI::String, std::vector<CEGUI::String>> m_attributesMonitoredMultiColumnList; // File containing the positions. CEGUI::String m_configFile; // List of the supported attributes. static std::vector<CEGUI::String> m_attributeList; }; #endif // _DialogConfigurations_h_
DialogConfigurations.cpp
#include "DialogConfigurations.h" #include "CEGUIPropertyHelper.h" //Definition of XML elements and attributes const CEGUI::String DialogConfigurations::m_DialogConfigurationElement( "DialogConfigurations" ); const CEGUI::String DialogConfigurations::m_configurationElementDialog( "DialogConfiguration" ); const char DialogConfigurations::windowNameAttribute[] = "Name"; const char DialogConfigurations::windowPosAttribute[] = "Position"; const char DialogConfigurations::windowVisibleAttribute[] = "Visible"; const char DialogConfigurations::windowRolledupAttribute[] = "RolledUp"; const char DialogConfigurations::monitoredAttributeAll[] = "All"; const char DialogConfigurations::monitoredAttributeNone[] = "None"; const CEGUI::String DialogConfigurations::m_configurationElementMultiColumnList( "MultiColumnListConfiguration" ); const char DialogConfigurations::mclColumnSequenceAttribute[] = "ColumnSequence"; const char DialogConfigurations::mclColumnWidthAttribute[] = "ColumnWidths"; const char DialogConfigurations::mclSortedColumnAttribute[] = "SortedColumn"; const char DialogConfigurations::mclAscendingSortAttribute[]= "AscendingSort"; const char DialogConfigurations::mclColumnSequenceSeparator = ','; const char DialogConfigurations::mclColumnWidthSeparator = ';'; // List of the supported attributes // Each attribute is enclosed within spaces //const CEGUI::String DialogConfigurations::m_attributeList = " All None Position Visible RolledUp MCLWidths MCLSequence MCLSort "; std::vector<CEGUI::String> DialogConfigurations::m_attributeList; void Tokenize(const CEGUI::String& str, std::vector<CEGUI::String>& tokens, const char delimiters = ' ') { // Break a sentence into multiple words separated by delimiters // Does not handle words within double-quotes: One "2 &2" three, four // Skip delimiters at beginning CEGUI::String::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter" CEGUI::String::size_type pos = str.find_first_of(delimiters, lastPos); CEGUI::String::size_type length = str.length(); while (str.npos != pos || str.npos != lastPos) { // Cap the position to the length of the string if(pos == str.npos) pos = length; if(lastPos == str.npos) lastPos = length; // Found a token, add it to the vector tokens.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters lastPos = str.find_first_not_of(delimiters, pos); // Find next "non-delimiter" pos = str.find_first_of(delimiters, lastPos); } } DialogConfigurations::DialogConfigurations() { setDialogDefaults(CEGUI::URect( CEGUI::UDim(0.0f, 0.0f), CEGUI::UDim(0.0f, 0.0f), CEGUI::UDim(0.5f, 0.0f), CEGUI::UDim(0.5f, 0.0f)), true, true); // setMultiColumnListDefaults() m_defaultSortedColumn = 0; m_defaultSortAscending = true; if(m_attributeList.empty()) { // This is a static variable, only initialize once m_attributeList.push_back(monitoredAttributeAll); m_attributeList.push_back(monitoredAttributeNone); m_attributeList.push_back(windowPosAttribute); m_attributeList.push_back(windowVisibleAttribute); m_attributeList.push_back(windowRolledupAttribute); m_attributeList.push_back(mclColumnSequenceAttribute); m_attributeList.push_back(mclSortedColumnAttribute); m_attributeList.push_back(mclAscendingSortAttribute); m_attributeList.push_back(mclColumnWidthAttribute); } // The default is to monitor every attribute _setDialogAttributesMonitored("", monitoredAttributeAll); _setMultiColumnListAttributesMonitored("", monitoredAttributeAll); } bool DialogConfigurations::loadConfigurations(const CEGUI::String& xmlFile, const CEGUI::String& schema, const bool& applyConfigurations) { // Load the config of every stored dialog assert(!xmlFile.empty() && "You must specify an xml file to loadConfigurations()"); m_configFile = xmlFile; bool loadedOk = true; try { CEGUI::System::getSingleton().getXMLParser()->parseXMLFile(*this, m_configFile, schema, ""); } catch(const CEGUI::Exception&) { // File is empty or parse error // Silently ignore; we'll create a new file when saving loadedOk = false; } if(applyConfigurations) { applyDialogConfiguration(); applyMultiColumnListConfiguration(); } return loadedOk; } 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 configuration DialogConfig dialogConfig; CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton(); std::map<CEGUI::String, DialogConfig>::iterator itDialogConfig; std::map<CEGUI::String, std::vector<CEGUI::String>>::iterator itAttributesMonitored; std::vector<CEGUI::String> attributesMonitored; CEGUI::Window* window; for(itDialogConfig = m_dialogConfigurations.begin(); itDialogConfig != m_dialogConfigurations.end(); ++itDialogConfig) { if(!winMgr.isWindowPresent(itDialogConfig->first)) continue; itAttributesMonitored = m_attributesMonitoredDialog.find(itDialogConfig->first); if(itAttributesMonitored != m_attributesMonitoredDialog.end()) attributesMonitored = (*itAttributesMonitored).second; // Dialog has specified attributes else { // Use default attributes itAttributesMonitored = m_attributesMonitoredDialog.find(""); if(itAttributesMonitored != m_attributesMonitoredDialog.end()) attributesMonitored = (*itAttributesMonitored).second; } window = winMgr.getWindow(itDialogConfig->first); dialogConfig.position = window->getArea(); dialogConfig.visible = window->isVisible(); if( window->testClassName("FrameWindow") ) dialogConfig.rolledup = static_cast<CEGUI::FrameWindow*>(window)->isRolledup(); else dialogConfig.rolledup = false; fileSave << " <" << m_configurationElementDialog.c_str() << " " << windowNameAttribute << "=\"" << itDialogConfig->first << "\" "; if(isMonitoringAttribute(attributesMonitored, windowPosAttribute)) fileSave << windowPosAttribute << "=\"" << CEGUI::PropertyHelper::urectToString(dialogConfig.position) << "\" "; if(isMonitoringAttribute(attributesMonitored, windowVisibleAttribute)) fileSave << windowVisibleAttribute << "=\"" << dialogConfig.visible << "\" "; if(isMonitoringAttribute(attributesMonitored, windowRolledupAttribute)) fileSave << windowRolledupAttribute << "=\"" << dialogConfig.rolledup << "\" "; fileSave << "/>" << std::endl; } // Write each MultiColumnList's configuration std::map<CEGUI::String, MultiColumnListConfig>::iterator itMultiColumnListConfig; attributesMonitored.clear(); CEGUI::MultiColumnList* multiColumnList; for(itMultiColumnListConfig = m_multiColumnListConfigurations.begin(); itMultiColumnListConfig != m_multiColumnListConfigurations.end(); ++itMultiColumnListConfig) { itAttributesMonitored = m_attributesMonitoredMultiColumnList.find(itMultiColumnListConfig->first); if(itAttributesMonitored != m_attributesMonitoredMultiColumnList.end()) attributesMonitored = (*itAttributesMonitored).second; // Dialog has specified attributes else { // Use default attributes itAttributesMonitored = m_attributesMonitoredMultiColumnList.find(""); if(itAttributesMonitored != m_attributesMonitoredMultiColumnList.end()) attributesMonitored = (*itAttributesMonitored).second; } multiColumnList = static_cast<CEGUI::MultiColumnList*>(winMgr.getWindow(itMultiColumnListConfig->first)); fileSave << " <" << m_configurationElementMultiColumnList.c_str() << " " << windowNameAttribute << "=\"" << itMultiColumnListConfig->first << "\" "; if(isMonitoringAttribute(attributesMonitored, mclColumnSequenceAttribute) && multiColumnList->getColumnCount()) { fileSave << mclColumnSequenceAttribute << "=\""; for(CEGUI::uint columnIndex = 0; columnIndex < multiColumnList->getColumnCount(); ++columnIndex) { if(columnIndex) fileSave << mclColumnSequenceSeparator; fileSave << multiColumnList->getColumnID(columnIndex); } fileSave << "\" "; } if(isMonitoringAttribute(attributesMonitored, mclColumnWidthAttribute) && multiColumnList->getColumnCount()) { fileSave << mclColumnWidthAttribute << "=\""; for(CEGUI::uint columnIndex = 0; columnIndex < multiColumnList->getColumnCount(); ++columnIndex) { if(columnIndex) fileSave << mclColumnWidthSeparator; fileSave << CEGUI::PropertyHelper::udimToString(multiColumnList->getColumnHeaderWidth(columnIndex)); } fileSave << "\" "; } if(isMonitoringAttribute(attributesMonitored, mclSortedColumnAttribute)) fileSave << mclSortedColumnAttribute << "=\"" << multiColumnList->getColumnID(multiColumnList->getSortColumn()) << "\" "; if(isMonitoringAttribute(attributesMonitored, mclAscendingSortAttribute)) fileSave << mclAscendingSortAttribute << "=\"" << (multiColumnList->getSortDirection() != CEGUI::ListHeaderSegment::Descending) << "\" "; fileSave << "/>" << std::endl; } // Write the footer fileSave << "</" << m_DialogConfigurationElement << ">" << std::endl; fileSave.close(); return true; } void DialogConfigurations::setDialogDefaults(const CEGUI::URect& position, const bool& visible, const bool& rolledup) { // Set default values // Should validate what's being passed as parameters m_defaultPosition = position; m_defaultVisible = visible; m_defaultRolledup = rolledup; } void DialogConfigurations::setDefaultDialogAttributesMonitored(const CEGUI::String& attributesMonitored) { _setDialogAttributesMonitored("", attributesMonitored); } bool DialogConfigurations::addDialog(const CEGUI::String& widget, const CEGUI::String& attributesMonitored) { // 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. assert(!widget.empty() && "You must pass a dialog name to addDialog()"); if(!attributesMonitored.empty()) _setDialogAttributesMonitored(widget, attributesMonitored); std::map<CEGUI::String, DialogConfig>::iterator itConfig; itConfig = m_dialogConfigurations.find(widget); if(itConfig != m_dialogConfigurations.end()) return false; // This dialog is already added CEGUI::Window* window = CEGUI::WindowManager::getSingleton().getWindow(widget); DialogConfig config; config.position = window->getArea(); config.visible = window->isVisible(); if( window->testClassName("FrameWindow") ) config.rolledup = static_cast<CEGUI::FrameWindow*>(window)->isRolledup(); else config.rolledup = m_defaultRolledup; // Not really needed, unless this window inherits from FrameWindow after being loaded m_dialogConfigurations[widget] = config; return true; } const CEGUI::URect& DialogConfigurations::getDialogSavedPosition(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 bool& DialogConfigurations::getDialogSavedVisible(const CEGUI::String& dialog) { // Return the visible/hidden 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::getDialogSavedRolledup(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; } bool DialogConfigurations::applyDialogConfiguration(const CEGUI::String& dialog) { // Apply the saved configuration to a dialog if(dialog.length() == 0) { // Load every dialog configurations std::map<CEGUI::String, DialogConfig>::iterator itConfig; for(itConfig = m_dialogConfigurations.begin(); itConfig != m_dialogConfigurations.end(); itConfig++) { applyDialogConfiguration((*itConfig).first); } } else { // Find this dialog's configuration CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton(); std::map<CEGUI::String, DialogConfig>::iterator itConfig; itConfig = m_dialogConfigurations.find(dialog); if(itConfig == m_dialogConfigurations.end() || !winMgr.isWindowPresent(dialog)) return false; // Determine which attributes are monitored std::vector<CEGUI::String> attributesMonitored; std::map<CEGUI::String, std::vector<CEGUI::String>>::iterator itAttributesMonitored; itAttributesMonitored = m_attributesMonitoredDialog.find(dialog); if(itAttributesMonitored != m_attributesMonitoredDialog.end()) attributesMonitored = (*itAttributesMonitored).second; // Dialog has specified attributes else { // Use default attributes itAttributesMonitored = m_attributesMonitoredDialog.find(""); if(itAttributesMonitored == m_attributesMonitoredDialog.end()) return false; // Could not retrieve the default attributes (should never happen) attributesMonitored = (*itAttributesMonitored).second; } // Apply the configuration to the dialog CEGUI::Window* window = winMgr.getWindow(dialog); if(isMonitoringAttribute(attributesMonitored, windowPosAttribute)) window->setArea((*itConfig).second.position); if(isMonitoringAttribute(attributesMonitored, windowVisibleAttribute)) window->setVisible((*itConfig).second.visible); if(isMonitoringAttribute(attributesMonitored, windowRolledupAttribute)) { if( window->testClassName("FrameWindow") ) { CEGUI::FrameWindow* frameWindow = static_cast<CEGUI::FrameWindow*>(window); if(frameWindow->isRolledup() != (*itConfig).second.rolledup) frameWindow->toggleRollup(); } } } return true; } void DialogConfigurations::setMultiColumnListDefaults(const std::vector<CEGUI::String>& columnSequence, const CEGUI::uint& sortedColumn, const bool& sortAscending, const std::vector<CEGUI::String>& columnWidths) { // Set default values // Should validate what's being passed as parameters m_defaultColumnSequence = columnSequence; m_defaultSortedColumn = sortedColumn; m_defaultSortAscending = sortAscending; m_defaultColumnWidths = columnWidths; } void DialogConfigurations::setDefaultMultiColumnListAttributesMonitored(const CEGUI::String& attributesMonitored) { _setMultiColumnListAttributesMonitored("", attributesMonitored); } bool DialogConfigurations::addMultiColumnList(const CEGUI::String& widget, const CEGUI::String& attributesMonitored) { // Add a MultiColumnList to be monitored // This can be used to automatically write the contents of the initial XML file // rather than editing it by hand. assert(!widget.empty() && "You must pass a multiColumnList name to addMultiColumnList()"); if(!attributesMonitored.empty()) _setMultiColumnListAttributesMonitored(widget, attributesMonitored); std::map<CEGUI::String, MultiColumnListConfig>::iterator itConfig; itConfig = m_multiColumnListConfigurations.find(widget); if(itConfig != m_multiColumnListConfigurations.end()) return false; // This MultiColumnList is already added MultiColumnListConfig config; CEGUI::MultiColumnList* multiColumnList = static_cast<CEGUI::MultiColumnList*>(CEGUI::WindowManager::getSingleton().getWindow(widget)); assert(multiColumnList->testClassName("MultiColumnList") && "This widget is not a MultiColumnList"); for(CEGUI::uint columnIndex = 0; columnIndex < multiColumnList->getColumnCount(); ++columnIndex) { config.columnSequence.push_back(CEGUI::PropertyHelper::uintToString(multiColumnList->getColumnID(columnIndex))); config.columnWidths.push_back(CEGUI::PropertyHelper::udimToString(multiColumnList->getColumnHeaderWidth(columnIndex))); } config.sortedColumn = multiColumnList->getSortColumn(); config.sortAscending = multiColumnList->getSortDirection() != CEGUI::ListHeaderSegment::Descending; m_multiColumnListConfigurations[widget] = config; return true; } bool DialogConfigurations::applyMultiColumnListConfiguration(const CEGUI::String& multiColumnList) { // Apply the saved configuration to a multi column list if(multiColumnList.length() == 0) { // Load every multi column list configurations std::map<CEGUI::String, MultiColumnListConfig>::iterator itConfig; for(itConfig = m_multiColumnListConfigurations.begin(); itConfig != m_multiColumnListConfigurations.end(); itConfig++) { applyMultiColumnListConfiguration((*itConfig).first); } } else { // Find this multi column list's configuration CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton(); std::map<CEGUI::String, MultiColumnListConfig>::iterator itConfig; itConfig = m_multiColumnListConfigurations.find(multiColumnList); if(itConfig == m_multiColumnListConfigurations.end() || !winMgr.isWindowPresent(multiColumnList)) return false; // Determine which attributes are monitored std::vector<CEGUI::String> attributesMonitored; std::map<CEGUI::String, std::vector<CEGUI::String>>::iterator itAttributesMonitored; itAttributesMonitored = m_attributesMonitoredMultiColumnList.find(multiColumnList); if(itAttributesMonitored != m_attributesMonitoredMultiColumnList.end()) attributesMonitored = (*itAttributesMonitored).second; // Multi column list has specified attributes else { // Use default attributes itAttributesMonitored = m_attributesMonitoredMultiColumnList.find(""); if(itAttributesMonitored == m_attributesMonitoredMultiColumnList.end()) return false; // Could not retrieve the default attributes (should never happen) attributesMonitored = (*itAttributesMonitored).second; } // Apply the configuration to the multi column list CEGUI::MultiColumnList* window = static_cast<CEGUI::MultiColumnList*>(winMgr.getWindow(multiColumnList)); if(isMonitoringAttribute(attributesMonitored, mclColumnSequenceAttribute)) { for(CEGUI::uint columnIndex = 0; columnIndex < window->getColumnCount() && columnIndex < (*itConfig).second.columnSequence.size(); ++columnIndex) { window->moveColumn(window->getColumnWithID(CEGUI::PropertyHelper::stringToUint((*itConfig).second.columnSequence.at(columnIndex))), columnIndex); } } if(isMonitoringAttribute(attributesMonitored, mclColumnWidthAttribute)) { CEGUI::UDim width; for(CEGUI::uint columnIndex = 0; columnIndex < window->getColumnCount() && columnIndex < (*itConfig).second.columnWidths.size(); ++columnIndex) { width = CEGUI::PropertyHelper::stringToUDim((*itConfig).second.columnWidths.at(columnIndex)); window->setColumnHeaderWidth(columnIndex, width); } } if(isMonitoringAttribute(attributesMonitored, mclSortedColumnAttribute)) { window->setSortColumn( window->getColumnWithID((*itConfig).second.sortedColumn) ); } if(isMonitoringAttribute(attributesMonitored, mclAscendingSortAttribute)) { window->setSortDirection( (*itConfig).second.sortAscending ? CEGUI::ListHeaderSegment::Ascending : CEGUI::ListHeaderSegment::Descending); } } return true; } bool DialogConfigurations::isMonitoringAttribute(const std::vector<CEGUI::String>& list, const CEGUI::String& attribute) { std::vector<CEGUI::String>::const_iterator itList; for(itList = list.begin(); itList != list.end(); itList++) { if(!(*itList).compare(attribute)) return true; } return false; } void DialogConfigurations::elementStart(const CEGUI::String& element, const CEGUI::XMLAttributes& xmlAttributes) { // Performs the actual load every stored configuration if (element == m_configurationElementDialog) { DialogConfig config; config.position = m_defaultPosition; config.visible = m_defaultVisible; config.rolledup = m_defaultRolledup; CEGUI::String name, monitoredAttributes; CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton(); name = xmlAttributes.getValueAsString(windowNameAttribute); if(xmlAttributes.exists(windowPosAttribute)) { config.position = CEGUI::PropertyHelper::stringToURect(xmlAttributes.getValueAsString(windowPosAttribute)); monitoredAttributes.append(" ").append(windowPosAttribute); } if(xmlAttributes.exists(windowVisibleAttribute)) { config.visible = xmlAttributes.getValueAsBool(windowVisibleAttribute); monitoredAttributes.append(" ").append(windowVisibleAttribute); } if(xmlAttributes.exists(windowRolledupAttribute)) { config.rolledup = xmlAttributes.getValueAsBool(windowRolledupAttribute); monitoredAttributes.append(" ").append(windowRolledupAttribute); } _setDialogAttributesMonitored(name, monitoredAttributes); m_dialogConfigurations[name] = config; } else if (element == m_configurationElementMultiColumnList) { MultiColumnListConfig config; config.sortedColumn = m_defaultSortedColumn; config.sortAscending = m_defaultSortAscending; CEGUI::String name, monitoredAttributes; name = xmlAttributes.getValueAsString(windowNameAttribute); if(xmlAttributes.exists(mclColumnSequenceAttribute)) { CEGUI::String columnSequence = xmlAttributes.getValueAsString(mclColumnSequenceAttribute); Tokenize(columnSequence, config.columnSequence, mclColumnSequenceSeparator); monitoredAttributes.append(" ").append(mclColumnSequenceAttribute); } if(xmlAttributes.exists(mclColumnWidthAttribute)) { CEGUI::String columnWidths = xmlAttributes.getValueAsString(mclColumnWidthAttribute); Tokenize(columnWidths, config.columnWidths, mclColumnWidthSeparator); monitoredAttributes.append(" ").append(mclColumnWidthAttribute); } if(xmlAttributes.exists(mclSortedColumnAttribute)) { config.sortedColumn = xmlAttributes.getValueAsInteger(mclSortedColumnAttribute); monitoredAttributes.append(" ").append(mclSortedColumnAttribute); } if(xmlAttributes.exists(mclAscendingSortAttribute)) { config.sortAscending = xmlAttributes.getValueAsBool(mclAscendingSortAttribute); monitoredAttributes.append(" ").append(mclAscendingSortAttribute); } _setMultiColumnListAttributesMonitored(name, monitoredAttributes); m_multiColumnListConfigurations[name] = config; } } void DialogConfigurations::_setDialogAttributesMonitored(const CEGUI::String& dialog, const CEGUI::String& attributesMonitored) { // Validate the attributes to be monitored std::vector<CEGUI::String> newAttributes; std::vector<CEGUI::String> listAttributesMonitored; Tokenize(attributesMonitored, listAttributesMonitored); // Convert attributesMonitored into a vector std::vector<CEGUI::String>::iterator itToken; for(itToken = listAttributesMonitored.begin(); itToken != listAttributesMonitored.end(); itToken++) { if(!(*itToken).compare(monitoredAttributeAll)) { newAttributes.push_back(windowPosAttribute); newAttributes.push_back(windowVisibleAttribute); newAttributes.push_back(windowRolledupAttribute); } else if(!(*itToken).compare(monitoredAttributeNone)) { newAttributes.clear(); } else if(!(*itToken).compare(windowPosAttribute)) newAttributes.push_back(windowPosAttribute); else if(!(*itToken).compare(windowVisibleAttribute)) newAttributes.push_back(windowVisibleAttribute); else if(!(*itToken).compare(windowRolledupAttribute)) newAttributes.push_back(windowRolledupAttribute); else { // Attribute is not supported assert(false && "Unsupported attribute specified for a dialog"); CEGUI::Logger::getSingleton().logEvent("The attribute \"" + (*itToken) + "\" is unsupported for dialogs.", CEGUI::Errors); } } // Store the attributes to be monitored m_attributesMonitoredDialog[dialog] = newAttributes; } void DialogConfigurations::_setMultiColumnListAttributesMonitored(const CEGUI::String& multiColumnList, const CEGUI::String& attributesMonitored) { // Validate the attributes to be monitored std::vector<CEGUI::String> newAttributes; std::vector<CEGUI::String> listAttributesMonitored; Tokenize(attributesMonitored, listAttributesMonitored); // Convert attributesMonitored into a vector std::vector<CEGUI::String>::iterator itToken; for(itToken = listAttributesMonitored.begin(); itToken != listAttributesMonitored.end(); itToken++) { if(!(*itToken).compare(monitoredAttributeAll)) { newAttributes.push_back(mclColumnSequenceAttribute); newAttributes.push_back(mclSortedColumnAttribute); newAttributes.push_back(mclAscendingSortAttribute); newAttributes.push_back(mclColumnWidthAttribute); } else if(!(*itToken).compare(monitoredAttributeNone)) { newAttributes.clear(); } else if(!(*itToken).compare(mclColumnSequenceAttribute)) newAttributes.push_back(mclColumnSequenceAttribute); else if(!(*itToken).compare(mclSortedColumnAttribute)) newAttributes.push_back(mclSortedColumnAttribute); else if(!(*itToken).compare(mclAscendingSortAttribute)) newAttributes.push_back(mclAscendingSortAttribute); else if(!(*itToken).compare(mclColumnWidthAttribute)) newAttributes.push_back(mclColumnWidthAttribute); else { // Attribute is not supported assert(false && "Unsupported attribute specified for a MultiColumnList"); CEGUI::Logger::getSingleton().logEvent("The attribute \"" + (*itToken) + "\" is unsupported for MultiColumnLists.", CEGUI::Errors); } } // Store the attributes to be monitored m_attributesMonitoredMultiColumnList[multiColumnList] = newAttributes; }
DialogConfigurationsDemo.h
#ifndef _DialogConfigurationsDemo_h_ #define _DialogConfigurationsDemo_h_ #include "CEGuiSample.h" #include "CEGUI.h" #include "DialogConfigurations.h" 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("DialogConfigurations.layout"); sheet->addChildWindow(guiLayout); // Configure the toolbar winMgr.getWindow("DialogConfigurations/Toolbar/ShowA")->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&DemoSample::showA, this)); winMgr.getWindow("DialogConfigurations/Toolbar/ShowC")->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&DemoSample::showC, this)); winMgr.getWindow("DialogConfigurations/Toolbar/ShowD")->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&DemoSample::showD, this)); // Activate the close buttons winMgr.getWindow("DialogConfigurations/Window_A")->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&DemoSample::closeA, this)); winMgr.getWindow("DialogConfigurations/Window_B")->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&DemoSample::closeB, this)); winMgr.getWindow("DialogConfigurations/Window_C")->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&DemoSample::closeC, this)); winMgr.getWindow("DialogConfigurations/Window_D")->subscribeEvent(FrameWindow::EventCloseClicked, Event::Subscriber(&DemoSample::closeD, this)); // Add columns to the MultiColumnList MultiColumnList* multiColumnList = static_cast<MultiColumnList*>(winMgr.getWindow("DialogConfigurations/Window_D/MultiColumnList")); multiColumnList->addColumn("Col A", 0, UDim(0.32f, 0)); multiColumnList->addColumn("Col B", 1, UDim(0.32f, 0)); multiColumnList->addColumn("Col C", 2, UDim(0.32f, 0)); // Add rows multiColumnList->addRow(); multiColumnList->setItem(new ListboxTextItem("1", 101), 0, 0); // ColumnID, RowID multiColumnList->setItem(new ListboxTextItem("4", 102), 1, 0); multiColumnList->setItem(new ListboxTextItem("7", 103), 2, 0); multiColumnList->addRow(); multiColumnList->setItem(new ListboxTextItem("4", 201), 0, 1); multiColumnList->setItem(new ListboxTextItem("7", 202), 1, 1); multiColumnList->setItem(new ListboxTextItem("1", 203), 2, 1); multiColumnList->addRow(); multiColumnList->setItem(new ListboxTextItem("7", 301), 0, 2); multiColumnList->setItem(new ListboxTextItem("1", 302), 1, 2); multiColumnList->setItem(new ListboxTextItem("4", 303), 2, 2); // Activate dialog configurations m_DialogConfigurations.loadConfigurations("DialogConfigurations.xml"); m_DialogConfigurations.addDialog("DialogConfigurations/Window_A", "Position Visible"); m_DialogConfigurations.addDialog("DialogConfigurations/Window_B", "Position RolledUp"); m_DialogConfigurations.addDialog("DialogConfigurations/Window_C", "Visible RolledUp"); m_DialogConfigurations.addDialog("DialogConfigurations/Window_D"); // Saves every attribute: Position, Visible, and RolledUp m_DialogConfigurations.addMultiColumnList("DialogConfigurations/Window_D/MultiColumnList"); // Saves every attribute: ColumnSequence, ColumnWidths, SortedColumn, and AscendingSort } catch(Exception &e) { #if defined( __WIN32__ ) || defined( _WIN32 ) MessageBox(NULL, e.getMessage().c_str(), "Error initializing the demo", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else std::cerr << "Error initializing the demo:" << e.getMessage().c_str() << "\n"; #endif } return true; } void cleanupSample(void) { m_DialogConfigurations.saveConfigurations(); } bool showA(const CEGUI::EventArgs& e) { CEGUI::WindowManager::getSingleton().getWindow("DialogConfigurations/Window_A")->setVisible(true); return true; } bool showC(const CEGUI::EventArgs& e) { CEGUI::WindowManager::getSingleton().getWindow("DialogConfigurations/Window_C")->setVisible(true); return true; } bool showD(const CEGUI::EventArgs& e) { CEGUI::WindowManager::getSingleton().getWindow("DialogConfigurations/Window_D")->setVisible(true); return true; } bool closeA(const CEGUI::EventArgs& e) { CEGUI::WindowManager::getSingleton().getWindow("DialogConfigurations/Window_A")->setVisible(false); return true; } bool closeB(const CEGUI::EventArgs& e) { CEGUI::WindowManager::getSingleton().getWindow("DialogConfigurations/Window_B")->setVisible(false); return true; } bool closeC(const CEGUI::EventArgs& e) { CEGUI::WindowManager::getSingleton().getWindow("DialogConfigurations/Window_C")->setVisible(false); return true; } bool closeD(const CEGUI::EventArgs& e) { CEGUI::WindowManager::getSingleton().getWindow("DialogConfigurations/Window_D")->setVisible(false); return true; } private: DialogConfigurations m_DialogConfigurations; }; #endif // _DialogConfigurationsDemo_h_
main.cpp
#if defined( __WIN32__ ) || defined( _WIN32 ) #define WIN32_LEAN_AND_MEAN #define NOMINMAX #include "windows.h" #endif #include "DialogConfigurationsDemo.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(); }
DialogConfigurations.layout
<?xml version="1.0" encoding="UTF-8"?> <GUILayout > <Window Type="DefaultWindow" Name="DialogConfigurations" > <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/StaticText" Name="DialogConfigurations/Toolbar" > <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.1875,0},{0.00833335,0},{0.8025,0},{0.111667,0}}" /> <Window Type="TaharezLook/Button" Name="DialogConfigurations/Toolbar/ShowA" > <Property Name="Text" Value="Show A" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.0581303,0},{0.156452,0},{0.30813,0},{0.858064,0}}" /> </Window> <Window Type="TaharezLook/Button" Name="DialogConfigurations/Toolbar/ShowC" > <Property Name="Font" Value="Commonwealth-10" /> <Property Name="Text" Value="Show C" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.388618,0},{0.156452,0},{0.638618,0},{0.858064,0}}" /> </Window> <Window Type="TaharezLook/Button" Name="DialogConfigurations/Toolbar/ShowD" > <Property Name="Font" Value="Commonwealth-10" /> <Property Name="Text" Value="Show D" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.704878,0},{0.156452,0},{0.954878,0},{0.858064,0}}" /> </Window> </Window> <Window Type="TaharezLook/FrameWindow" Name="DialogConfigurations/Window_A" > <Property Name="Text" Value="A - RolledUp not saved" /> <Property Name="TitlebarFont" Value="Commonwealth-10" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="TitlebarEnabled" Value="True" /> <Property Name="UnifiedAreaRect" Value="{{0.035,0},{0.123333,0},{0.325,0},{0.47,0}}" /> </Window> <Window Type="TaharezLook/FrameWindow" Name="DialogConfigurations/Window_B" > <Property Name="Text" Value="B - Visible not saved" /> <Property Name="TitlebarFont" Value="Commonwealth-10" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="TitlebarEnabled" Value="True" /> <Property Name="UnifiedAreaRect" Value="{{0.35125,0},{0.123333,0},{0.64125,0},{0.47,0}}" /> </Window> <Window Type="TaharezLook/FrameWindow" Name="DialogConfigurations/Window_C" > <Property Name="Text" Value="C - Position & Size not saved" /> <Property Name="TitlebarFont" Value="Commonwealth-10" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="TitlebarEnabled" Value="True" /> <Property Name="UnifiedAreaRect" Value="{{0.6675,0},{0.123333,0},{0.957501,0},{0.47,0}}" /> </Window> <Window Type="TaharezLook/FrameWindow" Name="DialogConfigurations/Window_D" > <Property Name="Text" Value="D Saves MultiColumnList column width, sequence, and sort" /> <Property Name="TitlebarFont" Value="Commonwealth-10" /> <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="TitlebarEnabled" Value="True" /> <Property Name="UnifiedAreaRect" Value="{{0.10625,0},{0.53375,0},{0.86625,0},{0.962083,0}}" /> <Window Type="TaharezLook/MultiColumnList" Name="DialogConfigurations/Window_D/MultiColumnList" > <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> <Property Name="UnifiedAreaRect" Value="{{0.031235,0},{0.140467,0},{0.968886,0},{0.950778,0}}" /> </Window> </Window> </Window> </GUILayout>