sprite_2d_editor_plugin.cpp 26 KB

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