Dialog Configurations

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Revision as of 00:31, 11 May 2006 by Rackle (Talk | contribs) (DialogPositions.h)

Jump to: navigation, search

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()
};

DialogPositions.cpp

#include "DialogPositions.h"

//Definition of XML elements and attributes
const CEGUI::String DialogPositions::m_DialogPositionElement( "DialogPositions" );
const CEGUI::String DialogPositions::m_positionElement( "Position" );
const char DialogPositions::windowNameAttribute[]	= "Name";
const char DialogPositions::windowXPosAttribute[]	= "XPos";
const char DialogPositions::windowYPosAttribute[]	= "YPos";

DialogPositions::DialogPositions()
{
	m_defaultPosition.d_x = 0.0f;
	m_defaultPosition.d_y = 0.0f;
}

bool DialogPositions::loadPositions(const CEGUI::String& xmlFile, const CEGUI::String& schema)
{
	// Load the position of every stored dialog
	assert(!xmlFile.empty() && "You must specify an xml file to loadPositions()");
	m_positionFile = xmlFile;
	CEGUI::System::getSingleton().getXMLParser()->parseXMLFile(*this, m_positionFile, schema, "");
	return true;
}

bool DialogPositions::savePositions()
{
	// Store the position of every registered dialog, either within
	// the the loaded XML file or those specified via addDialog() 
	assert(!m_positionFile.empty() && "You must specify an xml file by calling loadPositions() before savePositions()");
	CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();
	CEGUI::Point position;
	std::map<CEGUI::String, CEGUI::Point>::iterator itPosition;

	// Try to open the XML file in writing mode
    std::ofstream fileSave;
	fileSave.open(m_positionFile.c_str(), std::ios::out) ;
    if( !fileSave.is_open() )
    {
		CEGUI::Logger::getSingleton().logEvent("Could not write to windows position file ("
												+ m_positionFile
												+ ").  Is it read-only?",
												CEGUI::Errors);
        return false;
    }

	// Write the header
	fileSave << "<?xml version=\"1.0\" ?>" << std::endl
			<< "<" << m_DialogPositionElement << ">"
			<< std::endl;

	// Write each dialog's position
	for(itPosition = m_dialogPositions.begin(); itPosition != m_dialogPositions.end(); ++itPosition)
	{
		position = winMgr.getWindow(itPosition->first)->getPosition();
		fileSave << "  <" << m_positionElement.c_str() << " "
			<< windowNameAttribute << "=\"" << itPosition->first.c_str() << "\" "
			<< windowXPosAttribute << "=\"" << position.d_x  << "\" "
			<< windowYPosAttribute << "=\"" << position.d_y  << "\" "
			<< "/>" << std::endl;
	}

	// Write the footer
	fileSave << "</" << m_DialogPositionElement << ">" << std::endl;
	fileSave.close();
	return true;
}

bool DialogPositions::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 loadPositions() MUST
	// exist but it can be empty
	std::map<CEGUI::String, CEGUI::Point>::iterator itPosition;
	itPosition = m_dialogPositions.find(dialog);
	if(itPosition != m_dialogPositions.end())
		return false; // This dialog is already added

	m_dialogPositions[dialog] = CEGUI::WindowManager::getSingleton().getWindow(dialog)->getPosition();
	return true;
}

const CEGUI::Point& DialogPositions::getSavedPosition(const CEGUI::String& dialog)
{
	// Return the position of the specified dialog
	CEGUI::Point position;
	std::map<CEGUI::String, CEGUI::Point>::iterator itPosition;
	itPosition = m_dialogPositions.find(dialog);
	return itPosition == m_dialogPositions.end() ? m_defaultPosition : (*itPosition).second;
}

void DialogPositions::elementStart(const CEGUI::String& element, const CEGUI::XMLAttributes& attributes)
{
	// Parse the <Position> elements
	CEGUI::String name;
	CEGUI::Point position;
	if (element == m_positionElement)
    {
        name = attributes.getValueAsString(windowNameAttribute);
		position.d_x = attributes.getValueAsFloat(windowXPosAttribute);
        position.d_y = attributes.getValueAsFloat(windowYPosAttribute);

		// Prevent dialogs from opening beyond the screen limits
		if(position.d_x > 1.0f)
			position.d_x = 0.99f;
		if(position.d_y > 1.0f)
			position.d_y = 0.99f;

		// Set the position of the dialog
		CEGUI::Window* widget = CEGUI::WindowManager::getSingleton().getWindow(name);
		widget->setPosition(position);
		m_dialogPositions[name] = position;
	}
}

DialogPositions.xsd

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

	<xsd:element name="DialogPositions" type="DialogPositionsType"/>

	<xsd:complexType name="DialogPositionsType">
		<xsd:sequence>
			<xsd:element name="Position" type="PositionType" maxOccurs="unbounded"/>
		</xsd:sequence>
	</xsd:complexType>

	<xsd:complexType name="PositionType">
		<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:complexType>
	
</xsd:schema>