From b1ef166bf391a12f4a45136dd61c51573f026bcf Mon Sep 17 00:00:00 2001 From: Gee Date: Mon, 25 Jul 2005 19:06:18 +0000 Subject: [PATCH] Added support for customized colors. This was SVN commit r2547. --- source/gui/CGUI.cpp | 53 ++++++++++++++++++++++++++++++++++++++---- source/gui/CGUI.h | 41 +++++++++++++++++++++++++++++++- source/gui/GUIutil.cpp | 17 ++++++++++++++ 3 files changed, 106 insertions(+), 5 deletions(-) diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index a686fd28e7..af76e19c76 100755 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -950,6 +950,19 @@ void CGUI::DrawText(SGUIText &Text, const CColor &DefaultColor, // -- GL } +bool CGUI::GetPreDefinedColor(const CStr &name, CColor &Output) +{ + if (m_PreDefinedColors.count(name) == 0) + { + return false; + } + else + { + Output = m_PreDefinedColors[name]; + return true; + } +} + void CGUI::ReportParseError(const char *str, ...) { va_list argp; @@ -1118,6 +1131,11 @@ void CGUI::Xeromyces_ReadRootSetup(XMBElement Element, CXeromyces* pFile) Xeromyces_ReadTooltip(child, pFile); } else + if (name == "color") + { + Xeromyces_ReadColor(child, pFile); + } + else { debug_warn("Invalid data - DTD shouldn't allow this"); } @@ -1461,10 +1479,9 @@ void CGUI::Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite // Image object we're adding SGUIImage image; - // TODO Gee: (2004-08-30) This is not how to set defaults. - CStr DefaultTextureSize ("0 0 100% 100%"); - image.m_TextureSize = CClientArea(DefaultTextureSize); - image.m_Size = CClientArea(DefaultTextureSize); + // Set defaults (or maybe do that in DTD?) + image.m_TextureSize = CClientArea("0 0 100% 100%"); + image.m_Size = CClientArea("0 0 100% 100%"); // TODO Gee: Setup defaults here (or maybe they are in the SGUIImage ctor) @@ -1811,3 +1828,31 @@ void CGUI::Xeromyces_ReadTooltip(XMBElement Element, CXeromyces* pFile) AddObject(object); } + +// Reads Custom Color +void CGUI::Xeromyces_ReadColor(XMBElement Element, CXeromyces* pFile) +{ + // Read the color and stor in m_PreDefinedColors + + XMBAttributeList attributes = Element.getAttributes(); + + //IGUIObject* object = new CTooltip; + CColor color; + CStr name = attributes.getNamedItem(pFile->getAttributeID("name")); + + // Try parsing value + CStr value (Element.getText()); + if (value.Length()) + { + // Try setting color to value + if (!color.ParseString(value, 255.f)) + { + ReportParseError("Unable to create custom color '%s'. Invalid color syntax.", name.c_str()); + } + else + { + // input color + m_PreDefinedColors[name] = color; + } + } +} diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index 342589775c..2626938f33 100755 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -243,6 +243,12 @@ public: */ SGUIIcon GetIcon(const CStr &str) const { return m_Icons.find(str)->second; } + /** + * Get pre-defined color (if it exists) + * Returns false if it fails. + */ + bool GetPreDefinedColor(const CStr &name, CColor &Output); + private: /** * Updates the object pointers, needs to be called each @@ -334,6 +340,8 @@ private: +- (ReadScrollBar) | +- (ReadIcon) + | + +- (ReadColor) */ //@{ @@ -482,9 +490,31 @@ private: * @see LoadXMLFile() */ void Xeromyces_ReadIcon(XMBElement Element, CXeromyces* pFile); - + + /** + * Reads in the element (the XMBElement) and stores the + * result as an object with the name __tooltip_#. + * + * @param Element The Xeromyces object that represents + * the scrollbar-tag. + * @param pFile The Xeromyces object for the file being read + * + * @see LoadXMLFile() + */ void Xeromyces_ReadTooltip(XMBElement Element, CXeromyces* pFile); + /** + * Reads in the element (the XMBElement) and stores the + * result in m_PreDefinedColors + * + * @param Element The Xeromyces object that represents + * the scrollbar-tag. + * @param pFile The Xeromyces object for the file being read + * + * @see LoadXMLFile() + */ + void Xeromyces_ReadColor(XMBElement Element, CXeromyces* pFile); + //@} private: @@ -520,8 +550,17 @@ private: // TODO Gee: Used? int16_t m_Errors; + // Tooltip GUITooltip m_Tooltip; + /** + * This is a bank of custom colors, it is simply a look up table that + * will return a color object when someone inputs the name of that + * color. Of course the colors have to be declared in XML, there are + * no hard-coded values. + */ + std::map m_PreDefinedColors; + //@} //-------------------------------------------------------- /** @name Objects */ diff --git a/source/gui/GUIutil.cpp b/source/gui/GUIutil.cpp index 2b270f247d..4d9382191b 100755 --- a/source/gui/GUIutil.cpp +++ b/source/gui/GUIutil.cpp @@ -80,6 +80,15 @@ bool __ParseString(const CStr& Value, CClientArea &Output) template <> bool GUI::ParseColor(const CStr& Value, CColor &Output, float DefaultAlpha) { + // First, check our database in g_GUI for pre-defined colors + // If we find anything, we'll ignore DefaultAlpha +#ifdef g_GUI + // If it fails, it won't do anything with Output + if (g_GUI.GetPreDefinedColor(Value, Output)) + return true; + +#endif // g_GUI + return Output.ParseString(Value, DefaultAlpha); } @@ -87,6 +96,14 @@ bool GUI::ParseColor(const CStr& Value, CColor &Output, float DefaultAlpha) template <> bool __ParseString(const CStr& Value, CColor &Output) { + // First, check our database in g_GUI for pre-defined colors +#ifdef g_GUI + // If it fails, it won't do anything with Output + if (g_GUI.GetPreDefinedColor(Value, Output)) + return true; + +#endif // g_GUI + return Output.ParseString(Value, 255.f); }