From ff5d68f9f5794140adc46e7a16dd6e90ca1723e2 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Thu, 26 Oct 2017 09:27:50 +1100 Subject: [PATCH] Some refactoring of team color data handling These changes cover areas from PR #2101, excluding the removal of the numeric ID color ranges. That can't be done yet (it breaks things). This also excludes any changes to the connect_engine color handling. That needs to be refactored separately. Despite the fact that it's done very similar to the new get_side_color_id function, I need to deal with the fact that every side_engine member in the connect engine keeps a copy of the default color options. * Added a general-purpose function to extract color data from a config. * Refactored the team class's general get-side's-color-data function so it always returns a color ID instead of falling back on a number. * Deployed this change for the leader image coloring in the save_index; said coloring is now always done by ID. * Save color data by ID in team_info instead of using the side number if no color was provided. * Renamed get_side_color_index to get_side_color_id to better reflect its purpose. --- src/editor/action/mouse/mouse_action_item.cpp | 2 +- src/editor/action/mouse/mouse_action_unit.cpp | 2 +- src/editor/palette/unit_palette.cpp | 2 +- src/gui/widgets/unit_preview_pane.cpp | 2 +- src/reports.cpp | 2 +- src/save_index.cpp | 4 +- src/team.cpp | 56 ++++++++++++++----- src/team.hpp | 5 +- src/units/drawer.cpp | 2 +- src/units/unit.cpp | 2 +- src/whiteboard/manager.cpp | 2 +- src/whiteboard/move.cpp | 2 +- 12 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/editor/action/mouse/mouse_action_item.cpp b/src/editor/action/mouse/mouse_action_item.cpp index 762728cef63..ae78ceadfad 100644 --- a/src/editor/action/mouse/mouse_action_item.cpp +++ b/src/editor/action/mouse/mouse_action_item.cpp @@ -234,7 +234,7 @@ void mouse_action_item::set_item_mouse_overlay(editor_display& disp, const overl std::stringstream filename; filename << u.image; // << "~RC(" << u.flag_rgb() << '>' - // << team::get_side_color_index(disp.viewing_side()) << ')'; + // << team::get_side_color_id(disp.viewing_side()) << ')'; surface image(image::get_image(filename.str())); Uint8 alpha = 196; diff --git a/src/editor/action/mouse/mouse_action_unit.cpp b/src/editor/action/mouse/mouse_action_unit.cpp index 73a678bfcc9..3b31553fa95 100644 --- a/src/editor/action/mouse/mouse_action_unit.cpp +++ b/src/editor/action/mouse/mouse_action_unit.cpp @@ -235,7 +235,7 @@ void mouse_action_unit::set_unit_mouse_overlay(editor_display& disp, const unit_ std::stringstream filename; filename << u.image() << "~RC(" << u.flag_rgb() << '>' - << team::get_side_color_index(disp.viewing_side()) << ')'; + << team::get_side_color_id(disp.viewing_side()) << ')'; surface image(image::get_image(filename.str())); Uint8 alpha = 196; diff --git a/src/editor/palette/unit_palette.cpp b/src/editor/palette/unit_palette.cpp index e2d5ce1a53e..3e9df392c6f 100644 --- a/src/editor/palette/unit_palette.cpp +++ b/src/editor/palette/unit_palette.cpp @@ -84,7 +84,7 @@ void unit_palette::draw_item(const unit_type& u, surface& image, std::stringstre std::stringstream filename; filename << u.image() << "~RC(" << u.flag_rgb() << '>' - << team::get_side_color_index(gui_.viewing_side()) << ')'; + << team::get_side_color_id(gui_.viewing_side()) << ')'; image = image::get_image(filename.str()); if(image == nullptr) { diff --git a/src/gui/widgets/unit_preview_pane.cpp b/src/gui/widgets/unit_preview_pane.cpp index 4ae61bfa2b6..e8d5bb5d8af 100644 --- a/src/gui/widgets/unit_preview_pane.cpp +++ b/src/gui/widgets/unit_preview_pane.cpp @@ -258,7 +258,7 @@ void unit_preview_pane::set_displayed_type(const unit_type& type) if(resources::controller) { mods = "~RC(" + type.flag_rgb() + ">" + - team::get_side_color_index(resources::controller->current_side()) + team::get_side_color_id(resources::controller->current_side()) + ")"; } diff --git a/src/reports.cpp b/src/reports.cpp index cab376a0544..5a45d393536 100644 --- a/src/reports.cpp +++ b/src/reports.cpp @@ -1469,7 +1469,7 @@ REPORT_GENERATOR(side_playing, rc) const team &active_team = rc.teams()[rc.screen().playing_team()]; std::string flag_icon = active_team.flag_icon(); std::string old_rgb = game_config::flag_rgb; - std::string new_rgb = team::get_side_color_index(rc.screen().playing_side()); + std::string new_rgb = team::get_side_color_id(rc.screen().playing_side()); std::string mods = "~RC(" + old_rgb + ">" + new_rgb + ")"; if (flag_icon.empty()) flag_icon = game_config::images::flag_icon; diff --git a/src/save_index.cpp b/src/save_index.cpp index d21c31041b3..4d03b80cb32 100644 --- a/src/save_index.cpp +++ b/src/save_index.cpp @@ -396,12 +396,14 @@ void extract_summary_from_config(config& cfg_save, config& cfg_summary) continue; } + const std::string tc_color = team::get_side_color_id_from_config(u); + // Don't count it among the troops units--; leader = u["id"].str(); leader_name = u["name"].str(); leader_image = u["image"].str(); - leader_image_tc_modifier = "~RC(" + u["flag_rgb"].str() + ">" + u["side"].str() + ")"; + leader_image_tc_modifier = "~RC(" + u["flag_rgb"].str() + ">" + tc_color + ")"; } // We need a binary path-independent path to the leader image here so it can be displayed diff --git a/src/team.cpp b/src/team.cpp index bc473d47c53..81fb41d7617 100644 --- a/src/team.cpp +++ b/src/team.cpp @@ -197,11 +197,8 @@ void team::team_info::read(const config& cfg) variables = cfg.child_or_empty("variables"); is_local = cfg["is_local"].to_bool(true); - if(cfg.has_attribute("color")) { - color = cfg["color"].str(); - } else { - color = cfg["side"].str(); - } + color = get_side_color_id_from_config(cfg); + std::cerr << "Setting up side... set color to " << color << std::endl; // If starting new scenario override settings from [ai] tags if(!user_team_name.translatable()) @@ -918,7 +915,7 @@ bool team::shroud_map::copy_from(const std::vector& maps) const color_range team::get_side_color_range(int side) { - std::string index = get_side_color_index(side); + std::string index = get_side_color_id(side); std::map::iterator gp = game_config::team_rgb_range.find(index); if(gp != game_config::team_rgb_range.end()) { @@ -940,18 +937,51 @@ color_t team::get_minimap_color(int side) return get_side_color_range(side).rep(); } -std::string team::get_side_color_index(int side) +std::string team::get_side_color_id(unsigned side) { - size_t index = size_t(side - 1); + try { + const unsigned index = side - 1; - if(resources::gameboard && index < resources::gameboard->teams().size()) { - const std::string side_map = resources::gameboard->teams()[index].color(); - if(!side_map.empty()) { - return side_map; + // If no gameboard (and by extension, team list) is available, use the default side color. + if(!resources::gameboard) { + return game_config::default_colors.at(index); } + + // Else, try to fetch the color from the side's config. + const std::string& side_color = resources::gameboard->teams().at(index).color(); + + if(!side_color.empty()) { + return side_color; + } + + // If the side color data was empty, fall back to the default color. This should only + // happen if the side data hadn't been initialized yet, which is the case if this function + // is being called to set up said side data. :P + return game_config::default_colors.at(index); + } catch(const std::out_of_range&) { + // Side index was invalid! Coloring will fail! + return ""; + } +} + +std::string team::get_side_color_id_from_config(const config& cfg) +{ + const config::attribute_value& c = cfg["color"]; + + // If no color key or value was provided, use the given color for that side. + // If outside a game context (ie, where a list of teams has been constructed), + // this will just be the side's default color. + if(c.blank() || c.empty()) { + return get_side_color_id(cfg["side"].to_unsigned(1)); } - return std::to_string(side); + // Do the same as above for numeric color key values. + if(unsigned side = c.to_unsigned(1)) { + return get_side_color_id(side); + } + + // Else, we should have a color id at this point. Return it. + return c.str(); } std::string team::get_side_highlight_pango(int side) diff --git a/src/team.hpp b/src/team.hpp index 77b10918686..bcc99cc6d7d 100644 --- a/src/team.hpp +++ b/src/team.hpp @@ -361,12 +361,15 @@ public: //function which, when given a 1-based side will return the color used by that side. static const color_range get_side_color_range(int side); + static color_t get_side_rgb(int side) { return(get_side_color_range(side).mid()); } static color_t get_side_rgb_max(int side) { return(get_side_color_range(side).max()); } static color_t get_side_rgb_min(int side) { return(get_side_color_range(side).min()); } static color_t get_side_color(int side); static color_t get_minimap_color(int side); - static std::string get_side_color_index(int side); + + static std::string get_side_color_id(unsigned side); + static std::string get_side_color_id_from_config(const config& cfg); static std::string get_side_highlight_pango(int side); void log_recruitable() const; diff --git a/src/units/drawer.cpp b/src/units/drawer.cpp index d58e81e1b9a..3bf46c0d388 100644 --- a/src/units/drawer.cpp +++ b/src/units/drawer.cpp @@ -201,7 +201,7 @@ void unit_drawer::redraw_unit (const unit & u) const const std::string nozoc = !emit_zoc ? "nozoc-" : ""; const std::string leader = can_recruit ? "leader-" : ""; const std::string selected = sel_hex == loc ? "selected-" : ""; - const std::string tc = team::get_side_color_index(side); + const std::string tc = team::get_side_color_id(side); const std::string ellipse_top = formatter() << ellipse << "-" << leader << nozoc << selected << "top.png~RC(ellipse_red>" << tc << ")"; const std::string ellipse_bot = formatter() << ellipse << "-" << leader << nozoc << selected << "bottom.png~RC(ellipse_red>" << tc << ")"; diff --git a/src/units/unit.cpp b/src/units/unit.cpp index 7d850e2bb02..bfd95818c71 100644 --- a/src/units/unit.cpp +++ b/src/units/unit.cpp @@ -2448,7 +2448,7 @@ unit_movement_resetter::~unit_movement_resetter() std::string unit::TC_image_mods() const { - return formatter() << "~RC(" << flag_rgb() << ">" << team::get_side_color_index(side()) << ")"; + return formatter() << "~RC(" << flag_rgb() << ">" << team::get_side_color_id(side()) << ")"; } std::string unit::image_mods() const diff --git a/src/whiteboard/manager.cpp b/src/whiteboard/manager.cpp index c933311450e..27c31bbc3bd 100644 --- a/src/whiteboard/manager.cpp +++ b/src/whiteboard/manager.cpp @@ -711,7 +711,7 @@ void manager::create_temp_move() { // Create temp arrow move_arrow.reset(new arrow()); - move_arrow->set_color(team::get_side_color_index( + move_arrow->set_color(team::get_side_color_id( viewer_side())); move_arrow->set_style(arrow::STYLE_HIGHLIGHTED); } diff --git a/src/whiteboard/move.cpp b/src/whiteboard/move.cpp index 2ce623ae9c7..e8b28d1d46a 100644 --- a/src/whiteboard/move.cpp +++ b/src/whiteboard/move.cpp @@ -127,7 +127,7 @@ move::move(config const& cfg, bool hidden) throw action::ctor_err("move: Invalid route_"); // Construct arrow_ - arrow_->set_color(team::get_side_color_index(side_number())); + arrow_->set_color(team::get_side_color_id(side_number())); arrow_->set_style(arrow::STYLE_STANDARD); arrow_->set_path(route_->steps);