1 /*
2  * Copyright (C) 2015-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 
20 #include "wui/suggested_teams_box.h"
21 
22 #include "base/i18n.h"
23 #include "graphic/graphic.h"
24 #include "graphic/playercolor.h"
25 
26 namespace UI {
27 
SuggestedTeamsBox(Panel * parent,int32_t x,int32_t y,uint32_t orientation,int32_t padding,int32_t indent,int32_t max_x,int32_t max_y)28 SuggestedTeamsBox::SuggestedTeamsBox(Panel* parent,
29                                      int32_t x,
30                                      int32_t y,
31                                      uint32_t orientation,
32                                      int32_t padding,
33                                      int32_t indent,
34                                      int32_t max_x,
35                                      int32_t max_y)
36    : UI::Box(parent,
37              x,
38              y,
39              orientation,
40              max_x,
41              max_y,
42              g_gr->images().get("images/players/player_position_menu.png")->height()),
43      padding_(padding),
44      indent_(indent),
45      label_height_(g_gr->images().get("images/players/player_position_menu.png")->height() +
46                    padding),
47      suggested_teams_box_label_(new UI::Textarea(this)),
48      lineup_box_(nullptr) {
49 	add(suggested_teams_box_label_);
50 }
~SuggestedTeamsBox()51 SuggestedTeamsBox::~SuggestedTeamsBox() {
52 	SuggestedTeamsBox::hide();
53 }
54 
hide()55 void SuggestedTeamsBox::hide() {
56 	// Delete former images
57 	for (UI::Icon* player_icon : player_icons_) {
58 		player_icon->set_icon(nullptr);
59 		player_icon->set_visible(false);
60 		player_icon->set_no_frame();
61 	}
62 	player_icons_.clear();
63 
64 	// Delete vs. labels
65 	for (UI::Textarea* vs_label : vs_labels_) {
66 		vs_label->set_visible(false);
67 	}
68 	vs_labels_.clear();
69 
70 	set_visible(false);
71 	suggested_teams_box_label_->set_visible(false);
72 	suggested_teams_box_label_->set_text("");
73 }
74 
show(const std::vector<Widelands::SuggestedTeamLineup> & suggested_teams)75 void SuggestedTeamsBox::show(const std::vector<Widelands::SuggestedTeamLineup>& suggested_teams) {
76 	hide();
77 	suggested_teams_ = suggested_teams;
78 
79 	if (!suggested_teams_.empty()) {
80 
81 		// Initialize
82 		uint8_t lineup_counter = 0;
83 		set_visible(true);
84 		suggested_teams_box_label_->set_visible(true);
85 		/** TRANSLATORS: Label for the list of suggested teams when choosing a map */
86 		suggested_teams_box_label_->set_text(_("Suggested Teams"));
87 		int32_t teamlist_offset =
88 		   suggested_teams_box_label_->get_y() + suggested_teams_box_label_->get_h() + padding_;
89 
90 		// Parse suggested teams
91 		UI::Icon* player_icon;
92 		UI::Textarea* vs_label;
93 		for (const Widelands::SuggestedTeamLineup& lineup : suggested_teams_) {
94 
95 			lineup_box_ =
96 			   new UI::Box(this, indent_, teamlist_offset + lineup_counter * (label_height_),
97 			               UI::Box::Horizontal, get_w() - indent_);
98 
99 			lineup_box_->set_size(get_w(), label_height_);
100 
101 			bool is_first = true;
102 			for (const Widelands::SuggestedTeam& team : lineup) {
103 
104 				if (!is_first) {
105 					lineup_box_->add_space(padding_);
106 					vs_label = new UI::Textarea(lineup_box_, "x", UI::Align::kCenter);
107 					lineup_box_->add(vs_label);
108 					vs_label->set_visible(true);
109 					vs_labels_.push_back(vs_label);
110 					lineup_box_->add_space(padding_);
111 				}
112 				is_first = false;
113 
114 				for (Widelands::PlayerNumber player : team) {
115 					assert(player < kMaxPlayers);
116 					const Image* player_image =
117 					   playercolor_image(player, "images/players/player_position_menu.png");
118 
119 					assert(player_image);
120 					player_icon = new UI::Icon(
121 					   lineup_box_, 0, 0, player_image->width(), player_image->height(), player_image);
122 					player_icon->set_visible(true);
123 					player_icon->set_no_frame();
124 					lineup_box_->add(player_icon);
125 					player_icons_.push_back(player_icon);
126 				}  // Players in team
127 			}     // Teams in lineup
128 			++lineup_counter;
129 		}  // All lineups
130 
131 		// Adjust size to content
132 		set_size(get_w(), teamlist_offset + lineup_counter * (label_height_));
133 	}
134 }
135 }  // namespace UI
136