supertux
tilebox.hpp
1 // SuperTux
2 // Copyright (C) 2015 Hume2 <teratux.mail@gmail.com>
3 // 2024 Vankata453
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 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef HEADER_SUPERTUX_EDITOR_TILEBOX_HPP
19 #define HEADER_SUPERTUX_EDITOR_TILEBOX_HPP
20 
21 #include "editor/widget.hpp"
22 
23 #include <functional>
24 
25 #include "editor/tip.hpp"
26 #include "interface/control_scrollbar.hpp"
27 #include "math/rectf.hpp"
28 #include "math/vector.hpp"
29 #include "supertux/tile_set.hpp"
30 
31 class Editor;
32 class ObjectGroup;
33 class ObjectInfo;
34 class Rectf;
35 class TileSelection;
36 
44 class EditorTilebox final : public Widget
45 {
46 public:
47  enum class HoveredItem {
48  NONE, TILE, SCROLLBAR
49  };
50 
51  enum class TileScrolling {
52  NONE, UP, DOWN
53  };
54 
55  enum class InputType {
56  NONE, TILE, OBJECT
57  };
58 
59 public:
60  EditorTilebox(Editor& editor, const Rectf& rect);
61 
62  virtual void draw(DrawingContext& context) override;
63 
64  virtual bool on_mouse_button_up(const SDL_MouseButtonEvent& button) override;
65  virtual bool on_mouse_button_down(const SDL_MouseButtonEvent& button) override;
66  virtual bool on_mouse_motion(const SDL_MouseMotionEvent& motion) override;
67  virtual bool on_mouse_wheel(const SDL_MouseWheelEvent& wheel) override;
68 
69  virtual void setup() override;
70  virtual void resize() override;
71 
72  void set_rect(const Rectf& rect);
73  Rectf get_rect() const { return m_rect; }
74 
75  void on_select(const std::function<void(EditorTilebox&)>& callback);
76 
77  void select_tilegroup(int id);
78  void set_tilegroup(std::unique_ptr<Tilegroup> tilegroup);
79  void select_objectgroup(int id);
80  bool select_layers_objectgroup();
81 
82  const ObjectInfo& get_object_info() const { return *m_object_info; }
83  InputType get_input_type() const { return m_input_type; }
84  void set_input_type(InputType input_type) { m_input_type = input_type; }
85 
86  TileSelection* get_tiles() const { return m_tiles.get(); }
87  const std::string& get_object() const { return m_object; }
88  void set_object(const std::string& object) { m_object = object; }
89 
90  float get_tiles_height() const;
91 
92  bool has_active_object_tip() const { return m_object_tip->get_visible(); }
93 
94 private:
95  Vector get_tile_coords(int pos, bool relative = true) const;
96  int get_tile_pos(const Vector& coords) const;
97  Rectf get_tile_rect() const;
98 
99  void update_selection();
100  Rectf normalize_selection(bool rounded) const;
101  Rectf selection_draw_rect() const;
102 
103  void update_hovered_tile();
104 
105  void reset_scrollbar();
106 
107  void draw_tilegroup(DrawingContext& context);
108  void draw_objectgroup(DrawingContext& context);
109 
110 private:
111  Editor& m_editor;
112 
113  Rectf m_rect;
114 
115  std::unique_ptr<TileSelection> m_tiles;
116 
117  std::string m_object;
118  std::unique_ptr<Tip> m_object_tip;
119  InputType m_input_type;
120 
121  std::unique_ptr<Tilegroup> m_active_tilegroup;
122  ObjectGroup* m_active_objectgroup;
123  std::unique_ptr<ObjectInfo> m_object_info;
124 
125  std::function<void(EditorTilebox&)> m_on_select_callback;
126 
127  std::unique_ptr<ControlScrollbar> m_scrollbar;
128  float m_scroll_progress;
129 
130  HoveredItem m_hovered_item;
131  int m_hovered_tile;
132 
133  bool m_dragging;
134  Vector m_drag_start;
135 
136  Vector m_mouse_pos;
137 
138 private:
139  EditorTilebox(const EditorTilebox&) = delete;
140  EditorTilebox& operator=(const EditorTilebox&) = delete;
141 };
142 
143 #endif
144 
145 /* EOF */
Definition: rectf.hpp:31
Definition: object_info.hpp:24
Definition: tile_selection.hpp:24
The tilebox allows selection of tiles or objects from a specified tilegroup/objectgroup.
Definition: tilebox.hpp:44
Definition: object_group.hpp:27
Definition: editor.hpp:49
Definition: widget.hpp:24
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42