sprite_2d_editor_plugin.cpp 24 KB

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