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