1 | /**************************************************************************/ |
2 | /* tile_atlas_view.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef TILE_ATLAS_VIEW_H |
32 | #define TILE_ATLAS_VIEW_H |
33 | |
34 | #include "editor/gui/editor_zoom_widget.h" |
35 | #include "scene/gui/box_container.h" |
36 | #include "scene/gui/button.h" |
37 | #include "scene/gui/center_container.h" |
38 | #include "scene/gui/label.h" |
39 | #include "scene/gui/margin_container.h" |
40 | #include "scene/resources/tile_set.h" |
41 | |
42 | class ViewPanner; |
43 | |
44 | class TileAtlasView : public Control { |
45 | GDCLASS(TileAtlasView, Control); |
46 | |
47 | private: |
48 | TileSet *tile_set = nullptr; |
49 | TileSetAtlasSource *tile_set_atlas_source = nullptr; |
50 | int source_id = TileSet::INVALID_SOURCE; |
51 | |
52 | enum DragType { |
53 | DRAG_TYPE_NONE, |
54 | DRAG_TYPE_PAN, |
55 | }; |
56 | DragType drag_type = DRAG_TYPE_NONE; |
57 | float previous_zoom = 1.0; |
58 | EditorZoomWidget *zoom_widget = nullptr; |
59 | Button *button_center_view = nullptr; |
60 | CenterContainer *center_container = nullptr; |
61 | Vector2 panning; |
62 | void _update_zoom_and_panning(bool p_zoom_on_mouse_pos = false); |
63 | void _zoom_widget_changed(); |
64 | void _center_view(); |
65 | virtual void gui_input(const Ref<InputEvent> &p_event) override; |
66 | |
67 | Ref<ViewPanner> panner; |
68 | void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event); |
69 | void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event); |
70 | |
71 | HashMap<Vector2, HashMap<int, Rect2i>> alternative_tiles_rect_cache; |
72 | void _update_alternative_tiles_rect_cache(); |
73 | |
74 | MarginContainer *margin_container = nullptr; |
75 | int margin_container_paddings[4] = { 0, 0, 0, 0 }; |
76 | HBoxContainer *hbox = nullptr; |
77 | Label *missing_source_label = nullptr; |
78 | |
79 | // Background |
80 | Control *background_left = nullptr; |
81 | void _draw_background_left(); |
82 | Control *background_right = nullptr; |
83 | void _draw_background_right(); |
84 | |
85 | // Left side. |
86 | Control *base_tiles_root_control = nullptr; |
87 | void _base_tiles_root_control_gui_input(const Ref<InputEvent> &p_event); |
88 | |
89 | Control *base_tiles_drawing_root = nullptr; |
90 | |
91 | Control *base_tiles_draw = nullptr; |
92 | HashMap<Ref<Material>, RID> material_tiles_draw; |
93 | HashMap<Ref<Material>, RID> material_alternatives_draw; |
94 | void _draw_base_tiles(); |
95 | RID _get_canvas_item_to_draw(const TileData *p_for_data, const CanvasItem *p_base_item, HashMap<Ref<Material>, RID> &p_material_map); |
96 | void _clear_material_canvas_items(); |
97 | |
98 | Control *base_tiles_texture_grid = nullptr; |
99 | void _draw_base_tiles_texture_grid(); |
100 | |
101 | Control *base_tiles_shape_grid = nullptr; |
102 | void _draw_base_tiles_shape_grid(); |
103 | |
104 | Size2i _compute_base_tiles_control_size(); |
105 | |
106 | // Right side. |
107 | Control *alternative_tiles_root_control = nullptr; |
108 | void _alternative_tiles_root_control_gui_input(const Ref<InputEvent> &p_event); |
109 | |
110 | Control *alternative_tiles_drawing_root = nullptr; |
111 | |
112 | Control *alternatives_draw = nullptr; |
113 | void _draw_alternatives(); |
114 | |
115 | Size2i _compute_alternative_tiles_control_size(); |
116 | |
117 | struct ThemeCache { |
118 | Ref<Texture2D> center_view_icon; |
119 | Ref<Texture2D> checkerboard; |
120 | } theme_cache; |
121 | |
122 | protected: |
123 | virtual void _update_theme_item_cache() override; |
124 | |
125 | void _notification(int p_what); |
126 | static void _bind_methods(); |
127 | |
128 | public: |
129 | // Global. |
130 | void set_atlas_source(TileSet *p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id); |
131 | |
132 | float get_zoom() const; |
133 | void set_transform(float p_zoom, Vector2i p_panning); |
134 | |
135 | void set_padding(Side p_side, int p_padding); |
136 | |
137 | // Left side. |
138 | void set_texture_grid_visible(bool p_visible) { base_tiles_texture_grid->set_visible(p_visible); }; |
139 | void set_tile_shape_grid_visible(bool p_visible) { base_tiles_shape_grid->set_visible(p_visible); }; |
140 | |
141 | Vector2i get_atlas_tile_coords_at_pos(const Vector2 p_pos, bool p_clamp = false) const; |
142 | |
143 | void add_control_over_atlas_tiles(Control *p_control, bool scaled = true) { |
144 | if (scaled) { |
145 | base_tiles_drawing_root->add_child(p_control); |
146 | } else { |
147 | base_tiles_root_control->add_child(p_control); |
148 | } |
149 | p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
150 | p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS); |
151 | }; |
152 | |
153 | // Right side. |
154 | Vector3i get_alternative_tile_at_pos(const Vector2 p_pos) const; |
155 | Rect2i get_alternative_tile_rect(const Vector2i p_coords, int p_alternative_tile); |
156 | |
157 | void add_control_over_alternative_tiles(Control *p_control, bool scaled = true) { |
158 | if (scaled) { |
159 | alternative_tiles_drawing_root->add_child(p_control); |
160 | } else { |
161 | alternative_tiles_root_control->add_child(p_control); |
162 | } |
163 | p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
164 | p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS); |
165 | }; |
166 | |
167 | // Redraw everything. |
168 | void queue_redraw(); |
169 | |
170 | TileAtlasView(); |
171 | ~TileAtlasView(); |
172 | }; |
173 | |
174 | #endif // TILE_ATLAS_VIEW_H |
175 | |