~widelands-dev/widelands/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright (C) 2002-2024 by the Widelands Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "wui/general_statistics_menu.h"

#include <memory>

#include "base/i18n.h"
#include "logic/editor_game_base.h"
#include "logic/game.h"
#include "logic/game_data_error.h"
#include "logic/map_objects/tribes/tribe_descr.h"
#include "logic/map_objects/tribes/warelist.h"
#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
#include "ui_basic/button.h"
#include "ui_basic/checkbox.h"
#include "ui_basic/slider.h"
#include "wui/interactive_player.h"

constexpr int kPlotHeight = 145;
constexpr int kNrBaseDatasets = 15;

GeneralStatisticsMenu::GeneralStatisticsMenu(InteractiveGameBase& parent,
                                             GeneralStatisticsMenu::Registry& registry)
   : UI::UniqueWindow(&parent,
                      UI::WindowStyle::kWui,
                      "statistics_menu",
                      &registry,
                      440,
                      400,
                      _("General Statistics")),
     my_registry_(&registry),
     box_(this, UI::PanelStyle::kWui, "main_box", 0, 0, UI::Box::Vertical, 0, 0, 5),
     player_buttons_box_(
        &box_, UI::PanelStyle::kWui, "buttons_box", 0, 0, UI::Box::Horizontal, 0, 0, 1),
     plot_(&box_,
           "plot",
           0,
           0,
           430,
           kPlotHeight,
           Widelands::kStatisticsSampleTime.get(),
           WuiPlotArea::Plotmode::kAbsolute),

     game_(*parent.get_game()),
     subscriber_(Notifications::subscribe<Widelands::NotePlayerDetailsEvent>(
        [this](const Widelands::NotePlayerDetailsEvent& note) {
	        if (note.event ==
	            Widelands::NotePlayerDetailsEvent::Event::kGeneralStatisticsVisibilityChanged) {
		        save_state_to_registry();
		        create_player_buttons();
		        initialization_complete();
	        }
        })) {
	assert(my_registry_);

	selected_information_ = my_registry_->selected_information;

	set_center_panel(&box_);
	box_.set_border(5, 5, 5, 5);

	// Setup plot data
	const Widelands::Game::GeneralStatsVector& genstats = game_.get_general_statistics();
	const Widelands::Game::GeneralStatsVector::size_type general_statistics_size =
	   game_.map().get_nrplayers();
	if (genstats.size() != general_statistics_size) {
		// Create first data point
		game_.sample_statistics();
	}

	// Is there a hook dataset?
	ndatasets_ = kNrBaseDatasets;
	std::unique_ptr<LuaTable> hook = game_.lua().get_hook("custom_statistic");
	std::string cs_name;
	std::string cs_pic;
	if (hook) {
		i18n::Textdomain td("win_conditions");
		hook->do_not_warn_about_unaccessed_keys();
		cs_name = _(hook->get_string("name"));
		cs_pic = hook->get_string("pic");
		ndatasets_++;
	}

	for (Widelands::Game::GeneralStatsVector::size_type i = 0; i < general_statistics_size; ++i) {
		const Widelands::Player* p = parent.game().get_player(i + 1);
		const RGBColor& color = p != nullptr ? p->get_playercolor() :
		                                       // The plot is always invisible if this player doesn't
		                                       // exist, but we need to assign a color anyway
		                                       kPlayerColors[i];

		unsigned o = 0;
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].land_size, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_workers, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_buildings, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_wares, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].productivity, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_ships, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_naval_losses, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_naval_victories, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_casualties, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_kills, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_msites_lost, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_msites_defeated, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_civil_blds_lost, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].nr_civil_blds_defeated, color);
		plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].miltary_strength, color);
		if (hook) {
			plot_.register_plot_data(i * ndatasets_ + o++, &genstats[i].custom_statistic, color);
		}

		assert(o == ndatasets_);

		if (game_.get_player(i + 1) != nullptr) {  // Show area plot
			plot_.show_plot(i * ndatasets_ + selected_information_, my_registry_->selected_players[i]);
		}
	}

	plot_.set_time(my_registry_->time);

	// Setup Widgets
	box_.add(&plot_);

	create_player_buttons();
	box_.add(&player_buttons_box_, UI::Box::Resizing::kFullSize);

	UI::Box* hbox2 =
	   new UI::Box(&box_, UI::PanelStyle::kWui, "hbox", 0, 0, UI::Box::Horizontal, 0, 0, 1);

	UI::Radiobutton* btn;

	const Vector2i zero = Vector2i::zero();

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "landsize", zero,
	                       g_image_cache->get("images/wui/stats/genstats_landsize.png"), _("Land"),
	                       &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "workers", zero,
	                       g_image_cache->get("images/wui/stats/genstats_nrworkers.png"),
	                       _("Workers"), &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "buildings", zero,
	                       g_image_cache->get("images/wui/stats/genstats_nrbuildings.png"),
	                       _("Buildings"), &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "wares", zero,
	                       g_image_cache->get("images/wui/stats/genstats_nrwares.png"), _("Wares"),
	                       &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "productivity", zero,
	                       g_image_cache->get("images/wui/stats/genstats_productivity.png"),
	                       _("Productivity"), &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "ships", zero,
	                       g_image_cache->get("images/wui/stats/genstats_ships.png"), _("Ships"),
	                       &btn);
	if (parent.egbase().map().allows_seafaring()) {
		hbox2->add(btn, UI::Box::Resizing::kFillSpace);
	} else {
		btn->set_visible(false);
	}

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "naval_losses", zero,
	                       g_image_cache->get("images/wui/stats/genstats_naval_losses.png"),
	                       _("Warships lost"), &btn);
	if (parent.egbase().map().allows_seafaring()) {
		hbox2->add(btn, UI::Box::Resizing::kFillSpace);
	} else {
		btn->set_visible(false);
	}

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "naval_victories", zero,
	                       g_image_cache->get("images/wui/stats/genstats_naval_victories.png"),
	                       _("Warships defeated"), &btn);
	if (parent.egbase().map().allows_seafaring()) {
		hbox2->add(btn, UI::Box::Resizing::kFillSpace);
	} else {
		btn->set_visible(false);
	}

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "casualties", zero,
	                       g_image_cache->get("images/wui/stats/genstats_casualties.png"),
	                       _("Casualties"), &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "kills", zero,
	                       g_image_cache->get("images/wui/stats/genstats_kills.png"), _("Kills"),
	                       &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "military_lost", zero,
	                       g_image_cache->get("images/wui/stats/genstats_msites_lost.png"),
	                       _("Military buildings lost"), &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "military_defeated", zero,
	                       g_image_cache->get("images/wui/stats/genstats_msites_defeated.png"),
	                       _("Military buildings defeated"), &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "civil_lost", zero,
	                       g_image_cache->get("images/wui/stats/genstats_civil_blds_lost.png"),
	                       _("Civilian buildings lost"), &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "civil_defeated", zero,
	                       g_image_cache->get("images/wui/stats/genstats_civil_blds_defeated.png"),
	                       _("Civilian buildings destroyed"), &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	radiogroup_.add_button(hbox2, UI::PanelStyle::kWui, "strength", zero,
	                       g_image_cache->get("images/wui/stats/genstats_militarystrength.png"),
	                       _("Military"), &btn);
	hbox2->add(btn, UI::Box::Resizing::kFillSpace);

	if (hook) {
		radiogroup_.add_button(
		   hbox2, UI::PanelStyle::kWui, "hook", zero, g_image_cache->get(cs_pic), cs_name, &btn);
		hbox2->add(btn, UI::Box::Resizing::kFillSpace);
	}

	radiogroup_.set_state(selected_information_, false);
	radiogroup_.changedto.connect([this](int32_t i) { radiogroup_changed(i); });

	box_.add(hbox2, UI::Box::Resizing::kFullSize);

	slider_ = new WuiPlotAreaSlider(&box_, "slider", plot_, 0, 0, 100, 45);
	slider_->changedto.connect([this](int32_t i) { plot_.set_time_id(i); });
	box_.add(slider_, UI::Box::Resizing::kFullSize);

	initialization_complete();
}

