Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CEGUIImageset_xmlHandler.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 filename:       CEGUIImageset_xmlHandler.cpp
00003 created:        21/2/2004
00004 author:         Paul D Turner
00005 
00006 purpose:        Implements the Imageset class
00007 *************************************************************************/
00008 /*************************************************************************
00009 Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010 Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012 This library is free software; you can redistribute it and/or
00013 modify it under the terms of the GNU Lesser General Public
00014 License as published by the Free Software Foundation; either
00015 version 2.1 of the License, or (at your option) any later version.
00016 
00017 This library is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 Lesser General Public License for more details.
00021 
00022 You should have received a copy of the GNU Lesser General Public
00023 License along with this library; if not, write to the Free Software
00024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUIImageset_xmlHandler.h"
00027 
00028 #include "CEGUIExceptions.h"
00029 #include "CEGUISystem.h"
00030 #include "CEGUILogger.h"
00031 #include "CEGUIXmlHandlerHelper.h"
00032 
00033 #include "xercesc/sax2/SAX2XMLReader.hpp"
00034 #include "xercesc/sax2/XMLReaderFactory.hpp"
00035 
00036 // Start of CEGUI namespace section
00037 namespace CEGUI
00038 {
00039 
00040 /*************************************************************************
00041 Definition of constant data for Imageset (and sub-classes)
00042 *************************************************************************/
00043 // Declared in Imageset::xmlHandler
00044 const String Imageset_xmlHandler::ImagesetElement( (utf8*)"Imageset" );
00045 const String Imageset_xmlHandler::ImageElement( (utf8*)"Image" );
00046 const char      Imageset_xmlHandler::ImagesetImageFileAttribute[]               = "Imagefile";
00047 const char      Imageset_xmlHandler::ImagesetResourceGroupAttribute[]   = "ResourceGroup";
00048 const char      Imageset_xmlHandler::ImagesetNameAttribute[]                    = "Name";
00049 const char      Imageset_xmlHandler::ImagesetNativeHorzResAttribute[]   = "NativeHorzRes";
00050 const char      Imageset_xmlHandler::ImagesetNativeVertResAttribute[]   = "NativeVertRes";
00051 const char      Imageset_xmlHandler::ImagesetAutoScaledAttribute[]              = "AutoScaled";
00052 const char      Imageset_xmlHandler::ImageNameAttribute[]                               = "Name";
00053 const char      Imageset_xmlHandler::ImageXPosAttribute[]                               = "XPos";
00054 const char      Imageset_xmlHandler::ImageYPosAttribute[]                               = "YPos";
00055 const char      Imageset_xmlHandler::ImageWidthAttribute[]                              = "Width";
00056 const char      Imageset_xmlHandler::ImageHeightAttribute[]                             = "Height";
00057 const char      Imageset_xmlHandler::ImageXOffsetAttribute[]                    = "XOffset";
00058 const char      Imageset_xmlHandler::ImageYOffsetAttribute[]                    = "YOffset";
00059 
00060 /*************************************************************************
00061 SAX2 Handler methods
00062 *************************************************************************/
00063 void Imageset_xmlHandler::startElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname, const XERCES_CPP_NAMESPACE::Attributes& attrs)
00064 {
00065         XERCES_CPP_NAMESPACE_USE
00066         String element(XmlHandlerHelper::transcodeXmlCharToString(localname));
00067 
00068         // handle an Image element (extract all element attributes and use data to define an Image for the Imageset)
00069         if (element == ImageElement)
00070         {
00071                 String  name(XmlHandlerHelper::getAttributeValueAsString(attrs, ImageNameAttribute));
00072 
00073                 Rect    rect;
00074                 rect.d_left     = (float)XmlHandlerHelper::getAttributeValueAsInteger(attrs, ImageXPosAttribute);
00075                 rect.d_top      = (float)XmlHandlerHelper::getAttributeValueAsInteger(attrs, ImageYPosAttribute);
00076                 rect.setWidth((float)XmlHandlerHelper::getAttributeValueAsInteger(attrs, ImageWidthAttribute));
00077                 rect.setHeight((float)XmlHandlerHelper::getAttributeValueAsInteger(attrs, ImageHeightAttribute));
00078 
00079                 Point   offset;
00080                 offset.d_x      = (float)XmlHandlerHelper::getAttributeValueAsInteger(attrs, ImageXOffsetAttribute);
00081                 offset.d_y      = (float)XmlHandlerHelper::getAttributeValueAsInteger(attrs, ImageYOffsetAttribute);
00082 
00083                 d_imageset->defineImage(name, rect, offset);
00084         }
00085         // handle root Imageset element
00086         else if (element == ImagesetElement)
00087         {
00088                 d_imageset->d_name = XmlHandlerHelper::getAttributeValueAsString(attrs, ImagesetNameAttribute);
00089 
00090                 Logger::getSingleton().logEvent("Started creation of Imageset '" + d_imageset->d_name + "' via XML file.", Informative);
00091 
00092                 //
00093                 // load auto-scaling configuration
00094                 //
00095                 float hres, vres;
00096 
00097                 // get native horizontal resolution
00098                 hres = (float)XmlHandlerHelper::getAttributeValueAsInteger(attrs, ImagesetNativeHorzResAttribute);
00099 
00100                 // get native vertical resolution
00101                 vres = (float)XmlHandlerHelper::getAttributeValueAsInteger(attrs, ImagesetNativeVertResAttribute);
00102 
00103                 d_imageset->setNativeResolution(Size(hres, vres));
00104 
00105                 // get auto-scaling setting
00106                 String autoscale(XmlHandlerHelper::getAttributeValueAsString(attrs, ImagesetAutoScaledAttribute));
00107 
00108                 // enable / disable auto-scaling for this Imageset according to the setting
00109                 if ((autoscale == (utf8*)"true") || (autoscale == (utf8*)"1"))
00110                 {
00111                         d_imageset->setAutoScalingEnabled(true);
00112                 }
00113                 else
00114                 {
00115                         d_imageset->setAutoScalingEnabled(false);
00116                 }
00117 
00118                 //
00119                 // Create a Texture object via the specified filename, and set it as the texture for the Imageset
00120                 //
00121                 String filename(XmlHandlerHelper::getAttributeValueAsString(attrs, ImagesetImageFileAttribute));
00122         String resourceGroup(XmlHandlerHelper::getAttributeValueAsString(attrs, ImagesetResourceGroupAttribute));
00123 
00124                 try
00125                 {
00126                         d_imageset->d_texture = System::getSingleton().getRenderer()->createTexture(filename, resourceGroup);
00127                 }
00128                 catch(...)
00129                 {
00130                         throw RendererException((utf8*)"Imageset::xmlHandler::startElement - An unexpected error occurred while creating a Texture object from file '" + filename + "'");
00131                 }
00132 
00133         }
00134         // anything else is an error which *should* have already been caught by XML validation
00135         else
00136         {
00137                 throw FileIOException("Imageset::xmlHandler::startElement - Unexpected data was found while parsing the Imageset file: '" + element + "' is unknown.");
00138         }
00139 
00140 }
00141 
00142 void Imageset_xmlHandler::endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname)
00143 {
00144         XERCES_CPP_NAMESPACE_USE
00145         String element(XmlHandlerHelper::transcodeXmlCharToString(localname));
00146 
00147         if (element == ImagesetElement)
00148         {
00149                 Logger::getSingleton().logEvent("Finished creation of Imageset '" + d_imageset->d_name + "' via XML file.", Informative);
00150         }
00151 }
00152 
00153 
00154 void Imageset_xmlHandler::warning(const XERCES_CPP_NAMESPACE::SAXParseException &exc)
00155 {
00156         throw(exc);
00157 }
00158 
00159 void Imageset_xmlHandler::error(const XERCES_CPP_NAMESPACE::SAXParseException &exc)
00160 {
00161         throw(exc);
00162 }
00163 
00164 void Imageset_xmlHandler::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &exc)
00165 {
00166         throw(exc);
00167 }
00168 
00169 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:06 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1