path_3d_editor_plugin.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*************************************************************************/
  2. /* path_3d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "path_3d_editor_plugin.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "core/math/geometry_3d.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_undo_redo_manager.h"
  37. #include "node_3d_editor_plugin.h"
  38. #include "scene/resources/curve.h"
  39. static bool _is_in_handle(int p_id, int p_num_points) {
  40. int t = (p_id + 1) % 2;
  41. int idx = (p_id + 1) / 2;
  42. // order of points is [out_0, out_1, in_1, out_2, in_2, ... out_n-1, in_n-1, in_n]
  43. if (idx == 0) {
  44. return false;
  45. } else if (idx == (p_num_points - 1)) {
  46. return true;
  47. } else {
  48. return (t == 1);
  49. }
  50. }
  51. String Path3DGizmo::get_handle_name(int p_id, bool p_secondary) const {
  52. Ref<Curve3D> c = path->get_curve();
  53. if (c.is_null()) {
  54. return "";
  55. }
  56. if (!p_secondary) {
  57. return TTR("Curve Point #") + itos(p_id);
  58. }
  59. // (p_id + 1) Accounts for the first point only having an "out" handle
  60. int idx = (p_id + 1) / 2;
  61. String n = TTR("Curve Point #") + itos(idx);
  62. if (_is_in_handle(p_id, c->get_point_count())) {
  63. n += " In";
  64. } else {
  65. n += " Out";
  66. }
  67. return n;
  68. }
  69. Variant Path3DGizmo::get_handle_value(int p_id, bool p_secondary) const {
  70. Ref<Curve3D> c = path->get_curve();
  71. if (c.is_null()) {
  72. return Variant();
  73. }
  74. if (!p_secondary) {
  75. original = c->get_point_position(p_id);
  76. return original;
  77. }
  78. // (p_id + 1) Accounts for the first point only having an "out" handle
  79. int idx = (p_id + 1) / 2;
  80. Vector3 ofs;
  81. if (_is_in_handle(p_id, c->get_point_count())) {
  82. ofs = c->get_point_in(idx);
  83. } else {
  84. ofs = c->get_point_out(idx);
  85. }
  86. original = ofs + c->get_point_position(idx);
  87. return ofs;
  88. }
  89. void Path3DGizmo::set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  90. Ref<Curve3D> c = path->get_curve();
  91. if (c.is_null()) {
  92. return;
  93. }
  94. Transform3D gt = path->get_global_transform();
  95. Transform3D gi = gt.affine_inverse();
  96. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  97. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  98. // Setting curve point positions
  99. if (!p_secondary) {
  100. const Plane p = Plane(p_camera->get_transform().basis.get_column(2), gt.xform(original));
  101. Vector3 inters;
  102. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  103. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  104. float snap = Node3DEditor::get_singleton()->get_translate_snap();
  105. inters.snap(Vector3(snap, snap, snap));
  106. }
  107. Vector3 local = gi.xform(inters);
  108. c->set_point_position(p_id, local);
  109. }
  110. return;
  111. }
  112. // (p_id + 1) Accounts for the first point only having an "out" handle
  113. int idx = (p_id + 1) / 2;
  114. Vector3 base = c->get_point_position(idx);
  115. Plane p(p_camera->get_transform().basis.get_column(2), gt.xform(original));
  116. Vector3 inters;
  117. // Setting curve in/out positions
  118. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  119. if (!Path3DEditorPlugin::singleton->is_handle_clicked()) {
  120. orig_in_length = c->get_point_in(idx).length();
  121. orig_out_length = c->get_point_out(idx).length();
  122. Path3DEditorPlugin::singleton->set_handle_clicked(true);
  123. }
  124. Vector3 local = gi.xform(inters) - base;
  125. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  126. float snap = Node3DEditor::get_singleton()->get_translate_snap();
  127. local.snap(Vector3(snap, snap, snap));
  128. }
  129. if (_is_in_handle(p_id, c->get_point_count())) {
  130. c->set_point_in(idx, local);
  131. if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
  132. c->set_point_out(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_out_length));
  133. }
  134. } else {
  135. c->set_point_out(idx, local);
  136. if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
  137. c->set_point_in(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_in_length));
  138. }
  139. }
  140. }
  141. }
  142. void Path3DGizmo::commit_handle(int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  143. Ref<Curve3D> c = path->get_curve();
  144. if (c.is_null()) {
  145. return;
  146. }
  147. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  148. if (!p_secondary) {
  149. if (p_cancel) {
  150. c->set_point_position(p_id, p_restore);
  151. return;
  152. }
  153. ur->create_action(TTR("Set Curve Point Position"));
  154. ur->add_do_method(c.ptr(), "set_point_position", p_id, c->get_point_position(p_id));
  155. ur->add_undo_method(c.ptr(), "set_point_position", p_id, p_restore);
  156. ur->commit_action();
  157. return;
  158. }
  159. // (p_id + 1) Accounts for the first point only having an "out" handle
  160. int idx = (p_id + 1) / 2;
  161. if (_is_in_handle(p_id, c->get_point_count())) {
  162. if (p_cancel) {
  163. c->set_point_in(p_id, p_restore);
  164. return;
  165. }
  166. ur->create_action(TTR("Set Curve In Position"));
  167. ur->add_do_method(c.ptr(), "set_point_in", idx, c->get_point_in(idx));
  168. ur->add_undo_method(c.ptr(), "set_point_in", idx, p_restore);
  169. if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
  170. ur->add_do_method(c.ptr(), "set_point_out", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -c->get_point_in(idx) : (-c->get_point_in(idx).normalized() * orig_out_length));
  171. ur->add_undo_method(c.ptr(), "set_point_out", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_out_length));
  172. }
  173. ur->commit_action();
  174. } else {
  175. if (p_cancel) {
  176. c->set_point_out(idx, p_restore);
  177. return;
  178. }
  179. ur->create_action(TTR("Set Curve Out Position"));
  180. ur->add_do_method(c.ptr(), "set_point_out", idx, c->get_point_out(idx));
  181. ur->add_undo_method(c.ptr(), "set_point_out", idx, p_restore);
  182. if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
  183. ur->add_do_method(c.ptr(), "set_point_in", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -c->get_point_out(idx) : (-c->get_point_out(idx).normalized() * orig_in_length));
  184. ur->add_undo_method(c.ptr(), "set_point_in", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_in_length));
  185. }
  186. ur->commit_action();
  187. }
  188. }
  189. void Path3DGizmo::redraw() {
  190. clear();
  191. Ref<StandardMaterial3D> path_material = gizmo_plugin->get_material("path_material", this);
  192. Ref<StandardMaterial3D> path_thin_material = gizmo_plugin->get_material("path_thin_material", this);
  193. Ref<StandardMaterial3D> handles_material = gizmo_plugin->get_material("handles");
  194. Ref<StandardMaterial3D> sec_handles_material = gizmo_plugin->get_material("sec_handles");
  195. Ref<Curve3D> c = path->get_curve();
  196. if (c.is_null()) {
  197. return;
  198. }
  199. Vector<Vector3> v3a = c->tessellate();
  200. //Vector<Vector3> v3a=c->get_baked_points();
  201. int v3s = v3a.size();
  202. if (v3s == 0) {
  203. return;
  204. }
  205. Vector<Vector3> v3p;
  206. const Vector3 *r = v3a.ptr();
  207. // BUG: the following won't work when v3s, avoid drawing as a temporary workaround.
  208. for (int i = 0; i < v3s - 1; i++) {
  209. v3p.push_back(r[i]);
  210. v3p.push_back(r[i + 1]);
  211. //v3p.push_back(r[i]);
  212. //v3p.push_back(r[i]+Vector3(0,0.2,0));
  213. }
  214. if (v3p.size() > 1) {
  215. add_lines(v3p, path_material);
  216. add_collision_segments(v3p);
  217. }
  218. if (Path3DEditorPlugin::singleton->get_edited_path() == path) {
  219. v3p.clear();
  220. Vector<Vector3> handles;
  221. Vector<Vector3> sec_handles;
  222. for (int i = 0; i < c->get_point_count(); i++) {
  223. Vector3 p = c->get_point_position(i);
  224. handles.push_back(p);
  225. // push Out points first so they get selected if the In and Out points are on top of each other.
  226. if (i < c->get_point_count() - 1) {
  227. v3p.push_back(p);
  228. v3p.push_back(p + c->get_point_out(i));
  229. sec_handles.push_back(p + c->get_point_out(i));
  230. }
  231. if (i > 0) {
  232. v3p.push_back(p);
  233. v3p.push_back(p + c->get_point_in(i));
  234. sec_handles.push_back(p + c->get_point_in(i));
  235. }
  236. }
  237. if (v3p.size() > 1) {
  238. add_lines(v3p, path_thin_material);
  239. }
  240. if (handles.size()) {
  241. add_handles(handles, handles_material);
  242. }
  243. if (sec_handles.size()) {
  244. add_handles(sec_handles, sec_handles_material, Vector<int>(), false, true);
  245. }
  246. }
  247. }
  248. Path3DGizmo::Path3DGizmo(Path3D *p_path) {
  249. path = p_path;
  250. set_spatial_node(p_path);
  251. orig_in_length = 0;
  252. orig_out_length = 0;
  253. }
  254. EditorPlugin::AfterGUIInput Path3DEditorPlugin::forward_spatial_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  255. if (!path) {
  256. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  257. }
  258. Ref<Curve3D> c = path->get_curve();
  259. if (c.is_null()) {
  260. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  261. }
  262. Transform3D gt = path->get_global_transform();
  263. Transform3D it = gt.affine_inverse();
  264. static const int click_dist = 10; //should make global
  265. Ref<InputEventMouseButton> mb = p_event;
  266. if (mb.is_valid()) {
  267. Point2 mbpos(mb->get_position().x, mb->get_position().y);
  268. if (!mb->is_pressed()) {
  269. set_handle_clicked(false);
  270. }
  271. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->is_ctrl_pressed()))) {
  272. //click into curve, break it down
  273. Vector<Vector3> v3a = c->tessellate();
  274. int rc = v3a.size();
  275. int closest_seg = -1;
  276. Vector3 closest_seg_point;
  277. if (rc >= 2) {
  278. int idx = 0;
  279. const Vector3 *r = v3a.ptr();
  280. float closest_d = 1e20;
  281. if (p_camera->unproject_position(gt.xform(c->get_point_position(0))).distance_to(mbpos) < click_dist) {
  282. return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
  283. }
  284. for (int i = 0; i < c->get_point_count() - 1; i++) {
  285. //find the offset and point index of the place to break up
  286. int j = idx;
  287. if (p_camera->unproject_position(gt.xform(c->get_point_position(i + 1))).distance_to(mbpos) < click_dist) {
  288. return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
  289. }
  290. while (j < rc && c->get_point_position(i + 1) != r[j]) {
  291. Vector3 from = r[j];
  292. Vector3 to = r[j + 1];
  293. real_t cdist = from.distance_to(to);
  294. from = gt.xform(from);
  295. to = gt.xform(to);
  296. if (cdist > 0) {
  297. Vector2 s[2];
  298. s[0] = p_camera->unproject_position(from);
  299. s[1] = p_camera->unproject_position(to);
  300. Vector2 inters = Geometry2D::get_closest_point_to_segment(mbpos, s);
  301. float d = inters.distance_to(mbpos);
  302. if (d < 10 && d < closest_d) {
  303. closest_d = d;
  304. closest_seg = i;
  305. Vector3 ray_from = p_camera->project_ray_origin(mbpos);
  306. Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
  307. Vector3 ra, rb;
  308. Geometry3D::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
  309. closest_seg_point = it.xform(rb);
  310. }
  311. }
  312. j++;
  313. }
  314. if (idx == j) {
  315. idx++; //force next
  316. } else {
  317. idx = j; //swap
  318. }
  319. if (j == rc) {
  320. break;
  321. }
  322. }
  323. }
  324. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  325. if (closest_seg != -1) {
  326. //subdivide
  327. ur->create_action(TTR("Split Path"));
  328. ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);
  329. ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);
  330. ur->commit_action();
  331. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  332. } else {
  333. Vector3 origin;
  334. if (c->get_point_count() == 0) {
  335. origin = path->get_transform().get_origin();
  336. } else {
  337. origin = gt.xform(c->get_point_position(c->get_point_count() - 1));
  338. }
  339. Plane p(p_camera->get_transform().basis.get_column(2), origin);
  340. Vector3 ray_from = p_camera->project_ray_origin(mbpos);
  341. Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
  342. Vector3 inters;
  343. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  344. ur->create_action(TTR("Add Point to Curve"));
  345. ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);
  346. ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
  347. ur->commit_action();
  348. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  349. }
  350. //add new at pos
  351. }
  352. } else if (mb->is_pressed() && ((mb->get_button_index() == MouseButton::LEFT && curve_del->is_pressed()) || (mb->get_button_index() == MouseButton::RIGHT && curve_edit->is_pressed()))) {
  353. for (int i = 0; i < c->get_point_count(); i++) {
  354. real_t dist_to_p = p_camera->unproject_position(gt.xform(c->get_point_position(i))).distance_to(mbpos);
  355. real_t dist_to_p_out = p_camera->unproject_position(gt.xform(c->get_point_position(i) + c->get_point_out(i))).distance_to(mbpos);
  356. real_t dist_to_p_in = p_camera->unproject_position(gt.xform(c->get_point_position(i) + c->get_point_in(i))).distance_to(mbpos);
  357. // Find the offset and point index of the place to break up.
  358. // Also check for the control points.
  359. if (dist_to_p < click_dist) {
  360. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  361. ur->create_action(TTR("Remove Path Point"));
  362. ur->add_do_method(c.ptr(), "remove_point", i);
  363. ur->add_undo_method(c.ptr(), "add_point", c->get_point_position(i), c->get_point_in(i), c->get_point_out(i), i);
  364. ur->commit_action();
  365. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  366. } else if (dist_to_p_out < click_dist) {
  367. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  368. ur->create_action(TTR("Remove Out-Control Point"));
  369. ur->add_do_method(c.ptr(), "set_point_out", i, Vector3());
  370. ur->add_undo_method(c.ptr(), "set_point_out", i, c->get_point_out(i));
  371. ur->commit_action();
  372. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  373. } else if (dist_to_p_in < click_dist) {
  374. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  375. ur->create_action(TTR("Remove In-Control Point"));
  376. ur->add_do_method(c.ptr(), "set_point_in", i, Vector3());
  377. ur->add_undo_method(c.ptr(), "set_point_in", i, c->get_point_in(i));
  378. ur->commit_action();
  379. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  380. }
  381. }
  382. }
  383. }
  384. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  385. }
  386. void Path3DEditorPlugin::edit(Object *p_object) {
  387. if (p_object) {
  388. path = Object::cast_to<Path3D>(p_object);
  389. if (path) {
  390. if (path->get_curve().is_valid()) {
  391. path->get_curve()->emit_signal(SNAME("changed"));
  392. }
  393. }
  394. } else {
  395. Path3D *pre = path;
  396. path = nullptr;
  397. if (pre) {
  398. pre->get_curve()->emit_signal(SNAME("changed"));
  399. }
  400. }
  401. //collision_polygon_editor->edit(Object::cast_to<Node>(p_object));
  402. }
  403. bool Path3DEditorPlugin::handles(Object *p_object) const {
  404. return p_object->is_class("Path3D");
  405. }
  406. void Path3DEditorPlugin::make_visible(bool p_visible) {
  407. if (p_visible) {
  408. curve_create->show();
  409. curve_edit->show();
  410. curve_del->show();
  411. curve_close->show();
  412. handle_menu->show();
  413. sep->show();
  414. } else {
  415. curve_create->hide();
  416. curve_edit->hide();
  417. curve_del->hide();
  418. curve_close->hide();
  419. handle_menu->hide();
  420. sep->hide();
  421. {
  422. Path3D *pre = path;
  423. path = nullptr;
  424. if (pre && pre->get_curve().is_valid()) {
  425. pre->get_curve()->emit_signal(SNAME("changed"));
  426. }
  427. }
  428. }
  429. }
  430. void Path3DEditorPlugin::_mode_changed(int p_idx) {
  431. curve_create->set_pressed(p_idx == 0);
  432. curve_edit->set_pressed(p_idx == 1);
  433. curve_del->set_pressed(p_idx == 2);
  434. }
  435. void Path3DEditorPlugin::_close_curve() {
  436. Ref<Curve3D> c = path->get_curve();
  437. if (c.is_null()) {
  438. return;
  439. }
  440. if (c->get_point_count() < 2) {
  441. return;
  442. }
  443. if (c->get_point_position(0) == c->get_point_position(c->get_point_count() - 1)) {
  444. return;
  445. }
  446. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  447. ur->create_action(TTR("Close Curve"));
  448. ur->add_do_method(c.ptr(), "add_point", c->get_point_position(0), c->get_point_in(0), c->get_point_out(0), -1);
  449. ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
  450. ur->commit_action();
  451. }
  452. void Path3DEditorPlugin::_handle_option_pressed(int p_option) {
  453. PopupMenu *pm;
  454. pm = handle_menu->get_popup();
  455. switch (p_option) {
  456. case HANDLE_OPTION_ANGLE: {
  457. bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
  458. mirror_handle_angle = !is_checked;
  459. pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  460. pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
  461. } break;
  462. case HANDLE_OPTION_LENGTH: {
  463. bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
  464. mirror_handle_length = !is_checked;
  465. pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  466. } break;
  467. }
  468. }
  469. void Path3DEditorPlugin::_update_theme() {
  470. // TODO: Split the EditorPlugin instance from the UI instance and connect this properly.
  471. // See the 2D path editor for inspiration.
  472. curve_edit->set_icon(Node3DEditor::get_singleton()->get_theme_icon(SNAME("CurveEdit"), SNAME("EditorIcons")));
  473. curve_create->set_icon(Node3DEditor::get_singleton()->get_theme_icon(SNAME("CurveCreate"), SNAME("EditorIcons")));
  474. curve_del->set_icon(Node3DEditor::get_singleton()->get_theme_icon(SNAME("CurveDelete"), SNAME("EditorIcons")));
  475. curve_close->set_icon(Node3DEditor::get_singleton()->get_theme_icon(SNAME("CurveClose"), SNAME("EditorIcons")));
  476. }
  477. void Path3DEditorPlugin::_notification(int p_what) {
  478. switch (p_what) {
  479. case NOTIFICATION_ENTER_TREE: {
  480. curve_create->connect("pressed", callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(0));
  481. curve_edit->connect("pressed", callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(1));
  482. curve_del->connect("pressed", callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(2));
  483. curve_close->connect("pressed", callable_mp(this, &Path3DEditorPlugin::_close_curve));
  484. _update_theme();
  485. } break;
  486. case NOTIFICATION_READY: {
  487. Node3DEditor::get_singleton()->connect("theme_changed", callable_mp(this, &Path3DEditorPlugin::_update_theme));
  488. } break;
  489. }
  490. }
  491. void Path3DEditorPlugin::_bind_methods() {
  492. }
  493. Path3DEditorPlugin *Path3DEditorPlugin::singleton = nullptr;
  494. Path3DEditorPlugin::Path3DEditorPlugin() {
  495. path = nullptr;
  496. singleton = this;
  497. mirror_handle_angle = true;
  498. mirror_handle_length = true;
  499. Ref<Path3DGizmoPlugin> gizmo_plugin;
  500. gizmo_plugin.instantiate();
  501. Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  502. sep = memnew(VSeparator);
  503. sep->hide();
  504. Node3DEditor::get_singleton()->add_control_to_menu_panel(sep);
  505. curve_edit = memnew(Button);
  506. curve_edit->set_flat(true);
  507. curve_edit->set_toggle_mode(true);
  508. curve_edit->hide();
  509. curve_edit->set_focus_mode(Control::FOCUS_NONE);
  510. curve_edit->set_tooltip_text(TTR("Select Points") + "\n" + TTR("Shift+Drag: Select Control Points") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD) + TTR("Click: Add Point") + "\n" + TTR("Right Click: Delete Point"));
  511. Node3DEditor::get_singleton()->add_control_to_menu_panel(curve_edit);
  512. curve_create = memnew(Button);
  513. curve_create->set_flat(true);
  514. curve_create->set_toggle_mode(true);
  515. curve_create->hide();
  516. curve_create->set_focus_mode(Control::FOCUS_NONE);
  517. curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
  518. Node3DEditor::get_singleton()->add_control_to_menu_panel(curve_create);
  519. curve_del = memnew(Button);
  520. curve_del->set_flat(true);
  521. curve_del->set_toggle_mode(true);
  522. curve_del->hide();
  523. curve_del->set_focus_mode(Control::FOCUS_NONE);
  524. curve_del->set_tooltip_text(TTR("Delete Point"));
  525. Node3DEditor::get_singleton()->add_control_to_menu_panel(curve_del);
  526. curve_close = memnew(Button);
  527. curve_close->set_flat(true);
  528. curve_close->hide();
  529. curve_close->set_focus_mode(Control::FOCUS_NONE);
  530. curve_close->set_tooltip_text(TTR("Close Curve"));
  531. Node3DEditor::get_singleton()->add_control_to_menu_panel(curve_close);
  532. PopupMenu *menu;
  533. handle_menu = memnew(MenuButton);
  534. handle_menu->set_text(TTR("Options"));
  535. handle_menu->hide();
  536. Node3DEditor::get_singleton()->add_control_to_menu_panel(handle_menu);
  537. menu = handle_menu->get_popup();
  538. menu->add_check_item(TTR("Mirror Handle Angles"));
  539. menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  540. menu->add_check_item(TTR("Mirror Handle Lengths"));
  541. menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  542. menu->connect("id_pressed", callable_mp(this, &Path3DEditorPlugin::_handle_option_pressed));
  543. curve_edit->set_pressed(true);
  544. }
  545. Path3DEditorPlugin::~Path3DEditorPlugin() {
  546. }
  547. Ref<EditorNode3DGizmo> Path3DGizmoPlugin::create_gizmo(Node3D *p_spatial) {
  548. Ref<Path3DGizmo> ref;
  549. Path3D *path = Object::cast_to<Path3D>(p_spatial);
  550. if (path) {
  551. ref = Ref<Path3DGizmo>(memnew(Path3DGizmo(path)));
  552. }
  553. return ref;
  554. }
  555. String Path3DGizmoPlugin::get_gizmo_name() const {
  556. return "Path3D";
  557. }
  558. int Path3DGizmoPlugin::get_priority() const {
  559. return -1;
  560. }
  561. Path3DGizmoPlugin::Path3DGizmoPlugin() {
  562. Color path_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/path", Color(0.5, 0.5, 1.0, 0.8));
  563. create_material("path_material", path_color);
  564. create_material("path_thin_material", Color(0.5, 0.5, 0.5));
  565. create_handle_material("handles", false, Node3DEditor::get_singleton()->get_theme_icon(SNAME("EditorPathSmoothHandle"), SNAME("EditorIcons")));
  566. create_handle_material("sec_handles", false, Node3DEditor::get_singleton()->get_theme_icon(SNAME("EditorCurveHandle"), SNAME("EditorIcons")));
  567. }