Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/2d/tiles/tile_atlas_view.h
9906 views
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
enum DragType {
52
DRAG_TYPE_NONE,
53
DRAG_TYPE_PAN,
54
};
55
DragType drag_type = DRAG_TYPE_NONE;
56
float previous_zoom = 1.0;
57
EditorZoomWidget *zoom_widget = nullptr;
58
Button *button_center_view = nullptr;
59
CenterContainer *center_container = nullptr;
60
Vector2 panning;
61
void _update_zoom_and_panning(bool p_zoom_on_mouse_pos = false, const Vector2 &p_mouse_pos = Vector2());
62
void _zoom_widget_changed();
63
void _center_view();
64
virtual void gui_input(const Ref<InputEvent> &p_event) override;
65
66
Ref<ViewPanner> panner;
67
void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
68
void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
69
70
HashMap<Vector2, HashMap<int, Rect2i>> alternative_tiles_rect_cache;
71
void _update_alternative_tiles_rect_cache();
72
73
MarginContainer *margin_container = nullptr;
74
int margin_container_paddings[4] = { 0, 0, 0, 0 };
75
HBoxContainer *hbox = nullptr;
76
Label *missing_source_label = nullptr;
77
78
// Background
79
Control *background_left = nullptr;
80
void _draw_background_left();
81
Control *background_right = nullptr;
82
void _draw_background_right();
83
84
// Left side.
85
Control *base_tiles_root_control = nullptr;
86
void _base_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);
87
88
Control *base_tiles_drawing_root = nullptr;
89
90
Control *base_tiles_draw = nullptr;
91
HashMap<Ref<Material>, RID> material_tiles_draw;
92
HashMap<Ref<Material>, RID> material_alternatives_draw;
93
void _draw_base_tiles();
94
RID _get_canvas_item_to_draw(const TileData *p_for_data, const CanvasItem *p_base_item, HashMap<Ref<Material>, RID> &p_material_map);
95
void _clear_material_canvas_items();
96
97
Control *base_tiles_texture_grid = nullptr;
98
void _draw_base_tiles_texture_grid();
99
100
Control *base_tiles_shape_grid = nullptr;
101
void _draw_base_tiles_shape_grid();
102
103
Size2i _compute_base_tiles_control_size();
104
105
// Right side.
106
Control *alternative_tiles_root_control = nullptr;
107
void _alternative_tiles_root_control_gui_input(const Ref<InputEvent> &p_event);
108
109
Control *alternative_tiles_drawing_root = nullptr;
110
111
Control *alternatives_draw = nullptr;
112
void _draw_alternatives();
113
114
Size2i _compute_alternative_tiles_control_size();
115
116
struct ThemeCache {
117
Ref<Texture2D> center_view_icon;
118
Ref<Texture2D> checkerboard;
119
} theme_cache;
120
121
protected:
122
virtual void _update_theme_item_cache() override;
123
124
void _notification(int p_what);
125
static void _bind_methods();
126
127
public:
128
// Global.
129
void set_atlas_source(TileSet *p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id);
130
131
float get_zoom() const;
132
void set_transform(float p_zoom, Vector2i p_panning);
133
134
void set_padding(Side p_side, int p_padding);
135
136
// Left side.
137
void set_texture_grid_visible(bool p_visible) { base_tiles_texture_grid->set_visible(p_visible); }
138
void set_tile_shape_grid_visible(bool p_visible) { base_tiles_shape_grid->set_visible(p_visible); }
139
140
Vector2i get_atlas_tile_coords_at_pos(const Vector2 p_pos, bool p_clamp = false) const;
141
142
void add_control_over_atlas_tiles(Control *p_control, bool scaled = true) {
143
if (scaled) {
144
base_tiles_drawing_root->add_child(p_control);
145
} else {
146
base_tiles_root_control->add_child(p_control);
147
}
148
p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
149
p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
150
}
151
152
// Right side.
153
Vector3i get_alternative_tile_at_pos(const Vector2 p_pos) const;
154
Rect2i get_alternative_tile_rect(const Vector2i p_coords, int p_alternative_tile);
155
156
void add_control_over_alternative_tiles(Control *p_control, bool scaled = true) {
157
if (scaled) {
158
alternative_tiles_drawing_root->add_child(p_control);
159
} else {
160
alternative_tiles_root_control->add_child(p_control);
161
}
162
p_control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
163
p_control->set_mouse_filter(Control::MOUSE_FILTER_PASS);
164
}
165
166
// Redraw everything.
167
void queue_redraw();
168
169
TileAtlasView();
170
~TileAtlasView();
171
};
172
173