supertux
particle_editor.hpp
1 // SuperTux
2 // Copyright (C) 2020 A. Semphris <semphris@protonmail.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_PARTICLE_EDITOR_HPP
18 #define HEADER_SUPERTUX_EDITOR_PARTICLE_EDITOR_HPP
19 
20 #include <functional>
21 #include <vector>
22 #include <memory>
23 
24 #include <SDL.h>
25 
26 #include "interface/control.hpp"
27 #include "interface/control_button.hpp"
28 #include "interface/control_enum.hpp"
29 #include "interface/control_checkbox.hpp"
30 #include "interface/control_textbox_float.hpp"
31 #include "interface/control_textbox_int.hpp"
32 #include "interface/label.hpp"
33 #include "object/custom_particle_system.hpp"
34 #include "supertux/screen.hpp"
35 #include "util/currenton.hpp"
36 #include "util/writer.hpp"
37 
38 class ParticleEditor final : public Screen,
39  public Currenton<ParticleEditor>
40 {
41  friend class ParticleEditorMenu;
42 
43 public:
44  static bool is_active();
45 
46 private:
47  static bool (*m_clamp_0_1)(ControlTextboxFloat*, float);
48 
49 public:
51  ~ParticleEditor() override;
52 
53 public:
54  virtual void draw(Compositor&) override;
55  virtual void update(float dt_sec, const Controller& controller) override;
56 
57  virtual void setup() override;
58  virtual void leave() override;
59 
60  virtual IntegrationStatus get_status() const override;
61 
62  void event(const SDL_Event& ev);
63  void update_keyboard(const Controller& controller);
64  void check_unsaved_changes(const std::function<void ()>& action);
65  void quit_editor();
66 
67  void reactivate();
68 
69  void open_particle_directory();
70 
71  // saves the particle to file
72  void save(const std::string& filename, bool retry = false);
73  void save(Writer& writer);
74  void request_save(bool is_save_as = false,
75  const std::function<void(bool)>& callback = [](bool was_saved){});
76  void open(const std::string& filename) { m_filename = filename; reload(); }
77  void new_file() { m_filename = ""; reload(); }
78 
82  void reload();
83 
84 private:
85  void reload_particles();
86  void reset_main_ui();
87  void reset_texture_ui();
88 
89  void addTextboxFloat(const std::string& name, float* bind,
90  bool (*float_validator)(ControlTextboxFloat*,
91  float) = nullptr);
92  void addTextboxFloatWithImprecision(const std::string& name, float* bind,
93  float* imprecision_bind,
94  bool (*float_validator)(ControlTextboxFloat*,
95  float) = nullptr,
96  bool (*imp_validator)(ControlTextboxFloat*,
97  float) = nullptr);
98  void addTextboxInt(const std::string& name, int* bind,
99  bool (*int_validator)(ControlTextboxInt*,
100  int) = nullptr);
101  void addCheckbox(std::string name, bool* bind);
102  void addControl(std::string name,
103  std::unique_ptr<InterfaceControl> new_control);
104  void addEasingEnum(std::string name, EasingMode* bind);
105 
106  void push_version();
107  void undo();
108  void redo();
109 
110 public:
111  bool m_enabled;
112  bool m_quit_request;
113  std::vector<std::unique_ptr<InterfaceControl>> m_controls;
114  std::vector<std::unique_ptr<InterfaceControl>> m_controls_textures;
115  std::vector<std::function<void()>> m_texture_rebinds;
116  std::vector<std::shared_ptr<CustomParticleSystem::ParticleProps>> m_undo_stack;
117  std::vector<std::shared_ptr<CustomParticleSystem::ParticleProps>> m_redo_stack;
118  bool m_in_texture_tab;
119  int m_texture_current;
120 
121  std::shared_ptr<CustomParticleSystem::ParticleProps> m_saved_version;
122 
123  std::unique_ptr<CustomParticleSystem> m_particles;
124  std::string m_filename;
125 
126 private:
127  ParticleEditor(const ParticleEditor&) = delete;
128  ParticleEditor& operator=(const ParticleEditor&) = delete;
129 };
130 
131 #endif
132 
133 /* EOF */
Definition: particle_editor.hpp:38
Definition: controller.hpp:57
Abstract base class for code the MainLoop runs exclusively and full-screen.
Definition: screen.hpp:31
virtual void setup() override
gets called before this screen gets activated (which is at least once before the first draw or update...
Definition: particle_editor.cpp:754
virtual void draw(Compositor &) override
gets called once per frame.
Definition: particle_editor.cpp:651
Definition: particle_editor_menu.hpp:22
A &#39;Currenton&#39; allows access to the currently active instance of a class via the static current() func...
Definition: currenton.hpp:30
virtual void update(float dt_sec, const Controller &controller) override
gets called for once (per logical) frame.
Definition: particle_editor.cpp:690
virtual IntegrationStatus get_status() const override
Gives details about what the user is doing right now.
Definition: particle_editor.cpp:887
Definition: compositor.hpp:29
Definition: integration.hpp:26
Definition: control_textbox_float.hpp:22
virtual void leave() override
gets called when the current screen is temporarily suspended
Definition: particle_editor.cpp:748
Definition: control_textbox_int.hpp:22
void reload()
Reloads the particle object from the filename in m_filename.
Definition: particle_editor.cpp:78
Definition: writer.cpp:23