2
0

sprite_editor_plugin.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. ERR_FAIL_COND_V(!p2, points);
  78. while (p2->IsHole()) {
  79. p2 = p2->GetNext();
  80. }
  81. int lasti = p2->Contour.size() - 1;
  82. Vector2 prev = Vector2(p2->Contour[lasti].X / PRECISION, p2->Contour[lasti].Y / PRECISION);
  83. for (unsigned int i = 0; i < p2->Contour.size(); i++) {
  84. Vector2 cur = Vector2(p2->Contour[i].X / PRECISION, p2->Contour[i].Y / PRECISION);
  85. if (cur.distance_to(prev) > 0.5) {
  86. outPoints.push_back(cur);
  87. prev = cur;
  88. }
  89. }
  90. return outPoints;
  91. }
  92. void SpriteEditor::_menu_option(int p_option) {
  93. if (!node) {
  94. return;
  95. }
  96. switch (p_option) {
  97. case MENU_OPTION_CREATE_MESH_2D: {
  98. _update_mesh_data();
  99. debug_uv_dialog->popup_centered();
  100. debug_uv->update();
  101. } break;
  102. }
  103. }
  104. void SpriteEditor::_update_mesh_data() {
  105. Ref<Texture> texture = node->get_texture();
  106. if (texture.is_null()) {
  107. err_dialog->set_text(TTR("Sprite is empty!"));
  108. err_dialog->popup_centered_minsize();
  109. return;
  110. }
  111. if (node->get_hframes() > 1 || node->get_vframes() > 1) {
  112. err_dialog->set_text(TTR("Can't convert a sprite using animation frames to mesh."));
  113. err_dialog->popup_centered_minsize();
  114. return;
  115. }
  116. Ref<Image> image = texture->get_data();
  117. ERR_FAIL_COND(image.is_null());
  118. Rect2 rect;
  119. if (node->is_region())
  120. rect = node->get_region_rect();
  121. else
  122. rect.size = Size2(image->get_width(), image->get_height());
  123. Ref<BitMap> bm;
  124. bm.instance();
  125. bm->create_from_image_alpha(image);
  126. int grow = island_merging->get_value();
  127. if (grow > 0) {
  128. bm->grow_mask(grow, rect);
  129. }
  130. float epsilon = simplification->get_value();
  131. Vector<Vector<Vector2> > lines = bm->clip_opaque_to_polygons(rect, epsilon);
  132. uv_lines.clear();
  133. computed_vertices.clear();
  134. computed_uv.clear();
  135. computed_indices.clear();
  136. Size2 img_size = Vector2(image->get_width(), image->get_height());
  137. for (int j = 0; j < lines.size(); j++) {
  138. lines.write[j] = expand(lines[j], rect, epsilon);
  139. int index_ofs = computed_vertices.size();
  140. for (int i = 0; i < lines[j].size(); i++) {
  141. Vector2 vtx = lines[j][i];
  142. computed_uv.push_back(vtx / img_size);
  143. vtx -= rect.position; //offset by rect position
  144. //flip if flipped
  145. if (node->is_flipped_h())
  146. vtx.x = rect.size.x - vtx.x - 1.0;
  147. if (node->is_flipped_v())
  148. vtx.y = rect.size.y - vtx.y - 1.0;
  149. if (node->is_centered())
  150. vtx -= rect.size / 2.0;
  151. computed_vertices.push_back(vtx);
  152. }
  153. Vector<int> poly = Geometry::triangulate_polygon(lines[j]);
  154. for (int i = 0; i < poly.size(); i += 3) {
  155. for (int k = 0; k < 3; k++) {
  156. int idx = i + k;
  157. int idxn = i + (k + 1) % 3;
  158. uv_lines.push_back(lines[j][poly[idx]]);
  159. uv_lines.push_back(lines[j][poly[idxn]]);
  160. computed_indices.push_back(poly[idx] + index_ofs);
  161. }
  162. }
  163. }
  164. debug_uv->update();
  165. }
  166. void SpriteEditor::_create_mesh_node() {
  167. if (computed_vertices.size() < 3) {
  168. err_dialog->set_text(TTR("Invalid geometry, can't replace by mesh."));
  169. err_dialog->popup_centered_minsize();
  170. return;
  171. }
  172. Ref<ArrayMesh> mesh;
  173. mesh.instance();
  174. Array a;
  175. a.resize(Mesh::ARRAY_MAX);
  176. a[Mesh::ARRAY_VERTEX] = computed_vertices;
  177. a[Mesh::ARRAY_TEX_UV] = computed_uv;
  178. a[Mesh::ARRAY_INDEX] = computed_indices;
  179. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  180. MeshInstance2D *mesh_instance = memnew(MeshInstance2D);
  181. mesh_instance->set_mesh(mesh);
  182. EditorNode::get_singleton()->get_scene_tree_dock()->replace_node(node, mesh_instance);
  183. }
  184. #if 0
  185. void SpriteEditor::_create_uv_lines() {
  186. Ref<Mesh> sprite = node->get_sprite();
  187. ERR_FAIL_COND(!sprite.is_valid());
  188. Set<SpriteEditorEdgeSort> edges;
  189. uv_lines.clear();
  190. for (int i = 0; i < sprite->get_surface_count(); i++) {
  191. if (sprite->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  192. continue;
  193. Array a = sprite->surface_get_arrays(i);
  194. PoolVector<Vector2> uv = a[p_layer == 0 ? Mesh::ARRAY_TEX_UV : Mesh::ARRAY_TEX_UV2];
  195. if (uv.size() == 0) {
  196. err_dialog->set_text(TTR("Model has no UV in this layer"));
  197. err_dialog->popup_centered_minsize();
  198. return;
  199. }
  200. PoolVector<Vector2>::Read r = uv.read();
  201. PoolVector<int> indices = a[Mesh::ARRAY_INDEX];
  202. PoolVector<int>::Read ri;
  203. int ic;
  204. bool use_indices;
  205. if (indices.size()) {
  206. ic = indices.size();
  207. ri = indices.read();
  208. use_indices = true;
  209. } else {
  210. ic = uv.size();
  211. use_indices = false;
  212. }
  213. for (int j = 0; j < ic; j += 3) {
  214. for (int k = 0; k < 3; k++) {
  215. SpriteEditorEdgeSort edge;
  216. if (use_indices) {
  217. edge.a = r[ri[j + k]];
  218. edge.b = r[ri[j + ((k + 1) % 3)]];
  219. } else {
  220. edge.a = r[j + k];
  221. edge.b = r[j + ((k + 1) % 3)];
  222. }
  223. if (edges.has(edge))
  224. continue;
  225. uv_lines.push_back(edge.a);
  226. uv_lines.push_back(edge.b);
  227. edges.insert(edge);
  228. }
  229. }
  230. }
  231. debug_uv_dialog->popup_centered_minsize();
  232. }
  233. #endif
  234. void SpriteEditor::_debug_uv_draw() {
  235. if (uv_lines.size() == 0)
  236. return;
  237. Ref<Texture> tex = node->get_texture();
  238. ERR_FAIL_COND(!tex.is_valid());
  239. debug_uv->set_clip_contents(true);
  240. debug_uv->draw_texture(tex, Point2());
  241. debug_uv->set_custom_minimum_size(tex->get_size());
  242. //debug_uv->draw_set_transform(Vector2(), 0, debug_uv->get_size());
  243. debug_uv->draw_multiline(uv_lines, Color(1.0, 0.8, 0.7));
  244. }
  245. void SpriteEditor::_bind_methods() {
  246. ClassDB::bind_method("_menu_option", &SpriteEditor::_menu_option);
  247. ClassDB::bind_method("_debug_uv_draw", &SpriteEditor::_debug_uv_draw);
  248. ClassDB::bind_method("_update_mesh_data", &SpriteEditor::_update_mesh_data);
  249. ClassDB::bind_method("_create_mesh_node", &SpriteEditor::_create_mesh_node);
  250. }
  251. SpriteEditor::SpriteEditor() {
  252. options = memnew(MenuButton);
  253. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
  254. options->set_text(TTR("Sprite"));
  255. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Sprite", "EditorIcons"));
  256. options->get_popup()->add_item(TTR("Convert to 2D Mesh"), MENU_OPTION_CREATE_MESH_2D);
  257. options->get_popup()->connect("id_pressed", this, "_menu_option");
  258. err_dialog = memnew(AcceptDialog);
  259. add_child(err_dialog);
  260. debug_uv_dialog = memnew(ConfirmationDialog);
  261. debug_uv_dialog->get_ok()->set_text(TTR("Create 2D Mesh"));
  262. debug_uv_dialog->set_title("Mesh 2D Preview");
  263. VBoxContainer *vb = memnew(VBoxContainer);
  264. debug_uv_dialog->add_child(vb);
  265. ScrollContainer *scroll = memnew(ScrollContainer);
  266. scroll->set_custom_minimum_size(Size2(800, 500) * EDSCALE);
  267. scroll->set_enable_h_scroll(true);
  268. scroll->set_enable_v_scroll(true);
  269. vb->add_margin_child(TTR("Preview:"), scroll, true);
  270. debug_uv = memnew(Control);
  271. debug_uv->connect("draw", this, "_debug_uv_draw");
  272. scroll->add_child(debug_uv);
  273. debug_uv_dialog->connect("confirmed", this, "_create_mesh_node");
  274. HBoxContainer *hb = memnew(HBoxContainer);
  275. hb->add_child(memnew(Label(TTR("Simplification: "))));
  276. simplification = memnew(SpinBox);
  277. simplification->set_min(0.01);
  278. simplification->set_max(10.00);
  279. simplification->set_step(0.01);
  280. simplification->set_value(2);
  281. hb->add_child(simplification);
  282. hb->add_spacer();
  283. hb->add_child(memnew(Label(TTR("Grow (Pixels): "))));
  284. island_merging = memnew(SpinBox);
  285. island_merging->set_min(0);
  286. island_merging->set_max(10);
  287. island_merging->set_step(1);
  288. island_merging->set_value(2);
  289. hb->add_child(island_merging);
  290. hb->add_spacer();
  291. update_preview = memnew(Button);
  292. update_preview->set_text(TTR("Update Preview"));
  293. update_preview->connect("pressed", this, "_update_mesh_data");
  294. hb->add_child(update_preview);
  295. vb->add_margin_child(TTR("Settings:"), hb);
  296. add_child(debug_uv_dialog);
  297. }
  298. void SpriteEditorPlugin::edit(Object *p_object) {
  299. sprite_editor->edit(Object::cast_to<Sprite>(p_object));
  300. }
  301. bool SpriteEditorPlugin::handles(Object *p_object) const {
  302. return p_object->is_class("Sprite");
  303. }
  304. void SpriteEditorPlugin::make_visible(bool p_visible) {
  305. if (p_visible) {
  306. sprite_editor->options->show();
  307. } else {
  308. sprite_editor->options->hide();
  309. sprite_editor->edit(NULL);
  310. }
  311. }
  312. SpriteEditorPlugin::SpriteEditorPlugin(EditorNode *p_node) {
  313. editor = p_node;
  314. sprite_editor = memnew(SpriteEditor);
  315. editor->get_viewport()->add_child(sprite_editor);
  316. make_visible(false);
  317. //sprite_editor->options->hide();
  318. }
  319. SpriteEditorPlugin::~SpriteEditorPlugin() {
  320. }