Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/2d/sprite_2d_editor_plugin.cpp
9903 views
1
/**************************************************************************/
2
/* sprite_2d_editor_plugin.cpp */
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
#include "sprite_2d_editor_plugin.h"
32
33
#include "core/math/geometry_2d.h"
34
#include "editor/docks/scene_tree_dock.h"
35
#include "editor/editor_node.h"
36
#include "editor/editor_undo_redo_manager.h"
37
#include "editor/gui/editor_zoom_widget.h"
38
#include "editor/scene/canvas_item_editor_plugin.h"
39
#include "editor/settings/editor_settings.h"
40
#include "editor/themes/editor_scale.h"
41
#include "scene/2d/light_occluder_2d.h"
42
#include "scene/2d/mesh_instance_2d.h"
43
#include "scene/2d/physics/collision_polygon_2d.h"
44
#include "scene/2d/polygon_2d.h"
45
#include "scene/gui/box_container.h"
46
#include "scene/gui/menu_button.h"
47
#include "scene/gui/panel.h"
48
#include "scene/gui/view_panner.h"
49
#include "thirdparty/clipper2/include/clipper2/clipper.h"
50
51
#define PRECISION 1
52
53
void Sprite2DEditor::_node_removed(Node *p_node) {
54
if (p_node == node) {
55
node = nullptr;
56
options->hide();
57
}
58
}
59
60
Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float epsilon = 2.0) {
61
int size = points.size();
62
ERR_FAIL_COND_V(size < 2, Vector<Vector2>());
63
64
Clipper2Lib::PathD subj(points.size());
65
for (int i = 0; i < points.size(); i++) {
66
subj[i] = Clipper2Lib::PointD(points[i].x, points[i].y);
67
}
68
69
Clipper2Lib::PathsD solution = Clipper2Lib::InflatePaths({ subj }, epsilon, Clipper2Lib::JoinType::Miter, Clipper2Lib::EndType::Polygon, 2.0, PRECISION, 0.0);
70
// Here the miter_limit = 2.0 and arc_tolerance = 0.0 are Clipper2 defaults,
71
// and PRECISION is used to scale points up internally, to attain the desired precision.
72
73
ERR_FAIL_COND_V(solution.size() == 0, points);
74
75
// Clamp into the specified rect.
76
Clipper2Lib::RectD clamp(rect.position.x,
77
rect.position.y,
78
rect.position.x + rect.size.width,
79
rect.position.y + rect.size.height);
80
Clipper2Lib::PathsD out = Clipper2Lib::RectClip(clamp, solution[0], PRECISION);
81
// Here PRECISION is used to scale points up internally, to attain the desired precision.
82
83
ERR_FAIL_COND_V(out.size() == 0, points);
84
85
const Clipper2Lib::PathD &p2 = out[0];
86
87
Vector<Vector2> outPoints;
88
89
int lasti = p2.size() - 1;
90
Vector2 prev = Vector2(p2[lasti].x, p2[lasti].y);
91
for (uint64_t i = 0; i < p2.size(); i++) {
92
Vector2 cur = Vector2(p2[i].x, p2[i].y);
93
if (cur.distance_to(prev) > 0.5) {
94
outPoints.push_back(cur);
95
prev = cur;
96
}
97
}
98
return outPoints;
99
}
100
101
void Sprite2DEditor::_menu_option(int p_option) {
102
if (!node) {
103
return;
104
}
105
106
selected_menu_item = (Menu)p_option;
107
108
switch (p_option) {
109
case MENU_OPTION_CONVERT_TO_MESH_2D: {
110
debug_uv_dialog->set_ok_button_text(TTR("Create MeshInstance2D"));
111
debug_uv_dialog->set_title(TTR("MeshInstance2D Preview"));
112
113
_popup_debug_uv_dialog();
114
} break;
115
case MENU_OPTION_CONVERT_TO_POLYGON_2D: {
116
debug_uv_dialog->set_ok_button_text(TTR("Create Polygon2D"));
117
debug_uv_dialog->set_title(TTR("Polygon2D Preview"));
118
119
_popup_debug_uv_dialog();
120
} break;
121
case MENU_OPTION_CREATE_COLLISION_POLY_2D: {
122
debug_uv_dialog->set_ok_button_text(TTR("Create CollisionPolygon2D"));
123
debug_uv_dialog->set_title(TTR("CollisionPolygon2D Preview"));
124
125
_popup_debug_uv_dialog();
126
} break;
127
case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: {
128
debug_uv_dialog->set_ok_button_text(TTR("Create LightOccluder2D"));
129
debug_uv_dialog->set_title(TTR("LightOccluder2D Preview"));
130
131
_popup_debug_uv_dialog();
132
} break;
133
}
134
}
135
136
void Sprite2DEditor::_popup_debug_uv_dialog() {
137
String error_message;
138
if (node->get_owner() != get_tree()->get_edited_scene_root() && node != get_tree()->get_edited_scene_root()) {
139
error_message = TTR("Can't convert a sprite from a foreign scene.");
140
}
141
Ref<Texture2D> texture = node->get_texture();
142
if (texture.is_null()) {
143
error_message = TTR("Can't convert an empty sprite to mesh.");
144
}
145
146
if (!error_message.is_empty()) {
147
err_dialog->set_text(error_message);
148
err_dialog->popup_centered();
149
return;
150
}
151
152
_update_mesh_data();
153
debug_uv_dialog->popup_centered();
154
get_tree()->connect("process_frame", callable_mp(this, &Sprite2DEditor::_center_view), CONNECT_ONE_SHOT);
155
debug_uv->set_texture_filter(node->get_texture_filter_in_tree());
156
debug_uv->queue_redraw();
157
}
158
159
void Sprite2DEditor::_update_mesh_data() {
160
ERR_FAIL_NULL(node);
161
Ref<Texture2D> texture = node->get_texture();
162
ERR_FAIL_COND(texture.is_null());
163
Ref<Image> image = texture->get_image();
164
ERR_FAIL_COND(image.is_null());
165
166
if (image->is_compressed()) {
167
image->decompress();
168
}
169
170
Rect2 rect = node->is_region_enabled() ? node->get_region_rect() : Rect2(Point2(), image->get_size());
171
rect.size /= Vector2(node->get_hframes(), node->get_vframes());
172
rect.position += node->get_frame_coords() * rect.size;
173
174
Ref<BitMap> bm;
175
bm.instantiate();
176
bm->create_from_image_alpha(image);
177
178
int shrink = shrink_pixels->get_value();
179
if (shrink > 0) {
180
bm->shrink_mask(shrink, rect);
181
}
182
183
int grow = grow_pixels->get_value();
184
if (grow > 0) {
185
bm->grow_mask(grow, rect);
186
}
187
188
float epsilon = simplification->get_value();
189
190
Vector<Vector<Vector2>> lines = bm->clip_opaque_to_polygons(rect, epsilon);
191
192
uv_lines.clear();
193
194
computed_vertices.clear();
195
computed_uv.clear();
196
computed_indices.clear();
197
198
Size2 img_size = image->get_size();
199
for (int i = 0; i < lines.size(); i++) {
200
lines.write[i] = expand(lines[i], rect, epsilon);
201
}
202
203
if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D) {
204
for (int j = 0; j < lines.size(); j++) {
205
int index_ofs = computed_vertices.size();
206
207
for (int i = 0; i < lines[j].size(); i++) {
208
Vector2 vtx = lines[j][i];
209
computed_uv.push_back((vtx + rect.position) / img_size);
210
211
if (node->is_flipped_h()) {
212
vtx.x = rect.size.x - vtx.x;
213
}
214
if (node->is_flipped_v()) {
215
vtx.y = rect.size.y - vtx.y;
216
}
217
vtx += node->get_offset();
218
if (node->is_centered()) {
219
vtx -= rect.size / 2.0;
220
}
221
222
computed_vertices.push_back(vtx);
223
}
224
225
Vector<int> poly = Geometry2D::triangulate_polygon(lines[j]);
226
227
for (int i = 0; i < poly.size(); i += 3) {
228
for (int k = 0; k < 3; k++) {
229
int idx = i + k;
230
int idxn = i + (k + 1) % 3;
231
uv_lines.push_back(lines[j][poly[idx]] + rect.position);
232
uv_lines.push_back(lines[j][poly[idxn]] + rect.position);
233
234
computed_indices.push_back(poly[idx] + index_ofs);
235
}
236
}
237
}
238
}
239
240
outline_lines.clear();
241
computed_outline_lines.clear();
242
243
if (selected_menu_item == MENU_OPTION_CONVERT_TO_POLYGON_2D || selected_menu_item == MENU_OPTION_CREATE_COLLISION_POLY_2D || selected_menu_item == MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D) {
244
outline_lines.resize(lines.size());
245
computed_outline_lines.resize(lines.size());
246
for (int pi = 0; pi < lines.size(); pi++) {
247
Vector<Vector2> ol;
248
Vector<Vector2> col;
249
250
ol.resize(lines[pi].size());
251
col.resize(lines[pi].size());
252
253
for (int i = 0; i < lines[pi].size(); i++) {
254
Vector2 vtx = lines[pi][i];
255
ol.write[i] = vtx + rect.position;
256
257
if (node->is_flipped_h()) {
258
vtx.x = rect.size.x - vtx.x;
259
}
260
if (node->is_flipped_v()) {
261
vtx.y = rect.size.y - vtx.y;
262
}
263
// Don't bake offset to Polygon2D which has offset property.
264
if (selected_menu_item != MENU_OPTION_CONVERT_TO_POLYGON_2D) {
265
vtx += node->get_offset();
266
}
267
if (node->is_centered()) {
268
vtx -= rect.size / 2.0;
269
}
270
271
col.write[i] = vtx;
272
}
273
274
outline_lines.write[pi] = ol;
275
computed_outline_lines.write[pi] = col;
276
}
277
}
278
279
debug_uv->queue_redraw();
280
}
281
282
void Sprite2DEditor::_create_node() {
283
switch (selected_menu_item) {
284
case MENU_OPTION_CONVERT_TO_MESH_2D: {
285
_convert_to_mesh_2d_node();
286
} break;
287
case MENU_OPTION_CONVERT_TO_POLYGON_2D: {
288
_convert_to_polygon_2d_node();
289
} break;
290
case MENU_OPTION_CREATE_COLLISION_POLY_2D: {
291
_create_collision_polygon_2d_node();
292
} break;
293
case MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D: {
294
_create_light_occluder_2d_node();
295
} break;
296
}
297
}
298
299
void Sprite2DEditor::_convert_to_mesh_2d_node() {
300
if (computed_vertices.size() < 3) {
301
err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh."));
302
err_dialog->popup_centered();
303
return;
304
}
305
306
Ref<ArrayMesh> mesh;
307
mesh.instantiate();
308
309
Array a;
310
a.resize(Mesh::ARRAY_MAX);
311
a[Mesh::ARRAY_VERTEX] = computed_vertices;
312
a[Mesh::ARRAY_TEX_UV] = computed_uv;
313
a[Mesh::ARRAY_INDEX] = computed_indices;
314
315
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
316
317
MeshInstance2D *mesh_instance = memnew(MeshInstance2D);
318
mesh_instance->set_mesh(mesh);
319
320
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
321
ur->create_action(TTR("Convert to MeshInstance2D"), UndoRedo::MERGE_DISABLE, node);
322
SceneTreeDock::get_singleton()->replace_node(node, mesh_instance);
323
ur->commit_action(false);
324
}
325
326
void Sprite2DEditor::_convert_to_polygon_2d_node() {
327
if (computed_outline_lines.is_empty()) {
328
err_dialog->set_text(TTR("Invalid geometry, can't create polygon."));
329
err_dialog->popup_centered();
330
return;
331
}
332
333
Polygon2D *polygon_2d_instance = memnew(Polygon2D);
334
335
int total_point_count = 0;
336
for (int i = 0; i < computed_outline_lines.size(); i++) {
337
total_point_count += computed_outline_lines[i].size();
338
}
339
340
PackedVector2Array polygon;
341
polygon.resize(total_point_count);
342
Vector2 *polygon_write = polygon.ptrw();
343
344
PackedVector2Array uvs;
345
uvs.resize(total_point_count);
346
Vector2 *uvs_write = uvs.ptrw();
347
348
int current_point_index = 0;
349
350
Array polys;
351
polys.resize(computed_outline_lines.size());
352
353
for (int i = 0; i < computed_outline_lines.size(); i++) {
354
Vector<Vector2> outline = computed_outline_lines[i];
355
Vector<Vector2> uv_outline = outline_lines[i];
356
357
PackedInt32Array pia;
358
pia.resize(outline.size());
359
int *pia_write = pia.ptrw();
360
361
for (int pi = 0; pi < outline.size(); pi++) {
362
polygon_write[current_point_index] = outline[pi];
363
uvs_write[current_point_index] = uv_outline[pi];
364
pia_write[pi] = current_point_index;
365
current_point_index++;
366
}
367
368
polys[i] = pia;
369
}
370
371
polygon_2d_instance->set_uv(uvs);
372
polygon_2d_instance->set_polygon(polygon);
373
polygon_2d_instance->set_polygons(polys);
374
375
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
376
ur->create_action(TTR("Convert to Polygon2D"), UndoRedo::MERGE_DISABLE, node);
377
SceneTreeDock::get_singleton()->replace_node(node, polygon_2d_instance);
378
ur->commit_action(false);
379
}
380
381
void Sprite2DEditor::_create_collision_polygon_2d_node() {
382
if (computed_outline_lines.is_empty()) {
383
err_dialog->set_text(TTR("Invalid geometry, can't create collision polygon."));
384
err_dialog->popup_centered();
385
return;
386
}
387
388
for (int i = 0; i < computed_outline_lines.size(); i++) {
389
Vector<Vector2> outline = computed_outline_lines[i];
390
391
CollisionPolygon2D *collision_polygon_2d_instance = memnew(CollisionPolygon2D);
392
collision_polygon_2d_instance->set_polygon(outline);
393
394
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
395
ur->create_action(TTR("Create CollisionPolygon2D Sibling"), UndoRedo::MERGE_DISABLE, node);
396
ur->add_do_method(this, "_add_as_sibling_or_child", node, collision_polygon_2d_instance);
397
ur->add_do_reference(collision_polygon_2d_instance);
398
ur->add_undo_method(node != get_tree()->get_edited_scene_root() ? node->get_parent() : get_tree()->get_edited_scene_root(), "remove_child", collision_polygon_2d_instance);
399
ur->commit_action();
400
}
401
}
402
403
void Sprite2DEditor::_create_light_occluder_2d_node() {
404
if (computed_outline_lines.is_empty()) {
405
err_dialog->set_text(TTR("Invalid geometry, can't create light occluder."));
406
err_dialog->popup_centered();
407
return;
408
}
409
410
for (int i = 0; i < computed_outline_lines.size(); i++) {
411
Vector<Vector2> outline = computed_outline_lines[i];
412
413
Ref<OccluderPolygon2D> polygon;
414
polygon.instantiate();
415
416
PackedVector2Array a;
417
a.resize(outline.size());
418
Vector2 *aw = a.ptrw();
419
for (int io = 0; io < outline.size(); io++) {
420
aw[io] = outline[io];
421
}
422
polygon->set_polygon(a);
423
424
LightOccluder2D *light_occluder_2d_instance = memnew(LightOccluder2D);
425
light_occluder_2d_instance->set_occluder_polygon(polygon);
426
427
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
428
ur->create_action(TTR("Create LightOccluder2D Sibling"), UndoRedo::MERGE_DISABLE, node);
429
ur->add_do_method(this, "_add_as_sibling_or_child", node, light_occluder_2d_instance);
430
ur->add_do_reference(light_occluder_2d_instance);
431
ur->add_undo_method(node != get_tree()->get_edited_scene_root() ? node->get_parent() : get_tree()->get_edited_scene_root(), "remove_child", light_occluder_2d_instance);
432
ur->commit_action();
433
}
434
}
435
436
void Sprite2DEditor::_add_as_sibling_or_child(Node *p_own_node, Node *p_new_node) {
437
// Can't make sibling if own node is scene root
438
if (p_own_node != get_tree()->get_edited_scene_root()) {
439
p_own_node->get_parent()->add_child(p_new_node, true);
440
Object::cast_to<Node2D>(p_new_node)->set_transform(Object::cast_to<Node2D>(p_own_node)->get_transform());
441
} else {
442
p_own_node->add_child(p_new_node, true);
443
}
444
445
p_new_node->set_owner(get_tree()->get_edited_scene_root());
446
}
447
448
void Sprite2DEditor::_sync_sprite_resize_mode() {
449
if (node != nullptr) {
450
node->_editor_set_dragging_to_resize_rect(resize_region_rect->is_pressed());
451
}
452
}
453
454
void Sprite2DEditor::_update_sprite_resize_mode_button() {
455
if (node == nullptr) {
456
return;
457
}
458
resize_region_rect->set_disabled(!node->is_region_enabled());
459
resize_region_rect->set_pressed(node->_editor_is_dragging_to_resiz_rect());
460
resize_region_rect->set_tooltip_text(node->is_region_enabled() ? "" : TTRC("Sprite's region needs to be enabled in the inspector."));
461
}
462
463
void Sprite2DEditor::_debug_uv_input(const Ref<InputEvent> &p_input) {
464
if (panner->gui_input(p_input, debug_uv->get_global_rect())) {
465
accept_event();
466
}
467
}
468
469
void Sprite2DEditor::_debug_uv_draw() {
470
debug_uv->draw_set_transform(-draw_offset * draw_zoom, 0, Vector2(draw_zoom, draw_zoom));
471
472
Ref<Texture2D> tex = node->get_texture();
473
ERR_FAIL_COND(tex.is_null());
474
475
debug_uv->draw_texture(tex, Point2());
476
477
Color color = Color(1.0, 0.8, 0.7);
478
479
if (selected_menu_item == MENU_OPTION_CONVERT_TO_MESH_2D && uv_lines.size() > 0) {
480
debug_uv->draw_multiline(uv_lines, color);
481
482
} else if ((selected_menu_item == MENU_OPTION_CONVERT_TO_POLYGON_2D || selected_menu_item == MENU_OPTION_CREATE_COLLISION_POLY_2D || selected_menu_item == MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D) && outline_lines.size() > 0) {
483
for (int i = 0; i < outline_lines.size(); i++) {
484
Vector<Vector2> outline = outline_lines[i];
485
486
debug_uv->draw_polyline(outline, color);
487
debug_uv->draw_line(outline[0], outline[outline.size() - 1], color);
488
}
489
}
490
}
491
492
void Sprite2DEditor::_center_view() {
493
Ref<Texture2D> tex = node->get_texture();
494
ERR_FAIL_COND(tex.is_null());
495
Vector2 zoom_factor = (debug_uv->get_size() - Vector2(1, 1) * 50 * EDSCALE) / tex->get_size();
496
zoom_widget->set_zoom(MIN(zoom_factor.x, zoom_factor.y));
497
// Recalculate scroll limits.
498
_update_zoom_and_pan(false);
499
500
Vector2 offset = (tex->get_size() - debug_uv->get_size() / zoom_widget->get_zoom()) / 2;
501
h_scroll->set_value_no_signal(offset.x);
502
v_scroll->set_value_no_signal(offset.y);
503
_update_zoom_and_pan(false);
504
}
505
506
void Sprite2DEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
507
h_scroll->set_value_no_signal(h_scroll->get_value() - p_scroll_vec.x / draw_zoom);
508
v_scroll->set_value_no_signal(v_scroll->get_value() - p_scroll_vec.y / draw_zoom);
509
_update_zoom_and_pan(false);
510
}
511
512
void Sprite2DEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
513
const real_t prev_zoom = draw_zoom;
514
zoom_widget->set_zoom(draw_zoom * p_zoom_factor);
515
draw_offset += p_origin / prev_zoom - p_origin / zoom_widget->get_zoom();
516
h_scroll->set_value_no_signal(draw_offset.x);
517
v_scroll->set_value_no_signal(draw_offset.y);
518
_update_zoom_and_pan(false);
519
}
520
521
void Sprite2DEditor::_update_zoom_and_pan(bool p_zoom_at_center) {
522
real_t previous_zoom = draw_zoom;
523
draw_zoom = zoom_widget->get_zoom();
524
draw_offset = Vector2(h_scroll->get_value(), v_scroll->get_value());
525
if (p_zoom_at_center) {
526
Vector2 center = debug_uv->get_size() / 2;
527
draw_offset += center / previous_zoom - center / draw_zoom;
528
}
529
530
Ref<Texture2D> tex = node->get_texture();
531
ERR_FAIL_COND(tex.is_null());
532
533
Point2 min_corner;
534
Point2 max_corner = tex->get_size();
535
Size2 page_size = debug_uv->get_size() / draw_zoom;
536
Vector2 margin = Vector2(50, 50) * EDSCALE / draw_zoom;
537
min_corner -= page_size - margin;
538
max_corner += page_size - margin;
539
540
h_scroll->set_block_signals(true);
541
h_scroll->set_min(min_corner.x);
542
h_scroll->set_max(max_corner.x);
543
h_scroll->set_page(page_size.x);
544
h_scroll->set_value(draw_offset.x);
545
h_scroll->set_block_signals(false);
546
547
v_scroll->set_block_signals(true);
548
v_scroll->set_min(min_corner.y);
549
v_scroll->set_max(max_corner.y);
550
v_scroll->set_page(page_size.y);
551
v_scroll->set_value(draw_offset.y);
552
v_scroll->set_block_signals(false);
553
554
debug_uv->queue_redraw();
555
}
556
557
void Sprite2DEditor::_notification(int p_what) {
558
switch (p_what) {
559
case NOTIFICATION_READY: {
560
v_scroll->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE);
561
h_scroll->set_anchors_and_offsets_preset(Control::PRESET_BOTTOM_WIDE);
562
// Avoid scrollbar overlapping.
563
Size2 hmin = h_scroll->get_combined_minimum_size();
564
Size2 vmin = v_scroll->get_combined_minimum_size();
565
h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -vmin.width);
566
v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -hmin.height);
567
[[fallthrough]];
568
}
569
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
570
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
571
break;
572
}
573
[[fallthrough]];
574
}
575
case NOTIFICATION_ENTER_TREE: {
576
panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
577
panner->setup_warped_panning(debug_uv_dialog, EDITOR_GET("editors/panning/warped_mouse_panning"));
578
} break;
579
case NOTIFICATION_THEME_CHANGED: {
580
options->set_button_icon(get_editor_theme_icon(SNAME("Sprite2D")));
581
582
options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_MESH_2D, get_editor_theme_icon(SNAME("MeshInstance2D")));
583
options->get_popup()->set_item_icon(MENU_OPTION_CONVERT_TO_POLYGON_2D, get_editor_theme_icon(SNAME("Polygon2D")));
584
options->get_popup()->set_item_icon(MENU_OPTION_CREATE_COLLISION_POLY_2D, get_editor_theme_icon(SNAME("CollisionPolygon2D")));
585
options->get_popup()->set_item_icon(MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D, get_editor_theme_icon(SNAME("LightOccluder2D")));
586
587
resize_region_rect->set_button_icon(get_editor_theme_icon(SNAME("KeepAspect")));
588
} break;
589
}
590
}
591
592
void Sprite2DEditor::_bind_methods() {
593
ClassDB::bind_method("_add_as_sibling_or_child", &Sprite2DEditor::_add_as_sibling_or_child);
594
}
595
596
void Sprite2DEditor::edit(Sprite2D *p_sprite) {
597
Callable callback_update_button = callable_mp(this, &Sprite2DEditor::_update_sprite_resize_mode_button);
598
StringName signal_name = SNAME("_editor_region_rect_enabled");
599
600
if (node != nullptr && node->is_connected(signal_name, callback_update_button)) {
601
node->disconnect(signal_name, callback_update_button);
602
}
603
604
node = p_sprite;
605
606
if (node != nullptr && !node->is_connected(signal_name, callback_update_button)) {
607
node->connect(signal_name, callback_update_button);
608
}
609
610
_update_sprite_resize_mode_button();
611
}
612
613
Sprite2DEditor::Sprite2DEditor() {
614
// Top HBoxContainer definition
615
top_hb = memnew(HBoxContainer);
616
617
CanvasItemEditor::get_singleton()->add_control_to_menu_panel(top_hb);
618
619
// Options definition
620
options = memnew(MenuButton);
621
622
top_hb->add_child(options);
623
624
options->set_text(TTR("Sprite2D"));
625
options->set_flat(false);
626
options->set_theme_type_variation("FlatMenuButton");
627
628
options->get_popup()->add_item(TTR("Convert to MeshInstance2D"), MENU_OPTION_CONVERT_TO_MESH_2D);
629
options->get_popup()->add_item(TTR("Convert to Polygon2D"), MENU_OPTION_CONVERT_TO_POLYGON_2D);
630
options->get_popup()->add_item(TTR("Create CollisionPolygon2D Sibling"), MENU_OPTION_CREATE_COLLISION_POLY_2D);
631
options->get_popup()->add_item(TTR("Create LightOccluder2D Sibling"), MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D);
632
options->set_switch_on_hover(true);
633
634
options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Sprite2DEditor::_menu_option));
635
636
// Resize region rect definition
637
resize_region_rect = memnew(Button);
638
639
resize_region_rect->set_theme_type_variation("FlatMenuButton");
640
resize_region_rect->set_toggle_mode(true);
641
resize_region_rect->set_shortcut(ED_SHORTCUT("canvas_item_editor/resize_region_rect", TTRC("Drag to Resize Region Rect"), KeyModifierMask::CMD_OR_CTRL | Key::R));
642
643
resize_region_rect->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_sync_sprite_resize_mode));
644
645
top_hb->add_child(resize_region_rect);
646
647
// Other elements definition
648
err_dialog = memnew(AcceptDialog);
649
add_child(err_dialog);
650
651
debug_uv_dialog = memnew(ConfirmationDialog);
652
debug_uv_dialog->set_size(Size2(960, 540) * EDSCALE);
653
VBoxContainer *vb = memnew(VBoxContainer);
654
debug_uv_dialog->add_child(vb);
655
debug_uv = memnew(Panel);
656
debug_uv->connect(SceneStringName(gui_input), callable_mp(this, &Sprite2DEditor::_debug_uv_input));
657
debug_uv->connect(SceneStringName(draw), callable_mp(this, &Sprite2DEditor::_debug_uv_draw));
658
debug_uv->set_clip_contents(true);
659
vb->add_margin_child(TTR("Preview:"), debug_uv, true);
660
661
panner.instantiate();
662
panner->set_callbacks(callable_mp(this, &Sprite2DEditor::_pan_callback), callable_mp(this, &Sprite2DEditor::_zoom_callback));
663
664
zoom_widget = memnew(EditorZoomWidget);
665
debug_uv->add_child(zoom_widget);
666
zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);
667
zoom_widget->connect("zoom_changed", callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(true));
668
zoom_widget->set_shortcut_context(nullptr);
669
670
v_scroll = memnew(VScrollBar);
671
debug_uv->add_child(v_scroll);
672
v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(false));
673
h_scroll = memnew(HScrollBar);
674
debug_uv->add_child(h_scroll);
675
h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(false));
676
677
debug_uv_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Sprite2DEditor::_create_node));
678
679
HBoxContainer *hb = memnew(HBoxContainer);
680
hb->add_child(memnew(Label(TTR("Simplification:"))));
681
simplification = memnew(SpinBox);
682
simplification->set_min(0.01);
683
simplification->set_max(10.00);
684
simplification->set_step(0.01);
685
simplification->set_value(2);
686
simplification->set_accessibility_name(TTRC("Simplification:"));
687
hb->add_child(simplification);
688
hb->add_spacer();
689
hb->add_child(memnew(Label(TTR("Shrink (Pixels):"))));
690
shrink_pixels = memnew(SpinBox);
691
shrink_pixels->set_min(0);
692
shrink_pixels->set_max(10);
693
shrink_pixels->set_step(1);
694
shrink_pixels->set_value(0);
695
shrink_pixels->set_accessibility_name(TTRC("Shrink (Pixels):"));
696
hb->add_child(shrink_pixels);
697
hb->add_spacer();
698
hb->add_child(memnew(Label(TTR("Grow (Pixels):"))));
699
grow_pixels = memnew(SpinBox);
700
grow_pixels->set_min(0);
701
grow_pixels->set_max(10);
702
grow_pixels->set_step(1);
703
grow_pixels->set_value(2);
704
grow_pixels->set_accessibility_name(TTRC("Grow (Pixels):"));
705
hb->add_child(grow_pixels);
706
hb->add_spacer();
707
update_preview = memnew(Button);
708
update_preview->set_text(TTR("Update Preview"));
709
update_preview->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_update_mesh_data));
710
hb->add_child(update_preview);
711
vb->add_margin_child(TTR("Settings:"), hb);
712
713
add_child(debug_uv_dialog);
714
}
715
716
void Sprite2DEditorPlugin::edit(Object *p_object) {
717
sprite_editor->edit(Object::cast_to<Sprite2D>(p_object));
718
}
719
720
bool Sprite2DEditorPlugin::handles(Object *p_object) const {
721
return p_object->is_class("Sprite2D");
722
}
723
724
void Sprite2DEditorPlugin::make_visible(bool p_visible) {
725
if (p_visible) {
726
sprite_editor->top_hb->show();
727
} else {
728
sprite_editor->top_hb->hide();
729
sprite_editor->edit(nullptr);
730
}
731
}
732
733
Sprite2DEditorPlugin::Sprite2DEditorPlugin() {
734
sprite_editor = memnew(Sprite2DEditor);
735
EditorNode::get_singleton()->get_gui_base()->add_child(sprite_editor);
736
737
make_visible(false);
738
//sprite_editor->options->hide();
739
}
740
741