Debian Codegraph @

editor/scene/2d/tiles/tile_atlas_view.h

raw symbols SCIP
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 #pragma once
32
33 #include "editor/gui/editor_zoom_widget.h"
34 #include "scene/gui/box_container.h"
35 #include "scene/gui/button.h"
36 #include "scene/gui/center_container.h"
37 #include "scene/gui/label.h"
38 #include "scene/gui/margin_container.h"
39 #include "scene/resources/2d/tile_set.h"
40
41 class ViewPanner;
42
43 class TileAtlasView : public Control {
44 GDCLASS(TileAtlasView, Control);
45
46 private:
47 Ref<TileSet> tile_set;
48 Ref<TileSetAtlasSource> tile_set_atlas_source;
49 int source_id = TileSet::INVALID_SOURCE;
50
51 float previous_zoom = 1.0;
52 EditorZoomWidget *zoom_widget = nullptr;
53 Button *button_center_view = nullptr;
54 CenterContainer *center_container = nullptr;
55 Vector2 panning;
56 void _update_zoom_and_panning(bool p_zoom_on_mouse_pos = false, const Vector2 &p_mouse_pos = Vector2());
57 void _zoom_widget_changed();
58 void _center_view();
59 virtual void gui_input(const Ref<InputEvent> &p_event) override;
60
61 Ref<ViewPanner> panner;
62 void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
63 void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
64
65 HashMap<Vector2, HashMap<int, Rect2i>> alternative_tiles_rect_cache;
66 void _update_alternative_tiles_rect_cache();
67
68 MarginContainer *margin_container = nullptr;
69 int margin_container_paddings[4] = { 0, 0, 0, 0 };
70 HBoxContainer *hbox = nullptr;
71 Label *missing_source_label = nullptr;
72
73 // Background
74 Control *background_left = nullptr;
75 void _draw_background_left();
76 Control *background_right = nullptr;
77 void _draw_background_right();
78
79 // Left side.
80 Control *base_tiles_root_control = nullptr;
81 void _base_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);
82
83 Control *base_tiles_drawing_root = nullptr;
84
85 Control *base_tiles_draw = nullptr;
86 HashMap<Ref<Material>, RID> material_tiles_draw;
87 HashMap<Ref<Material>, RID> material_alternatives_draw;
88 void _draw_base_tiles();
89 RID _get_canvas_item_to_draw(const TileData *p_for_data, const CanvasItem *p_base_item, HashMap<Ref<Material>, RID> &p_material_map);
90 void _clear_material_canvas_items();
91
92 Control *base_tiles_texture_grid = nullptr;
93 void _draw_base_tiles_texture_grid();
94
95 Control *base_tiles_shape_grid = nullptr;
96 void _draw_base_tiles_shape_grid();
97
98 Size2i _compute_base_tiles_control_size();
99
100 // Right side.
101 Control *alternative_tiles_root_control = nullptr;
102 void _alternative_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);
103
104 Control *alternative_tiles_drawing_root = nullptr;
105
106 Control *alternatives_draw = nullptr;
107 void _draw_alternatives();
108
109 Size2i _compute_alternative_tiles_control_size();
110
111 struct ThemeCache {
112 Ref<Texture2D> center_view_icon;
113 Ref<Texture2D> checkerboard;
114 } theme_cache;
115
116 protected:
117 virtual void _update_theme_item_cache() override;
118
119 void _notification(int p_what);
120 static void _bind_methods();
121
122 public:
123 // Global.
124 void set_atlas_source(TileSet *p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id);
125
126 float get_zoom() const;
127 void set_transform(float p_zoom, Vector2i p_panning);
128
129 void set_padding(Side p_side, int p_padding);
130
131 // Left side.
132 void set_texture_grid_visible(bool p_visible) { base_tiles_texture_grid->set_visible(p_visible); }
133 void set_tile_shape_grid_visible(bool p_visible) { base_tiles_shape_grid->set_visible(p_visible); }
134
135 Vector2i get_atlas_tile_coords_at_pos(const Vector2 p_pos, bool p_clamp = false) const;
136
137 void add_control_over_atlas_tiles(Control *p_control, bool scaled = true) {
138 if (scaled) {
139 base_tiles_drawing_root->add_child(p_control);
140 } else {
141 base_tiles_root_control->add_child(p_control);
142 }
143 p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
144 p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
145 }
146
147 // Right side.
148 Vector3i get_alternative_tile_at_pos(const Vector2 p_pos) const;
149 Rect2i get_alternative_tile_rect(const Vector2i p_coords, int p_alternative_tile);
150
151 void add_control_over_alternative_tiles(Control *p_control, bool scaled = true) {
152 if (scaled) {
153 alternative_tiles_drawing_root->add_child(p_control);
154 } else {
155 alternative_tiles_root_control->add_child(p_control);
156 }
157 p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
158 p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
159 }
160
161 // Redraw everything.
162 void queue_redraw();
163
164 TileAtlasView();
165 ~TileAtlasView();
166 };