collision_polygon_2d_editor_plugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*************************************************************************/
  2. /* collision_polygon_2d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "collision_polygon_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "editor/editor_settings.h"
  33. #include "os/file_access.h"
  34. void CollisionPolygon2DEditor::_notification(int p_what) {
  35. switch (p_what) {
  36. case NOTIFICATION_READY: {
  37. button_create->set_icon(get_icon("Edit", "EditorIcons"));
  38. button_edit->set_icon(get_icon("MovePoint", "EditorIcons"));
  39. button_edit->set_pressed(true);
  40. get_tree()->connect("node_removed", this, "_node_removed");
  41. } break;
  42. case NOTIFICATION_FIXED_PROCESS: {
  43. } break;
  44. }
  45. }
  46. void CollisionPolygon2DEditor::_node_removed(Node *p_node) {
  47. if (p_node == node) {
  48. node = NULL;
  49. hide();
  50. canvas_item_editor->get_viewport_control()->update();
  51. }
  52. }
  53. void CollisionPolygon2DEditor::_menu_option(int p_option) {
  54. switch (p_option) {
  55. case MODE_CREATE: {
  56. mode = MODE_CREATE;
  57. button_create->set_pressed(true);
  58. button_edit->set_pressed(false);
  59. } break;
  60. case MODE_EDIT: {
  61. mode = MODE_EDIT;
  62. button_create->set_pressed(false);
  63. button_edit->set_pressed(true);
  64. } break;
  65. }
  66. }
  67. void CollisionPolygon2DEditor::_wip_close() {
  68. undo_redo->create_action(TTR("Create Poly"));
  69. undo_redo->add_undo_method(node, "set_polygon", node->get_polygon());
  70. undo_redo->add_do_method(node, "set_polygon", wip);
  71. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  72. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  73. undo_redo->commit_action();
  74. wip.clear();
  75. wip_active = false;
  76. mode = MODE_EDIT;
  77. button_edit->set_pressed(true);
  78. button_create->set_pressed(false);
  79. edited_point = -1;
  80. }
  81. bool CollisionPolygon2DEditor::forward_input_event(const InputEvent &p_event) {
  82. if (!node)
  83. return false;
  84. switch (p_event.type) {
  85. case InputEvent::MOUSE_BUTTON: {
  86. const InputEventMouseButton &mb = p_event.mouse_button;
  87. Matrix32 xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  88. Vector2 gpoint = Point2(mb.x, mb.y);
  89. Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint);
  90. cpoint = canvas_item_editor->snap_point(cpoint);
  91. cpoint = node->get_global_transform().affine_inverse().xform(cpoint);
  92. Vector<Vector2> poly = node->get_polygon();
  93. //first check if a point is to be added (segment split)
  94. real_t grab_treshold = EDITOR_DEF("poly_editor/point_grab_radius", 8);
  95. switch (mode) {
  96. case MODE_CREATE: {
  97. if (mb.button_index == BUTTON_LEFT && mb.pressed) {
  98. if (!wip_active) {
  99. wip.clear();
  100. wip.push_back(cpoint);
  101. wip_active = true;
  102. edited_point_pos = cpoint;
  103. canvas_item_editor->get_viewport_control()->update();
  104. edited_point = 1;
  105. return true;
  106. } else {
  107. if (wip.size() > 1 && xform.xform(wip[0]).distance_to(gpoint) < grab_treshold) {
  108. //wip closed
  109. _wip_close();
  110. return true;
  111. } else {
  112. wip.push_back(cpoint);
  113. edited_point = wip.size();
  114. canvas_item_editor->get_viewport_control()->update();
  115. return true;
  116. //add wip point
  117. }
  118. }
  119. } else if (mb.button_index == BUTTON_RIGHT && mb.pressed && wip_active) {
  120. _wip_close();
  121. }
  122. } break;
  123. case MODE_EDIT: {
  124. if (mb.button_index == BUTTON_LEFT) {
  125. if (mb.pressed) {
  126. if (mb.mod.control) {
  127. if (poly.size() < 3) {
  128. undo_redo->create_action(TTR("Edit Poly"));
  129. undo_redo->add_undo_method(node, "set_polygon", poly);
  130. poly.push_back(cpoint);
  131. undo_redo->add_do_method(node, "set_polygon", poly);
  132. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  133. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  134. undo_redo->commit_action();
  135. return true;
  136. }
  137. //search edges
  138. int closest_idx = -1;
  139. Vector2 closest_pos;
  140. real_t closest_dist = 1e10;
  141. for (int i = 0; i < poly.size(); i++) {
  142. Vector2 points[2] = { xform.xform(poly[i]),
  143. xform.xform(poly[(i + 1) % poly.size()]) };
  144. Vector2 cp = Geometry::get_closest_point_to_segment_2d(gpoint, points);
  145. if (cp.distance_squared_to(points[0]) < CMP_EPSILON2 || cp.distance_squared_to(points[1]) < CMP_EPSILON2)
  146. continue; //not valid to reuse point
  147. real_t d = cp.distance_to(gpoint);
  148. if (d < closest_dist && d < grab_treshold) {
  149. closest_dist = d;
  150. closest_pos = cp;
  151. closest_idx = i;
  152. }
  153. }
  154. if (closest_idx >= 0) {
  155. pre_move_edit = poly;
  156. poly.insert(closest_idx + 1, xform.affine_inverse().xform(closest_pos));
  157. edited_point = closest_idx + 1;
  158. edited_point_pos = xform.affine_inverse().xform(closest_pos);
  159. node->set_polygon(poly);
  160. canvas_item_editor->get_viewport_control()->update();
  161. return true;
  162. }
  163. } else {
  164. //look for points to move
  165. int closest_idx = -1;
  166. Vector2 closest_pos;
  167. real_t closest_dist = 1e10;
  168. for (int i = 0; i < poly.size(); i++) {
  169. Vector2 cp = xform.xform(poly[i]);
  170. real_t d = cp.distance_to(gpoint);
  171. if (d < closest_dist && d < grab_treshold) {
  172. closest_dist = d;
  173. closest_pos = cp;
  174. closest_idx = i;
  175. }
  176. }
  177. if (closest_idx >= 0) {
  178. pre_move_edit = poly;
  179. edited_point = closest_idx;
  180. edited_point_pos = xform.affine_inverse().xform(closest_pos);
  181. canvas_item_editor->get_viewport_control()->update();
  182. return true;
  183. }
  184. }
  185. } else {
  186. if (edited_point != -1) {
  187. //apply
  188. ERR_FAIL_INDEX_V(edited_point, poly.size(), false);
  189. poly[edited_point] = edited_point_pos;
  190. undo_redo->create_action(TTR("Edit Poly"));
  191. undo_redo->add_do_method(node, "set_polygon", poly);
  192. undo_redo->add_undo_method(node, "set_polygon", pre_move_edit);
  193. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  194. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  195. undo_redo->commit_action();
  196. edited_point = -1;
  197. return true;
  198. }
  199. }
  200. }
  201. if (mb.button_index == BUTTON_RIGHT && mb.pressed && edited_point == -1) {
  202. int closest_idx = -1;
  203. Vector2 closest_pos;
  204. real_t closest_dist = 1e10;
  205. for (int i = 0; i < poly.size(); i++) {
  206. Vector2 cp = xform.xform(poly[i]);
  207. real_t d = cp.distance_to(gpoint);
  208. if (d < closest_dist && d < grab_treshold) {
  209. closest_dist = d;
  210. closest_pos = cp;
  211. closest_idx = i;
  212. }
  213. }
  214. if (closest_idx >= 0) {
  215. undo_redo->create_action(TTR("Edit Poly (Remove Point)"));
  216. undo_redo->add_undo_method(node, "set_polygon", poly);
  217. poly.remove(closest_idx);
  218. undo_redo->add_do_method(node, "set_polygon", poly);
  219. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  220. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  221. undo_redo->commit_action();
  222. return true;
  223. }
  224. }
  225. } break;
  226. }
  227. } break;
  228. case InputEvent::MOUSE_MOTION: {
  229. const InputEventMouseMotion &mm = p_event.mouse_motion;
  230. if (edited_point != -1 && (wip_active || mm.button_mask & BUTTON_MASK_LEFT)) {
  231. Vector2 gpoint = Point2(mm.x, mm.y);
  232. Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint);
  233. cpoint = canvas_item_editor->snap_point(cpoint);
  234. edited_point_pos = node->get_global_transform().affine_inverse().xform(cpoint);
  235. canvas_item_editor->get_viewport_control()->update();
  236. }
  237. } break;
  238. }
  239. return false;
  240. }
  241. void CollisionPolygon2DEditor::_canvas_draw() {
  242. if (!node)
  243. return;
  244. Control *vpc = canvas_item_editor->get_viewport_control();
  245. Vector<Vector2> poly;
  246. if (wip_active)
  247. poly = wip;
  248. else
  249. poly = node->get_polygon();
  250. Matrix32 xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  251. Ref<Texture> handle = get_icon("EditorHandle", "EditorIcons");
  252. for (int i = 0; i < poly.size(); i++) {
  253. Vector2 p, p2;
  254. p = i == edited_point ? edited_point_pos : poly[i];
  255. if ((wip_active && i == poly.size() - 1) || (((i + 1) % poly.size()) == edited_point))
  256. p2 = edited_point_pos;
  257. else
  258. p2 = poly[(i + 1) % poly.size()];
  259. Vector2 point = xform.xform(p);
  260. Vector2 next_point = xform.xform(p2);
  261. Color col = Color(1, 0.3, 0.1, 0.8);
  262. vpc->draw_line(point, next_point, col, 2);
  263. vpc->draw_texture(handle, point - handle->get_size() * 0.5);
  264. }
  265. }
  266. void CollisionPolygon2DEditor::edit(Node *p_collision_polygon) {
  267. if (!canvas_item_editor) {
  268. canvas_item_editor = CanvasItemEditor::get_singleton();
  269. }
  270. if (p_collision_polygon) {
  271. node = p_collision_polygon->cast_to<CollisionPolygon2D>();
  272. if (!canvas_item_editor->get_viewport_control()->is_connected("draw", this, "_canvas_draw"))
  273. canvas_item_editor->get_viewport_control()->connect("draw", this, "_canvas_draw");
  274. wip.clear();
  275. wip_active = false;
  276. edited_point = -1;
  277. canvas_item_editor->get_viewport_control()->update();
  278. } else {
  279. node = NULL;
  280. if (canvas_item_editor->get_viewport_control()->is_connected("draw", this, "_canvas_draw"))
  281. canvas_item_editor->get_viewport_control()->disconnect("draw", this, "_canvas_draw");
  282. }
  283. }
  284. void CollisionPolygon2DEditor::_bind_methods() {
  285. ObjectTypeDB::bind_method(_MD("_menu_option"), &CollisionPolygon2DEditor::_menu_option);
  286. ObjectTypeDB::bind_method(_MD("_canvas_draw"), &CollisionPolygon2DEditor::_canvas_draw);
  287. ObjectTypeDB::bind_method(_MD("_node_removed"), &CollisionPolygon2DEditor::_node_removed);
  288. }
  289. CollisionPolygon2DEditor::CollisionPolygon2DEditor(EditorNode *p_editor) {
  290. node = NULL;
  291. canvas_item_editor = NULL;
  292. editor = p_editor;
  293. undo_redo = editor->get_undo_redo();
  294. add_child(memnew(VSeparator));
  295. button_create = memnew(ToolButton);
  296. add_child(button_create);
  297. button_create->connect("pressed", this, "_menu_option", varray(MODE_CREATE));
  298. button_create->set_toggle_mode(true);
  299. button_create->set_tooltip(TTR("Create a new polygon from scratch."));
  300. button_edit = memnew(ToolButton);
  301. add_child(button_edit);
  302. button_edit->connect("pressed", this, "_menu_option", varray(MODE_EDIT));
  303. button_edit->set_toggle_mode(true);
  304. button_edit->set_tooltip("Edit existing polygon:\nLMB: Move Point.\nCtrl+LMB: Split Segment.\nRMB: Erase Point.");
  305. //add_constant_override("separation",0);
  306. #if 0
  307. options = memnew( MenuButton );
  308. add_child(options);
  309. options->set_area_as_parent_rect();
  310. options->set_text("Polygon");
  311. //options->get_popup()->add_item("Parse BBCode",PARSE_BBCODE);
  312. options->get_popup()->connect("item_pressed", this,"_menu_option");
  313. #endif
  314. mode = MODE_EDIT;
  315. wip_active = false;
  316. }
  317. void CollisionPolygon2DEditorPlugin::edit(Object *p_object) {
  318. collision_polygon_editor->edit(p_object->cast_to<Node>());
  319. }
  320. bool CollisionPolygon2DEditorPlugin::handles(Object *p_object) const {
  321. return p_object->is_type("CollisionPolygon2D");
  322. }
  323. void CollisionPolygon2DEditorPlugin::make_visible(bool p_visible) {
  324. if (p_visible) {
  325. collision_polygon_editor->show();
  326. } else {
  327. collision_polygon_editor->hide();
  328. collision_polygon_editor->edit(NULL);
  329. }
  330. }
  331. CollisionPolygon2DEditorPlugin::CollisionPolygon2DEditorPlugin(EditorNode *p_node) {
  332. editor = p_node;
  333. collision_polygon_editor = memnew(CollisionPolygon2DEditor(p_node));
  334. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(collision_polygon_editor);
  335. collision_polygon_editor->hide();
  336. }
  337. CollisionPolygon2DEditorPlugin::~CollisionPolygon2DEditorPlugin() {
  338. }