Difference between revisions of "Dialog Configurations"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
(DialogConfigurations.xsd)
(DialogConfigurations.xsd)
Line 261: Line 261:
 
<xsd:complexType name="ConfigurationType">
 
<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="Pos"     type="xsd:string"  use="required"/>
+
<xsd:attribute name="Position" type="xsd:string"  use="required"/>
 
<xsd:attribute name="Visible"  type="xsd:boolean" use="required"/>
 
<xsd:attribute name="Visible"  type="xsd:boolean" use="required"/>
 
<xsd:attribute name="RolledUp" type="xsd:boolean" use="required"/>
 
<xsd:attribute name="RolledUp" type="xsd:boolean" use="required"/>

Revision as of 06:03, 22 June 2006

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 dialogs will be restored to their last states.

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::URect& position, 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::URect& getSavedPosition(const CEGUI::String& dialog); // Return the position 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 windowPosAttribute[]; // Attribute name that stores the position 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::URect position;
		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::URect m_defaultPosition; // Default position, used by getSavedPosition()
	bool m_defaultVisible; // Default visible/hidden, used by getSavedVisible()
	bool m_defaultRolledup; // Default rolled up/expanded, used by getSavedRolledup()
};

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_configurationElement( "Configuration" );
const char DialogConfigurations::windowNameAttribute[]		= "Name";
const char DialogConfigurations::windowPosAttribute[]		= "Position";
const char DialogConfigurations::windowVisibleAttribute[]	= "Visible";
const char DialogConfigurations::windowRolledupAttribute[]	= "RolledUp";

DialogConfigurations::DialogConfigurations()
{
	setDefaults(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);
}

void DialogConfigurations::setDefaults(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;
}

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;
	try
	{
		CEGUI::System::getSingleton().getXMLParser()->parseXMLFile(*this, m_configFile, schema, "");
	}
	catch(CEGUI::GenericException& e)
	{
		// File is empty
	}
	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 configuration
	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->getWindowArea();
		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	<< "\" "
			<< windowPosAttribute		<< "=\"" << CEGUI::PropertyHelper::urectToString(config.position)	<< "\" "
			<< 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->getWindowArea();
	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::URect& 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 bool& DialogConfigurations::getSavedVisible(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::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	= CEGUI::PropertyHelper::stringToURect(attributes.getValueAsString(windowPosAttribute));
		config.visible	= attributes.getValueAsBool(windowVisibleAttribute);
		config.rolledup	= attributes.getValueAsBool(windowRolledupAttribute);

		// Set the configuration of the dialog
		CEGUI::Window* widget = winMgr.getWindow(name);
		widget->setWindowArea(config.position);
		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="Position" type="xsd:string"  use="required"/>
		<xsd:attribute name="Visible"  type="xsd:boolean" use="required"/>
		<xsd:attribute name="RolledUp" type="xsd:boolean" use="required"/>
	</xsd:complexType>
	
</xsd:schema>