sprite_editor_plugin.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*************************************************************************/
  2. /* sprite_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "scene/2d/mesh_instance_2d.h"
  33. #include "scene/gui/box_container.h"
  34. #include "thirdparty/misc/clipper.hpp"
  35. void SpriteEditor::_node_removed(Node *p_node) {
  36. if (p_node == node) {
  37. node = NULL;
  38. options->hide();
  39. }
  40. }
  41. void SpriteEditor::edit(Sprite *p_sprite) {
  42. node = p_sprite;
  43. }
  44. #define PRECISION 10.0
  45. Vector<Vector2> expand(const Vector<Vector2> &points, const Rect2i &rect, float epsilon = 2.0) {
  46. int size = points.size();
  47. ERR_FAIL_COND_V(size < 2, Vector<Vector2>());
  48. ClipperLib::Path subj;
  49. ClipperLib::PolyTree solution;
  50. ClipperLib::PolyTree out;
  51. for (int i = 0; i < points.size(); i++) {
  52. subj << ClipperLib::IntPoint(points[i].x * PRECISION, points[i].y * PRECISION);
  53. }
  54. ClipperLib::ClipperOffset co;
  55. co.AddPath(subj, ClipperLib::jtMiter, ClipperLib::etClosedPolygon);
  56. co.Execute(solution, epsilon * PRECISION);
  57. ClipperLib::PolyNode *p = solution.GetFirst();
  58. ERR_FAIL_COND_V(!p, points);
  59. while (p->IsHole()) {
  60. p = p->GetNext();
  61. }
  62. //turn the result into simply polygon (AKA, fix overlap)
  63. //clamp into the specified rect
  64. ClipperLib::Clipper cl;
  65. cl.StrictlySimple(true);
  66. cl.AddPath(p->Contour, ClipperLib::ptSubject, true);
  67. //create the clipping rect
  68. ClipperLib::Path clamp;
  69. clamp.push_back(ClipperLib::IntPoint(0, 0));
  70. clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, 0));
  71. clamp.push_back(ClipperLib::IntPoint(rect.size.width * PRECISION, rect.size.height * PRECISION));
  72. clamp.push_back(ClipperLib::IntPoint(0, rect.size.height * PRECISION));
  73. cl.AddPath(clamp, ClipperLib::ptClip, true);
  74. cl.Execute(ClipperLib::ctIntersection, out);
  75. Vector<Vector2> outPoints;
  76. ClipperLib::PolyNode *p2 = out.GetFirst();
  77. while (p2->IsHole()) {
  78. p2 = p2->GetNext();
  79. }
  80. int lasti = p2->Contour.size() - 1;
  81. Vector2 prev = Vector2(p2->Contour[lasti].X / PRECISION, p2->Contour[lasti].Y / PRECISION);
  82. for (unsigned int i = 0; i < p2->Contour.size(); i++) {
  83. Vector2 cur = Vector2(p2->Contour[i].X / PRECISION, p2->Contour[i].Y / PRECISION);
  84. if (cur.distance_to(prev) > 0.5) {
  85. outPoints.push_back(cur);
  86. prev = cur;
  87. }
  88. }
  89. return outPoints;
  90. }
  91. void SpriteEditor::_menu_option(int p_option) {
  92. if (!node) {
  93. return;
  94. }
  95. switch (p_option) {
  96. case MENU_OPTION_CREATE_MESH_2D: {
  97. _update_mesh_data();
  98. debug_uv_dialog->popup_centered();
  99. debug_uv->update();
  100. } break;
  101. }
  102. }
  103. void SpriteEditor::_update_mesh_data() {
  104. Ref<Texture> texture = node->get_texture();
  105. if (texture.is_null()) {
  106. err_dialog->set_text(TTR("Sprite is empty!"));
  107. err_dialog->popup_centered_minsize();
  108. return;
  109. }
  110. if (node->get_hframes() > 1 || node->get_vframes() > 1) {
  111. err_dialog->set_text(TTR("Can't convert a sprite using animation frames to mesh."));
  112. err_dialog->popup_centered_minsize();
  113. return;
  114. }
  115. Ref<Image> image = texture->get_data();
  116. ERR_FAIL_COND(image.is_null());
  117. Rect2 rect;
  118. if (node->is_region())
  119. rect = node->get_region_rect();
  120. else
  121. rect.size = Size2(image->get_width(), image->get_height());
  122. Ref<BitMap> bm;
  123. bm.instance();
  124. bm->create_from_image_alpha(image);
  125. int grow = island_merging->get_value();
  126. if (grow > 0) {
  127. bm->grow_mask(grow, rect);
  128. }
  129. float epsilon = simplification->get_value();
  130. Vector<Vector<Vector2> > lines = bm->clip_opaque_to_polygons(rect, epsilon);
  131. uv_lines.clear();
  132. computed_vertices.clear();
  133. computed_uv.clear();
  134. computed_indices.clear();
  135. Size2 img_size = Vector2(image->get_width(), image->get_height());
  136. for (int j = 0; j < lines.size(); j++) {
  137. lines.write[j] = expand(lines[j], rect, epsilon);
  138. int index_ofs = computed_vertices.size();
  139. for (int i = 0; i < lines[j].size(); i++) {
  140. Vector2 vtx = lines[j][i];
  141. computed_uv.push_back(vtx / img_size);
  142. vtx -= rect.position; //offset by rect position
  143. //flip if flipped
  144. if (node->is_flipped_h())
  145. vtx.x = rect.size.x - vtx.x - 1.0;
  146. if (node->is_flipped_v())
  147. vtx.y = rect.size.y - vtx.y - 1.0;
  148. if (node->is_centered())
  149. vtx -= rect.size / 2.0;
  150. computed_vertices.push_back(vtx);
  151. }
  152. Vector<int> poly = Geometry::triangulate_polygon(lines[j]);
  153. for (int i = 0; i < poly.size(); i += 3) {
  154. for (int k = 0; k < 3; k++) {
  155. int idx = i + k;
  156. int idxn = i + (k + 1) % 3;
  157. uv_lines.push_back(lines[j][poly[idx]]);
  158. uv_lines.push_back(lines[j][poly[idxn]]);
  159. computed_indices.push_back(poly[idx] + index_ofs);
  160. }
  161. }
  162. }
  163. debug_uv->update();
  164. }
  165. void SpriteEditor::_create_mesh_node() {
  166. if (computed_vertices.size() < 3) {
  167. err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh."));
  168. err_dialog->popup_centered_minsize();
  169. return;
  170. }
  171. Ref<ArrayMesh> mesh;
  172. mesh.instance();
  173. Array a;
  174. a.resize(Mesh::ARRAY_MAX);
  175. a[Mesh::ARRAY_VERTEX] = computed_vertices;
  176. a[Mesh::ARRAY_TEX_UV] = computed_uv;
  177. a[Mesh::ARRAY_INDEX] = computed_indices;
  178. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  179. MeshInstance2D *mesh_instance = memnew(MeshInstance2D);
  180. mesh_instance->set_mesh(mesh);
  181. EditorNode::get_singleton()->get_scene_tree_dock()->replace_node(node, mesh_instance);
  182. }
  183. #if 0
  184. void SpriteEditor::_create_uv_lines() {
  185. Ref<Mesh> sprite = node->get_sprite();
  186. ERR_FAIL_COND(!sprite.is_valid());
  187. Set<SpriteEditorEdgeSort> edges;
  188. uv_lines.clear();
  189. for (int i = 0; i < sprite->get_surface_count(); i++) {
  190. if (sprite->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  191. continue;
  192. Array a = sprite->surface_get_arrays(i);
  193. PoolVector<Vector2> uv = a[p_layer == 0 ? Mesh::ARRAY_TEX_UV : Mesh::ARRAY_TEX_UV2];
  194. if (uv.size() == 0) {
  195. err_dialog->set_text(TTR("Model has no UV in this layer"));
  196. err_dialog->popup_centered_minsize();
  197. return;
  198. }
  199. PoolVector<Vector2>::Read r = uv.read();
  200. PoolVector<int> indices = a[Mesh::ARRAY_INDEX];
  201. PoolVector<int>::Read ri;
  202. int ic;
  203. bool use_indices;
  204. if (indices.size()) {
  205. ic = indices.size();
  206. ri = indices.read();
  207. use_indices = true;
  208. } else {
  209. ic = uv.size();
  210. use_indices = false;
  211. }
  212. for (int j = 0; j < ic; j += 3) {
  213. for (int k = 0; k < 3; k++) {
  214. SpriteEditorEdgeSort edge;
  215. if (use_indices) {
  216. edge.a = r[ri[j + k]];
  217. edge.b = r[ri[j + ((k + 1) % 3)]];
  218. } else {
  219. edge.a = r[j + k];
  220. edge.b = r[j + ((k + 1) % 3)];
  221. }
  222. if (edges.has(edge))
  223. continue;
  224. uv_lines.push_back(edge.a);
  225. uv_lines.push_back(edge.b);
  226. edges.insert(edge);
  227. }
  228. }
  229. }
  230. debug_uv_dialog->popup_centered_minsize();
  231. }
  232. #endif
  233. void SpriteEditor::_debug_uv_draw() {
  234. if (uv_lines.size() == 0)
  235. return;
  236. Ref<Texture> tex = node->get_texture();
  237. ERR_FAIL_COND(!tex.is_valid());
  238. debug_uv->set_clip_contents(true);
  239. debug_uv->draw_texture(tex, Point2());
  240. debug_uv->set_custom_minimum_size(tex->get_size());
  241. //debug_uv->draw_set_transform(Vector2(), 0, debug_uv->get_size());
  242. debug_uv->draw_multiline(uv_lines, Color(1.0, 0.8, 0.7));
  243. }
  244. void SpriteEditor::_bind_methods() {
  245. ClassDB::bind_method("_menu_option", &SpriteEditor::_menu_option);
  246. ClassDB::bind_method("_debug_uv_draw", &SpriteEditor::_debug_uv_draw);
  247. ClassDB::bind_method("_update_mesh_data", &SpriteEditor::_update_mesh_data);
  248. ClassDB::bind_method("_create_mesh_node", &SpriteEditor::_create_mesh_node);
  249. }
  250. SpriteEditor::SpriteEditor() {
  251. options = memnew(MenuButton);
  252. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
  253. options->set_text(TTR("Sprite"));
  254. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Sprite", "EditorIcons"));
  255. options->get_popup()->add_item(TTR("Convert to 2D Mesh"), MENU_OPTION_CREATE_MESH_2D);
  256. options->get_popup()->connect("id_pressed", this, "_menu_option");
  257. err_dialog = memnew(AcceptDialog);
  258. add_child(err_dialog);
  259. debug_uv_dialog = memnew(ConfirmationDialog);
  260. debug_uv_dialog->get_ok()->set_text(TTR("Create 2D Mesh"));
  261. debug_uv_dialog->set_title("Mesh 2D Preview");
  262. VBoxContainer *vb = memnew(VBoxContainer);
  263. debug_uv_dialog->add_child(vb);
  264. ScrollContainer *scroll = memnew(ScrollContainer);
  265. scroll->set_custom_minimum_size(Size2(800, 500) * EDSCALE);
  266. scroll->set_enable_h_scroll(true);
  267. scroll->set_enable_v_scroll(true);
  268. vb->add_margin_child(TTR("Preview:"), scroll, true);
  269. debug_uv = memnew(Control);
  270. debug_uv->connect("draw", this, "_debug_uv_draw");
  271. scroll->add_child(debug_uv);
  272. debug_uv_dialog->connect("confirmed", this, "_create_mesh_node");
  273. HBoxContainer *hb = memnew(HBoxContainer);
  274. hb->add_child(memnew(Label(TTR("Simplification: "))));
  275. simplification = memnew(SpinBox);
  276. simplification->set_min(0.01);
  277. simplification->set_max(10.00);
  278. simplification->set_step(0.01);
  279. simplification->set_value(2);
  280. hb->add_child(simplification);
  281. hb->add_spacer();
  282. hb->add_child(memnew(Label(TTR("Grow (Pixels): "))));
  283. island_merging = memnew(SpinBox);
  284. island_merging->set_min(0);
  285. island_merging->set_max(10);
  286. island_merging->set_step(1);
  287. island_merging->set_value(2);
  288. hb->add_child(island_merging);
  289. hb->add_spacer();
  290. update_preview = memnew(Button);
  291. update_preview->set_text(TTR("Update Preview"));
  292. update_preview->connect("pressed", this, "_update_mesh_data");
  293. hb->add_child(update_preview);
  294. vb->add_margin_child(TTR("Settings:"), hb);
  295. add_child(debug_uv_dialog);
  296. }
  297. void SpriteEditorPlugin::edit(Object *p_object) {
  298. sprite_editor->edit(Object::cast_to<Sprite>(p_object));
  299. }
  300. bool SpriteEditorPlugin::handles(Object *p_object) const {
  301. return p_object->is_class("Sprite");
  302. }
  303. void SpriteEditorPlugin::make_visible(bool p_visible) {
  304. if (p_visible) {
  305. sprite_editor->options->show();
  306. } else {
  307. sprite_editor->options->hide();
  308. sprite_editor->edit(NULL);
  309. }
  310. }
  311. SpriteEditorPlugin::SpriteEditorPlugin(EditorNode *p_node) {
  312. editor = p_node;
  313. sprite_editor = memnew(SpriteEditor);
  314. editor->get_viewport()->add_child(sprite_editor);
  315. make_visible(false);
  316. //sprite_editor->options->hide();
  317. }
  318. SpriteEditorPlugin::~SpriteEditorPlugin() {
  319. }