supertux
overlay_widget.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_OVERLAY_WIDGET_HPP
18 #define HEADER_SUPERTUX_EDITOR_OVERLAY_WIDGET_HPP
19 
20 #include <SDL.h>
21 #include <chrono>
22 
23 #include "control/input_manager.hpp"
24 #include "editor/tile_selection.hpp"
25 #include "editor/widget.hpp"
26 #include "math/vector.hpp"
27 #include "object/tilemap.hpp"
28 #include "supertux/timer.hpp"
29 #include "util/typed_uid.hpp"
30 
31 class Color;
32 class DrawingContext;
33 class Editor;
34 class GameObject;
35 class MovingObject;
36 class NodeMarker;
37 class Path;
38 class Rectf;
39 class Tip;
40 
43 class EditorOverlayWidget final : public Widget
44 {
45 public:
46  static Color text_autotile_available_color;
47  static Color text_autotile_active_color;
48  static Color text_autotile_error_color;
49  static Color warning_color;
50  static Color error_color;
51 
52 public:
53  EditorOverlayWidget(Editor& editor);
54  ~EditorOverlayWidget() override;
55 
56  virtual void draw(DrawingContext&) override;
57  virtual void update(float dt_sec) override;
58 
59  virtual bool on_mouse_button_up(const SDL_MouseButtonEvent& button) override;
60  virtual bool on_mouse_button_down(const SDL_MouseButtonEvent& button) override;
61  virtual bool on_mouse_motion(const SDL_MouseMotionEvent& motion) override;
62  virtual bool on_key_up(const SDL_KeyboardEvent& key) override;
63  virtual bool on_key_down(const SDL_KeyboardEvent& key) override;
64  virtual void resize() override;
65 
66  void update_pos();
67  void delete_markers();
68  void update_node_iterators();
69  void on_level_change();
70 
71  void edit_path(PathGameObject* path, GameObject* new_marked_object = nullptr);
72  void reset_action_press();
73 
74 private:
75  static bool action_pressed;
76  static bool alt_pressed;
77 
78 private:
79  void input_tile(const Vector& pos, uint32_t tile);
80  void autotile(const Vector& pos, uint32_t tile);
81  void input_autotile(const Vector& pos, uint32_t tile);
82  void autotile_corner(const Vector& pos, uint32_t tile, TileMap::AutotileCornerOperation op);
83  void input_autotile_corner(const Vector& corner, uint32_t tile, const Vector& override_pos = Vector(-1.f, -1.f));
84  void put_tile(const Vector& target_tile);
85  void put_next_tiles();
86  void draw_rectangle();
87  void preview_rectangle();
88  bool check_tiles_for_fill(uint32_t replace_tile, uint32_t target_tile, uint32_t third_tile) const;
89  void fill();
90  void replace();
91  void put_object();
92 
93  void rubber_object();
94  void rubber_rect();
95 
96  void grab_object();
97  void move_object();
98  void clone_object();
99  void hover_object();
100  void show_object_menu(GameObject& object);
101  void select_object();
102  void add_path_node();
103 
104  void draw_tile_tip(DrawingContext&);
105  void draw_tile_grid(DrawingContext&, int tile_size, bool draw_shadow) const;
106  void draw_tilemap_border(DrawingContext&);
107  void draw_path(DrawingContext&);
108  void draw_rectangle_preview(DrawingContext& context);
109 
110  void process_left_click();
111  void process_right_click();
112  void process_middle_click();
113 
114  // sp is sector pos, tp is pos on tilemap.
115  Vector tp_to_sp(const Vector& tp, int tile_size = 32) const;
116  Vector sp_to_tp(const Vector& sp, int tile_size = 32) const;
117  Vector tile_screen_pos(const Vector& tp, int tile_size = 32) const;
118  Vector align_to_tilemap(const Vector& sp, int tile_size = 32) const;
119  bool is_position_inside_tilemap(const TileMap* tilemap, const Vector& pos) const;
120 
121  // in sector position
122  Rectf drag_rect() const;
123  Rectf tile_drag_rect() const;
124  Rectf selection_draw_rect() const;
125  void update_tile_selection();
126 
127  void set_warning(const std::string& text, float time);
128 
129 private:
130  Editor& m_editor;
131  Vector m_hovered_tile;
132  Vector m_hovered_tile_prev;
133  Vector m_sector_pos;
134  Vector m_mouse_pos;
135  Vector m_previous_mouse_pos;
136 
137  std::chrono::steady_clock::time_point m_time_prev_put_tile;
138 
139  bool m_dragging;
140  bool m_dragging_right;
141  bool m_scrolling;
142  Vector m_drag_start;
143  TypedUID<MovingObject> m_dragged_object;
144 
145  TypedUID<MovingObject> m_hovered_object;
146  TypedUID<GameObject> m_selected_object;
147  TypedUID<PathGameObject> m_edited_path;
148  TypedUID<NodeMarker> m_last_node_marker;
149 
150  std::unique_ptr<Tip> m_object_tip;
151  Vector m_obj_mouse_desync;
152 
153  std::unique_ptr<TileSelection> m_rectangle_preview;
154 
155  // Warnings
156  Timer m_warning_timer;
157  std::string m_warning_text;
158 
159  bool m_selection_warning;
160 
161 private:
162  EditorOverlayWidget(const EditorOverlayWidget&) = delete;
163  EditorOverlayWidget& operator=(const EditorOverlayWidget&) = delete;
164 };
165 
166 #endif
167 
168 /* EOF */
Definition: tip.hpp:28
A widget that is drawn on top of the current sector and handles mouse input and tool drawing...
Definition: overlay_widget.hpp:43
Definition: path.hpp:48
Definition: rectf.hpp:31
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
Base class for all dynamic/moving game objects.
Definition: moving_object.hpp:35
Definition: editor.hpp:49
Definition: widget.hpp:24
Definition: color.hpp:26
Definition: node_marker.hpp:24
Simple timer designed to be used in the update functions of objects.
Definition: timer.hpp:24
This class is responsible for managing an array of tiles.
Definition: tilemap.hpp:49
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
Definition: path_gameobject.hpp:32