void GeneralStatisticsMenu::create_player_buttons() {
	// Delete existing buttons, if any
	player_buttons_box_.clear();
	player_buttons_box_.free_children();

	Widelands::PlayerNumber const nr_players = game_.map().get_nrplayers();

	bool show_all_players = true;
	const Widelands::Player* self = nullptr;
	if (upcast(InteractivePlayer, ipl, get_parent())) {
		show_all_players = ipl->player().see_all() || ipl->omnipotent();
		self = &ipl->player();
	}

	for (UI::Button*& c : cbs_) {
		c = nullptr;
	}
	iterate_players_existing_const(p, nr_players, game_, player) {
		if (player != self && !show_all_players && player->is_hidden_from_general_statistics()) {
			// Hide player from stats
			cbs_[p - 1] = nullptr;
			// Also hide the plot for this player if it was previously visible
			show_or_hide_plot(p, false);
			continue;
		}

		const Image* player_image = THREADSAFE_T(
		   const Image*, const Image* (*)(const RGBColor&, const std::string&), playercolor_image,
		   player->get_playercolor(), "images/players/genstats_player.png");
		assert(player_image);
		UI::Button& cb = *new UI::Button(&player_buttons_box_, "playerbutton", 0, 0, 25, 25,
		                                 UI::ButtonStyle::kWuiMenu, player_image, player->get_name());
		cb.sigclicked.connect([this, p]() { cb_changed_to(p); });
		cb.set_perm_pressed(my_registry_->selected_players[p - 1]);

		cbs_[p - 1] = &cb;
		show_or_hide_plot(p, my_registry_->selected_players[p - 1]);

		player_buttons_box_.add(&cb, UI::Box::Resizing::kFillSpace);
	}
}

