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.
This commit is contained in:
Charles Dang 2017-10-26 09:27:50 +11:00
parent 02de08338c
commit ff5d68f9f5
12 changed files with 59 additions and 24 deletions

View file

@ -234,7 +234,7 @@ void mouse_action_item::set_item_mouse_overlay(editor_display& disp, const overl
std::stringstream filename; std::stringstream filename;
filename << u.image; // << "~RC(" << u.flag_rgb() << '>' 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())); surface image(image::get_image(filename.str()));
Uint8 alpha = 196; Uint8 alpha = 196;

View file

@ -235,7 +235,7 @@ void mouse_action_unit::set_unit_mouse_overlay(editor_display& disp, const unit_
std::stringstream filename; std::stringstream filename;
filename << u.image() << "~RC(" << u.flag_rgb() << '>' 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())); surface image(image::get_image(filename.str()));
Uint8 alpha = 196; Uint8 alpha = 196;

View file

@ -84,7 +84,7 @@ void unit_palette::draw_item(const unit_type& u, surface& image, std::stringstre
std::stringstream filename; std::stringstream filename;
filename << u.image() << "~RC(" << u.flag_rgb() << '>' 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()); image = image::get_image(filename.str());
if(image == nullptr) { if(image == nullptr) {

View file

@ -258,7 +258,7 @@ void unit_preview_pane::set_displayed_type(const unit_type& type)
if(resources::controller) { if(resources::controller) {
mods = "~RC(" + type.flag_rgb() + ">" + mods = "~RC(" + type.flag_rgb() + ">" +
team::get_side_color_index(resources::controller->current_side()) team::get_side_color_id(resources::controller->current_side())
+ ")"; + ")";
} }

View file

@ -1469,7 +1469,7 @@ REPORT_GENERATOR(side_playing, rc)
const team &active_team = rc.teams()[rc.screen().playing_team()]; const team &active_team = rc.teams()[rc.screen().playing_team()];
std::string flag_icon = active_team.flag_icon(); std::string flag_icon = active_team.flag_icon();
std::string old_rgb = game_config::flag_rgb; 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 + ")"; std::string mods = "~RC(" + old_rgb + ">" + new_rgb + ")";
if (flag_icon.empty()) if (flag_icon.empty())
flag_icon = game_config::images::flag_icon; flag_icon = game_config::images::flag_icon;

View file

@ -396,12 +396,14 @@ void extract_summary_from_config(config& cfg_save, config& cfg_summary)
continue; continue;
} }
const std::string tc_color = team::get_side_color_id_from_config(u);
// Don't count it among the troops // Don't count it among the troops
units--; units--;
leader = u["id"].str(); leader = u["id"].str();
leader_name = u["name"].str(); leader_name = u["name"].str();
leader_image = u["image"].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 // We need a binary path-independent path to the leader image here so it can be displayed

View file

@ -197,11 +197,8 @@ void team::team_info::read(const config& cfg)
variables = cfg.child_or_empty("variables"); variables = cfg.child_or_empty("variables");
is_local = cfg["is_local"].to_bool(true); is_local = cfg["is_local"].to_bool(true);
if(cfg.has_attribute("color")) { color = get_side_color_id_from_config(cfg);
color = cfg["color"].str(); std::cerr << "Setting up side... set color to " << color << std::endl;
} else {
color = cfg["side"].str();
}
// If starting new scenario override settings from [ai] tags // If starting new scenario override settings from [ai] tags
if(!user_team_name.translatable()) if(!user_team_name.translatable())
@ -918,7 +915,7 @@ bool team::shroud_map::copy_from(const std::vector<const shroud_map*>& maps)
const color_range team::get_side_color_range(int side) 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<std::string, color_range>::iterator gp = game_config::team_rgb_range.find(index); std::map<std::string, color_range>::iterator gp = game_config::team_rgb_range.find(index);
if(gp != game_config::team_rgb_range.end()) { 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(); 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()) { // If no gameboard (and by extension, team list) is available, use the default side color.
const std::string side_map = resources::gameboard->teams()[index].color(); if(!resources::gameboard) {
if(!side_map.empty()) { return game_config::default_colors.at(index);
return side_map;
} }
// 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) std::string team::get_side_highlight_pango(int side)

View file

@ -361,12 +361,15 @@ public:
//function which, when given a 1-based side will return the color used by that side. //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 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(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_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_rgb_min(int side) { return(get_side_color_range(side).min()); }
static color_t get_side_color(int side); static color_t get_side_color(int side);
static color_t get_minimap_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); static std::string get_side_highlight_pango(int side);
void log_recruitable() const; void log_recruitable() const;

View file

@ -201,7 +201,7 @@ void unit_drawer::redraw_unit (const unit & u) const
const std::string nozoc = !emit_zoc ? "nozoc-" : ""; const std::string nozoc = !emit_zoc ? "nozoc-" : "";
const std::string leader = can_recruit ? "leader-" : ""; const std::string leader = can_recruit ? "leader-" : "";
const std::string selected = sel_hex == loc ? "selected-" : ""; 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_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 << ")"; const std::string ellipse_bot = formatter() << ellipse << "-" << leader << nozoc << selected << "bottom.png~RC(ellipse_red>" << tc << ")";

View file

@ -2448,7 +2448,7 @@ unit_movement_resetter::~unit_movement_resetter()
std::string unit::TC_image_mods() const 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 std::string unit::image_mods() const

View file

@ -711,7 +711,7 @@ void manager::create_temp_move()
{ {
// Create temp arrow // Create temp arrow
move_arrow.reset(new 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())); viewer_side()));
move_arrow->set_style(arrow::STYLE_HIGHLIGHTED); move_arrow->set_style(arrow::STYLE_HIGHLIGHTED);
} }

View file

@ -127,7 +127,7 @@ move::move(config const& cfg, bool hidden)
throw action::ctor_err("move: Invalid route_"); throw action::ctor_err("move: Invalid route_");
// Construct arrow_ // 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_style(arrow::STYLE_STANDARD);
arrow_->set_path(route_->steps); arrow_->set_path(route_->steps);