Permalink
Browse files

load skin rects from config

  • Loading branch information...
robn committed Oct 8, 2012
1 parent 3849e1e commit 834eef6642a1b20fe348faf38f32d6fa46b4845d
Showing with 100 additions and 31 deletions.
  1. +14 −0 data/ui/Skin.ini
  2. +64 −14 src/ui/Skin.cpp
  3. +22 −17 src/ui/Skin.h
View
@@ -1 +1,15 @@
TextureFile=textures/widgets.png
+BackgroundNormal=170,81,31,31,10
+BackgroundActive=170,206,31,31,10
+ButtonDisabled=10,12,35,35,12
+ButtonNormal=10,77,35,35,12
+ButtonHover=10,142,35,35,12
+ButtonActive=10,207,35,35,12
+CheckboxDisabled=75,17,25,25
+CheckboxNormal=75,87,25,25
+CheckboxHover=75,152,25,25
+CheckboxActive=75,212,25,25
+CheckboxCheckedDisabled=120,17,25,25
+CheckboxCheckedNormal=120,87,25,25
+CheckboxCheckedHover=120,152,25,25
+CheckboxCheckedActive=120,212,25,25
View
@@ -9,23 +9,23 @@ namespace UI {
static const float SKIN_SIZE = 512.0f;
-const Skin::BorderedRectElement Skin::s_backgroundNormal(170, 81, 31, 31, 10);
-const Skin::BorderedRectElement Skin::s_backgroundActive(170, 206, 31, 31, 10);
+Skin::BorderedRectElement Skin::s_backgroundNormal;
+Skin::BorderedRectElement Skin::s_backgroundActive;
-const Skin::BorderedRectElement Skin::s_buttonDisabled(10, 12, 35, 35, 12);
-const Skin::BorderedRectElement Skin::s_buttonNormal(10, 77, 35, 35, 12);
-const Skin::BorderedRectElement Skin::s_buttonHover(10, 142, 35, 35, 12);
-const Skin::BorderedRectElement Skin::s_buttonActive(10, 207, 35, 35, 12);
+Skin::BorderedRectElement Skin::s_buttonDisabled;
+Skin::BorderedRectElement Skin::s_buttonNormal;
+Skin::BorderedRectElement Skin::s_buttonHover;
+Skin::BorderedRectElement Skin::s_buttonActive;
-const Skin::RectElement Skin::s_checkboxDisabled(75, 17, 25, 25);
-const Skin::RectElement Skin::s_checkboxNormal(75, 87, 25, 25);
-const Skin::RectElement Skin::s_checkboxHover(75, 152, 25, 25);
-const Skin::RectElement Skin::s_checkboxActive(75, 212, 25, 25);
+Skin::RectElement Skin::s_checkboxDisabled;
+Skin::RectElement Skin::s_checkboxNormal;
+Skin::RectElement Skin::s_checkboxHover;
+Skin::RectElement Skin::s_checkboxActive;
-const Skin::RectElement Skin::s_checkboxCheckedDisabled(120, 17, 25, 25);
-const Skin::RectElement Skin::s_checkboxCheckedNormal(120, 87, 25, 25);
-const Skin::RectElement Skin::s_checkboxCheckedHover(120, 152, 25, 25);
-const Skin::RectElement Skin::s_checkboxCheckedActive(120, 212, 25, 25);
+Skin::RectElement Skin::s_checkboxCheckedDisabled;
+Skin::RectElement Skin::s_checkboxCheckedNormal;
+Skin::RectElement Skin::s_checkboxCheckedHover;
+Skin::RectElement Skin::s_checkboxCheckedActive;
Skin::Skin(const std::string &filename, Graphics::Renderer *renderer) :
m_config(filename),
@@ -38,6 +38,21 @@ Skin::Skin(const std::string &filename, Graphics::Renderer *renderer) :
m_material.Reset(m_renderer->CreateMaterial(desc));
m_material->texture0 = m_texture.Get();
m_material->diffuse = Color::WHITE;
+
+ s_backgroundNormal = LoadBorderedRectElement(m_config.String("BackgroundNormal"));
+ s_backgroundActive = LoadBorderedRectElement(m_config.String("BackgroundActive"));
+ s_buttonDisabled = LoadBorderedRectElement(m_config.String("ButtonDisabled"));
+ s_buttonNormal = LoadBorderedRectElement(m_config.String("ButtonNormal"));
+ s_buttonHover = LoadBorderedRectElement(m_config.String("ButtonHover"));
+ s_buttonActive = LoadBorderedRectElement(m_config.String("ButtonActive"));
+ s_checkboxDisabled = LoadRectElement(m_config.String("CheckboxDisabled"));
+ s_checkboxNormal = LoadRectElement(m_config.String("CheckboxNormal"));
+ s_checkboxHover = LoadRectElement(m_config.String("CheckboxHover"));
+ s_checkboxActive = LoadRectElement(m_config.String("CheckboxActive"));
+ s_checkboxCheckedDisabled = LoadRectElement(m_config.String("CheckboxCheckedDisabled"));
+ s_checkboxCheckedNormal = LoadRectElement(m_config.String("CheckboxCheckedNormal"));
+ s_checkboxCheckedHover = LoadRectElement(m_config.String("CheckboxCheckedHover"));
+ s_checkboxCheckedActive = LoadRectElement(m_config.String("CheckboxCheckedActive"));
}
static inline vector2f scaled(const vector2f &v)
@@ -100,6 +115,41 @@ void Skin::DrawBorderedRectElement(const BorderedRectElement &element, const Poi
m_renderer->DrawTriangles(&va, m_material.Get(), Graphics::TRIANGLE_STRIP);
}
+static void SplitSpec(const std::string &spec, std::vector<int> &output)
+{
+ static const std::string delim(",");
+
+ size_t i = 0, start = 0, end = 0;
+ while (end != std::string::npos) {
+ // get to the first non-delim char
+ start = spec.find_first_not_of(delim, end);
+
+ // read the end, no more to do
+ if (start == std::string::npos)
+ break;
+
+ // find the end - next delim or end of string
+ end = spec.find_first_of(delim, start);
+
+ // extract the fragment and remember it
+ output[i++] = atoi(spec.substr(start, (end == std::string::npos) ? std::string::npos : end - start).c_str());
+ }
+}
+
+Skin::RectElement Skin::LoadRectElement(const std::string &spec)
+{
+ std::vector<int> v(4);
+ SplitSpec(spec, v);
+ return RectElement(v[0], v[1], v[2], v[3]);
+}
+
+Skin::BorderedRectElement Skin::LoadBorderedRectElement(const std::string &spec)
+{
+ std::vector<int> v(5);
+ SplitSpec(spec, v);
+ return BorderedRectElement(v[0], v[1], v[2], v[3], v[4]);
+}
+
Skin::Config::Config(const std::string &filename) : IniConfig(filename)
{
m_map["TextureFile"] = "textures/widgets.png";
View
@@ -72,32 +72,34 @@ class Skin {
#endif
struct RectElement {
+ RectElement() {}
RectElement(unsigned int x, unsigned int y, unsigned int w, unsigned int h) : pos(x,y), size(w,h) {}
- const Point pos;
- const Point size;
+ Point pos;
+ Point size;
};
struct BorderedRectElement : public RectElement {
+ BorderedRectElement() : borderWidth(0) {}
BorderedRectElement(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned int _borderWidth) : RectElement(x, y, w, h), borderWidth(_borderWidth) {}
- const unsigned int borderWidth;
+ unsigned int borderWidth;
};
- static const BorderedRectElement s_backgroundNormal;
- static const BorderedRectElement s_backgroundActive;
+ static BorderedRectElement s_backgroundNormal;
+ static BorderedRectElement s_backgroundActive;
- static const BorderedRectElement s_buttonDisabled;
- static const BorderedRectElement s_buttonNormal;
- static const BorderedRectElement s_buttonHover;
- static const BorderedRectElement s_buttonActive;
+ static BorderedRectElement s_buttonDisabled;
+ static BorderedRectElement s_buttonNormal;
+ static BorderedRectElement s_buttonHover;
+ static BorderedRectElement s_buttonActive;
- static const RectElement s_checkboxDisabled;
- static const RectElement s_checkboxNormal;
- static const RectElement s_checkboxHover;
- static const RectElement s_checkboxActive;
- static const RectElement s_checkboxCheckedDisabled;
- static const RectElement s_checkboxCheckedNormal;
- static const RectElement s_checkboxCheckedHover;
- static const RectElement s_checkboxCheckedActive;
+ static RectElement s_checkboxDisabled;
+ static RectElement s_checkboxNormal;
+ static RectElement s_checkboxHover;
+ static RectElement s_checkboxActive;
+ static RectElement s_checkboxCheckedDisabled;
+ static RectElement s_checkboxCheckedNormal;
+ static RectElement s_checkboxCheckedHover;
+ static RectElement s_checkboxCheckedActive;
private:
class Config : public IniConfig {
@@ -113,6 +115,9 @@ class Skin {
void DrawRectElement(const RectElement &element, const Point &pos, const Point &size) const;
void DrawBorderedRectElement(const BorderedRectElement &element, const Point &pos, const Point &size) const;
+
+ RectElement LoadRectElement(const std::string &spec);
+ BorderedRectElement LoadBorderedRectElement(const std::string &spec);
};
}

0 comments on commit 834eef6

Please sign in to comment.