GeneralStatisticsMenu::~GeneralStatisticsMenu() {
	save_state_to_registry();
}

void GeneralStatisticsMenu::save_state_to_registry() {
	if (game_.is_loaded()) {
		// Save information for recreation, if window is reopened
		my_registry_->selected_information = selected_information_;
		my_registry_->time = plot_.get_time();
		Widelands::PlayerNumber const nr_players = game_.map().get_nrplayers();
		iterate_players_existing_novar(p, nr_players, game_) {
			my_registry_->selected_players[p - 1] =
			   (cbs_[p - 1] == nullptr) ||
			   cbs_[p - 1]->style() == UI::Button::VisualState::kPermpressed;
		}
	}
}

/*
 * Cb has been changed to this state
 */
void GeneralStatisticsMenu::cb_changed_to(int32_t const id) {
	// This represents our player number
	cbs_[id - 1]->toggle();
	show_or_hide_plot(id, cbs_[id - 1]->style() == UI::Button::VisualState::kPermpressed);
}

void GeneralStatisticsMenu::show_or_hide_plot(const int32_t id, const bool show) {
	plot_.show_plot((id - 1) * ndatasets_ + selected_information_, show);
}

/*
 * The radiogroup has changed
 */
void GeneralStatisticsMenu::radiogroup_changed(int32_t const id) {
	size_t const statistics_size = game_.get_general_statistics().size();
	for (uint32_t i = 0; i < statistics_size; ++i) {
		if (cbs_[i] != nullptr) {
			plot_.show_plot(
			   i * ndatasets_ + id, cbs_[i]->style() == UI::Button::VisualState::kPermpressed);
			plot_.show_plot(i * ndatasets_ + selected_information_, false);
		}
	}
	selected_information_ = id;
}

constexpr uint16_t kCurrentPacketVersion = 1;
UI::Window& GeneralStatisticsMenu::load(FileRead& fr, InteractiveBase& ib) {
	try {
		const uint16_t packet_version = fr.unsigned_16();
		if (packet_version == kCurrentPacketVersion) {
			UI::UniqueWindow::Registry& r =
			   dynamic_cast<InteractivePlayer&>(ib).menu_windows_.stats_general;
			r.create();
			assert(r.window);
			GeneralStatisticsMenu& m = dynamic_cast<GeneralStatisticsMenu&>(*r.window);
			m.radiogroup_.set_state(fr.unsigned_8(), true);
			for (unsigned i = 0; i < kMaxPlayers; ++i) {
				if (fr.unsigned_8() != 0u) {
					m.cb_changed_to(i + 1);
				}
			}
			m.slider_->get_slider().set_value(fr.signed_32());
			return m;
		}
		throw Widelands::UnhandledVersionError(
		   "General Statistics Menu", packet_version, kCurrentPacketVersion);

	} catch (const WException& e) {
		throw Widelands::GameDataError("general statistics menu: %s", e.what());
	}
}
void GeneralStatisticsMenu::save(FileWrite& fw, Widelands::MapObjectSaver& /* mos */) const {
	fw.unsigned_16(kCurrentPacketVersion);
	fw.unsigned_8(radiogroup_.get_state());
	for (UI::Button* c : cbs_) {
		// The saved value indicates whether we explicitly need to toggle this button
		fw.unsigned_8(((c != nullptr) && c->style() != UI::Button::VisualState::kPermpressed) ? 1 :
		                                                                                        0);
	}
	fw.signed_32(slider_->get_slider().get_value());
}