The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
custom_tod.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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-editor"
16 
18 
19 #include "filesystem.hpp"
20 #include "filechooser.hpp"
23 #include "gui/auxiliary/field.hpp"
24 #include "gui/dialogs/helper.hpp"
25 #include "gui/widgets/button.hpp"
26 #include "gui/widgets/label.hpp"
27 #include "gui/widgets/slider.hpp"
28 #include "gui/widgets/settings.hpp"
30 #include "gui/widgets/image.hpp"
31 #include "gui/widgets/text_box.hpp"
32 #include "gettext.hpp"
33 #include "image.hpp"
34 
35 #include "utils/functional.hpp"
36 
37 namespace gui2
38 {
39 
40 /*WIKI
41  * @page = GUIWindowDefinitionWML
42  * @order = 2_custom_tod
43  *
44  * == Custom Schedules ==
45  *
46  * This shows the dialog to modify tod schedules.
47  *
48  * @begin{table}{dialog_widgets}
49  *
50  * current_tod_name & & text_box & m &
51  * The name of the time of day(ToD). $
52  *
53  * current_tod_id & & text_box & m &
54  * The id of the time of day(ToD). $
55  *
56  * current_tod_image & & image & m &
57  * The image for the time of day(ToD). $
58  *
59  * current_tod_mask & & image & m &
60  * The image mask for the time of day(ToD). $
61  *
62  * current_tod_sonund & & label & m &
63  * The sound for the time of day(ToD). $
64  *
65  * next_tod & & button & m &
66  * Selects the next ToD. $
67  *
68  * prev_tod & & button & m &
69  * Selects the previous ToD. $
70  *
71  * lawful_bonus & & slider & m &
72  * Sets the Lawful Bonus for the current ToD. $
73  *
74  * tod_red & & slider & m &
75  * Sets the red component of the current ToD. $
76  *
77  * tod_green & & slider & m &
78  * Sets the green component of the current ToD. $
79  *
80  * tod_blue & & slider & m &
81  * Sets the blue component of the current ToD. $
82  *
83  * @end{table}
84  */
85 
86 REGISTER_DIALOG(custom_tod)
87 
89  const std::vector<time_of_day>& tods)
90  : tods_(tods)
91  , current_tod_(0)
92  , current_tod_name_(nullptr)
93  , current_tod_id_(nullptr)
94  , current_tod_image_(nullptr)
95  , current_tod_mask_(nullptr)
96  , current_tod_sound_(nullptr)
97  , current_tod_number_(nullptr)
98  , lawful_bonus_field_(register_integer("lawful_bonus", true))
99  , tod_red_field_(nullptr)
100  , tod_green_field_(nullptr)
101  , tod_blue_field_(nullptr)
102  , display_(display)
103 {
104 }
105 
107  const std::string& default_dir,
108  const std::string& attribute,
109  twindow& window)
110 {
111  std::string fn = filesystem::base_name(filename);
113  if(dn.empty()) {
114  dn = default_dir;
115  }
116 
118  display_->video(), dn, _("Choose File"));
119  if(res == 0) {
120  if(attribute == "image") {
121  tods_[current_tod_].image = dn;
122  } else if(attribute == "mask") {
123  tods_[current_tod_].image_mask = dn;
124  } else if(attribute == "sound") {
125  tods_[current_tod_].sounds = dn;
126  }
127  }
128  update_selected_tod_info(window);
129 }
130 
132 {
133  current_tod_ = (current_tod_ + 1) % tods_.size();
134  update_selected_tod_info(window);
135 }
136 
138 {
139  current_tod_ = (current_tod_ ? current_tod_ : tods_.size()) - 1;
140  update_selected_tod_info(window);
141 }
142 
144 {
145  tods_.insert(tods_.begin() + current_tod_, time_of_day());
146  update_selected_tod_info(window);
147 }
148 
150 {
151  assert(tods_.begin() + current_tod_ < tods_.end());
152  if(tods_.size() == 1) {
153  tods_.at(0) = time_of_day();
154  update_selected_tod_info(window);
155  return;
156  }
157  tods_.erase(tods_.begin() + current_tod_);
158  if(tods_.begin() + current_tod_ >= tods_.end()) {
159  current_tod_ = tods_.size() - 1;
160  }
161  update_selected_tod_info(window);
162 }
163 
165 {
166  assert(static_cast<size_t>(current_tod_) < tods_.size());
167  return tods_[current_tod_];
168 }
169 
171 {
175 
176  if(!display_) {
177  return;
178  }
179 
180  // Prevent a floating slice of window appearing alone over the
181  // theme UI sidebar after redrawing tiles and before we have a
182  // chance to redraw the rest of this window.
183  window.undraw();
184 
185  // NOTE: We only really want to re-render the gamemap tiles here.
186  // Redrawing everything is a significantly more expensive task.
187  // At this time, tiles are the only elements on which ToD tint is
188  // meant to have a visible effect. This is very strongly tied to
189  // the image caching mechanism.
190  //
191  // If this ceases to be the case in the future, you'll need to call
192  // redraw_everything() instead.
193 
194  // invalidate all tiles so they are redrawn with the new ToD tint next
196 
197  // redraw tiles
198  display_->draw(false);
199 
200  window.invalidate_layout();
201 }
202 
204 {
205  tods_[current_tod_].lawful_bonus
207 }
208 
210 {
214  current_tod_mask_->set_image(get_selected_tod().image_mask);
216 
217  std::stringstream ss;
218  ss << (current_tod_ + 1) << "/" << tods_.size();
219  current_tod_number_->set_label(ss.str());
220 
222  get_selected_tod().lawful_bonus);
226 
227  update_tod_display(window);
228 }
229 
231 {
232  assert(!tods_.empty());
233 
235  = find_widget<tslider>(&window, "tod_red", false, true);
236 
238  = find_widget<tslider>(&window, "tod_green", false, true);
239 
241  = find_widget<tslider>(&window, "tod_blue", false, true);
242 
244  = find_widget<ttext_box>(&window, "tod_name", false, true);
245 
246  current_tod_id_ = find_widget<ttext_box>(&window, "tod_id", false, true);
247 
249  = find_widget<timage>(&window, "current_tod_image", false, true);
250 
252  = find_widget<timage>(&window, "current_tod_mask", false, true);
253 
255  = find_widget<tlabel>(&window, "current_sound", false, true);
256 
258  = find_widget<tlabel>(&window, "tod_number", false, true);
259 
260  connect_signal_mouse_left_click(find_widget<tbutton>(&window, "image_button", false),
261  std::bind(&tcustom_tod::select_file,
262  this,
264  "data/core/images/misc",
265  "image",
266  std::ref(window)));
267 
268  connect_signal_mouse_left_click(find_widget<tbutton>(&window, "mask_button", false),
269  std::bind(&tcustom_tod::select_file,
270  this,
271  get_selected_tod().image_mask,
272  "data/core/images",
273  "mask",
274  std::ref(window)));
275 
276  connect_signal_mouse_left_click(find_widget<tbutton>(&window, "sound_button", false),
277  std::bind(&tcustom_tod::select_file,
278  this,
279  get_selected_tod().sounds,
280  "data/core/sounds/ambient",
281  "sound",
282  std::ref(window)));
283 
285  find_widget<tbutton>(&window, "next_tod", false),
286  std::bind(&tcustom_tod::do_next_tod, this, std::ref(window)));
287 
289  find_widget<tbutton>(&window, "previous_tod", false),
290  std::bind(&tcustom_tod::do_prev_tod, this, std::ref(window)));
291 
293  find_widget<tbutton>(&window, "new", false),
294  std::bind(&tcustom_tod::do_new_tod, this, std::ref(window)));
295 
297  find_widget<tbutton>(&window, "delete", false),
298  std::bind(&tcustom_tod::do_delete_tod, this, std::ref(window)));
299 
303  this,
304  std::ref(window)));
305 
307  *tod_red_field_,
309  this,
310  std::ref(window)));
311 
313  *tod_green_field_,
315  this,
316  std::ref(window)));
317 
319  *tod_blue_field_,
321  this,
322  std::ref(window)));
323 
324  for(size_t i = 0; i < tods_.size(); ++i) {
325  time_of_day& tod = tods_[i];
326  const int r = tod_red_field_->get_value();
327  const int g = tod_green_field_->get_value();
328  const int b = tod_blue_field_->get_value();
329  if(tod.color.r == r && tod.color.g == g && tod.color.b == b) {
330  current_tod_ = i;
331  update_selected_tod_info(window);
332  return;
333  }
334  }
335 
336  update_selected_tod_info(window);
337 }
338 
340 {
341  update_tod_display(window);
342 
343  if(get_retval() == twindow::OK) {
344  // TODO: save ToD
345  }
346 }
347 
348 } // namespace gui2
ttext_box * current_tod_name_
Text boxes for name and id.
Definition: custom_tod.hpp:93
tlabel * current_tod_number_
Definition: custom_tod.hpp:102
tcontrol * widget()
Definition: field.hpp:208
int get_retval() const
Definition: dialog.hpp:161
tod_color color
The color modifications that should be made to the game board to reflect the time of day...
Definition: time_of_day.hpp:89
void update_selected_tod_info(twindow &window)
Definition: custom_tod.cpp:209
void update_tod_display(twindow &window)
Definition: custom_tod.cpp:170
void connect_signal_notify_modified(tdispatcher &dispatcher, const tsignal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.hpp:725
REGISTER_DIALOG(label_settings)
virtual void set_label(const t_string &label)
Definition: control.cpp:330
void connect_signal_mouse_left_click(tdispatcher &dispatcher, const tsignal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.hpp:710
STL namespace.
GLboolean GLboolean g
Definition: glew.h:7319
void set_value(const int value)
Inherited from tinteger_selector_.
Definition: slider.cpp:77
T get_widget_value(twindow &window)
Gets the value of the field.
Definition: field.hpp:398
void undraw()
Undraws the window.
Definition: window.cpp:917
virtual void draw()
Draws invalidated items.
Definition: display.cpp:2706
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed...
const time_of_day & get_selected_tod() const
Definition: custom_tod.cpp:164
virtual void set_value(const std::string &text)
The set_value is virtual for the tpassword_box class.
Definition: text.cpp:95
timage * current_tod_image_
Images for the current tod.
Definition: custom_tod.hpp:97
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
editor::editor_display * display_
The display to update when the ToD changes.
Definition: custom_tod.hpp:115
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:48
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
Dialog is closed with ok button.
Definition: window.hpp:125
This file contains the settings handling of the widget library.
ttext_box * current_tod_id_
Definition: custom_tod.hpp:94
int get_value() const
Inherited from tinteger_selector_.
Definition: slider.hpp:48
void pre_show(twindow &window)
Inherited from tdialog.
Definition: custom_tod.cpp:230
std::string base_name(const std::string &file)
Returns the base filename of a file, with directory name stripped.
int show_file_chooser_dialog(CVideo &video, std::string &filename, std::string const &title, bool show_directory_buttons, const std::string &type_a_head, int xloc, int yloc)
Show a dialog where the user can navigate through files and select a file.
Definition: filechooser.cpp:30
Manage the empty-palette in the editor.
Definition: action.cpp:28
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:3525
GLuint color
Definition: glew.h:5801
void set_color_adjustment(int r, int g, int b)
will make all scaled images have these rgb values added to all their pixels.
Definition: image.cpp:698
void select_file(const std::string &filename, const std::string &default_dir, const std::string &attribute, twindow &window)
Definition: custom_tod.cpp:106
GLuint res
Definition: glew.h:9258
void set_widget_value(twindow &window, CT value)
Sets the value of the field.
Definition: field.hpp:361
void update_lawful_bonus(twindow &window)
Definition: custom_tod.cpp:203
tslider * tod_blue_field_
Definition: custom_tod.hpp:107
void do_delete_tod(twindow &window)
Definition: custom_tod.cpp:149
size_t i
Definition: function.cpp:1057
tlabel * current_tod_sound_
Labels for the current tod.
Definition: custom_tod.hpp:101
Declarations for File-IO.
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
std::vector< time_of_day > tods_
Available time_of_days.
Definition: custom_tod.hpp:57
int current_tod_
Current map generator index.
Definition: custom_tod.hpp:90
GLuint const GLchar * name
Definition: glew.h:1782
void do_new_tod(twindow &window)
Definition: custom_tod.cpp:143
GLenum GLint ref
Definition: glew.h:1813
tfield_integer * lawful_bonus_field_
Definition: custom_tod.hpp:104
timage * current_tod_mask_
Definition: custom_tod.hpp:98
this module manages the cache of images.
Definition: image.cpp:75
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:202
tslider * tod_red_field_
Definition: custom_tod.hpp:105
void do_next_tod(twindow &window)
Callback for the next tod button.
Definition: custom_tod.cpp:131
void do_prev_tod(twindow &window)
Definition: custom_tod.cpp:137
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void post_show(twindow &window)
Inherited from tdialog.
Definition: custom_tod.cpp:339
tslider * tod_green_field_
Definition: custom_tod.hpp:106
std::string directory_name(const std::string &file)
Returns the directory name of a file, with filename stripped.
void invalidate_layout()
Updates the size of the window.
Definition: window.cpp:941