The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
label_settings.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 - 2025
3 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY.
11
12 See the COPYING file for more details.
13*/
14
15#define GETTEXT_DOMAIN "wesnoth-lib"
16
18
19#include "display.hpp"
21#include "formatter.hpp"
23#include "gettext.hpp"
24#include "gui/widgets/label.hpp"
28#include "map/label.hpp"
29#include "team.hpp"
30
31#include <vector>
32
33namespace gui2::dialogs
34{
35REGISTER_DIALOG(label_settings)
36
38 : modal_dialog(window_id())
39 , viewer_(dc)
40{
41 const std::vector<std::string>& all_categories = display::get_singleton()->labels().all_categories();
42 const std::vector<std::string>& hidden_categories = viewer_.hidden_label_categories();
43
44 for(const std::string& cat : all_categories) {
45 all_labels_[cat] = true;
46
47 // TODO: Translatable names for categories?
48 if(cat.substr(0, 4) == "cat:") {
49 labels_display_[cat] = cat.substr(4);
50 } else if(cat == "team") {
51 labels_display_[cat] = _("Team Labels");
52 }
53 }
54
55 for(const std::string& hidden_cat : hidden_categories) {
56 all_labels_[hidden_cat] = false;
57 }
58
59 for(const team& team : viewer_.teams()) {
60 const std::string label_cat_key = "side:" + std::to_string(team.side());
61
62 if(team.hidden()) {
63 labels_display_[label_cat_key] = "";
64 continue;
65 }
66
67 std::string team_name = team.side_name();
68 if(team_name.empty()) {
69 team_name = team.user_team_name();
70 }
71
72 if(team_name.empty()) {
73 team_name = _("Unknown");
74 }
75
77 subst["side_number"] = std::to_string(team.side());
78 subst["name"] = team_name;
79 labels_display_[label_cat_key] = VGETTEXT("Side $side_number ($name)", subst);
80 }
81}
82
84{
85 listbox& cats_listbox = find_widget<listbox>("label_types");
86
87 for(const auto& label_entry : all_labels_) {
88 const std::string& category = label_entry.first;
89 const bool visible = label_entry.second;
90
91 std::string name = labels_display_[category];
92 if(category.substr(0, 5) == "side:") {
93 // This means it's a hidden side, so don't show it.
94 if(name.empty()) {
95 continue;
96 }
97
98 const int team = std::stoi(category.substr(5)) - 1;
99 const color_t tc = game_config::tc_info(viewer_.teams()[team].color())[0];
100
101 name = markup::span_color(tc, name);
102 }
103
104 grid* grid = &cats_listbox.add_row(widget_data{{ "cat_name", {{ "label", name }}}});
105
108
109 connect_signal_notify_modified(status, std::bind(&label_settings::toggle_category, this, std::placeholders::_1, category));
110
111 if(category.substr(0, 5) == "side:") {
112 label& cat_name = grid->find_widget<label>("cat_name");
113 cat_name.set_use_markup(true);
114 }
115 }
116}
117
119{
120 if(get_retval() == retval::OK) {
121 std::vector<std::string> hidden_categories;
122
123 for(const auto& lbl : all_labels_) {
124 if(lbl.second == false) {
125 hidden_categories.push_back(lbl.first);
126 }
127 }
128
129 viewer_.hidden_label_categories().swap(hidden_categories);
130 }
131}
132
133void label_settings::toggle_category(widget& box, const std::string& category)
134{
135 all_labels_[category] = static_cast<toggle_button&>(box).get_value_bool();
136}
137
138} // namespace dialogs
Abstract class for exposing game data that doesn't depend on the GUI, however which for historical re...
virtual const std::vector< team > & teams() const =0
virtual const std::vector< std::string > & hidden_label_categories() const =0
map_labels & labels()
Definition display.cpp:2392
static display * get_singleton()
Returns the display object if a display object exists.
Definition display.hpp:102
std::map< std::string, t_string > labels_display_
void toggle_category(widget &box, const std::string &category)
Callback for toggling a checkbox state.
virtual void pre_show() override
Actions to be taken before showing the window.
std::map< std::string, bool > all_labels_
The execute function.
virtual void post_show() override
Actions to be taken after the window has been shown.
Abstract base class for all modal dialogs.
Base container class.
Definition grid.hpp:32
The listbox class.
Definition listbox.hpp:41
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition listbox.cpp:92
virtual void set_use_markup(bool use_markup)
virtual void set_value(unsigned selected, bool fire_event=false) override
Inherited from selectable_item.
Base class for all widgets.
Definition widget.hpp:55
T * find_widget(const std::string_view id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition widget.hpp:747
@ visible
The user sets the widget visible, that means:
status
The status of the window.
Definition window.hpp:203
int get_retval()
Definition window.hpp:394
const std::vector< std::string > & all_categories() const
Definition label.cpp:288
This class stores all the data for a single 'side' (in game nomenclature).
Definition team.hpp:74
const std::string & side_name() const
Definition team.hpp:334
int side() const
Definition team.hpp:182
bool hidden() const
Definition team.hpp:375
const t_string & user_team_name() const
Definition team.hpp:324
map_display and display: classes which take care of displaying the map and game-data on the screen.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
static std::string _(const char *str)
Definition gettext.hpp:100
This file contains the window object, this object is a top level container which has the event manage...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
const std::vector< color_t > & tc_info(std::string_view name)
std::map< std::string, widget_item > widget_data
Definition widget.hpp:36
@ OK
Dialog was closed with the OK button.
Definition retval.hpp:35
std::string span_color(const color_t &color, Args &&... data)
Applies Pango markup to the input specifying its display color.
Definition markup.hpp:110
std::map< std::string, t_string > string_map
The basic class for representing 8-bit RGB or RGBA colour values.
Definition color.hpp:51