Skip to content

Commit

Permalink
fix custom team color in mpwait and flg_manager
Browse files Browse the repository at this point in the history
This also fixes another bug where colors in flg_manager (the
faction/leader comboboxes in mpconnect)  were not updated when color was
changed.
  • Loading branch information
gfgtdf committed Jun 20, 2015
1 parent ebe08f0 commit e9acbd1
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 58 deletions.
6 changes: 5 additions & 1 deletion src/game_initialization/connect_engine.cpp
Expand Up @@ -175,6 +175,7 @@ connect_engine::connect_engine(saved_game& state,
era_factions_.push_back(&era);
}

game_config::add_color_info(scenario());
// Create side engines.
int index = 0;
BOOST_FOREACH(const config &s, sides) {
Expand Down Expand Up @@ -849,7 +850,7 @@ side_engine::side_engine(const config& cfg, connect_engine& parent_engine,
chose_random_(cfg["chose_random"].to_bool(false)),
disallow_shuffle_(cfg["disallow_shuffle"].to_bool(false)),
flg_(parent_.era_factions_, cfg_, parent_.force_lock_settings_,
parent_.params_.use_map_settings, parent_.params_.saved_game, color_),
parent_.params_.use_map_settings, parent_.params_.saved_game),
allow_changes_(!parent_.params_.saved_game && !(flg_.choosable_factions().size() == 1 && flg_.choosable_leaders().size() == 1 && flg_.choosable_genders().size() == 1)),
waiting_to_choose_faction_(allow_changes_),
custom_color_()
Expand Down Expand Up @@ -1345,6 +1346,9 @@ std::vector<std::string> side_engine::get_colors() const

std::string side_engine::get_color(size_t index) const
{
if(index == -1) {
index = color();
}
if(!custom_color_.empty()) {
if(index == 0) {
return custom_color_;
Expand Down
2 changes: 1 addition & 1 deletion src/game_initialization/connect_engine.hpp
Expand Up @@ -220,7 +220,7 @@ class side_engine
flg_manager& flg() { return flg_; }

std::vector<std::string> get_colors() const;
std::string get_color(size_t index) const;
std::string get_color(size_t index = -1) const;
int num_colors() const;
private:
side_engine(const side_engine& engine);
Expand Down
15 changes: 7 additions & 8 deletions src/game_initialization/flg_manager.cpp
Expand Up @@ -37,17 +37,17 @@ std::string get_RC_suffix(const std::string&, const int)
return "";
}
#else
std::string get_RC_suffix(const std::string& unit_color, const int color)
std::string get_RC_suffix(const std::string& unit_color, const std::string& color)
{
return "~RC(" + unit_color + ">" + lexical_cast<std::string>(color + 1) +
return "~RC(" + unit_color + ">" + color +
")";
}
#endif


flg_manager::flg_manager(const std::vector<const config*>& era_factions,
const config& side, const bool lock_settings, const bool use_map_settings,
const bool saved_game, const int color) :
const bool saved_game) :
era_factions_(era_factions),
side_(side),
use_map_settings_(use_map_settings),
Expand All @@ -60,7 +60,6 @@ flg_manager::flg_manager(const std::vector<const config*>& era_factions,
side_["previous_recruits"].empty()),
faction_lock_(side_["faction_lock"].to_bool(lock_settings) && use_map_settings),
leader_lock_(side_["leader_lock"].to_bool(lock_settings) && use_map_settings),
color_(color),
available_factions_(),
available_leaders_(),
available_genders_(),
Expand Down Expand Up @@ -151,14 +150,14 @@ void flg_manager::set_current_gender(const unsigned index)
current_gender_ = choosable_genders_[index];
}

void flg_manager::reset_leader_combo(gui::combo& combo_leader) const
void flg_manager::reset_leader_combo(gui::combo& combo_leader, const std::string& color) const
{
std::vector<std::string> leaders;
BOOST_FOREACH(const std::string& leader, choosable_leaders_) {
const unit_type* unit = unit_types.find(leader);
if (unit) {
leaders.push_back(IMAGE_PREFIX + unit->image() +
get_RC_suffix(unit->flag_rgb(), color_) +
get_RC_suffix(unit->flag_rgb(), color) +
COLUMN_SEPARATOR + unit->type_name());
} else if (leader == "random") {
leaders.push_back(IMAGE_PREFIX + random_enemy_picture +
Expand All @@ -176,7 +175,7 @@ void flg_manager::reset_leader_combo(gui::combo& combo_leader) const
combo_leader.set_selected(current_leader_index());
}

void flg_manager::reset_gender_combo(gui::combo& combo_gender) const
void flg_manager::reset_gender_combo(gui::combo& combo_gender, const std::string& color) const
{
const unit_type* unit = unit_types.find(current_leader_);

Expand All @@ -190,7 +189,7 @@ void flg_manager::reset_gender_combo(gui::combo& combo_gender) const
std::string gender_name = (gender == unit_race::s_female) ?
_("Female ♀") : _("Male ♂");
genders.push_back(IMAGE_PREFIX + gender_unit.image() +
get_RC_suffix(gender_unit.flag_rgb(), color_) +
get_RC_suffix(gender_unit.flag_rgb(), color) +
COLUMN_SEPARATOR + gender_name);
}
} else if (gender == "random") {
Expand Down
10 changes: 4 additions & 6 deletions src/game_initialization/flg_manager.hpp
Expand Up @@ -26,15 +26,15 @@ namespace ng {

const std::string random_enemy_picture("units/random-dice.png");

std::string get_RC_suffix(const std::string& unit_color, const int color);
std::string get_RC_suffix(const std::string& unit_color, const std::string& color);

/// FLG stands for faction, leader and gender.
class flg_manager
{
public:
flg_manager(const std::vector<const config*>& era_factions,
const config& side, const bool faction_lock, const bool leader_lock,
const bool saved_game, const int color);
const bool saved_game);
~flg_manager();

void set_current_faction(const unsigned index);
Expand All @@ -50,8 +50,8 @@ class flg_manager

// Update the status of combo: items, selection and whether
// it should be enabled or not.
void reset_leader_combo(gui::combo& combo_leader) const;
void reset_gender_combo(gui::combo& combo_gender) const;
void reset_leader_combo(gui::combo& combo_leader, const std::string& color) const;
void reset_gender_combo(gui::combo& combo_gender, const std::string& color) const;

bool is_random_faction();
void resolve_random(rand_rng::mt_rng & rng, const std::vector<std::string> & avoid); //Second Argument is a list of faction ids we don't want to match, used to implement random faction modes. If it is not possible to resolve then we just proceed anyways rather than give an error.
Expand Down Expand Up @@ -115,8 +115,6 @@ class flg_manager
const bool faction_lock_;
const bool leader_lock_;

const int color_;

// All factions which could be played by a side (including Random).
std::vector<const config*> available_factions_;
std::vector<std::string> available_leaders_;
Expand Down
22 changes: 10 additions & 12 deletions src/game_initialization/multiplayer_connect.cpp
Expand Up @@ -190,14 +190,14 @@ void connect::side::process_event()
if (combo_color_.changed() && combo_color_.selected() >= 0) {
engine_->set_color(combo_color_.selected());
update_faction_combo();
engine_->flg().reset_leader_combo(combo_leader_);
engine_->flg().reset_gender_combo(combo_gender_);
engine_->flg().reset_leader_combo(combo_leader_, engine_->get_color());
engine_->flg().reset_gender_combo(combo_gender_, engine_->get_color());
changed_ = true;
}
if (combo_faction_.changed() && combo_faction_.selected() >= 0) {
engine_->flg().set_current_faction(combo_faction_.selected());
engine_->flg().reset_leader_combo(combo_leader_);
engine_->flg().reset_gender_combo(combo_gender_);
engine_->flg().reset_leader_combo(combo_leader_, engine_->get_color());
engine_->flg().reset_gender_combo(combo_gender_, engine_->get_color());
changed_ = true;
}
if (combo_ai_algorithm_.changed() && combo_ai_algorithm_.selected() >= 0) {
Expand All @@ -207,7 +207,7 @@ void connect::side::process_event()
}
if (combo_leader_.changed() && combo_leader_.selected() >= 0) {
engine_->flg().set_current_leader(combo_leader_.selected());
engine_->flg().reset_gender_combo(combo_gender_);
engine_->flg().reset_gender_combo(combo_gender_, engine_->get_color());
changed_ = true;
}
if (combo_gender_.changed() && combo_gender_.selected() >= 0) {
Expand Down Expand Up @@ -243,8 +243,8 @@ bool connect::side::changed()
void connect::side::update_ui()
{
update_faction_combo();
engine_->flg().reset_leader_combo(combo_leader_);
engine_->flg().reset_gender_combo(combo_gender_);
engine_->flg().reset_leader_combo(combo_leader_, engine_->get_color());
engine_->flg().reset_gender_combo(combo_gender_, engine_->get_color());

update_controller_ui();

Expand Down Expand Up @@ -304,12 +304,10 @@ void connect::side::update_faction_combo()
const std::string& icon = (*faction)["image"];
if (!icon.empty()) {
std::string rgb = (*faction)["flag_rgb"];
if (rgb.empty())
if (rgb.empty()) {
rgb = "magenta";

factions.push_back(IMAGE_PREFIX + icon + "~RC(" + rgb + ">" +
lexical_cast<std::string>(engine_->color() + 1) + ")" +
COLUMN_SEPARATOR + name);
}
factions.push_back(IMAGE_PREFIX + icon + "~RC(" + rgb + ">" + engine_->get_color() + ")" + COLUMN_SEPARATOR + name);
} else {
factions.push_back(name);
}
Expand Down
40 changes: 12 additions & 28 deletions src/game_initialization/multiplayer_wait.cpp
Expand Up @@ -58,15 +58,15 @@ const int leader_pane_border = 10;
namespace mp {

wait::leader_preview_pane::leader_preview_pane(game_display& disp,
ng::flg_manager& flg, const int color) :
ng::flg_manager& flg, const std::string& color) :
gui::preview_pane(disp.video()),
flg_(flg),
color_(color),
combo_leader_(disp, std::vector<std::string>()),
combo_gender_(disp, std::vector<std::string>())
{
flg_.reset_leader_combo(combo_leader_);
flg_.reset_gender_combo(combo_gender_);
flg_.reset_leader_combo(combo_leader_, color_);
flg_.reset_gender_combo(combo_gender_, color_);

set_location(leader_pane_position);
}
Expand All @@ -76,7 +76,7 @@ void wait::leader_preview_pane::process_event()
if (combo_leader_.changed() && combo_leader_.selected() >= 0) {
flg_.set_current_leader(combo_leader_.selected());

flg_.reset_gender_combo(combo_gender_);
flg_.reset_gender_combo(combo_gender_, color_);

set_dirty();
}
Expand Down Expand Up @@ -176,8 +176,8 @@ void wait::leader_preview_pane::set_selection(int selection)
if (selection >= 0) {
flg_.set_current_faction(selection);

flg_.reset_leader_combo(combo_leader_);
flg_.reset_gender_combo(combo_gender_);
flg_.reset_leader_combo(combo_leader_, color_);
flg_.reset_gender_combo(combo_gender_, color_);

set_dirty();
}
Expand Down Expand Up @@ -295,7 +295,8 @@ void wait::join_game(bool observe)

// Add the map name to the title.
append_to_title(": " + get_scenario()["name"].t_str());


game_config::add_color_info(get_scenario());
if (!observe) {
//search for an appropriate vacant slot. If a description is set
//(i.e. we're loading from a saved game), then prefer to get the side
Expand Down Expand Up @@ -354,10 +355,7 @@ void wait::join_game(bool observe)
throw config::error(_("No multiplayer sides found"));
}

int color = side_num;
const std::string color_str = (*side_choice)["color"];
if (!color_str.empty())
color = game_config::color_info(color_str).index() - 1;
const std::string color = (*side_choice)["color"].str();

std::vector<const config*> era_factions;
BOOST_FOREACH(const config &side, possible_sides) {
Expand All @@ -372,7 +370,7 @@ void wait::join_game(bool observe)
level_.child("multiplayer")["savegame"].to_bool();

ng::flg_manager flg(era_factions, *side_choice, lock_settings, use_map_settings,
saved_game, color);
saved_game);

std::vector<std::string> choices;
BOOST_FOREACH(const config *s, flg.choosable_factions())
Expand All @@ -387,7 +385,7 @@ void wait::join_game(bool observe)
rgb = "magenta";

choices.push_back(IMAGE_PREFIX + icon + "~RC(" + rgb + ">" +
lexical_cast<std::string>(color+1) + ")" + COLUMN_SEPARATOR + name);
color + ")" + COLUMN_SEPARATOR + name);
} else {
choices.push_back(name);
}
Expand Down Expand Up @@ -631,21 +629,7 @@ void wait::generate_menu()

str << COLUMN_SEPARATOR << t_string::from_serialized(sd["user_team_name"].str());

int disp_color = sd["color"];
if(!sd["color"].empty()) {
try {
disp_color = game_config::color_info(sd["color"]).index();
} catch(config::error&) {
//ignore
}
} else {
/**
* @todo we fall back to the side color, but that's ugly rather
* make the color mandatory in 1.5.
*/
disp_color = sd["side"];
}
str << COLUMN_SEPARATOR << get_color_string(disp_color - 1);
str << COLUMN_SEPARATOR << get_color_string(sd["color"].str());
details.push_back(str.str());
}

Expand Down
4 changes: 2 additions & 2 deletions src/game_initialization/multiplayer_wait.hpp
Expand Up @@ -44,7 +44,7 @@ class wait : public ui
{
public:
leader_preview_pane(game_display& disp, ng::flg_manager& flg,
const int color);
const std::string& color);

bool show_above() const;
bool left_side() const;
Expand All @@ -58,7 +58,7 @@ class wait : public ui

ng::flg_manager& flg_;

const int color_;
const std::string color_;

gui::combo combo_leader_;
gui::combo combo_gender_;
Expand Down

0 comments on commit e9acbd1

Please sign in to comment.