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>
|
2013-12-31 03:48:28 +00:00
|
|
|
#include <deque>
|
2013-11-12 23:28:15 +00:00
|
|
|
#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,
|
2014-01-19 18:23:01 +00:00
|
|
|
SOUND = 128,
|
2014-01-22 02:43:33 +00:00
|
|
|
ALIGNMENT = 256,
|
2014-01-19 18:23:01 +00:00
|
|
|
TEXT = 512,
|
2014-01-03 14:26:39 +00:00
|
|
|
|
|
|
|
ALL = 0xFFFFFFFF
|
2013-12-30 23:23:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThemeException : public std::exception
|
|
|
|
{
|
2013-12-31 03:48:28 +00:00
|
|
|
public:
|
2013-12-30 23:23:34 +00:00
|
|
|
std::string msg;
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
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);
|
|
|
|
|
2013-12-31 03:48:28 +00:00
|
|
|
inline void setFiles(const std::deque<boost::filesystem::path>& deque)
|
|
|
|
{
|
|
|
|
*this << "from theme \"" << deque.front().string() << "\"\n";
|
|
|
|
for(auto it = deque.begin() + 1; it != deque.end(); it++)
|
|
|
|
*this << " (from included file \"" << (*it).string() << "\")\n";
|
|
|
|
*this << " ";
|
|
|
|
}
|
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
|
|
|
|
2014-01-03 14:26:39 +00:00
|
|
|
class ThemeExtras : public GuiComponent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ThemeExtras(Window* window) : GuiComponent(window) {};
|
2014-01-09 23:13:52 +00:00
|
|
|
virtual ~ThemeExtras();
|
2014-01-03 14:26:39 +00:00
|
|
|
|
|
|
|
// will take ownership of the components within extras (delete them in destructor or when setExtras is called again)
|
|
|
|
void setExtras(const std::vector<GuiComponent*>& extras);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<GuiComponent*> mExtras;
|
|
|
|
};
|
|
|
|
|
2013-12-30 23:23:34 +00:00
|
|
|
class ThemeData
|
|
|
|
{
|
2014-01-01 05:39:22 +00:00
|
|
|
public:
|
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>
|
2014-01-01 05:39:22 +00:00
|
|
|
T get(const std::string& prop) const { return boost::get<T>(properties.at(prop)); }
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2014-01-01 05:39:22 +00:00
|
|
|
inline bool has(const std::string& prop) const { return (properties.find(prop) != properties.end()); }
|
2013-12-30 23:23:34 +00:00
|
|
|
};
|
2013-11-12 23:28:15 +00:00
|
|
|
|
2014-01-01 05:39:22 +00:00
|
|
|
private:
|
2013-12-30 23:23:34 +00:00
|
|
|
class ThemeView
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::map<std::string, ThemeElement> elements;
|
2014-01-09 23:13:52 +00:00
|
|
|
std::vector<std::string> orderedKeys;
|
2013-12-30 23:23:34 +00:00
|
|
|
};
|
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
|
|
|
|
};
|
|
|
|
|
2014-01-01 05:39:22 +00:00
|
|
|
// If expectedType is an empty string, will do no type checking.
|
|
|
|
const ThemeElement* getElement(const std::string& view, const std::string& element, const std::string& expectedType) const;
|
2013-12-31 03:48:28 +00:00
|
|
|
|
2014-01-03 14:26:39 +00:00
|
|
|
static std::vector<GuiComponent*> makeExtras(const std::shared_ptr<ThemeData>& theme, const std::string& view, Window* window);
|
|
|
|
|
2014-01-03 16:40:36 +00:00
|
|
|
static const std::shared_ptr<ThemeData>& getDefault();
|
|
|
|
|
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;
|
|
|
|
|
2013-12-31 03:48:28 +00:00
|
|
|
std::deque<boost::filesystem::path> mPaths;
|
2013-12-30 23:23:34 +00:00
|
|
|
float mVersion;
|
|
|
|
|
2013-12-31 03:48:28 +00:00
|
|
|
void parseIncludes(const pugi::xml_node& themeRoot);
|
|
|
|
void parseViews(const pugi::xml_node& themeRoot);
|
|
|
|
void parseView(const pugi::xml_node& viewNode, ThemeView& view);
|
|
|
|
void parseElement(const pugi::xml_node& elementNode, const std::map<std::string, ElementPropertyType>& typeMap, ThemeElement& element);
|
2013-12-30 23:23:34 +00:00
|
|
|
|
|
|
|
std::map<std::string, ThemeView> mViews;
|
2013-11-12 23:28:15 +00:00
|
|
|
};
|