supertux
object_option.hpp
1 // SuperTux
2 // Copyright (C) 2015 Hume2 <teratux.mail@gmail.com>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_EDITOR_OBJECT_OPTION_HPP
18 #define HEADER_SUPERTUX_EDITOR_OBJECT_OPTION_HPP
19 
20 #include <optional>
21 #include <string>
22 #include <vector>
23 
24 #include <sexp/value.hpp>
25 
26 #include "gui/menu_action.hpp"
27 #include "object/path_walker.hpp"
28 #include "video/color.hpp"
29 
30 enum ObjectOptionFlag {
33  OPTION_HIDDEN = (1 << 0),
34 
36  OPTION_TRANSLATABLE = (1 << 1)
37 };
38 
39 namespace sexp {
40 class Value;
41 } // namespace sexp
42 class Color;
43 enum class Direction;
44 class GameObject;
45 class Menu;
46 class Path;
47 class PathObject;
48 class Rectf;
49 class TileMap;
50 class Writer;
51 
53 {
54 public:
55  BaseObjectOption(const std::string& text, const std::string& key, unsigned int flags);
56  virtual ~BaseObjectOption() = default;
57 
58  virtual void save(Writer& write) const = 0;
59  virtual std::string to_string() const = 0;
60  virtual void add_to_menu(Menu& menu) const = 0;
61 
62  const std::string& get_key() const { return m_key; }
63  const std::string& get_text() const { return m_text; }
64  unsigned int get_flags() const { return m_flags; }
65 
66 protected:
67  const std::string m_text;
68  const std::string m_key;
69  const unsigned int m_flags;
70 
71 private:
72  BaseObjectOption(const BaseObjectOption&) = delete;
73  BaseObjectOption& operator=(const BaseObjectOption&) = delete;
74 };
75 
76 template <typename T = void>
78 {
79 public:
80  ObjectOption(const std::string& text, const std::string& key, unsigned int flags, T* pointer = nullptr);
81  virtual ~ObjectOption() override = default;
82 
83  virtual T* get_value() const { return m_value_pointer; }
84 
85 protected:
86  T* const m_value_pointer;
87 
88 private:
89  ObjectOption(const ObjectOption&) = delete;
90  ObjectOption& operator=(const ObjectOption&) = delete;
91 };
92 
93 class BoolObjectOption final : public ObjectOption<bool>
94 {
95 public:
96  BoolObjectOption(const std::string& text, bool* pointer, const std::string& key,
97  std::optional<bool> default_value,
98  unsigned int flags);
99 
100  virtual void save(Writer& write) const override;
101  virtual std::string to_string() const override;
102  virtual void add_to_menu(Menu& menu) const override;
103 
104 private:
105  const std::optional<bool> m_default_value;
106 
107 private:
108  BoolObjectOption(const BoolObjectOption&) = delete;
109  BoolObjectOption& operator=(const BoolObjectOption&) = delete;
110 };
111 
112 class IntObjectOption final : public ObjectOption<int>
113 {
114 public:
115  IntObjectOption(const std::string& text, int* pointer, const std::string& key,
116  std::optional<int> default_value,
117  unsigned int flags);
118 
119  virtual void save(Writer& write) const override;
120  virtual std::string to_string() const override;
121  virtual void add_to_menu(Menu& menu) const override;
122 
123 private:
124  const std::optional<int> m_default_value;
125 
126 private:
127  IntObjectOption(const IntObjectOption&) = delete;
128  IntObjectOption& operator=(const IntObjectOption&) = delete;
129 };
130 
131 class LabelObjectOption final : public ObjectOption<>
132 {
133 public:
134  LabelObjectOption(const std::string& text,
135  unsigned int flags);
136 
137  virtual void save(Writer& write) const override;
138  virtual std::string to_string() const override;
139  virtual void add_to_menu(Menu& menu) const override;
140 
141 private:
142  LabelObjectOption(const LabelObjectOption&) = delete;
143  LabelObjectOption& operator=(const LabelObjectOption&) = delete;
144 };
145 
146 class RectfObjectOption final : public ObjectOption<Rectf>
147 {
148 public:
149  RectfObjectOption(const std::string& text, Rectf* pointer, const std::string& key,
150  unsigned int flags);
151 
152  virtual void save(Writer& write) const override;
153  virtual std::string to_string() const override;
154  virtual void add_to_menu(Menu& menu) const override;
155 
156 private:
157  float m_width;
158  float m_height;
159 
160 private:
161  RectfObjectOption(const RectfObjectOption&) = delete;
162  RectfObjectOption& operator=(const RectfObjectOption&) = delete;
163 };
164 
165 class FloatObjectOption final : public ObjectOption<float>
166 {
167 public:
168  FloatObjectOption(const std::string& text, float* pointer, const std::string& key,
169  std::optional<float> default_value,
170  unsigned int flags);
171 
172  virtual void save(Writer& write) const override;
173  virtual std::string to_string() const override;
174  virtual void add_to_menu(Menu& menu) const override;
175 
176 private:
177  const std::optional<float> m_default_value;
178 
179 private:
180  FloatObjectOption(const FloatObjectOption&) = delete;
181  FloatObjectOption& operator=(const FloatObjectOption&) = delete;
182 };
183 
184 class StringObjectOption final : public ObjectOption<std::string>
185 {
186 public:
187  StringObjectOption(const std::string& text, std::string* pointer, const std::string& key,
188  std::optional<std::string> default_value,
189  unsigned int flags);
190 
191  virtual void save(Writer& write) const override;
192  virtual std::string to_string() const override;
193  virtual void add_to_menu(Menu& menu) const override;
194 
195 private:
196  std::optional<std::string> m_default_value;
197 
198 private:
199  StringObjectOption(const StringObjectOption&) = delete;
200  StringObjectOption& operator=(const StringObjectOption&) = delete;
201 };
202 
203 class StringMultilineObjectOption final : public ObjectOption<std::string>
204 {
205 public:
206  StringMultilineObjectOption(const std::string& text, std::string* pointer, const std::string& key,
207  std::optional<std::string> default_value,
208  unsigned int flags);
209 
210  virtual void save(Writer& write) const override;
211  virtual std::string to_string() const override;
212  virtual void add_to_menu(Menu& menu) const override;
213 
214 private:
215  std::optional<std::string> m_default_value;
216 
217 private:
219  StringMultilineObjectOption& operator=(const StringMultilineObjectOption&) = delete;
220 };
221 
222 class StringSelectObjectOption final : public ObjectOption<int>
223 {
224 public:
225  StringSelectObjectOption(const std::string& text, int* pointer, const std::vector<std::string>& select,
226  std::optional<int> default_value,
227  const std::string& key, unsigned int flags);
228 
229  virtual void save(Writer& write) const override;
230  virtual std::string to_string() const override;
231  virtual void add_to_menu(Menu& menu) const override;
232 
233 private:
234  const std::vector<std::string> m_select;
235  const std::optional<int> m_default_value;
236 
237 private:
239  StringSelectObjectOption& operator=(const StringSelectObjectOption&) = delete;
240 };
241 
242 class EnumObjectOption final : public ObjectOption<int>
243 {
244 public:
245  EnumObjectOption(const std::string& text, int* pointer,
246  const std::vector<std::string>& labels,
247  const std::vector<std::string>& symbols,
248  std::optional<int> default_value,
249  const std::string& key, unsigned int flags);
250 
251  virtual void save(Writer& write) const override;
252  virtual std::string to_string() const override;
253  virtual void add_to_menu(Menu& menu) const override;
254 
255 private:
256  const std::vector<std::string> m_labels;
257  const std::vector<std::string> m_symbols;
258  const std::optional<int> m_default_value;
259 
260 private:
261  EnumObjectOption(const EnumObjectOption&) = delete;
262  EnumObjectOption& operator=(const EnumObjectOption&) = delete;
263 };
264 
265 class ScriptObjectOption final : public ObjectOption<std::string>
266 {
267 public:
268  ScriptObjectOption(const std::string& text, std::string* pointer, const std::string& key,
269  unsigned int flags);
270 
271  virtual void save(Writer& write) const override;
272  virtual std::string to_string() const override;
273  virtual void add_to_menu(Menu& menu) const override;
274 
275 private:
276  ScriptObjectOption(const ScriptObjectOption&) = delete;
277  ScriptObjectOption& operator=(const ScriptObjectOption&) = delete;
278 };
279 
280 class FileObjectOption final : public ObjectOption<std::string>
281 {
282 public:
283  FileObjectOption(const std::string& text, std::string* pointer,
284  std::optional<std::string> default_value,
285  const std::string& key,
286  std::vector<std::string> filter,
287  const std::string& basedir,
288  bool path_relative_to_basedir,
289  unsigned int flags);
290 
291  virtual void save(Writer& write) const override;
292  virtual std::string to_string() const override;
293  virtual void add_to_menu(Menu& menu) const override;
294 
295 private:
296  std::optional<std::string> m_default_value;
297  const std::vector<std::string> m_filter;
298  std::string m_basedir;
299  bool m_path_relative_to_basedir;
300 
301 private:
302  FileObjectOption(const FileObjectOption&) = delete;
303  FileObjectOption& operator=(const FileObjectOption&) = delete;
304 };
305 
306 class ColorObjectOption final : public ObjectOption<Color>
307 {
308 public:
309  ColorObjectOption(const std::string& text, Color* pointer, const std::string& key,
310  std::optional<Color> default_value, bool use_alpha,
311  unsigned int flags);
312 
313  virtual void save(Writer& write) const override;
314  virtual std::string to_string() const override;
315  virtual void add_to_menu(Menu& menu) const override;
316 
317 private:
318  const std::optional<Color> m_default_value;
319  bool m_use_alpha;
320 
321 private:
322  ColorObjectOption(const ColorObjectOption&) = delete;
323  ColorObjectOption& operator=(const ColorObjectOption&) = delete;
324 };
325 
326 class ObjectSelectObjectOption final : public ObjectOption<std::vector<std::unique_ptr<GameObject>>>
327 {
328 public:
329  ObjectSelectObjectOption(const std::string& text, std::vector<std::unique_ptr<GameObject>>* pointer,
330  uint8_t get_objects_param, const std::function<void (std::unique_ptr<GameObject>)>& add_object_func,
331  const std::string& key, unsigned int flags);
332 
333  virtual void save(Writer& write) const override;
334  virtual std::string to_string() const override;
335  virtual void add_to_menu(Menu& menu) const override;
336 
337 private:
338  uint8_t m_get_objects_param;
339  const std::function<void (std::unique_ptr<GameObject>)> m_add_object_function;
340 
341 private:
343  ObjectSelectObjectOption& operator=(const ObjectSelectObjectOption&) = delete;
344 };
345 
346 class TilesObjectOption final : public ObjectOption<TileMap>
347 {
348 public:
349  TilesObjectOption(const std::string& text, TileMap* tilemap, const std::string& key,
350  unsigned int flags);
351 
352  virtual void save(Writer& write) const override;
353  virtual std::string to_string() const override;
354  virtual void add_to_menu(Menu& menu) const override;
355 
356 private:
357  TileMap* m_tilemap;
358 
359 private:
360  TilesObjectOption(const TilesObjectOption&) = delete;
361  TilesObjectOption& operator=(const TilesObjectOption&) = delete;
362 };
363 
364 class PathObjectOption final : public ObjectOption<Path>
365 {
366 public:
367  PathObjectOption(const std::string& text, Path* path, const std::string& key,
368  unsigned int flags);
369 
370  virtual void save(Writer& write) const override;
371  virtual std::string to_string() const override;
372  virtual void add_to_menu(Menu& menu) const override;
373 
374 private:
375  PathObjectOption(const PathObjectOption&) = delete;
376  PathObjectOption& operator=(const PathObjectOption&) = delete;
377 };
378 
379 class PathRefObjectOption final : public ObjectOption<PathObject>
380 {
381 public:
382  PathRefObjectOption(const std::string& text, PathObject& target, const std::string& path_ref,
383  const std::string& key, unsigned int flags);
384 
385  virtual void save(Writer& write) const override;
386  virtual std::string to_string() const override;
387  virtual void add_to_menu(Menu& menu) const override;
388 
389 private:
390  std::string m_path_ref;
391 
392 private:
393  PathRefObjectOption(const PathRefObjectOption&) = delete;
394  PathRefObjectOption& operator=(const PathRefObjectOption&) = delete;
395 };
396 
397 class SExpObjectOption final : public ObjectOption<sexp::Value>
398 {
399 public:
400  SExpObjectOption(const std::string& text, const std::string& key, sexp::Value& value, unsigned int flags);
401 
402  virtual void save(Writer& write) const override;
403  virtual std::string to_string() const override;
404  virtual void add_to_menu(Menu& menu) const override;
405 
406 private:
407  SExpObjectOption(const SExpObjectOption&) = delete;
408  SExpObjectOption& operator=(const SExpObjectOption&) = delete;
409 };
410 
411 class PathHandleOption final : public ObjectOption<PathWalker::Handle>
412 {
413 public:
414  PathHandleOption(const std::string& text, PathWalker::Handle& handle,
415  const std::string& key, unsigned int flags);
416 
417  virtual void save(Writer& write) const override;
418  virtual std::string to_string() const override;
419  virtual void add_to_menu(Menu& menu) const override;
420 
421 private:
422  PathWalker::Handle& m_target;
423 
424 private:
425  PathHandleOption(const PathHandleOption&) = delete;
426  PathHandleOption& operator=(const PathHandleOption&) = delete;
427 };
428 
429 class RemoveObjectOption final : public ObjectOption<>
430 {
431 public:
433 
434  virtual void save(Writer& write) const override {}
435  virtual std::string to_string() const override;
436  virtual void add_to_menu(Menu& menu) const override;
437 
438 private:
439  RemoveObjectOption(const RemoveObjectOption&) = delete;
440  RemoveObjectOption& operator=(const RemoveObjectOption&) = delete;
441 };
442 
443 class TestFromHereOption final : public ObjectOption<>
444 {
445 public:
447 
448  virtual void save(Writer& write) const override {}
449  virtual std::string to_string() const override;
450  virtual void add_to_menu(Menu& menu) const override;
451 
452 private:
453  TestFromHereOption(const TestFromHereOption&) = delete;
454  TestFromHereOption& operator=(const TestFromHereOption&) = delete;
455 };
456 
457 class ParticleEditorOption final : public ObjectOption<>
458 {
459 public:
461 
462  virtual void save(Writer& write) const override {}
463  virtual std::string to_string() const override;
464  virtual void add_to_menu(Menu& menu) const override;
465 
466 private:
468  ParticleEditorOption& operator=(const ParticleEditorOption&) = delete;
469 };
470 
471 class ButtonOption final : public ObjectOption<>
472 {
473 public:
474  ButtonOption(const std::string& text, std::function<void()> callback);
475 
476  virtual void save(Writer& write) const override {}
477  virtual std::string to_string() const override;
478  virtual void add_to_menu(Menu& menu) const override;
479 
480 private:
481  std::function<void()> m_callback;
482 
483 private:
484  ButtonOption(const ButtonOption&) = delete;
485  ButtonOption& operator=(const ButtonOption&) = delete;
486 };
487 
488 class StringArrayOption final : public ObjectOption<>
489 {
490 public:
491  StringArrayOption(const std::string& text, const std::string& key, std::vector<std::string>& items);
492 
493  virtual void save(Writer& write) const override;
494  virtual std::string to_string() const override { return "text-area"; }
495  virtual void add_to_menu(Menu& menu) const override;
496 
497 private:
498  std::vector<std::string>& m_items;
499 
500 private:
501  StringArrayOption(const StringArrayOption&) = delete;
502  StringArrayOption& operator=(const StringArrayOption&) = delete;
503 };
504 
505 class ListOption final : public ObjectOption<std::string>
506 {
507 public:
508  ListOption(const std::string& text, const std::string& key, const std::vector<std::string>& items, std::string* value_ptr);
509 
510  virtual void save(Writer& write) const override;
511  virtual std::string to_string() const override { return *m_value_pointer; }
512  virtual void add_to_menu(Menu& menu) const override;
513 
514 private:
515  const std::vector<std::string>& m_items;
516 
517 private:
518  ListOption(const ListOption&) = delete;
519  ListOption& operator=(const ListOption&) = delete;
520 };
521 
522 class DirectionOption final : public ObjectOption<Direction>
523 {
524 public:
525  DirectionOption(const std::string& text, Direction* value_ptr,
526  std::vector<Direction> possible_directions,
527  const std::string& key, unsigned int flags);
528 
529  virtual void save(Writer& write) const override;
530  virtual std::string to_string() const override;
531  virtual void add_to_menu(Menu& menu) const override;
532 
533 private:
534  std::vector<Direction> m_possible_directions;
535 
536 private:
537  DirectionOption(const DirectionOption&) = delete;
538  DirectionOption& operator=(const DirectionOption&) = delete;
539 };
540 
541 #endif
542 
543 /* EOF */
Definition: object_option.hpp:203
Definition: object_option.hpp:222
Definition: object_option.hpp:505
Definition: object_option.hpp:265
Definition: object_option.hpp:112
Definition: object_option.hpp:77
Definition: object_option.hpp:429
Definition: object_option.hpp:93
Definition: object_option.hpp:165
Definition: object_option.hpp:457
Helper class that allows to displace a handle on an object.
Definition: path_walker.hpp:35
Definition: object_option.hpp:306
Definition: object_option.hpp:146
Definition: object_option.hpp:379
Definition: object_option.hpp:184
Definition: path.hpp:48
Definition: rectf.hpp:31
Definition: object_option.hpp:522
Definition: object_option.hpp:471
Definition: object_option.hpp:52
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
Definition: object_option.hpp:364
Definition: object_option.hpp:397
Definition: object_option.hpp:346
Definition: object_option.hpp:411
Definition: menu.hpp:55
Definition: object_option.hpp:488
Definition: object_option.hpp:280
Definition: object_option.hpp:39
Definition: object_option.hpp:326
Definition: color.hpp:26
A base class for all objects that contain, or make use of a path.
Definition: path_object.hpp:36
This class is responsible for managing an array of tiles.
Definition: tilemap.hpp:49
Definition: object_option.hpp:242
Definition: object_option.hpp:443
Definition: object_option.hpp:131
Definition: writer.cpp:23