The Battle for Wesnoth  1.17.0-dev
wml_message.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
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 #pragma once
16 
18 
19 namespace gui2::dialogs
20 {
21 
22 /**
23  * Helper class for message options
24  */
26 public:
27  explicit wml_message_option(std::string label, std::string description = "", std::string image = "")
28  : label_(label)
30  , image_(image)
31  {}
32  std::string label() const {return label_;}
33  std::string description() const {return description_;}
34  std::string image() const {return image_;}
35 private:
36  std::string label_, description_, image_;
37 };
38 
39 /**
40  * Base class for the wml generated messages.
41  *
42  * We have a separate sub class for left and right images.
43  */
45 {
46 public:
47  wml_message_base(const std::string& title,
48  const std::string& message,
49  const std::string& portrait,
50  const bool mirror)
51  : title_(title)
52  , image_("")
53  , message_(message)
54  , portrait_(portrait)
55  , mirror_(mirror)
56  , has_input_(false)
57  , input_caption_("")
58  , input_text_(nullptr)
59  , input_maximum_length_(0)
60  , option_list_()
61  , chosen_option_(nullptr)
62  {
63  }
64 
65  /**
66  * Sets the input text variables.
67  *
68  * @param caption The caption for the label.
69  * @param [in,out] text The initial text, after showing the final
70  * text.
71  * @param maximum_length The maximum length of the text.
72  */
73  void set_input(const std::string& caption,
74  std::string* text,
75  const unsigned maximum_length);
76  /**
77  * Sets the option list
78  *
79  * @param option_list The list of options to display.
80  * @param [in,out] chosen_option Pointer to the index of the initially
81  * selected option; after showing, the
82  * chosen option.
83  */
84  void set_option_list(const std::vector<wml_message_option>& option_list,
85  int* chosen_option);
86 
87 private:
88  /** The title for the dialog. */
89  std::string title_;
90 
91  /**
92  * The image which is shown in the dialog.
93  *
94  * This image can be an icon or portrait or any other image.
95  */
96  std::string image_;
97 
98  /** The message to show to the user. */
99  std::string message_;
100  /** Filename of the portrait. */
101  std::string portrait_;
102 
103  /** Mirror the portrait? */
104  bool mirror_;
105 
106  /** Do we need to show an input box? */
108 
109  /** The caption to show for the input text. */
110  std::string input_caption_;
111 
112  /** The text input. */
113  std::string* input_text_;
114 
115  /** The maximum length of the input text. */
117 
118  /** The list of options the user can choose. */
119  std::vector<wml_message_option> option_list_;
120 
121  /** The chosen option. */
123 
124 protected:
125  virtual void pre_show(window& window) override;
126 
127 private:
128  virtual void post_show(window& window) override;
129 };
130 
131 /** Shows a dialog with the portrait on the left side. */
133 {
134 public:
135  wml_message_left(const std::string& title,
136  const std::string& message,
137  const std::string& portrait,
138  const bool mirror)
139  : wml_message_base(title, message, portrait, mirror)
140  {
141  }
142 
143 private:
144  virtual const std::string& window_id() const override;
145 };
146 
147 /** Shows a dialog with the portrait on the right side. */
149 {
150 public:
151  wml_message_right(const std::string& title,
152  const std::string& message,
153  const std::string& portrait,
154  const bool mirror)
155  : wml_message_base(title, message, portrait, mirror)
156  {
157  }
158 
159 private:
160  virtual const std::string& window_id() const override;
161 };
162 
163 /** Shows a dialog with two portraits, one on each side. */
165 {
166 public:
167  wml_message_double(const std::string& title,
168  const std::string& message,
169  const std::string& portrait,
170  const bool mirror,
171  const std::string& second_portrait,
172  const bool second_mirror)
173  : wml_message_left(title, message, portrait, mirror)
174  , second_portrait_(second_portrait)
175  , second_mirror_(second_mirror)
176  {
177  }
178 
179 private:
180  virtual const std::string& window_id() const override;
181 
182  virtual void pre_show(window& window) override;
183 
184  std::string second_portrait_;
185 
187 };
188 
189 /**
190  * Parameter pack for message list input options
191  */
193 {
194  /** A list of options to select in the dialog. */
195  std::vector<wml_message_option> option_list;
196  /**
197  * The initially chosen option.
198  * Will be set to the chosen option when the dialog closes.
199  */
200  mutable int chosen_option;
201 };
202 
203 /**
204  * Parameter pack for message text input options
205  */
207 {
208  /**
209  * The caption for the optional input text box.
210  * If empty, there is no input box.
211  */
212  std::string caption;
213  /**
214  * The initial text value.
215  * Will be set to the result.
216  */
217  mutable std::string text;
218  /** The maximum length of the text. */
219  unsigned maximum_length;
220  /** True when [text_input] appeared in [message] */
222 };
223 
224 /**
225  * Parameter pack for message portrait
226  */
228 {
229  /** Filename of the portrait. */
230  std::string portrait;
231  /** Does the portrait need to be mirrored? */
232  bool mirror;
233 };
234 
235 /**
236  * Helper function to show a portrait.
237  *
238  * @param title The title of the dialog.
239  * @param message The message to show.
240  * @param left Portrait to show on the left.
241  * @param right Portrait to show on the right.
242  * @param options Options to offer.
243  * @param input Info on text input.
244  */
245 int show_wml_message(const std::string& title,
246  const std::string& message,
247  const wml_message_portrait* left,
250  const wml_message_input& input);
251 
252 } // 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:89
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:34
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:56
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:27
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:568
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:47
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:44
Helper class for message options.
Definition: wml_message.hpp:25
std::string message_
The message to show to the user.
Definition: wml_message.hpp:99
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:64
bool mirror
Does the portrait need to be mirrored?
Parameter pack for message text input options.
std::string description() const
Definition: wml_message.hpp:33
std::string image_
The image which is shown in the dialog.
Definition: wml_message.hpp:96