From 0c373cda6acd4d60046c74fe39746acf8e76d353 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Tue, 10 Nov 2009 21:38:07 +0000 Subject: [PATCH] Allow setting label colour from layout XML --- include/Colour.hpp | 4 +++ include/IXMLParser.hpp | 58 ++++++++++++++++++++++++++++------ include/gui2/Label.hpp | 4 +++ include/gui2/RenderContext.hpp | 6 ++-- include/gui2/Widget.hpp | 3 ++ layouts/game.xml | 5 +++ schemas/layout.xsd | 1 + src/Game.cpp | 2 ++ src/gui2/ContainerWidget.cpp | 6 ++-- src/gui2/Label.cpp | 7 ++-- src/gui2/RenderContext.cpp | 5 +-- src/gui2/Widget.cpp | 3 +- 12 files changed, 85 insertions(+), 19 deletions(-) diff --git a/include/Colour.hpp b/include/Colour.hpp index 19fcd28..2b37bbf 100644 --- a/include/Colour.hpp +++ b/include/Colour.hpp @@ -30,4 +30,8 @@ inline Colour makeColour(float r, float g, float b, float a=1.0f) return c; } +namespace colour { + const Colour WHITE = makeColour(1.0f, 1.0f, 1.0f); +} + #endif diff --git a/include/IXMLParser.hpp b/include/IXMLParser.hpp index d463b25..97cccf0 100644 --- a/include/IXMLParser.hpp +++ b/include/IXMLParser.hpp @@ -19,14 +19,62 @@ #define INC_IXMLPARSER_HPP #include "Platform.hpp" +#include "Colour.hpp" #include #include #include +#include + #include #include +namespace { + + template + T xmlAttrCast(const string& str); + + template <> + bool xmlAttrCast(const string& str) + { + istringstream ss(str); + bool result; + + ss >> boolalpha >> result; // Is boolalpha affected by locale? + + if (ss.fail()) + throw runtime_error( + "Cannot parse Boolean attribute with value '" + + str + "'"); + + return result; + } + + template <> + Colour xmlAttrCast(const string& str) + { + istringstream ss(str); + int r, g, b; + + ss >> r >> g >> b; + + if (ss.fail()) + throw runtime_error( + "Cannot parse colour attribute with value '" + + str + "'"); + + return makeColour(r / 255.0f, g / 255.0f, b / 255.0f); + } + + template + T xmlAttrCast(const string& str) + { + return boost::lexical_cast(str); + } + +} + // Container for attributes class AttributeSet { public: @@ -55,16 +103,8 @@ public: char* ascii = xercesc::XMLString::transcode( myAttrs.getValue(index)); - istringstream ss(ascii); - T result; + T result = xmlAttrCast(ascii); - ss >> boolalpha >> result; // Is boolalpha affected by locale? - - if (ss.fail()) - throw runtime_error( - "Cannot parse attribute '" + aName - + "' with value '" + ascii + "'"); - xercesc::XMLString::release(&ascii); return result; } diff --git a/include/gui2/Label.hpp b/include/gui2/Label.hpp index 2bf56dd..837e0d0 100644 --- a/include/gui2/Label.hpp +++ b/include/gui2/Label.hpp @@ -32,12 +32,16 @@ namespace gui { const string& text() const { return text_; } void text(const string& t) { text_ = t; } + Colour colour() const { return colour_; } + void colour(Colour c) { colour_ = c; } + void format(const char* fmt, ...); void render(RenderContext& rc) const; void adjustForTheme(const Theme& theme); private: string text_, fontName; + Colour colour_; }; } diff --git a/include/gui2/RenderContext.hpp b/include/gui2/RenderContext.hpp index d70cf50..3319a88 100644 --- a/include/gui2/RenderContext.hpp +++ b/include/gui2/RenderContext.hpp @@ -44,12 +44,14 @@ namespace gui { void rectangle(int x, int y, int w, int h, Colour c); void border(int x, int y, int w, int h, Colour c); - void print(IFontPtr font, int x, int y, const string& s); + void print(IFontPtr font, int x, int y, const string& s, + Colour col = colour::WHITE); const Theme& theme() const { return theme_; } - private: + void offset(int& x, int& y) const; + private: const Theme& theme_; int origin_x, origin_y; diff --git a/include/gui2/Widget.hpp b/include/gui2/Widget.hpp index 7ba3ec5..b47d5ff 100644 --- a/include/gui2/Widget.hpp +++ b/include/gui2/Widget.hpp @@ -38,9 +38,11 @@ namespace gui { int y() const { return y_; } int width() const { return width_; } int height() const { return height_; } + bool visible() const { return visible_; } void width(int w) { width_ = w; } void height(int h) { height_ = h; } + void visible(bool v) { visible_ = v; } enum Signal { SIG_CLICK @@ -63,6 +65,7 @@ namespace gui { string name_; int x_, y_, width_, height_; + bool visible_; map handlers; diff --git a/layouts/game.xml b/layouts/game.xml index 8f60eac..68ebc2a 100644 --- a/layouts/game.xml +++ b/layouts/game.xml @@ -10,4 +10,9 @@ name="speed_label" font="status-font" x="10" y="10" /> +