//+----------------------------------------------------------------------------+ //| Description: Magic Set Editor - Program to make card games | //| Copyright: (C) Twan van Laarhoven and the other MSE developers | //| License: GNU General Public License 2 or later (see file COPYING) | //+----------------------------------------------------------------------------+ // ----------------------------------------------------------------------------- : Includes #include #include #include #include #include #include #include // for selecting stylesheets on load error // ----------------------------------------------------------------------------- : StyleSheet IMPLEMENT_DYNAMIC_ARG(StyleSheet*, stylesheet_for_reading, nullptr); StyleSheet::StyleSheet() : card_width(100), card_height(100) , card_dpi(96), card_background(*wxWHITE) , dependencies_initialized(false) {} StyleSheetP StyleSheet::byGameAndName(const Game& game, const String& name) { /// Alternative stylesheets for game static map stylesheet_alternatives; String full_name = name; if (!full_name.EndsWith(_(".mse-style"))) full_name = full_name + _(".mse-style"); if (!full_name.StartsWith(game.name() + _("-"))) full_name = game.name() + _("-") + full_name; try { map::const_iterator it = stylesheet_alternatives.find(full_name); if (it != stylesheet_alternatives.end()) { return package_manager.open(it->second); } else { return package_manager.open(full_name); } } catch (PackageNotFoundError& e) { // load an alternative stylesheet StyleSheetP ss = select_stylesheet(game, full_name); if (ss) { stylesheet_alternatives[full_name] = ss->relativeFilename(); return ss; } else { queue_message(MESSAGE_ERROR, _("Missing stylesheet: ") + full_name); throw e; } } } String StyleSheet::stylesheetName() const { String sn = name(), gn = game->name(); if (sn.size() + 1 > gn.size()) { return sn.substr(gn.size() + 1); // remove "gamename-" } else { return sn; } } String StyleSheet::typeNameStatic() { return _("style"); } String StyleSheet::typeName() const { return _("style"); } Version StyleSheet::fileVersion() const { return file_version_stylesheet; } void StyleSheet::validate(Version ver) { Packaged::validate(ver); if (!game) { throw Error(_ERROR_1_("no game specified",_TYPE_("stylesheet"))); } // a stylesheet depends on the game it is made for requireDependency(game.get()); // sort the update_cards_scripts from oldest to newest std::sort(update_cards_scripts.begin(), update_cards_scripts.end(), [](const auto& a, const auto& b) { return *a < *b; }); } StyleP StyleSheet::styleFor(const FieldP& field) { if (card_style.containsKey(field)) { return card_style[field]; } else if (set_info_style.containsKey(field)) { return set_info_style[field]; } else if (styling_style.containsKey(field)) { return styling_style[field]; } else { throw InternalError(_("Can not find styling for field '")+field->name+_("'in stylesheet")); } } void mark_dependency_value(const StyleSheet& stylesheet, const Dependency& dep) { stylesheet.game->dependent_scripts_stylesheet.add(dep); } IMPLEMENT_REFLECTION(StyleSheet) { REFLECT_BASE(Packaged); REFLECT(game); REFLECT(card_width); REFLECT(card_height); REFLECT(card_dpi); REFLECT(card_background); REFLECT(card_regions); REFLECT(init_script); // styling REFLECT(styling_fields); REFLECT_IF_READING styling_style.init(styling_fields); REFLECT(styling_style); // style of game fields if (game) { REFLECT_IF_READING { card_style.init(game->card_fields); set_info_style.cloneFrom(game->default_set_style); } REFLECT(set_info_style); REFLECT(card_style); } // extra card fields REFLECT(extra_card_fields); REFLECT_IF_READING { extra_card_style.init(extra_card_fields); } REFLECT(extra_card_style); REFLECT_NO_SCRIPT(update_cards_scripts); } // special behaviour of reading/writing StyleSheetPs: only read/write the name void Reader::handle(StyleSheetP& stylesheet) { if (!game_for_reading()) { throw InternalError(_("game_for_reading not set")); } stylesheet = StyleSheet::byGameAndName(*game_for_reading(), getValue()); } void Writer::handle(const StyleSheetP& stylesheet) { if (stylesheet) handle(stylesheet->stylesheetName()); }