abstract_polygon_2d_editor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*************************************************************************/
  2. /* abstract_polygon_2d_editor.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 "abstract_polygon_2d_editor.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/os/keyboard.h"
  33. AbstractPolygon2DEditor::Vertex::Vertex() :
  34. polygon(-1),
  35. vertex(-1) {
  36. // invalid vertex
  37. }
  38. AbstractPolygon2DEditor::Vertex::Vertex(int p_vertex) :
  39. polygon(-1),
  40. vertex(p_vertex) {
  41. // vertex p_vertex of current wip polygon
  42. }
  43. AbstractPolygon2DEditor::Vertex::Vertex(int p_polygon, int p_vertex) :
  44. polygon(p_polygon),
  45. vertex(p_vertex) {
  46. // vertex p_vertex of polygon p_polygon
  47. }
  48. bool AbstractPolygon2DEditor::Vertex::operator==(const AbstractPolygon2DEditor::Vertex &p_vertex) const {
  49. return polygon == p_vertex.polygon && vertex == p_vertex.vertex;
  50. }
  51. bool AbstractPolygon2DEditor::Vertex::operator!=(const AbstractPolygon2DEditor::Vertex &p_vertex) const {
  52. return !(*this == p_vertex);
  53. }
  54. bool AbstractPolygon2DEditor::Vertex::valid() const {
  55. return vertex >= 0;
  56. }
  57. AbstractPolygon2DEditor::PosVertex::PosVertex() {
  58. // invalid vertex
  59. }
  60. AbstractPolygon2DEditor::PosVertex::PosVertex(const Vertex &p_vertex, const Vector2 &p_pos) :
  61. Vertex(p_vertex.polygon, p_vertex.vertex),
  62. pos(p_pos) {
  63. }
  64. AbstractPolygon2DEditor::PosVertex::PosVertex(int p_polygon, int p_vertex, const Vector2 &p_pos) :
  65. Vertex(p_polygon, p_vertex),
  66. pos(p_pos) {
  67. }
  68. bool AbstractPolygon2DEditor::_is_empty() const {
  69. if (!_get_node())
  70. return true;
  71. const int n = _get_polygon_count();
  72. for (int i = 0; i < n; i++) {
  73. Vector<Vector2> vertices = _get_polygon(i);
  74. if (vertices.size() != 0)
  75. return false;
  76. }
  77. return true;
  78. }
  79. bool AbstractPolygon2DEditor::_is_line() const {
  80. return false;
  81. }
  82. bool AbstractPolygon2DEditor::_has_uv() const {
  83. return false;
  84. }
  85. int AbstractPolygon2DEditor::_get_polygon_count() const {
  86. return 1;
  87. }
  88. Variant AbstractPolygon2DEditor::_get_polygon(int p_idx) const {
  89. return _get_node()->get("polygon");
  90. }
  91. void AbstractPolygon2DEditor::_set_polygon(int p_idx, const Variant &p_polygon) const {
  92. _get_node()->set("polygon", p_polygon);
  93. }
  94. void AbstractPolygon2DEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) {
  95. Node2D *node = _get_node();
  96. undo_redo->add_do_method(node, "set_polygon", p_polygon);
  97. undo_redo->add_undo_method(node, "set_polygon", p_previous);
  98. }
  99. Vector2 AbstractPolygon2DEditor::_get_offset(int p_idx) const {
  100. return Vector2(0, 0);
  101. }
  102. void AbstractPolygon2DEditor::_commit_action() {
  103. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  104. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  105. undo_redo->commit_action();
  106. }
  107. void AbstractPolygon2DEditor::_action_add_polygon(const Variant &p_polygon) {
  108. _action_set_polygon(0, p_polygon);
  109. }
  110. void AbstractPolygon2DEditor::_action_remove_polygon(int p_idx) {
  111. _action_set_polygon(p_idx, _get_polygon(p_idx), PoolVector<Vector2>());
  112. }
  113. void AbstractPolygon2DEditor::_action_set_polygon(int p_idx, const Variant &p_polygon) {
  114. _action_set_polygon(p_idx, _get_polygon(p_idx), p_polygon);
  115. }
  116. bool AbstractPolygon2DEditor::_has_resource() const {
  117. return true;
  118. }
  119. void AbstractPolygon2DEditor::_create_resource() {
  120. }
  121. void AbstractPolygon2DEditor::_menu_option(int p_option) {
  122. switch (p_option) {
  123. case MODE_CREATE: {
  124. mode = MODE_CREATE;
  125. button_create->set_pressed(true);
  126. button_edit->set_pressed(false);
  127. button_delete->set_pressed(false);
  128. } break;
  129. case MODE_EDIT: {
  130. _wip_close();
  131. mode = MODE_EDIT;
  132. button_create->set_pressed(false);
  133. button_edit->set_pressed(true);
  134. button_delete->set_pressed(false);
  135. } break;
  136. case MODE_DELETE: {
  137. _wip_close();
  138. mode = MODE_DELETE;
  139. button_create->set_pressed(false);
  140. button_edit->set_pressed(false);
  141. button_delete->set_pressed(true);
  142. } break;
  143. }
  144. }
  145. void AbstractPolygon2DEditor::_notification(int p_what) {
  146. switch (p_what) {
  147. case NOTIFICATION_READY: {
  148. button_create->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCreate", "EditorIcons"));
  149. button_edit->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveEdit", "EditorIcons"));
  150. button_delete->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveDelete", "EditorIcons"));
  151. button_edit->set_pressed(true);
  152. get_tree()->connect("node_removed", this, "_node_removed");
  153. create_resource->connect("confirmed", this, "_create_resource");
  154. } break;
  155. }
  156. }
  157. void AbstractPolygon2DEditor::_node_removed(Node *p_node) {
  158. if (p_node == _get_node()) {
  159. edit(NULL);
  160. hide();
  161. canvas_item_editor->update_viewport();
  162. }
  163. }
  164. void AbstractPolygon2DEditor::_wip_changed() {
  165. if (wip_active && _is_line()) {
  166. _set_polygon(0, wip);
  167. }
  168. }
  169. void AbstractPolygon2DEditor::_wip_cancel() {
  170. wip.clear();
  171. wip_active = false;
  172. edited_point = PosVertex();
  173. hover_point = Vertex();
  174. selected_point = Vertex();
  175. canvas_item_editor->update_viewport();
  176. }
  177. void AbstractPolygon2DEditor::_wip_close() {
  178. if (!wip_active)
  179. return;
  180. if (_is_line()) {
  181. _set_polygon(0, wip);
  182. } else if (wip.size() >= (_is_line() ? 2 : 3)) {
  183. undo_redo->create_action(TTR("Create Polygon"));
  184. _action_add_polygon(wip);
  185. if (_has_uv()) {
  186. undo_redo->add_do_method(_get_node(), "set_uv", PoolVector<Vector2>());
  187. undo_redo->add_undo_method(_get_node(), "set_uv", _get_node()->get("uv"));
  188. }
  189. _commit_action();
  190. } else {
  191. return;
  192. }
  193. mode = MODE_EDIT;
  194. button_edit->set_pressed(true);
  195. button_create->set_pressed(false);
  196. button_delete->set_pressed(false);
  197. wip.clear();
  198. wip_active = false;
  199. edited_point = PosVertex();
  200. hover_point = Vertex();
  201. selected_point = Vertex();
  202. }
  203. bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
  204. if (!_get_node())
  205. return false;
  206. Ref<InputEventMouseButton> mb = p_event;
  207. if (!_has_resource()) {
  208. if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) {
  209. create_resource->set_text(String("No polygon resource on this node.\nCreate and assign one?"));
  210. create_resource->popup_centered_minsize();
  211. }
  212. return (mb.is_valid() && mb->get_button_index() == 1);
  213. }
  214. CanvasItemEditor::Tool tool = CanvasItemEditor::get_singleton()->get_current_tool();
  215. if (tool != CanvasItemEditor::TOOL_SELECT)
  216. return false;
  217. if (mb.is_valid()) {
  218. Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_global_transform();
  219. Vector2 gpoint = mb->get_position();
  220. Vector2 cpoint = _get_node()->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mb->get_position())));
  221. if (mode == MODE_EDIT || (_is_line() && mode == MODE_CREATE)) {
  222. if (mb->get_button_index() == BUTTON_LEFT) {
  223. if (mb->is_pressed()) {
  224. if (mb->get_control() || mb->get_shift() || mb->get_alt())
  225. return false;
  226. const PosVertex insert = closest_edge_point(gpoint);
  227. if (insert.valid()) {
  228. Vector<Vector2> vertices = _get_polygon(insert.polygon);
  229. if (vertices.size() < (_is_line() ? 2 : 3)) {
  230. vertices.push_back(cpoint);
  231. undo_redo->create_action(TTR("Edit Polygon"));
  232. selected_point = Vertex(insert.polygon, vertices.size());
  233. _action_set_polygon(insert.polygon, vertices);
  234. _commit_action();
  235. return true;
  236. } else {
  237. Vector<Vector2> vertices = _get_polygon(insert.polygon);
  238. pre_move_edit = vertices;
  239. printf("setting pre_move_edit\n");
  240. edited_point = PosVertex(insert.polygon, insert.vertex + 1, xform.affine_inverse().xform(insert.pos));
  241. vertices.insert(edited_point.vertex, edited_point.pos);
  242. selected_point = edited_point;
  243. edge_point = PosVertex();
  244. undo_redo->create_action(TTR("Insert Point"));
  245. _action_set_polygon(insert.polygon, vertices);
  246. _commit_action();
  247. return true;
  248. }
  249. } else {
  250. //look for points to move
  251. const PosVertex closest = closest_point(gpoint);
  252. if (closest.valid()) {
  253. printf("setting pre_move_edit\n");
  254. pre_move_edit = _get_polygon(closest.polygon);
  255. edited_point = PosVertex(closest, xform.affine_inverse().xform(closest.pos));
  256. selected_point = closest;
  257. edge_point = PosVertex();
  258. canvas_item_editor->update_viewport();
  259. return true;
  260. } else {
  261. selected_point = Vertex();
  262. }
  263. }
  264. } else {
  265. if (edited_point.valid()) {
  266. //apply
  267. Vector<Vector2> vertices = _get_polygon(edited_point.polygon);
  268. ERR_FAIL_INDEX_V(edited_point.vertex, vertices.size(), false);
  269. vertices.write[edited_point.vertex] = edited_point.pos - _get_offset(edited_point.polygon);
  270. undo_redo->create_action(TTR("Edit Polygon"));
  271. _action_set_polygon(edited_point.polygon, pre_move_edit, vertices);
  272. _commit_action();
  273. edited_point = PosVertex();
  274. return true;
  275. }
  276. }
  277. } else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed() && !edited_point.valid()) {
  278. const PosVertex closest = closest_point(gpoint);
  279. if (closest.valid()) {
  280. remove_point(closest);
  281. return true;
  282. }
  283. }
  284. } else if (mode == MODE_DELETE) {
  285. if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  286. const PosVertex closest = closest_point(gpoint);
  287. if (closest.valid()) {
  288. remove_point(closest);
  289. return true;
  290. }
  291. }
  292. }
  293. if (mode == MODE_CREATE) {
  294. if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  295. if (_is_line()) {
  296. // for lines, we don't have a wip mode, and we can undo each single add point.
  297. Vector<Vector2> vertices = _get_polygon(0);
  298. vertices.push_back(cpoint);
  299. undo_redo->create_action(TTR("Insert Point"));
  300. _action_set_polygon(0, vertices);
  301. _commit_action();
  302. return true;
  303. } else if (!wip_active) {
  304. wip.clear();
  305. wip.push_back(cpoint);
  306. wip_active = true;
  307. _wip_changed();
  308. edited_point = PosVertex(-1, 1, cpoint);
  309. canvas_item_editor->update_viewport();
  310. hover_point = Vertex();
  311. selected_point = Vertex(0);
  312. edge_point = PosVertex();
  313. return true;
  314. } else {
  315. const real_t grab_threshold = EDITOR_GET("editors/poly_editor/point_grab_radius");
  316. if (!_is_line() && wip.size() > 1 && xform.xform(wip[0]).distance_to(gpoint) < grab_threshold) {
  317. //wip closed
  318. _wip_close();
  319. return true;
  320. } else {
  321. //add wip point
  322. wip.push_back(cpoint);
  323. _wip_changed();
  324. edited_point = PosVertex(-1, wip.size(), cpoint);
  325. selected_point = Vertex(wip.size() - 1);
  326. canvas_item_editor->update_viewport();
  327. return true;
  328. }
  329. }
  330. } else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed() && wip_active) {
  331. _wip_cancel();
  332. }
  333. }
  334. }
  335. Ref<InputEventMouseMotion> mm = p_event;
  336. if (mm.is_valid()) {
  337. Vector2 gpoint = mm->get_position();
  338. if (edited_point.valid() && (wip_active || (mm->get_button_mask() & BUTTON_MASK_LEFT))) {
  339. Vector2 cpoint = _get_node()->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint)));
  340. edited_point = PosVertex(edited_point, cpoint);
  341. if (!wip_active) {
  342. Vector<Vector2> vertices = _get_polygon(edited_point.polygon);
  343. ERR_FAIL_INDEX_V(edited_point.vertex, vertices.size(), false);
  344. vertices.write[edited_point.vertex] = cpoint - _get_offset(edited_point.polygon);
  345. _set_polygon(edited_point.polygon, vertices);
  346. }
  347. canvas_item_editor->update_viewport();
  348. } else if (mode == MODE_EDIT || (_is_line() && mode == MODE_CREATE)) {
  349. const PosVertex onEdgeVertex = closest_edge_point(gpoint);
  350. if (onEdgeVertex.valid()) {
  351. hover_point = Vertex();
  352. edge_point = onEdgeVertex;
  353. canvas_item_editor->update_viewport();
  354. } else {
  355. if (edge_point.valid()) {
  356. edge_point = PosVertex();
  357. canvas_item_editor->update_viewport();
  358. }
  359. const PosVertex new_hover_point = closest_point(gpoint);
  360. if (hover_point != new_hover_point) {
  361. hover_point = new_hover_point;
  362. canvas_item_editor->update_viewport();
  363. }
  364. }
  365. }
  366. }
  367. Ref<InputEventKey> k = p_event;
  368. if (k.is_valid() && k->is_pressed()) {
  369. if (k->get_scancode() == KEY_DELETE || k->get_scancode() == KEY_BACKSPACE) {
  370. if (wip_active && selected_point.polygon == -1) {
  371. if (wip.size() > selected_point.vertex) {
  372. wip.remove(selected_point.vertex);
  373. _wip_changed();
  374. selected_point = wip.size() - 1;
  375. canvas_item_editor->update_viewport();
  376. return true;
  377. }
  378. } else {
  379. const Vertex active_point = get_active_point();
  380. if (active_point.valid()) {
  381. remove_point(active_point);
  382. return true;
  383. }
  384. }
  385. } else if (wip_active && k->get_scancode() == KEY_ENTER) {
  386. _wip_close();
  387. } else if (wip_active && k->get_scancode() == KEY_ESCAPE) {
  388. _wip_cancel();
  389. }
  390. }
  391. return false;
  392. }
  393. void AbstractPolygon2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  394. if (!_get_node())
  395. return;
  396. Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_global_transform();
  397. const Ref<Texture> handle = get_icon("EditorHandle", "EditorIcons");
  398. const Vertex active_point = get_active_point();
  399. const int n_polygons = _get_polygon_count();
  400. const bool is_closed = !_is_line();
  401. for (int j = -1; j < n_polygons; j++) {
  402. if (wip_active && wip_destructive && j != -1)
  403. continue;
  404. PoolVector<Vector2> points;
  405. Vector2 offset;
  406. if (wip_active && j == edited_point.polygon) {
  407. points = Variant(wip);
  408. offset = Vector2(0, 0);
  409. } else {
  410. if (j == -1)
  411. continue;
  412. points = _get_polygon(j);
  413. offset = _get_offset(j);
  414. }
  415. if (!wip_active && j == edited_point.polygon && EDITOR_GET("editors/poly_editor/show_previous_outline")) {
  416. const Color col = Color(0.5, 0.5, 0.5); // FIXME polygon->get_outline_color();
  417. const int n = pre_move_edit.size();
  418. for (int i = 0; i < n - (is_closed ? 0 : 1); i++) {
  419. Vector2 p, p2;
  420. p = pre_move_edit[i] + offset;
  421. p2 = pre_move_edit[(i + 1) % n] + offset;
  422. Vector2 point = xform.xform(p);
  423. Vector2 next_point = xform.xform(p2);
  424. p_overlay->draw_line(point, next_point, col, 2 * EDSCALE);
  425. }
  426. }
  427. const int n_points = points.size();
  428. const Color col = Color(1, 0.3, 0.1, 0.8);
  429. for (int i = 0; i < n_points; i++) {
  430. const Vertex vertex(j, i);
  431. const Vector2 p = (vertex == edited_point) ? edited_point.pos : (points[i] + offset);
  432. const Vector2 point = xform.xform(p);
  433. if (is_closed || i < n_points - 1) {
  434. Vector2 p2;
  435. if (j == edited_point.polygon &&
  436. ((wip_active && i == n_points - 1) || (((i + 1) % n_points) == edited_point.vertex)))
  437. p2 = edited_point.pos;
  438. else
  439. p2 = points[(i + 1) % n_points] + offset;
  440. const Vector2 next_point = xform.xform(p2);
  441. p_overlay->draw_line(point, next_point, col, 2 * EDSCALE);
  442. }
  443. }
  444. for (int i = 0; i < n_points; i++) {
  445. const Vertex vertex(j, i);
  446. const Vector2 p = (vertex == edited_point) ? edited_point.pos : (points[i] + offset);
  447. const Vector2 point = xform.xform(p);
  448. const Color modulate = vertex == active_point ? Color(0.5, 1, 2) : Color(1, 1, 1);
  449. p_overlay->draw_texture(handle, point - handle->get_size() * 0.5, modulate);
  450. }
  451. }
  452. if (edge_point.valid()) {
  453. Ref<Texture> add_handle = get_icon("EditorHandleAdd", "EditorIcons");
  454. p_overlay->draw_texture(add_handle, edge_point.pos - add_handle->get_size() * 0.5);
  455. }
  456. }
  457. void AbstractPolygon2DEditor::edit(Node *p_polygon) {
  458. if (!canvas_item_editor) {
  459. canvas_item_editor = CanvasItemEditor::get_singleton();
  460. }
  461. if (p_polygon) {
  462. _set_node(p_polygon);
  463. // Enable the pencil tool if the polygon is empty.
  464. if (_is_empty())
  465. _menu_option(MODE_CREATE);
  466. else
  467. _menu_option(MODE_EDIT);
  468. wip.clear();
  469. wip_active = false;
  470. edited_point = PosVertex();
  471. hover_point = Vertex();
  472. selected_point = Vertex();
  473. canvas_item_editor->update_viewport();
  474. } else {
  475. _set_node(NULL);
  476. }
  477. }
  478. void AbstractPolygon2DEditor::_bind_methods() {
  479. ClassDB::bind_method(D_METHOD("_node_removed"), &AbstractPolygon2DEditor::_node_removed);
  480. ClassDB::bind_method(D_METHOD("_menu_option"), &AbstractPolygon2DEditor::_menu_option);
  481. ClassDB::bind_method(D_METHOD("_create_resource"), &AbstractPolygon2DEditor::_create_resource);
  482. }
  483. void AbstractPolygon2DEditor::remove_point(const Vertex &p_vertex) {
  484. PoolVector<Vector2> vertices = _get_polygon(p_vertex.polygon);
  485. if (vertices.size() > (_is_line() ? 2 : 3)) {
  486. vertices.remove(p_vertex.vertex);
  487. undo_redo->create_action(TTR("Edit Polygon (Remove Point)"));
  488. _action_set_polygon(p_vertex.polygon, vertices);
  489. _commit_action();
  490. } else {
  491. undo_redo->create_action(TTR("Remove Polygon And Point"));
  492. _action_remove_polygon(p_vertex.polygon);
  493. _commit_action();
  494. }
  495. if (_is_empty())
  496. _menu_option(MODE_CREATE);
  497. hover_point = Vertex();
  498. if (selected_point == p_vertex)
  499. selected_point = Vertex();
  500. }
  501. AbstractPolygon2DEditor::Vertex AbstractPolygon2DEditor::get_active_point() const {
  502. return hover_point.valid() ? hover_point : selected_point;
  503. }
  504. AbstractPolygon2DEditor::PosVertex AbstractPolygon2DEditor::closest_point(const Vector2 &p_pos) const {
  505. const real_t grab_threshold = EDITOR_GET("editors/poly_editor/point_grab_radius");
  506. const int n_polygons = _get_polygon_count();
  507. const Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_global_transform();
  508. PosVertex closest;
  509. real_t closest_dist = 1e10;
  510. for (int j = 0; j < n_polygons; j++) {
  511. PoolVector<Vector2> points = _get_polygon(j);
  512. const Vector2 offset = _get_offset(j);
  513. const int n_points = points.size();
  514. for (int i = 0; i < n_points; i++) {
  515. Vector2 cp = xform.xform(points[i] + offset);
  516. real_t d = cp.distance_to(p_pos);
  517. if (d < closest_dist && d < grab_threshold) {
  518. closest_dist = d;
  519. closest = PosVertex(j, i, cp);
  520. }
  521. }
  522. }
  523. return closest;
  524. }
  525. AbstractPolygon2DEditor::PosVertex AbstractPolygon2DEditor::closest_edge_point(const Vector2 &p_pos) const {
  526. const real_t grab_threshold = EDITOR_GET("editors/poly_editor/point_grab_radius");
  527. const real_t eps = grab_threshold * 2;
  528. const real_t eps2 = eps * eps;
  529. const int n_polygons = _get_polygon_count();
  530. const Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_global_transform();
  531. PosVertex closest;
  532. real_t closest_dist = 1e10;
  533. for (int j = 0; j < n_polygons; j++) {
  534. PoolVector<Vector2> points = _get_polygon(j);
  535. const Vector2 offset = _get_offset(j);
  536. const int n_points = points.size();
  537. const int n_segments = n_points - (_is_line() ? 1 : 0);
  538. for (int i = 0; i < n_segments; i++) {
  539. Vector2 segment[2] = { xform.xform(points[i] + offset),
  540. xform.xform(points[(i + 1) % n_points] + offset) };
  541. Vector2 cp = Geometry::get_closest_point_to_segment_2d(p_pos, segment);
  542. if (cp.distance_squared_to(segment[0]) < eps2 || cp.distance_squared_to(segment[1]) < eps2)
  543. continue; //not valid to reuse point
  544. real_t d = cp.distance_to(p_pos);
  545. if (d < closest_dist && d < grab_threshold) {
  546. closest_dist = d;
  547. closest = PosVertex(j, i, cp);
  548. }
  549. }
  550. }
  551. return closest;
  552. }
  553. AbstractPolygon2DEditor::AbstractPolygon2DEditor(EditorNode *p_editor, bool p_wip_destructive) {
  554. canvas_item_editor = NULL;
  555. editor = p_editor;
  556. undo_redo = editor->get_undo_redo();
  557. wip_active = false;
  558. edited_point = PosVertex();
  559. wip_destructive = p_wip_destructive;
  560. hover_point = Vertex();
  561. selected_point = Vertex();
  562. edge_point = PosVertex();
  563. add_child(memnew(VSeparator));
  564. button_create = memnew(ToolButton);
  565. add_child(button_create);
  566. button_create->connect("pressed", this, "_menu_option", varray(MODE_CREATE));
  567. button_create->set_toggle_mode(true);
  568. button_create->set_tooltip(TTR("Create points."));
  569. button_edit = memnew(ToolButton);
  570. add_child(button_edit);
  571. button_edit->connect("pressed", this, "_menu_option", varray(MODE_EDIT));
  572. button_edit->set_toggle_mode(true);
  573. button_edit->set_tooltip(TTR("Edit points.\nLMB: Move Point\nRMB: Erase Point"));
  574. button_delete = memnew(ToolButton);
  575. add_child(button_delete);
  576. button_delete->connect("pressed", this, "_menu_option", varray(MODE_DELETE));
  577. button_delete->set_toggle_mode(true);
  578. button_delete->set_tooltip(TTR("Erase points."));
  579. create_resource = memnew(ConfirmationDialog);
  580. add_child(create_resource);
  581. create_resource->get_ok()->set_text(TTR("Create"));
  582. mode = MODE_EDIT;
  583. }
  584. void AbstractPolygon2DEditorPlugin::edit(Object *p_object) {
  585. polygon_editor->edit(Object::cast_to<Node>(p_object));
  586. }
  587. bool AbstractPolygon2DEditorPlugin::handles(Object *p_object) const {
  588. return p_object->is_class(klass);
  589. }
  590. void AbstractPolygon2DEditorPlugin::make_visible(bool p_visible) {
  591. if (p_visible) {
  592. polygon_editor->show();
  593. } else {
  594. polygon_editor->hide();
  595. polygon_editor->edit(NULL);
  596. }
  597. }
  598. AbstractPolygon2DEditorPlugin::AbstractPolygon2DEditorPlugin(EditorNode *p_node, AbstractPolygon2DEditor *p_polygon_editor, String p_class) :
  599. polygon_editor(p_polygon_editor),
  600. editor(p_node),
  601. klass(p_class) {
  602. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(polygon_editor);
  603. polygon_editor->hide();
  604. }
  605. AbstractPolygon2DEditorPlugin::~AbstractPolygon2DEditorPlugin() {
  606. }