#pragma once #include #include #include #include #include #include #include #include #include #include "pugixml/pugixml.hpp" #include "GuiComponent.h" template class TextListComponent; class Sound; class ImageComponent; class NinePatchComponent; class TextComponent; class Window; namespace ThemeFlags { enum PropertyFlags : unsigned int { PATH = 1, POSITION = 2, SIZE = 4, ORIGIN = 8, COLOR = 16, FONT_PATH = 32, FONT_SIZE = 64, SOUND = 128, ALIGNMENT = 256, TEXT = 512, FORCE_UPPERCASE = 1024, LINE_SPACING = 2048, DELAY = 4096, ALL = 0xFFFFFFFF }; } class ThemeException : public std::exception { public: std::string msg; virtual const char* what() const throw() { return msg.c_str(); } template friend ThemeException& operator<<(ThemeException& e, T msg); inline void setFiles(const std::deque& 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 << " "; } }; template ThemeException& operator<<(ThemeException& e, T appendMsg) { std::stringstream ss; ss << e.msg << appendMsg; e.msg = ss.str(); return e; } class ThemeExtras : public GuiComponent { public: ThemeExtras(Window* window) : GuiComponent(window) {}; virtual ~ThemeExtras(); // will take ownership of the components within extras (delete them in destructor or when setExtras is called again) void setExtras(const std::vector& extras); private: std::vector mExtras; }; struct ThemeSet { boost::filesystem::path path; inline std::string getName() const { return path.stem().string(); } inline boost::filesystem::path getThemePath(const std::string& system) const { return path/system/"theme.xml"; } }; class ThemeData { public: class ThemeElement { public: bool extra; std::string type; std::map< std::string, boost::variant > properties; template T get(const std::string& prop) const { return boost::get(properties.at(prop)); } inline bool has(const std::string& prop) const { return (properties.find(prop) != properties.end()); } }; private: class ThemeView { public: std::map elements; std::vector orderedKeys; }; public: ThemeData(); // throws ThemeException void loadFile(const std::string& path); enum ElementPropertyType { NORMALIZED_PAIR, PATH, STRING, COLOR, FLOAT, BOOLEAN }; // 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; static std::vector makeExtras(const std::shared_ptr& theme, const std::string& view, Window* window); static const std::shared_ptr& getDefault(); static std::map getThemeSets(); static boost::filesystem::path getThemeFromCurrentSet(const std::string& system); private: static std::map< std::string, std::map > sElementMap; std::deque mPaths; float mVersion; 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& typeMap, ThemeElement& element); std::map mViews; };