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,
|
|
|
|
TILING = 128,
|
|
|
|
SOUND = 256,
|
|
|
|
CENTER = 512,
|
2014-01-03 14:26:39 +00:00
|
|
|
TEXT = 1024,
|
|
|
|
|
|
|
|
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) {};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
virtual ~ThemeExtras();
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
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);
|
|
|
|
|
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
|
|
|
};
|
2013-12-30 23:23:34 +00:00
|
|
|
|
2014-01-01 05:39:22 +00:00
|
|
|
// okay ideas for applying themes to GuiComponents:
|
|
|
|
// 1. ThemeData::applyToImage(component, args)
|
|
|
|
// NO, BECAUSE:
|
|
|
|
// - for templated types (TextListComponent) have to include the whole template in a header
|
|
|
|
// - inconsistent definitions if mixing templated types (some in a .cpp, some in a .h/.inl)
|
|
|
|
// 2. template<typename T> ThemeData::apply(component, args) with specialized templates
|
|
|
|
// NO, BECAUSE:
|
|
|
|
// - doesn't solve the first drawback
|
|
|
|
// - can't customize arguments for specific types
|
|
|
|
// 3. GuiComponent::applyTheme(theme, args) - WINNER
|
|
|
|
// NO, BECAUSE:
|
|
|
|
// - can't access private members of ThemeData
|
|
|
|
// - can't this be solved with enough getters?
|
|
|
|
// - theme->hasElement and theme->getProperty will require 2x as many map lookups (4 vs 2)
|
|
|
|
// - why not just return a const ThemeElement...
|