2013-11-12 23:28:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2013-11-12 23:28:15 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2013-12-30 23:23:34 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/variant.hpp>
|
|
|
|
#include <Eigen/Dense>
|
|
|
|
#include "pugiXML/pugixml.hpp"
|
|
|
|
#include "GuiComponent.h"
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class TextListComponent;
|
|
|
|
|
|
|
|
class Sound;
|
|
|
|
class ImageComponent;
|
|
|
|
class NinePatchComponent;
|
|
|
|
class TextComponent;
|
|
|
|
class Window;
|
|
|
|
|
|
|
|
namespace ThemeFlags
|
2013-11-12 23:28:15 +00:00
|
|
|
{
|
2013-12-30 23:23:34 +00:00
|
|
|
enum PropertyFlags : unsigned int
|
|
|
|
{
|
|
|
|
PATH = 1,
|
|
|
|
POSITION = 2,
|
|
|
|
SIZE = 4,
|
|
|
|
ORIGIN = 8,
|
|
|
|
COLOR = 16,
|
|
|
|
FONT_PATH = 32,
|
|
|
|
FONT_SIZE = 64,
|
|
|
|
TILING = 128,
|
|
|
|
SOUND = 256,
|
|
|
|
CENTER = 512,
|
|
|
|
TEXT = 1024
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThemeException : public std::exception
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
std::string msg;
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
public:
|
|
|
|
virtual const char* what() const throw() { return msg.c_str(); }
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
template<typename T>
|
|
|
|
friend ThemeException& operator<<(ThemeException& e, T msg);
|
|
|
|
|
|
|
|
inline void setFile(const std::string& filename) { *this << "Error loading theme from \"" << filename << "\":\n "; }
|
2013-11-12 23:28:15 +00:00
|
|
|
};
|
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
template<typename T>
|
|
|
|
ThemeException& operator<<(ThemeException& e, T appendMsg)
|
2013-11-12 23:28:15 +00:00
|
|
|
{
|
2013-12-30 23:23:34 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << e.msg << appendMsg;
|
|
|
|
e.msg = ss.str();
|
|
|
|
return e;
|
|
|
|
}
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
class ThemeData
|
|
|
|
{
|
|
|
|
private:
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
class ThemeElement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool extra;
|
|
|
|
std::string type;
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
std::map< std::string, boost::variant<Eigen::Vector2f, std::string, unsigned int, float, bool> > properties;
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
template<typename T>
|
|
|
|
T get(const std::string& prop) { return boost::get<T>(properties.at(prop)); }
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
inline bool has(const std::string& prop) { return (properties.find(prop) != properties.end()); }
|
|
|
|
};
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
class ThemeView
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ThemeView() : mExtrasDirty(true) {}
|
|
|
|
virtual ~ThemeView() { for(auto it = mExtras.begin(); it != mExtras.end(); it++) delete *it; }
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
std::map<std::string, ThemeElement> elements;
|
|
|
|
|
|
|
|
const std::vector<GuiComponent*>& getExtras(Window* window);
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mExtrasDirty;
|
|
|
|
std::vector<GuiComponent*> mExtras;
|
|
|
|
};
|
2013-11-12 23:28:15 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
ThemeData();
|
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
// throws ThemeException
|
2013-11-12 23:28:15 +00:00
|
|
|
void loadFile(const std::string& path);
|
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
enum ElementPropertyType
|
|
|
|
{
|
|
|
|
NORMALIZED_PAIR,
|
|
|
|
PATH,
|
|
|
|
STRING,
|
|
|
|
COLOR,
|
|
|
|
FLOAT,
|
|
|
|
BOOLEAN
|
|
|
|
};
|
|
|
|
|
|
|
|
void renderExtras(const std::string& view, Window* window, const Eigen::Affine3f& transform);
|
|
|
|
|
|
|
|
void applyToImage(const std::string& view, const std::string& element, ImageComponent* image, unsigned int properties);
|
|
|
|
void applyToNinePatch(const std::string& view, const std::string& element, NinePatchComponent* patch, unsigned int properties);
|
|
|
|
void applyToText(const std::string& view, const std::string& element, TextComponent* text, unsigned int properties);
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
template <typename T>
|
|
|
|
void applyToTextList(const std::string& view, const std::string& element, TextListComponent<T>* list, unsigned int properties);
|
|
|
|
|
|
|
|
void playSound(const std::string& name);
|
2013-11-23 20:04:11 +00:00
|
|
|
|
2013-11-12 23:28:15 +00:00
|
|
|
private:
|
2013-12-30 23:23:34 +00:00
|
|
|
static std::map< std::string, std::map<std::string, ElementPropertyType> > sElementMap;
|
|
|
|
|
|
|
|
boost::filesystem::path mPath;
|
|
|
|
float mVersion;
|
|
|
|
|
|
|
|
ThemeView parseView(const pugi::xml_node& view);
|
|
|
|
ThemeElement parseElement(const pugi::xml_node& element, const std::map<std::string, ElementPropertyType>& typeMap);
|
|
|
|
|
|
|
|
ThemeElement* getElement(const std::string& viewName, const std::string& elementName);
|
|
|
|
|
|
|
|
std::map<std::string, ThemeView> mViews;
|
|
|
|
|
|
|
|
std::map< std::string, std::shared_ptr<Sound> > mSoundCache;
|
2013-11-12 23:28:15 +00:00
|
|
|
};
|
2013-12-30 23:23:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void ThemeData::applyToTextList(const std::string& view, const std::string& element, TextListComponent<T>* list, unsigned int properties)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|