The Battle for Wesnoth  1.19.0-dev
achievements_dialog.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 "game_config_manager.hpp"
20 #include "gettext.hpp"
22 #include "gui/widgets/drawing.hpp"
23 #include "gui/widgets/grid.hpp"
24 #include "gui/widgets/label.hpp"
26 #include "gui/widgets/window.hpp"
27 #include "log.hpp"
28 #include "preferences/general.hpp"
29 
30 static lg::log_domain log_config("config");
31 #define ERR_CONFIG LOG_STREAM(err, log_config)
32 
33 constexpr int sub_achievements_limit = 28;
34 
35 namespace gui2::dialogs
36 {
37 
39 
41  : modal_dialog(window_id())
42  , achieve_()
43  , last_selected_(preferences::selected_achievement_group())
44  , achievements_box_(nullptr)
45  , content_names_(nullptr)
46 {
47 }
48 
50 {
51  std::vector<config> content_list;
52  content_names_ = &find_widget<menu_button>(&win, "selected_achievements_list", false);
54 
55  achievements_box_ = find_widget<listbox>(&win, "achievements_list", false, true);
56 
57  std::vector<achievement_group> groups = game_config_manager::get()->get_achievements();
58  int selected = 0;
59 
60  for(const auto& list : groups) {
61  // only display the achievements for the first dropdown option on first showing the dialog
62  if(list.content_for_ == last_selected_ || last_selected_ == "") {
63  selected = content_list.size();
64  }
65 
66  // populate all possibilities into the dropdown
67  content_list.emplace_back("label", list.display_name_);
68  }
69 
70  if(content_list.size() > 0) {
71  content_names_->set_values(content_list);
74  }
75 }
76 
78 {
80 }
81 
83 {
87  int achieved_count = 0;
88 
89  for(const auto& ach : list.achievements_) {
90  if(ach.achieved_) {
91  achieved_count++;
92  } else if(ach.hidden_ && !ach.achieved_) {
93  continue;
94  }
95 
96  widget_data row;
98 
99  item["label"] = !ach.achieved_ ? ach.icon_ : ach.icon_completed_;
100  row.emplace("icon", item);
101 
102  if(!ach.achieved_) {
103  t_string name = ach.name_;
104  if(ach.max_progress_ != 0 && ach.current_progress_ != -1) {
105  name += " ("+std::to_string(ach.current_progress_)+"/"+std::to_string(ach.max_progress_)+")";
106  }
107  item["label"] = name;
108  } else {
109  item["label"] = ach.name_completed_;
110  item["definition"] = "gold_large";
111  }
112  row.emplace("name", item);
113 
114  if(!ach.achieved_) {
115  item["label"] = ach.description_;
116  } else {
117  item["label"] = "<span color='green'>"+ach.description_completed_+"</span>";
118  }
119  row.emplace("description", item);
120 
121  grid& newrow = achievements_box_->add_row(row);
122  progress_bar* achievement_progress = static_cast<progress_bar*>(newrow.find("achievement_progress", false));
123  if(ach.max_progress_ != 0 && ach.current_progress_ != -1) {
124  achievement_progress->set_percentage((ach.current_progress_/double(ach.max_progress_))*100);
125  } else {
126  achievement_progress->set_visible(gui2::widget::visibility::invisible);
127  }
128 
129  label* name = static_cast<label*>(newrow.find("name", false));
130  canvas& canvas = name->get_canvas(0);
131  canvas.set_variable("achieved", wfl::variant(ach.achieved_));
132 
133  set_sub_achievements(newrow, ach);
134  }
135 
136  label* achieved_label = find_widget<label>(get_window(), "achievement_count", false, true);
137  achieved_label->set_label(_("Completed")+" "+std::to_string(achieved_count)+"/"+std::to_string(list.achievements_.size()));
138 }
139 
141 {
142  int i = 0;
143 
144  // set any sub achievements
145  for(const sub_achievement& sub_ach : ach.sub_achievements_)
146  {
148  {
149  ERR_CONFIG << "Too many sub achievements";
150  break;
151  }
152  else
153  {
154  drawing* img = static_cast<drawing*>(newrow.find("sub_icon"+std::to_string(i), false));
155  img->set_label(sub_ach.achieved_ ? sub_ach.icon_completed_ : sub_ach.icon_);
156  img->set_tooltip(sub_ach.description_);
157  }
158  i++;
159  }
160 
161  // if an achievement hasn't defined the maximum possible sub-achievements, hide the [image]s for the rest
162  for(; i < sub_achievements_limit; i++)
163  {
164  static_cast<drawing*>(newrow.find("sub_icon"+std::to_string(i), false))->set_visible(visibility::invisible);
165  }
166 }
167 
168 } // namespace gui2::dialogs
constexpr int sub_achievements_limit
#define ERR_CONFIG
static lg::log_domain log_config("config")
static game_config_manager * get()
std::vector< achievement_group > & get_achievements()
A simple canvas which can be drawn upon.
Definition: canvas.hpp:44
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:153
This shows a dialog displaying achievements.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
void set_sub_achievements(grid &newrow, const achievement &ach)
Abstract base class for all modal dialogs.
window * get_window()
Returns a pointer to the dialog's window.
A drawing is widget with a fixed size and gives access to the canvas of the widget in the window inst...
Definition: drawing.hpp:49
Base container class.
Definition: grid.hpp:32
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
A label displays text that can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
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:59
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:118
void set_selected(unsigned selected, bool fire_event=true)
void set_values(const std::vector<::config > &values, unsigned selected=0)
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:81
This object shows the progress of a certain action, or the value state of a certain item.
void set_percentage(unsigned percentage)
void set_tooltip(const t_string &tooltip)
virtual void set_label(const t_string &text)
canvas & get_canvas(const unsigned index)
void set_visible(const visibility visible)
Definition: widget.cpp:470
@ invisible
The user set the widget invisible, that means:
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
std::size_t i
Definition: function.cpp:968
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
Standard logging facilities (interface).
std::string selected
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
Modify, read and display user preferences.
std::string selected_achievement_group()
Definition: general.cpp:1011
void set_selected_achievement_group(const std::string &content_for)
Definition: general.cpp:1016
A set of achievements tied to a particular content.
std::string content_for_
The internal ID used for this content.
std::vector< achievement > achievements_
The achievements associated to this content.
Represents a single achievement and its data.
std::vector< sub_achievement > sub_achievements_
The list of distinct sub-achievements for this achievement.
Represents a distinct sub-achievement within another achievement.
t_string description_
The description of the sub-achievement to be shown in its tooltip.
std::string icon_completed_
The icon of the sub-achievement to show on the UI when completed.
std::string icon_
The icon of the sub-achievement to show on the UI when not completed.
bool achieved_
Whether the sub-achievement has been completed.