collision_shape_2d_editor_plugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*************************************************************************/
  2. /* collision_shape_2d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_shape_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "scene/resources/capsule_shape_2d.h"
  33. #include "scene/resources/circle_shape_2d.h"
  34. #include "scene/resources/concave_polygon_shape_2d.h"
  35. #include "scene/resources/convex_polygon_shape_2d.h"
  36. #include "scene/resources/rectangle_shape_2d.h"
  37. #include "scene/resources/segment_shape_2d.h"
  38. #include "scene/resources/shape_line_2d.h"
  39. Variant CollisionShape2DEditor::get_handle_value(int idx) const {
  40. switch (shape_type) {
  41. case CAPSULE_SHAPE: {
  42. Ref<CapsuleShape2D> capsule = node->get_shape();
  43. if (idx == 0) {
  44. return capsule->get_radius();
  45. } else if (idx == 1) {
  46. return capsule->get_height();
  47. }
  48. } break;
  49. case CIRCLE_SHAPE: {
  50. Ref<CircleShape2D> circle = node->get_shape();
  51. if (idx == 0) {
  52. return circle->get_radius();
  53. }
  54. } break;
  55. case CONCAVE_POLYGON_SHAPE: {
  56. } break;
  57. case CONVEX_POLYGON_SHAPE: {
  58. } break;
  59. case LINE_SHAPE: {
  60. Ref<LineShape2D> line = node->get_shape();
  61. if (idx == 0) {
  62. return line->get_d();
  63. } else {
  64. return line->get_normal();
  65. }
  66. } break;
  67. case RAY_SHAPE: {
  68. Ref<RayShape2D> ray = node->get_shape();
  69. if (idx == 0) {
  70. return ray->get_length();
  71. }
  72. } break;
  73. case RECTANGLE_SHAPE: {
  74. Ref<RectangleShape2D> rect = node->get_shape();
  75. if (idx < 2) {
  76. return rect->get_extents().abs();
  77. }
  78. } break;
  79. case SEGMENT_SHAPE: {
  80. Ref<SegmentShape2D> seg = node->get_shape();
  81. if (idx == 0) {
  82. return seg->get_a();
  83. } else if (idx == 1) {
  84. return seg->get_b();
  85. }
  86. } break;
  87. }
  88. return Variant();
  89. }
  90. void CollisionShape2DEditor::set_handle(int idx, Point2 &p_point) {
  91. switch (shape_type) {
  92. case CAPSULE_SHAPE: {
  93. if (idx < 2) {
  94. Ref<CapsuleShape2D> capsule = node->get_shape();
  95. real_t parameter = Math::abs(p_point[idx]);
  96. if (idx == 0) {
  97. capsule->set_radius(parameter);
  98. } else if (idx == 1) {
  99. capsule->set_height(parameter * 2 - capsule->get_radius() * 2);
  100. }
  101. canvas_item_editor->get_viewport_control()->update();
  102. }
  103. } break;
  104. case CIRCLE_SHAPE: {
  105. Ref<CircleShape2D> circle = node->get_shape();
  106. circle->set_radius(p_point.length());
  107. canvas_item_editor->get_viewport_control()->update();
  108. } break;
  109. case CONCAVE_POLYGON_SHAPE: {
  110. } break;
  111. case CONVEX_POLYGON_SHAPE: {
  112. } break;
  113. case LINE_SHAPE: {
  114. if (idx < 2) {
  115. Ref<LineShape2D> line = node->get_shape();
  116. if (idx == 0) {
  117. line->set_d(p_point.length());
  118. } else {
  119. line->set_normal(p_point.normalized());
  120. }
  121. canvas_item_editor->get_viewport_control()->update();
  122. }
  123. } break;
  124. case RAY_SHAPE: {
  125. Ref<RayShape2D> ray = node->get_shape();
  126. ray->set_length(Math::abs(p_point.y));
  127. canvas_item_editor->get_viewport_control()->update();
  128. } break;
  129. case RECTANGLE_SHAPE: {
  130. if (idx < 2) {
  131. Ref<RectangleShape2D> rect = node->get_shape();
  132. Vector2 extents = rect->get_extents();
  133. extents[idx] = p_point[idx];
  134. rect->set_extents(extents.abs());
  135. canvas_item_editor->get_viewport_control()->update();
  136. }
  137. } break;
  138. case SEGMENT_SHAPE: {
  139. if (edit_handle < 2) {
  140. Ref<SegmentShape2D> seg = node->get_shape();
  141. if (idx == 0) {
  142. seg->set_a(p_point);
  143. } else if (idx == 1) {
  144. seg->set_b(p_point);
  145. }
  146. canvas_item_editor->get_viewport_control()->update();
  147. }
  148. } break;
  149. }
  150. }
  151. void CollisionShape2DEditor::commit_handle(int idx, Variant &p_org) {
  152. Control *c = canvas_item_editor->get_viewport_control();
  153. undo_redo->create_action(TTR("Set Handle"));
  154. switch (shape_type) {
  155. case CAPSULE_SHAPE: {
  156. Ref<CapsuleShape2D> capsule = node->get_shape();
  157. if (idx == 0) {
  158. undo_redo->add_do_method(capsule.ptr(), "set_radius", capsule->get_radius());
  159. undo_redo->add_do_method(c, "update");
  160. undo_redo->add_undo_method(capsule.ptr(), "set_radius", p_org);
  161. undo_redo->add_do_method(c, "update");
  162. } else if (idx == 1) {
  163. undo_redo->add_do_method(capsule.ptr(), "set_height", capsule->get_height());
  164. undo_redo->add_do_method(c, "update");
  165. undo_redo->add_undo_method(capsule.ptr(), "set_height", p_org);
  166. undo_redo->add_undo_method(c, "update");
  167. }
  168. } break;
  169. case CIRCLE_SHAPE: {
  170. Ref<CircleShape2D> circle = node->get_shape();
  171. undo_redo->add_do_method(circle.ptr(), "set_radius", circle->get_radius());
  172. undo_redo->add_do_method(c, "update");
  173. undo_redo->add_undo_method(circle.ptr(), "set_radius", p_org);
  174. undo_redo->add_undo_method(c, "update");
  175. } break;
  176. case CONCAVE_POLYGON_SHAPE: {
  177. } break;
  178. case CONVEX_POLYGON_SHAPE: {
  179. } break;
  180. case LINE_SHAPE: {
  181. Ref<LineShape2D> line = node->get_shape();
  182. if (idx == 0) {
  183. undo_redo->add_do_method(line.ptr(), "set_d", line->get_d());
  184. undo_redo->add_do_method(c, "update");
  185. undo_redo->add_undo_method(line.ptr(), "set_d", p_org);
  186. undo_redo->add_undo_method(c, "update");
  187. } else {
  188. undo_redo->add_do_method(line.ptr(), "set_normal", line->get_normal());
  189. undo_redo->add_do_method(c, "update");
  190. undo_redo->add_undo_method(line.ptr(), "set_normal", p_org);
  191. undo_redo->add_undo_method(c, "update");
  192. }
  193. } break;
  194. case RAY_SHAPE: {
  195. Ref<RayShape2D> ray = node->get_shape();
  196. undo_redo->add_do_method(ray.ptr(), "set_length", ray->get_length());
  197. undo_redo->add_do_method(c, "update");
  198. undo_redo->add_undo_method(ray.ptr(), "set_length", p_org);
  199. undo_redo->add_undo_method(c, "update");
  200. } break;
  201. case RECTANGLE_SHAPE: {
  202. Ref<RectangleShape2D> rect = node->get_shape();
  203. undo_redo->add_do_method(rect.ptr(), "set_extents", rect->get_extents());
  204. undo_redo->add_do_method(c, "update");
  205. undo_redo->add_undo_method(rect.ptr(), "set_extents", p_org);
  206. undo_redo->add_undo_method(c, "update");
  207. } break;
  208. case SEGMENT_SHAPE: {
  209. Ref<SegmentShape2D> seg = node->get_shape();
  210. if (idx == 0) {
  211. undo_redo->add_do_method(seg.ptr(), "set_a", seg->get_a());
  212. undo_redo->add_do_method(c, "update");
  213. undo_redo->add_undo_method(seg.ptr(), "set_a", p_org);
  214. undo_redo->add_undo_method(c, "update");
  215. } else if (idx == 1) {
  216. undo_redo->add_do_method(seg.ptr(), "set_b", seg->get_b());
  217. undo_redo->add_do_method(c, "update");
  218. undo_redo->add_undo_method(seg.ptr(), "set_b", p_org);
  219. undo_redo->add_undo_method(c, "update");
  220. }
  221. } break;
  222. }
  223. undo_redo->commit_action();
  224. }
  225. bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  226. if (!node) {
  227. return false;
  228. }
  229. if (!node->get_shape().is_valid()) {
  230. return false;
  231. }
  232. if (shape_type == -1) {
  233. return false;
  234. }
  235. Ref<InputEventMouseButton> mb = p_event;
  236. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  237. if (mb.is_valid()) {
  238. Vector2 gpoint = mb->get_position();
  239. Vector2 cpoint = node->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mb->get_position())));
  240. if (mb->get_button_index() == BUTTON_LEFT) {
  241. if (mb->is_pressed()) {
  242. for (int i = 0; i < handles.size(); i++) {
  243. if (xform.xform(handles[i]).distance_to(gpoint) < 8) {
  244. edit_handle = i;
  245. break;
  246. }
  247. }
  248. if (edit_handle == -1) {
  249. pressed = false;
  250. return false;
  251. }
  252. original = get_handle_value(edit_handle);
  253. pressed = true;
  254. return true;
  255. } else {
  256. if (pressed) {
  257. commit_handle(edit_handle, original);
  258. edit_handle = -1;
  259. pressed = false;
  260. return true;
  261. }
  262. }
  263. }
  264. return false;
  265. }
  266. Ref<InputEventMouseMotion> mm = p_event;
  267. if (mm.is_valid()) {
  268. if (edit_handle == -1 || !pressed) {
  269. return false;
  270. }
  271. Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
  272. cpoint = node->get_global_transform().affine_inverse().xform(cpoint);
  273. set_handle(edit_handle, cpoint);
  274. return true;
  275. }
  276. return false;
  277. }
  278. void CollisionShape2DEditor::_get_current_shape_type() {
  279. if (!node) {
  280. return;
  281. }
  282. Ref<Shape2D> s = node->get_shape();
  283. if (!s.is_valid()) {
  284. return;
  285. }
  286. if (Object::cast_to<CapsuleShape2D>(*s)) {
  287. shape_type = CAPSULE_SHAPE;
  288. } else if (Object::cast_to<CircleShape2D>(*s)) {
  289. shape_type = CIRCLE_SHAPE;
  290. } else if (Object::cast_to<ConcavePolygonShape2D>(*s)) {
  291. shape_type = CONCAVE_POLYGON_SHAPE;
  292. } else if (Object::cast_to<ConvexPolygonShape2D>(*s)) {
  293. shape_type = CONVEX_POLYGON_SHAPE;
  294. } else if (Object::cast_to<LineShape2D>(*s)) {
  295. shape_type = LINE_SHAPE;
  296. } else if (Object::cast_to<RayShape2D>(*s)) {
  297. shape_type = RAY_SHAPE;
  298. } else if (Object::cast_to<RectangleShape2D>(*s)) {
  299. shape_type = RECTANGLE_SHAPE;
  300. } else if (Object::cast_to<SegmentShape2D>(*s)) {
  301. shape_type = SEGMENT_SHAPE;
  302. } else {
  303. shape_type = -1;
  304. }
  305. canvas_item_editor->get_viewport_control()->update();
  306. }
  307. void CollisionShape2DEditor::forward_draw_over_canvas(Control *p_canvas) {
  308. if (!node) {
  309. return;
  310. }
  311. if (!node->get_shape().is_valid()) {
  312. return;
  313. }
  314. _get_current_shape_type();
  315. if (shape_type == -1) {
  316. return;
  317. }
  318. Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  319. Ref<Texture> h = get_icon("EditorHandle", "EditorIcons");
  320. Vector2 size = h->get_size() * 0.5;
  321. handles.clear();
  322. switch (shape_type) {
  323. case CAPSULE_SHAPE: {
  324. Ref<CapsuleShape2D> shape = node->get_shape();
  325. handles.resize(2);
  326. float radius = shape->get_radius();
  327. float height = shape->get_height() / 2;
  328. handles[0] = Point2(radius, -height);
  329. handles[1] = Point2(0, -(height + radius));
  330. p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
  331. p_canvas->draw_texture(h, gt.xform(handles[1]) - size);
  332. } break;
  333. case CIRCLE_SHAPE: {
  334. Ref<CircleShape2D> shape = node->get_shape();
  335. handles.resize(1);
  336. handles[0] = Point2(shape->get_radius(), 0);
  337. p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
  338. } break;
  339. case CONCAVE_POLYGON_SHAPE: {
  340. } break;
  341. case CONVEX_POLYGON_SHAPE: {
  342. } break;
  343. case LINE_SHAPE: {
  344. Ref<LineShape2D> shape = node->get_shape();
  345. handles.resize(2);
  346. handles[0] = shape->get_normal() * shape->get_d();
  347. handles[1] = shape->get_normal() * (shape->get_d() + 30.0);
  348. p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
  349. p_canvas->draw_texture(h, gt.xform(handles[1]) - size);
  350. } break;
  351. case RAY_SHAPE: {
  352. Ref<RayShape2D> shape = node->get_shape();
  353. handles.resize(1);
  354. handles[0] = Point2(0, shape->get_length());
  355. p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
  356. } break;
  357. case RECTANGLE_SHAPE: {
  358. Ref<RectangleShape2D> shape = node->get_shape();
  359. handles.resize(2);
  360. Vector2 ext = shape->get_extents();
  361. handles[0] = Point2(ext.x, 0);
  362. handles[1] = Point2(0, -ext.y);
  363. p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
  364. p_canvas->draw_texture(h, gt.xform(handles[1]) - size);
  365. } break;
  366. case SEGMENT_SHAPE: {
  367. Ref<SegmentShape2D> shape = node->get_shape();
  368. handles.resize(2);
  369. handles[0] = shape->get_a();
  370. handles[1] = shape->get_b();
  371. p_canvas->draw_texture(h, gt.xform(handles[0]) - size);
  372. p_canvas->draw_texture(h, gt.xform(handles[1]) - size);
  373. } break;
  374. }
  375. }
  376. void CollisionShape2DEditor::edit(Node *p_node) {
  377. if (!canvas_item_editor) {
  378. canvas_item_editor = CanvasItemEditor::get_singleton();
  379. }
  380. if (p_node) {
  381. node = Object::cast_to<CollisionShape2D>(p_node);
  382. _get_current_shape_type();
  383. } else {
  384. edit_handle = -1;
  385. shape_type = -1;
  386. node = NULL;
  387. }
  388. canvas_item_editor->get_viewport_control()->update();
  389. }
  390. void CollisionShape2DEditor::_bind_methods() {
  391. ClassDB::bind_method("_get_current_shape_type", &CollisionShape2DEditor::_get_current_shape_type);
  392. }
  393. CollisionShape2DEditor::CollisionShape2DEditor(EditorNode *p_editor) {
  394. node = NULL;
  395. canvas_item_editor = NULL;
  396. editor = p_editor;
  397. undo_redo = p_editor->get_undo_redo();
  398. edit_handle = -1;
  399. pressed = false;
  400. }
  401. void CollisionShape2DEditorPlugin::edit(Object *p_obj) {
  402. collision_shape_2d_editor->edit(Object::cast_to<Node>(p_obj));
  403. }
  404. bool CollisionShape2DEditorPlugin::handles(Object *p_obj) const {
  405. return p_obj->is_class("CollisionShape2D");
  406. }
  407. void CollisionShape2DEditorPlugin::make_visible(bool visible) {
  408. if (!visible) {
  409. edit(NULL);
  410. }
  411. }
  412. CollisionShape2DEditorPlugin::CollisionShape2DEditorPlugin(EditorNode *p_editor) {
  413. editor = p_editor;
  414. collision_shape_2d_editor = memnew(CollisionShape2DEditor(p_editor));
  415. p_editor->get_gui_base()->add_child(collision_shape_2d_editor);
  416. }
  417. CollisionShape2DEditorPlugin::~CollisionShape2DEditorPlugin() {
  418. }