The Battle for Wesnoth  1.17.0-dev
wml_message.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
19 
20 namespace gui2::dialogs
21 {
22 
23 /**
24  * Helper class for message options
25  */
27 public:
28  explicit wml_message_option(std::string label, std::string description = "", std::string image = "")
29  : label_(label)
31  , image_(image)
32  {}
33  std::string label() const {return label_;}
34  std::string description() const {return description_;}
35  std::string image() const {return image_;}
36 private:
37  std::string label_, description_, image_;
38 };
39 
40 /**
41  * Base class for the wml generated messages.
42  *
43  * We have a separate sub class for left and right images.
44  */
46 {
47 public:
48  wml_message_base(const std::string& title,
49  const std::string& message,
50  const std::string& portrait,
51  const bool mirror)
52  : title_(title)
53  , image_("")
54  , message_(message)
55  , portrait_(portrait)
56  , mirror_(mirror)
57  , has_input_(false)
58  , input_caption_("")
59  , input_text_(nullptr)
60  , input_maximum_length_(0)
61  , option_list_()
62  , chosen_option_(nullptr)
63  {
64  }
65 
66  /**
67  * Sets the input text variables.
68  *
69  * @param caption The caption for the label.
70  * @param [in,out] text The initial text, after showing the final
71  * text.
72  * @param maximum_length The maximum length of the text.
73  */
74  void set_input(const std::string& caption,
75  std::string* text,
76  const unsigned maximum_length);
77  /**
78  * Sets the option list
79  *
80  * @param option_list The list of options to display.
81  * @param [in,out] chosen_option Pointer to the index of the initially
82  * selected option; after showing, the
83  * chosen option.
84  */
85  void set_option_list(const std::vector<wml_message_option>& option_list,
86  int* chosen_option);
87 
88 private:
89  /** The title for the dialog. */
90  std::string title_;
91 
92  /**
93  * The image which is shown in the dialog.
94  *
95  * This image can be an icon or portrait or any other image.
96  */
97  std::string image_;
98 
99  /** The message to show to the user. */
100  std::string message_;
101  /** Filename of the portrait. */
102  std::string portrait_;
103 
104  /** Mirror the portrait? */
105  bool mirror_;
106 
107  /** Do we need to show an input box? */
109 
110  /** The caption to show for the input text. */
111  std::string input_caption_;
112 
113  /** The text input. */
114  std::string* input_text_;
115 
116  /** The maximum length of the input text. */
118 
119  /** The list of options the user can choose. */
120  std::vector<wml_message_option> option_list_;
121 
122  /** The chosen option. */
124 
125 protected:
126  virtual void pre_show(window& window) override;
127 
128 private:
129  virtual void post_show(window& window) override;
130 };
131 
132 /** Shows a dialog with the portrait on the left side. */
134 {
135 public:
136  wml_message_left(const std::string& title,
137  const std::string& message,
138  const std::string& portrait,
139  const bool mirror)
140  : wml_message_base(title, message, portrait, mirror)
141  {
142  }
143 
144 private:
145  virtual const std::string& window_id() const override;
146 };
147 
148 /** Shows a dialog with the portrait on the right side. */
150 {
151 public:
152  wml_message_right(const std::string& title,
153  const std::string& message,
154  const std::string& portrait,
155  const bool mirror)
156  : wml_message_base(title, message, portrait, mirror)
157  {
158  }
159 
160 private:
161  virtual const std::string& window_id() const override;
162 };
163 
164 /** Shows a dialog with two portraits, one on each side. */
166 {
167 public:
168  wml_message_double(const std::string& title,
169  const std::string& message,
170  const std::string& portrait,
171  const bool mirror,
172  const std::string& second_portrait,
173  const bool second_mirror)
174  : wml_message_left(title, message, portrait, mirror)
175  , second_portrait_(second_portrait)
176  , second_mirror_(second_mirror)
177  {
178  }
179 
180 private:
181  virtual const std::string& window_id() const override;
182 
183  virtual void pre_show(window& window) override;
184 
185  std::string second_portrait_;
186 
188 };
189 
190 /**
191  * Parameter pack for message list input options
192  */
194 {
195  /** A list of options to select in the dialog. */
196  std::vector<wml_message_option> option_list;
197  /**
198  * The initially chosen option.
199  * Will be set to the chosen option when the dialog closes.
200  */
201  mutable int chosen_option;
202 };
203 
204 /**
205  * Parameter pack for message text input options
206  */
208 {
209  /**
210  * The caption for the optional input text box.
211  * If empty, there is no input box.
212  */
213  std::string caption;
214  /**
215  * The initial text value.
216  * Will be set to the result.
217  */
218  mutable std::string text;
219  /** The maximum length of the text. */
220  unsigned maximum_length;
221  /** True when [text_input] appeared in [message] */
223 };
224 
225 /**
226  * Parameter pack for message portrait
227  */
229 {
230  /** Filename of the portrait. */
231  std::string portrait;
232  /** Does the portrait need to be mirrored? */
233  bool mirror;
234 };
235 
236 /**
237  * Helper function to show a portrait.
238  *
239  * @param title The title of the dialog.
240  * @param message The message to show.
241  * @param left Portrait to show on the left.
242  * @param right Portrait to show on the right.
243  * @param options Options to offer.
244  * @param input Info on text input.
245  */
246 int show_wml_message(const std::string& title,
247  const std::string& message,
248  const wml_message_portrait* left,
251  const wml_message_input& input);
252 
253 } // namespace dialogs
wml_message_right(const std::string &title, const std::string &message, const std::string &portrait, const bool mirror)
Parameter pack for message portrait.
bool text_input_was_specified
True when [text_input] appeared in [message].
std::string portrait_
Filename of the portrait.
int chosen_option
The initially chosen option.
std::string caption
The caption for the optional input text box.
std::string title_
The title for the dialog.
Definition: wml_message.hpp:90
std::vector< wml_message_option > option_list_
The list of options the user can choose.
std::string text
The initial text value.
Main class to show messages to the user.
Definition: message.hpp:35
std::string input_caption_
The caption to show for the input text.
Parameter pack for message list input options.
unsigned maximum_length
The maximum length of the text.
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:57
wml_message_left(const std::string &title, const std::string &message, const std::string &portrait, const bool mirror)
wml_message_option(std::string label, std::string description="", std::string image="")
Definition: wml_message.hpp:28
wml_message_double(const std::string &title, const std::string &message, const std::string &portrait, const bool mirror, const std::string &second_portrait, const bool second_mirror)
Shows a dialog with the portrait on the left side.
const config & options()
Definition: game.cpp:569
unsigned input_maximum_length_
The maximum length of the input text.
wml_message_base(const std::string &title, const std::string &message, const std::string &portrait, const bool mirror)
Definition: wml_message.hpp:48
lu_byte right
Definition: lparser.cpp:1227
std::string portrait
Filename of the portrait.
bool has_input_
Do we need to show an input box?
int show_wml_message(const std::string &title, const std::string &message, const wml_message_portrait *left, const wml_message_portrait *right, const wml_message_options &options, const wml_message_input &input)
Helper function to show a portrait.
bool mirror_
Mirror the portrait?
Base class for the wml generated messages.
Definition: wml_message.hpp:45
Helper class for message options.
Definition: wml_message.hpp:26
std::string message_
The message to show to the user.
Shows a dialog with two portraits, one on each side.
std::vector< wml_message_option > option_list
A list of options to select in the dialog.
lu_byte left
Definition: lparser.cpp:1226
std::string * input_text_
The text input.
Abstract base class for all modal dialogs.
Functions to load and save images from/to disk.
int * chosen_option_
The chosen option.
Shows a dialog with the portrait on the right side.
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
bool mirror
Does the portrait need to be mirrored?
Parameter pack for message text input options.
std::string description() const
Definition: wml_message.hpp:34
std::string image_
The image which is shown in the dialog.
Definition: wml_message.hpp:97