-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
label_settings.cpp
128 lines (113 loc) · 4.17 KB
/
label_settings.cpp
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
/*
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
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.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "label_settings.hpp"
#include <vector>
#include <boost/bind.hpp>
#include "gettext.hpp"
#include "game_display.hpp"
#include "map/label.hpp"
#include "resources.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/widgets/control.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/window.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/label.hpp"
#include "formula/string_utils.hpp"
namespace gui2 {
REGISTER_DIALOG(label_settings);
tlabel_settings::tlabel_settings(display_context& dc) : viewer(dc) {
const std::vector<std::string>& all_categories = resources::screen->labels().all_categories();
const std::vector<std::string>& hidden_categories = viewer.hidden_label_categories();
for(size_t i = 0; i < all_categories.size(); i++) {
all_labels[all_categories[i]] = true;
if(all_categories[i].substr(0,4) == "cat:")
labels_display[all_categories[i]] = all_categories[i].substr(4);
else if(all_categories[i] == "team")
labels_display[all_categories[i]] = _("Team Labels");
// TODO: Translatable names for categories?
}
for(size_t i = 0; i < hidden_categories.size(); i++) {
all_labels[hidden_categories[i]] = false;
}
for(size_t i = 0; i < dc.teams().size(); i++) {
const team& team = dc.teams()[i];
const std::string label_cat_key = "side:" + str_cast(i + 1);
if(team.hidden()) {
labels_display[label_cat_key] = "";
continue;
}
std::string team_name = team.side_name();
if(team_name.empty()) {
team_name = team.user_team_name();
}
if(team_name.empty()) {
team_name = _("Unknown");
}
string_map subst;
subst["side_number"] = str_cast(i + 1);
subst["name"] = team_name;
labels_display[label_cat_key] = vgettext("Side $side_number ($name)", subst);
}
}
void tlabel_settings::pre_show(twindow& window) {
std::map<std::string, string_map> list_data;
tlistbox& cats_listbox = find_widget<tlistbox>(&window, "label_types", false);
FOREACH(const AUTO & label_entry, all_labels) {
const std::string& category = label_entry.first;
const bool& visible = label_entry.second;
std::string name = labels_display[category];
if(category.substr(0,5) == "side:") {
if(name.empty()) {
// This means it's a hidden side, so don't show it.
continue;
}
int team = lexical_cast<int>(category.substr(5)) - 1;
Uint32 which_color = game_config::tc_info(viewer.teams()[team].color())[0];
std::ostringstream sout;
sout << "<span color='#" << std::hex << which_color << "'>" << name << "</span>";
name = sout.str();
}
list_data["cat_name"]["label"] = name;
cats_listbox.add_row(list_data);
tgrid* grid = cats_listbox.get_row_grid(cats_listbox.get_item_count() - 1);
ttoggle_button& status = find_widget<ttoggle_button>(grid, "cat_status", false);
status.set_value(visible);
status.set_callback_state_change(boost::bind(&tlabel_settings::toggle_category, this, _1, category));
if(category.substr(0,5) == "side:") {
tlabel& label = find_widget<tlabel>(grid, "cat_name", false);
label.set_use_markup(true);
}
}
}
bool tlabel_settings::execute(display_context& dc, CVideo& video) {
tlabel_settings window(dc);
if(!window.show(video)) return false;
std::vector<std::string> hidden_categories;
typedef std::map<std::string,bool>::value_type value_type;
BOOST_FOREACH(value_type lbl, window.all_labels) {
if(lbl.second == false) {
hidden_categories.push_back(lbl.first);
}
}
dc.hidden_label_categories_ref().swap(hidden_categories);
return true;
}
void tlabel_settings::toggle_category(twidget& box, std::string category) {
all_labels[category] = (static_cast<ttoggle_button&>(box).get_value() != 0);
}
}