curve_editor_plugin.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /**************************************************************************/
  2. /* curve_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "curve_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/input/input.h"
  33. #include "core/math/geometry_2d.h"
  34. #include "core/os/keyboard.h"
  35. #include "editor/editor_interface.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/gui/editor_spin_slider.h"
  41. #include "editor/themes/editor_scale.h"
  42. #include "scene/gui/flow_container.h"
  43. #include "scene/gui/menu_button.h"
  44. #include "scene/gui/popup_menu.h"
  45. #include "scene/gui/separator.h"
  46. #include "scene/resources/image_texture.h"
  47. CurveEdit::CurveEdit() {
  48. set_focus_mode(FOCUS_ALL);
  49. set_clip_contents(true);
  50. }
  51. void CurveEdit::_bind_methods() {
  52. ClassDB::bind_method(D_METHOD("set_selected_index", "index"), &CurveEdit::set_selected_index);
  53. }
  54. void CurveEdit::set_curve(Ref<Curve> p_curve) {
  55. if (p_curve == curve) {
  56. return;
  57. }
  58. if (curve.is_valid()) {
  59. curve->disconnect_changed(callable_mp(this, &CurveEdit::_curve_changed));
  60. curve->disconnect(Curve::SIGNAL_RANGE_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
  61. curve->disconnect(Curve::SIGNAL_DOMAIN_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
  62. }
  63. curve = p_curve;
  64. if (curve.is_valid()) {
  65. curve->connect_changed(callable_mp(this, &CurveEdit::_curve_changed));
  66. curve->connect(Curve::SIGNAL_RANGE_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
  67. curve->connect(Curve::SIGNAL_DOMAIN_CHANGED, callable_mp(this, &CurveEdit::_curve_changed));
  68. }
  69. // Note: if you edit a curve, then set another, and try to undo,
  70. // it will normally apply on the previous curve, but you won't see it.
  71. }
  72. Ref<Curve> CurveEdit::get_curve() {
  73. return curve;
  74. }
  75. void CurveEdit::set_snap_enabled(bool p_enabled) {
  76. snap_enabled = p_enabled;
  77. queue_redraw();
  78. if (curve.is_valid()) {
  79. if (snap_enabled) {
  80. curve->set_meta(SNAME("_snap_enabled"), true);
  81. } else {
  82. curve->remove_meta(SNAME("_snap_enabled"));
  83. }
  84. }
  85. }
  86. void CurveEdit::set_snap_count(int p_snap_count) {
  87. snap_count = p_snap_count;
  88. queue_redraw();
  89. if (curve.is_valid()) {
  90. if (snap_count != CurveEditor::DEFAULT_SNAP) {
  91. curve->set_meta(SNAME("_snap_count"), snap_count);
  92. } else {
  93. curve->remove_meta(SNAME("_snap_count"));
  94. }
  95. }
  96. }
  97. Size2 CurveEdit::get_minimum_size() const {
  98. return Vector2(64, MAX(135, get_size().x * ASPECT_RATIO)) * EDSCALE;
  99. }
  100. void CurveEdit::_notification(int p_what) {
  101. switch (p_what) {
  102. case NOTIFICATION_MOUSE_EXIT: {
  103. if (hovered_index != -1 || hovered_tangent_index != TANGENT_NONE) {
  104. hovered_index = -1;
  105. hovered_tangent_index = TANGENT_NONE;
  106. queue_redraw();
  107. }
  108. } break;
  109. case NOTIFICATION_THEME_CHANGED: {
  110. float gizmo_scale = EDITOR_GET("interface/touchscreen/scale_gizmo_handles");
  111. point_radius = Math::round(BASE_POINT_RADIUS * get_theme_default_base_scale() * gizmo_scale);
  112. hover_radius = Math::round(BASE_HOVER_RADIUS * get_theme_default_base_scale() * gizmo_scale);
  113. tangent_radius = Math::round(BASE_TANGENT_RADIUS * get_theme_default_base_scale() * gizmo_scale);
  114. tangent_hover_radius = Math::round(BASE_TANGENT_HOVER_RADIUS * get_theme_default_base_scale() * gizmo_scale);
  115. tangent_length = Math::round(BASE_TANGENT_LENGTH * get_theme_default_base_scale());
  116. } break;
  117. case NOTIFICATION_DRAW: {
  118. _redraw();
  119. } break;
  120. case NOTIFICATION_VISIBILITY_CHANGED: {
  121. if (!is_visible()) {
  122. grabbing = GRAB_NONE;
  123. }
  124. } break;
  125. }
  126. }
  127. void CurveEdit::gui_input(const Ref<InputEvent> &p_event) {
  128. ERR_FAIL_COND(p_event.is_null());
  129. if (curve.is_null()) {
  130. return;
  131. }
  132. Ref<InputEventKey> k = p_event;
  133. if (k.is_valid()) {
  134. // Deleting points or making tangents linear.
  135. if (k->is_pressed() && k->get_keycode() == Key::KEY_DELETE) {
  136. if (selected_tangent_index != TANGENT_NONE) {
  137. toggle_linear(selected_index, selected_tangent_index);
  138. } else if (selected_index != -1) {
  139. if (grabbing == GRAB_ADD) {
  140. curve->remove_point(selected_index); // Point is temporary, so remove directly from curve.
  141. set_selected_index(-1);
  142. } else {
  143. remove_point(selected_index);
  144. }
  145. grabbing = GRAB_NONE;
  146. hovered_index = -1;
  147. hovered_tangent_index = TANGENT_NONE;
  148. }
  149. accept_event();
  150. }
  151. if (k->get_keycode() == Key::SHIFT || k->get_keycode() == Key::ALT) {
  152. queue_redraw(); // Redraw to show the axes or constraints.
  153. }
  154. }
  155. Ref<InputEventMouseButton> mb = p_event;
  156. if (mb.is_valid() && mb->is_pressed()) {
  157. Vector2 mpos = mb->get_position();
  158. if (mb->get_button_index() == MouseButton::RIGHT || mb->get_button_index() == MouseButton::MIDDLE) {
  159. if (mb->get_button_index() == MouseButton::RIGHT && grabbing == GRAB_MOVE) {
  160. // Move a point to its old position.
  161. curve->set_point_value(selected_index, initial_grab_pos.y);
  162. curve->set_point_offset(selected_index, initial_grab_pos.x);
  163. set_selected_index(initial_grab_index);
  164. hovered_index = get_point_at(mpos);
  165. grabbing = GRAB_NONE;
  166. } else {
  167. // Remove a point or make a tangent linear.
  168. selected_tangent_index = get_tangent_at(mpos);
  169. if (selected_tangent_index != TANGENT_NONE) {
  170. toggle_linear(selected_index, selected_tangent_index);
  171. } else {
  172. int point_to_remove = get_point_at(mpos);
  173. if (point_to_remove == -1) {
  174. set_selected_index(-1); // Nothing on the place of the click, just deselect the point.
  175. } else {
  176. if (grabbing == GRAB_ADD) {
  177. curve->remove_point(point_to_remove); // Point is temporary, so remove directly from curve.
  178. set_selected_index(-1);
  179. } else {
  180. remove_point(point_to_remove);
  181. }
  182. hovered_index = get_point_at(mpos);
  183. grabbing = GRAB_NONE;
  184. }
  185. }
  186. }
  187. }
  188. // Selecting or creating points.
  189. if (mb->get_button_index() == MouseButton::LEFT) {
  190. if (grabbing == GRAB_NONE) {
  191. selected_tangent_index = get_tangent_at(mpos);
  192. if (selected_tangent_index == TANGENT_NONE) {
  193. set_selected_index(get_point_at(mpos));
  194. }
  195. queue_redraw();
  196. }
  197. if (selected_index != -1) {
  198. // If an existing point/tangent was grabbed, remember a few things about it.
  199. grabbing = GRAB_MOVE;
  200. initial_grab_pos = curve->get_point_position(selected_index);
  201. initial_grab_index = selected_index;
  202. if (selected_index > 0) {
  203. initial_grab_left_tangent = curve->get_point_left_tangent(selected_index);
  204. }
  205. if (selected_index < curve->get_point_count() - 1) {
  206. initial_grab_right_tangent = curve->get_point_right_tangent(selected_index);
  207. }
  208. } else if (grabbing == GRAB_NONE) {
  209. // Adding a new point. Insert a temporary point for the user to adjust, so it's not in the undo/redo.
  210. Vector2 new_pos = get_world_pos(mpos).clamp(Vector2(curve->get_min_domain(), curve->get_min_value()), Vector2(curve->get_max_domain(), curve->get_max_value()));
  211. if (snap_enabled || mb->is_command_or_control_pressed()) {
  212. new_pos.x = Math::snapped(new_pos.x - curve->get_min_domain(), curve->get_domain_range() / snap_count) + curve->get_min_domain();
  213. new_pos.y = Math::snapped(new_pos.y - curve->get_min_value(), curve->get_value_range() / snap_count) + curve->get_min_value();
  214. }
  215. new_pos.x = get_offset_without_collision(selected_index, new_pos.x, mpos.x >= get_view_pos(new_pos).x);
  216. // Add a temporary point for the user to adjust before adding it permanently.
  217. int new_idx = curve->add_point_no_update(new_pos);
  218. set_selected_index(new_idx);
  219. grabbing = GRAB_ADD;
  220. initial_grab_pos = new_pos;
  221. }
  222. }
  223. }
  224. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  225. if (selected_tangent_index != TANGENT_NONE) {
  226. // Finish moving a tangent control.
  227. if (selected_index == 0) {
  228. set_point_right_tangent(selected_index, curve->get_point_right_tangent(selected_index));
  229. } else if (selected_index == curve->get_point_count() - 1) {
  230. set_point_left_tangent(selected_index, curve->get_point_left_tangent(selected_index));
  231. } else {
  232. set_point_tangents(selected_index, curve->get_point_left_tangent(selected_index), curve->get_point_right_tangent(selected_index));
  233. }
  234. grabbing = GRAB_NONE;
  235. } else if (grabbing == GRAB_MOVE) {
  236. // Finish moving a point.
  237. set_point_position(selected_index, curve->get_point_position(selected_index));
  238. grabbing = GRAB_NONE;
  239. } else if (grabbing == GRAB_ADD) {
  240. // Finish inserting a new point. Remove the temporary point and insert a permanent one in its place.
  241. Vector2 new_pos = curve->get_point_position(selected_index);
  242. curve->remove_point(selected_index);
  243. add_point(new_pos);
  244. grabbing = GRAB_NONE;
  245. }
  246. queue_redraw();
  247. }
  248. Ref<InputEventMouseMotion> mm = p_event;
  249. if (mm.is_valid()) {
  250. Vector2 mpos = mm->get_position();
  251. if (grabbing != GRAB_NONE && curve.is_valid()) {
  252. if (selected_index != -1) {
  253. if (selected_tangent_index == TANGENT_NONE) {
  254. // Drag point.
  255. Vector2 new_pos = get_world_pos(mpos).clamp(Vector2(curve->get_min_domain(), curve->get_min_value()), Vector2(curve->get_max_domain(), curve->get_max_value()));
  256. if (snap_enabled || mm->is_command_or_control_pressed()) {
  257. new_pos.x = Math::snapped(new_pos.x - curve->get_min_domain(), curve->get_domain_range() / snap_count) + curve->get_min_domain();
  258. new_pos.y = Math::snapped(new_pos.y - curve->get_min_value(), curve->get_value_range() / snap_count) + curve->get_min_value();
  259. }
  260. // Allow to snap to axes with Shift.
  261. if (mm->is_shift_pressed()) {
  262. Vector2 initial_mpos = get_view_pos(initial_grab_pos);
  263. if (Math::abs(mpos.x - initial_mpos.x) > Math::abs(mpos.y - initial_mpos.y)) {
  264. new_pos.y = initial_grab_pos.y;
  265. } else {
  266. new_pos.x = initial_grab_pos.x;
  267. }
  268. }
  269. // Allow to constraint the point between the adjacent two with Alt.
  270. if (mm->is_alt_pressed()) {
  271. float prev_point_offset = (selected_index > 0) ? (curve->get_point_position(selected_index - 1).x + 0.00001) : curve->get_min_domain();
  272. float next_point_offset = (selected_index < curve->get_point_count() - 1) ? (curve->get_point_position(selected_index + 1).x - 0.00001) : curve->get_max_domain();
  273. new_pos.x = CLAMP(new_pos.x, prev_point_offset, next_point_offset);
  274. }
  275. new_pos.x = get_offset_without_collision(selected_index, new_pos.x, mpos.x >= get_view_pos(new_pos).x);
  276. // The index may change if the point is dragged across another one.
  277. int i = curve->set_point_offset(selected_index, new_pos.x);
  278. hovered_index = i;
  279. set_selected_index(i);
  280. new_pos.y = CLAMP(new_pos.y, curve->get_min_value(), curve->get_max_value());
  281. curve->set_point_value(selected_index, new_pos.y);
  282. } else {
  283. // Drag tangent.
  284. const Vector2 new_pos = curve->get_point_position(selected_index);
  285. const Vector2 control_pos = get_world_pos(mpos);
  286. Vector2 dir = (control_pos - new_pos).normalized();
  287. real_t tangent = dir.y / (dir.x > 0 ? MAX(dir.x, 0.00001) : MIN(dir.x, -0.00001));
  288. // Must keep track of the hovered index as the cursor might move outside of the editor while dragging.
  289. hovered_tangent_index = selected_tangent_index;
  290. // Adjust the tangents.
  291. if (selected_tangent_index == TANGENT_LEFT) {
  292. curve->set_point_left_tangent(selected_index, tangent);
  293. // Align the other tangent if it isn't linear and Shift is not pressed.
  294. // If Shift is pressed at any point, restore the initial angle of the other tangent.
  295. if (selected_index != (curve->get_point_count() - 1) && curve->get_point_right_mode(selected_index) != Curve::TANGENT_LINEAR) {
  296. curve->set_point_right_tangent(selected_index, mm->is_shift_pressed() ? initial_grab_right_tangent : tangent);
  297. }
  298. } else {
  299. curve->set_point_right_tangent(selected_index, tangent);
  300. if (selected_index != 0 && curve->get_point_left_mode(selected_index) != Curve::TANGENT_LINEAR) {
  301. curve->set_point_left_tangent(selected_index, mm->is_shift_pressed() ? initial_grab_left_tangent : tangent);
  302. }
  303. }
  304. }
  305. }
  306. } else {
  307. // Grab mode is GRAB_NONE, so do hovering logic.
  308. hovered_index = get_point_at(mpos);
  309. hovered_tangent_index = get_tangent_at(mpos);
  310. queue_redraw();
  311. }
  312. }
  313. }
  314. void CurveEdit::use_preset(int p_preset_id) {
  315. ERR_FAIL_COND(p_preset_id < 0 || p_preset_id >= PRESET_COUNT);
  316. ERR_FAIL_COND(curve.is_null());
  317. Array previous_data = curve->get_data();
  318. curve->clear_points();
  319. const float min_y = curve->get_min_value();
  320. const float max_y = curve->get_max_value();
  321. const float min_x = curve->get_min_domain();
  322. const float max_x = curve->get_max_domain();
  323. switch (p_preset_id) {
  324. case PRESET_CONSTANT:
  325. curve->add_point(Vector2(min_x, (min_y + max_y) / 2.0));
  326. curve->add_point(Vector2(max_x, (min_y + max_y) / 2.0));
  327. curve->set_point_right_mode(0, Curve::TANGENT_LINEAR);
  328. curve->set_point_left_mode(1, Curve::TANGENT_LINEAR);
  329. break;
  330. case PRESET_LINEAR:
  331. curve->add_point(Vector2(min_x, min_y));
  332. curve->add_point(Vector2(max_x, max_y));
  333. curve->set_point_right_mode(0, Curve::TANGENT_LINEAR);
  334. curve->set_point_left_mode(1, Curve::TANGENT_LINEAR);
  335. break;
  336. case PRESET_EASE_IN:
  337. curve->add_point(Vector2(min_x, min_y));
  338. curve->add_point(Vector2(max_x, max_y), curve->get_value_range() / curve->get_domain_range() * 1.4, 0);
  339. break;
  340. case PRESET_EASE_OUT:
  341. curve->add_point(Vector2(min_x, min_y), 0, curve->get_value_range() / curve->get_domain_range() * 1.4);
  342. curve->add_point(Vector2(max_x, max_y));
  343. break;
  344. case PRESET_SMOOTHSTEP:
  345. curve->add_point(Vector2(min_x, min_y));
  346. curve->add_point(Vector2(max_x, max_y));
  347. break;
  348. default:
  349. break;
  350. }
  351. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  352. undo_redo->create_action(TTR("Load Curve Preset"));
  353. undo_redo->add_do_method(*curve, "_set_data", curve->get_data());
  354. undo_redo->add_do_method(this, "set_selected_index", -1);
  355. undo_redo->add_undo_method(*curve, "_set_data", previous_data);
  356. undo_redo->add_undo_method(this, "set_selected_index", selected_index);
  357. undo_redo->commit_action();
  358. }
  359. void CurveEdit::_curve_changed() {
  360. queue_redraw();
  361. // Point count can change in case of undo.
  362. if (selected_index >= curve->get_point_count()) {
  363. set_selected_index(-1);
  364. }
  365. }
  366. int CurveEdit::get_point_at(const Vector2 &p_pos) const {
  367. if (curve.is_null()) {
  368. return -1;
  369. }
  370. // Use a square-shaped hover region. If hovering multiple points, pick the closer one.
  371. const Rect2 hover_rect = Rect2(p_pos, Vector2(0, 0)).grow(hover_radius);
  372. int closest_idx = -1;
  373. float closest_dist_squared = hover_radius * hover_radius * 2;
  374. for (int i = 0; i < curve->get_point_count(); ++i) {
  375. Vector2 p = get_view_pos(curve->get_point_position(i));
  376. if (hover_rect.has_point(p) && p.distance_squared_to(p_pos) < closest_dist_squared) {
  377. closest_dist_squared = p.distance_squared_to(p_pos);
  378. closest_idx = i;
  379. }
  380. }
  381. return closest_idx;
  382. }
  383. CurveEdit::TangentIndex CurveEdit::get_tangent_at(const Vector2 &p_pos) const {
  384. if (curve.is_null() || selected_index < 0) {
  385. return TANGENT_NONE;
  386. }
  387. const Rect2 hover_rect = Rect2(p_pos, Vector2(0, 0)).grow(tangent_hover_radius);
  388. if (selected_index != 0) {
  389. Vector2 control_pos = get_tangent_view_pos(selected_index, TANGENT_LEFT);
  390. if (hover_rect.has_point(control_pos)) {
  391. return TANGENT_LEFT;
  392. }
  393. }
  394. if (selected_index != curve->get_point_count() - 1) {
  395. Vector2 control_pos = get_tangent_view_pos(selected_index, TANGENT_RIGHT);
  396. if (hover_rect.has_point(control_pos)) {
  397. return TANGENT_RIGHT;
  398. }
  399. }
  400. return TANGENT_NONE;
  401. }
  402. // FIXME: This function should be bounded better.
  403. float CurveEdit::get_offset_without_collision(int p_current_index, float p_offset, bool p_prioritize_right) {
  404. float safe_offset = p_offset;
  405. bool prioritizing_right = p_prioritize_right;
  406. for (int i = 0; i < curve->get_point_count(); i++) {
  407. if (i == p_current_index) {
  408. continue;
  409. }
  410. if (curve->get_point_position(i).x > safe_offset) {
  411. break;
  412. }
  413. if (curve->get_point_position(i).x == safe_offset) {
  414. if (prioritizing_right) {
  415. safe_offset += 0.00001;
  416. if (safe_offset > 1.0) {
  417. safe_offset = 1.0;
  418. prioritizing_right = false;
  419. }
  420. } else {
  421. safe_offset -= 0.00001;
  422. if (safe_offset < 0.0) {
  423. safe_offset = 0.0;
  424. prioritizing_right = true;
  425. }
  426. }
  427. i = -1;
  428. }
  429. }
  430. return safe_offset;
  431. }
  432. void CurveEdit::add_point(const Vector2 &p_pos) {
  433. ERR_FAIL_COND(curve.is_null());
  434. // Add a point to get its index, then remove it immediately. Trick to feed the UndoRedo.
  435. int new_idx = curve->add_point(p_pos);
  436. curve->remove_point(new_idx);
  437. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  438. undo_redo->create_action(TTR("Add Curve Point"));
  439. undo_redo->add_do_method(*curve, "add_point", p_pos);
  440. undo_redo->add_do_method(this, "set_selected_index", new_idx);
  441. undo_redo->add_undo_method(*curve, "remove_point", new_idx);
  442. undo_redo->add_undo_method(this, "set_selected_index", -1);
  443. undo_redo->commit_action();
  444. }
  445. void CurveEdit::remove_point(int p_index) {
  446. ERR_FAIL_COND(curve.is_null());
  447. ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
  448. Curve::Point p = curve->get_point(p_index);
  449. Vector2 old_pos = (grabbing == GRAB_MOVE) ? initial_grab_pos : p.position;
  450. int new_selected_index = selected_index;
  451. // Reselect the old selected point if it's not the deleted one.
  452. if (new_selected_index > p_index) {
  453. new_selected_index -= 1;
  454. } else if (new_selected_index == p_index) {
  455. new_selected_index = -1;
  456. }
  457. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  458. undo_redo->create_action(TTR("Remove Curve Point"));
  459. undo_redo->add_do_method(*curve, "remove_point", p_index);
  460. undo_redo->add_do_method(this, "set_selected_index", new_selected_index);
  461. undo_redo->add_undo_method(*curve, "add_point", old_pos, p.left_tangent, p.right_tangent, p.left_mode, p.right_mode);
  462. undo_redo->add_undo_method(this, "set_selected_index", selected_index);
  463. undo_redo->commit_action();
  464. }
  465. void CurveEdit::set_point_position(int p_index, const Vector2 &p_pos) {
  466. ERR_FAIL_COND(curve.is_null());
  467. ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
  468. if (initial_grab_pos == p_pos) {
  469. return;
  470. }
  471. // Pretend the point started from its old place.
  472. curve->set_point_value(p_index, initial_grab_pos.y);
  473. curve->set_point_offset(p_index, initial_grab_pos.x);
  474. // Note: Changing the offset may modify the order.
  475. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  476. undo_redo->create_action(TTR("Modify Curve Point"));
  477. undo_redo->add_do_method(*curve, "set_point_value", initial_grab_index, p_pos.y);
  478. undo_redo->add_do_method(*curve, "set_point_offset", initial_grab_index, p_pos.x);
  479. undo_redo->add_do_method(this, "set_selected_index", p_index);
  480. undo_redo->add_undo_method(*curve, "set_point_value", p_index, initial_grab_pos.y);
  481. undo_redo->add_undo_method(*curve, "set_point_offset", p_index, initial_grab_pos.x);
  482. undo_redo->add_undo_method(this, "set_selected_index", initial_grab_index);
  483. undo_redo->commit_action();
  484. }
  485. void CurveEdit::set_point_tangents(int p_index, float p_left, float p_right) {
  486. ERR_FAIL_COND(curve.is_null());
  487. ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
  488. if (initial_grab_left_tangent == p_left) {
  489. set_point_right_tangent(p_index, p_right);
  490. return;
  491. } else if (initial_grab_right_tangent == p_right) {
  492. set_point_left_tangent(p_index, p_left);
  493. return;
  494. }
  495. curve->set_point_left_tangent(p_index, initial_grab_left_tangent);
  496. curve->set_point_right_tangent(p_index, initial_grab_right_tangent);
  497. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  498. undo_redo->create_action(TTR("Modify Curve Point's Tangents"));
  499. undo_redo->add_do_method(*curve, "set_point_left_tangent", p_index, p_left);
  500. undo_redo->add_do_method(*curve, "set_point_right_tangent", p_index, p_right);
  501. undo_redo->add_do_method(this, "set_selected_index", p_index);
  502. undo_redo->add_undo_method(*curve, "set_point_left_tangent", p_index, initial_grab_left_tangent);
  503. undo_redo->add_undo_method(*curve, "set_point_right_tangent", p_index, initial_grab_right_tangent);
  504. undo_redo->add_undo_method(this, "set_selected_index", p_index);
  505. undo_redo->commit_action();
  506. }
  507. void CurveEdit::set_point_left_tangent(int p_index, float p_tangent) {
  508. ERR_FAIL_COND(curve.is_null());
  509. ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
  510. if (initial_grab_left_tangent == p_tangent) {
  511. return;
  512. }
  513. curve->set_point_left_tangent(p_index, initial_grab_left_tangent);
  514. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  515. undo_redo->create_action(TTR("Modify Curve Point's Left Tangent"));
  516. undo_redo->add_do_method(*curve, "set_point_left_tangent", p_index, p_tangent);
  517. undo_redo->add_do_method(this, "set_selected_index", p_index);
  518. undo_redo->add_undo_method(*curve, "set_point_left_tangent", p_index, initial_grab_left_tangent);
  519. undo_redo->add_undo_method(this, "set_selected_index", p_index);
  520. undo_redo->commit_action();
  521. }
  522. void CurveEdit::set_point_right_tangent(int p_index, float p_tangent) {
  523. ERR_FAIL_COND(curve.is_null());
  524. ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
  525. if (initial_grab_right_tangent == p_tangent) {
  526. return;
  527. }
  528. curve->set_point_right_tangent(p_index, initial_grab_right_tangent);
  529. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  530. undo_redo->create_action(TTR("Modify Curve Point's Right Tangent"));
  531. undo_redo->add_do_method(*curve, "set_point_right_tangent", p_index, p_tangent);
  532. undo_redo->add_do_method(this, "set_selected_index", p_index);
  533. undo_redo->add_undo_method(*curve, "set_point_right_tangent", p_index, initial_grab_right_tangent);
  534. undo_redo->add_undo_method(this, "set_selected_index", p_index);
  535. undo_redo->commit_action();
  536. }
  537. void CurveEdit::toggle_linear(int p_index, TangentIndex p_tangent) {
  538. ERR_FAIL_COND(curve.is_null());
  539. ERR_FAIL_INDEX_MSG(p_index, curve->get_point_count(), "Curve point is out of bounds.");
  540. if (p_tangent == TANGENT_NONE) {
  541. return;
  542. }
  543. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  544. undo_redo->create_action(TTR("Toggle Linear Curve Point's Tangent"));
  545. Curve::TangentMode prev_mode = (p_tangent == TANGENT_LEFT) ? curve->get_point_left_mode(p_index) : curve->get_point_right_mode(p_index);
  546. Curve::TangentMode mode = (prev_mode == Curve::TANGENT_LINEAR) ? Curve::TANGENT_FREE : Curve::TANGENT_LINEAR;
  547. float prev_angle = (p_tangent == TANGENT_LEFT) ? curve->get_point_left_tangent(p_index) : curve->get_point_right_tangent(p_index);
  548. // Add different methods in the UndoRedo based on the tangent passed.
  549. if (p_tangent == TANGENT_LEFT) {
  550. undo_redo->add_do_method(*curve, "set_point_left_mode", p_index, mode);
  551. undo_redo->add_undo_method(*curve, "set_point_left_mode", p_index, prev_mode);
  552. undo_redo->add_undo_method(*curve, "set_point_left_tangent", p_index, prev_angle);
  553. } else {
  554. undo_redo->add_do_method(*curve, "set_point_right_mode", p_index, mode);
  555. undo_redo->add_undo_method(*curve, "set_point_right_mode", p_index, prev_mode);
  556. undo_redo->add_undo_method(*curve, "set_point_right_tangent", p_index, prev_angle);
  557. }
  558. undo_redo->commit_action();
  559. }
  560. void CurveEdit::set_selected_index(int p_index) {
  561. if (p_index != selected_index) {
  562. selected_index = p_index;
  563. queue_redraw();
  564. }
  565. }
  566. void CurveEdit::update_view_transform() {
  567. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  568. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  569. const real_t margin = font->get_height(font_size) + 2 * EDSCALE;
  570. float min_x = curve.is_valid() ? curve->get_min_domain() : 0.0;
  571. float max_x = curve.is_valid() ? curve->get_max_domain() : 1.0;
  572. float min_y = curve.is_valid() ? curve->get_min_value() : 0.0;
  573. float max_y = curve.is_valid() ? curve->get_max_value() : 1.0;
  574. const Rect2 world_rect = Rect2(min_x, min_y, max_x - min_x, max_y - min_y);
  575. const Size2 view_margin(margin, margin);
  576. const Size2 view_size = get_size() - view_margin * 2;
  577. const Vector2 scale = view_size / world_rect.size;
  578. Transform2D world_trans;
  579. world_trans.translate_local(-world_rect.position - Vector2(0, world_rect.size.y));
  580. world_trans.scale(Vector2(scale.x, -scale.y));
  581. Transform2D view_trans;
  582. view_trans.translate_local(view_margin);
  583. _world_to_view = view_trans * world_trans;
  584. }
  585. Vector2 CurveEdit::get_tangent_view_pos(int p_index, TangentIndex p_tangent) const {
  586. Vector2 dir;
  587. if (p_tangent == TANGENT_LEFT) {
  588. dir = -Vector2(1, curve->get_point_left_tangent(p_index));
  589. } else {
  590. dir = Vector2(1, curve->get_point_right_tangent(p_index));
  591. }
  592. Vector2 point_pos = curve->get_point_position(p_index);
  593. Vector2 point_view_pos = get_view_pos(point_pos);
  594. Vector2 control_view_pos = get_view_pos(point_pos + dir);
  595. Vector2 distance_from_point = tangent_length * (control_view_pos - point_view_pos).normalized();
  596. Vector2 tangent_view_pos = point_view_pos + distance_from_point;
  597. // Since the tangent is long, it might slip outside of the area of the editor for points close to the domain/range boundaries.
  598. // The code below shrinks the tangent control by up to 50% so it always stays inside the editor for points within the bounds.
  599. float fraction_inside = 1.0;
  600. if (distance_from_point.x != 0.0) {
  601. fraction_inside = MIN(fraction_inside, ((distance_from_point.x > 0 ? get_rect().size.x : 0) - point_view_pos.x) / distance_from_point.x);
  602. }
  603. if (distance_from_point.y != 0.0) {
  604. fraction_inside = MIN(fraction_inside, ((distance_from_point.y > 0 ? get_rect().size.y : 0) - point_view_pos.y) / distance_from_point.y);
  605. }
  606. if (fraction_inside < 1.0 && fraction_inside > 0.5) {
  607. tangent_view_pos = point_view_pos + distance_from_point * fraction_inside;
  608. }
  609. return tangent_view_pos;
  610. }
  611. Vector2 CurveEdit::get_view_pos(const Vector2 &p_world_pos) const {
  612. return _world_to_view.xform(p_world_pos);
  613. }
  614. Vector2 CurveEdit::get_world_pos(const Vector2 &p_view_pos) const {
  615. return _world_to_view.affine_inverse().xform(p_view_pos);
  616. }
  617. // Uses non-baked points, but takes advantage of ordered iteration to be faster.
  618. void CurveEdit::plot_curve_accurate(float p_step, const Color &p_line_color, const Color &p_edge_line_color) {
  619. const real_t min_x = curve->get_min_domain();
  620. const real_t max_x = curve->get_max_domain();
  621. if (curve->get_point_count() <= 1) { // Draw single line through entire plot.
  622. real_t y = curve->sample(0);
  623. draw_line(get_view_pos(Vector2(min_x, y)) + Vector2(0.5, 0), get_view_pos(Vector2(max_x, y)) - Vector2(1.5, 0), p_line_color, LINE_WIDTH, true);
  624. return;
  625. }
  626. Vector2 first_point = curve->get_point_position(0);
  627. Vector2 last_point = curve->get_point_position(curve->get_point_count() - 1);
  628. // Transform pixels-per-step into curve domain. Only works for non-rotated transforms.
  629. const float world_step_size = p_step / _world_to_view.get_scale().x;
  630. // Edge lines.
  631. draw_line(get_view_pos(Vector2(min_x, first_point.y)) + Vector2(0.5, 0), get_view_pos(first_point), p_edge_line_color, LINE_WIDTH, true);
  632. draw_line(get_view_pos(last_point), get_view_pos(Vector2(max_x, last_point.y)) - Vector2(1.5, 0), p_edge_line_color, LINE_WIDTH, true);
  633. // Draw section by section, so that we get maximum precision near points.
  634. // It's an accurate representation, but slower than using the baked one.
  635. for (int i = 1; i < curve->get_point_count(); ++i) {
  636. Vector2 a = curve->get_point_position(i - 1);
  637. Vector2 b = curve->get_point_position(i);
  638. Vector2 pos = a;
  639. Vector2 prev_pos = a;
  640. float samples = (b.x - a.x) / world_step_size;
  641. for (int j = 1; j < samples; j++) {
  642. float x = j * world_step_size;
  643. pos.x = a.x + x;
  644. pos.y = curve->sample_local_nocheck(i - 1, x);
  645. draw_line(get_view_pos(prev_pos), get_view_pos(pos), p_line_color, LINE_WIDTH, true);
  646. prev_pos = pos;
  647. }
  648. draw_line(get_view_pos(prev_pos), get_view_pos(b), p_line_color, LINE_WIDTH, true);
  649. }
  650. }
  651. void CurveEdit::_redraw() {
  652. if (curve.is_null()) {
  653. return;
  654. }
  655. update_view_transform();
  656. // Draw background.
  657. Vector2 view_size = get_rect().size;
  658. draw_style_box(get_theme_stylebox(SceneStringName(panel), SNAME("Tree")), Rect2(Point2(), view_size));
  659. // Draw primary grid.
  660. draw_set_transform_matrix(_world_to_view);
  661. Vector2 min_edge = get_world_pos(Vector2(0, view_size.y));
  662. Vector2 max_edge = get_world_pos(Vector2(view_size.x, 0));
  663. const Color grid_color_primary = get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.25);
  664. const Color grid_color = get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.1);
  665. const Vector2i grid_steps = Vector2i(4, 2);
  666. const Vector2 step_size = Vector2(curve->get_domain_range(), curve->get_value_range()) / grid_steps;
  667. draw_line(Vector2(min_edge.x, curve->get_min_value()), Vector2(max_edge.x, curve->get_min_value()), grid_color_primary);
  668. draw_line(Vector2(max_edge.x, curve->get_max_value()), Vector2(min_edge.x, curve->get_max_value()), grid_color_primary);
  669. draw_line(Vector2(curve->get_min_domain(), min_edge.y), Vector2(curve->get_min_domain(), max_edge.y), grid_color_primary);
  670. draw_line(Vector2(curve->get_max_domain(), max_edge.y), Vector2(curve->get_max_domain(), min_edge.y), grid_color_primary);
  671. for (int i = 1; i < grid_steps.x; i++) {
  672. real_t x = curve->get_min_domain() + i * step_size.x;
  673. draw_line(Vector2(x, min_edge.y), Vector2(x, max_edge.y), grid_color);
  674. }
  675. for (int i = 1; i < grid_steps.y; i++) {
  676. real_t y = curve->get_min_value() + i * step_size.y;
  677. draw_line(Vector2(min_edge.x, y), Vector2(max_edge.x, y), grid_color);
  678. }
  679. // Draw number markings.
  680. draw_set_transform_matrix(Transform2D());
  681. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  682. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  683. float font_height = font->get_height(font_size);
  684. Color text_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
  685. int pad = Math::round(2 * EDSCALE);
  686. for (int i = 0; i <= grid_steps.x; ++i) {
  687. real_t x = curve->get_min_domain() + i * step_size.x;
  688. draw_string(font, get_view_pos(Vector2(x, curve->get_min_value())) + Vector2(pad, font_height - pad), String::num(x, 2), HORIZONTAL_ALIGNMENT_CENTER, -1, font_size, text_color);
  689. }
  690. for (int i = 0; i <= grid_steps.y; ++i) {
  691. real_t y = curve->get_min_value() + i * step_size.y;
  692. draw_string(font, get_view_pos(Vector2(curve->get_min_domain(), y)) + Vector2(pad, -pad), String::num(y, 2), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, text_color);
  693. }
  694. // Draw curve in view coordinates. Curve world-to-view point conversion happens in plot_curve_accurate().
  695. const Color line_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
  696. const Color edge_line_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor)) * Color(1, 1, 1, 0.75);
  697. plot_curve_accurate(STEP_SIZE, line_color, edge_line_color);
  698. // Draw points, except for the selected one.
  699. bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  700. const Color point_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
  701. for (int i = 0; i < curve->get_point_count(); ++i) {
  702. Vector2 pos = get_view_pos(curve->get_point_position(i));
  703. if (selected_index != i) {
  704. draw_rect(Rect2(pos, Vector2(0, 0)).grow(point_radius), point_color);
  705. }
  706. if (hovered_index == i && hovered_tangent_index == TANGENT_NONE) {
  707. draw_rect(Rect2(pos, Vector2(0, 0)).grow(hover_radius - Math::round(3 * EDSCALE)), line_color, false, Math::round(1 * EDSCALE));
  708. }
  709. }
  710. // Draw selected point and its tangents.
  711. if (selected_index >= 0) {
  712. const Vector2 point_pos = curve->get_point_position(selected_index);
  713. const Color selected_point_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  714. // Draw tangents if not dragging a point, or if holding a point without having moved it yet.
  715. if (grabbing == GRAB_NONE || initial_grab_pos == point_pos || selected_tangent_index != TANGENT_NONE) {
  716. const Color selected_tangent_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor)).darkened(0.25);
  717. const Color tangent_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor)).darkened(0.25);
  718. if (selected_index != 0) {
  719. Vector2 control_pos = get_tangent_view_pos(selected_index, TANGENT_LEFT);
  720. Color left_tangent_color = (selected_tangent_index == TANGENT_LEFT) ? selected_tangent_color : tangent_color;
  721. draw_line(get_view_pos(point_pos), control_pos, left_tangent_color, 0.5 * EDSCALE, true);
  722. // Square for linear mode, circle otherwise.
  723. if (curve->get_point_left_mode(selected_index) == Curve::TANGENT_FREE) {
  724. draw_circle(control_pos, tangent_radius, left_tangent_color);
  725. } else {
  726. draw_rect(Rect2(control_pos, Vector2(0, 0)).grow(tangent_radius), left_tangent_color);
  727. }
  728. // Hover indicator.
  729. if (hovered_tangent_index == TANGENT_LEFT || (hovered_tangent_index == TANGENT_RIGHT && !shift_pressed && curve->get_point_left_mode(selected_index) != Curve::TANGENT_LINEAR)) {
  730. draw_rect(Rect2(control_pos, Vector2(0, 0)).grow(tangent_hover_radius - Math::round(3 * EDSCALE)), tangent_color, false, Math::round(1 * EDSCALE));
  731. }
  732. }
  733. if (selected_index != curve->get_point_count() - 1) {
  734. Vector2 control_pos = get_tangent_view_pos(selected_index, TANGENT_RIGHT);
  735. Color right_tangent_color = (selected_tangent_index == TANGENT_RIGHT) ? selected_tangent_color : tangent_color;
  736. draw_line(get_view_pos(point_pos), control_pos, right_tangent_color, 0.5 * EDSCALE, true);
  737. // Square for linear mode, circle otherwise.
  738. if (curve->get_point_right_mode(selected_index) == Curve::TANGENT_FREE) {
  739. draw_circle(control_pos, tangent_radius, right_tangent_color);
  740. } else {
  741. draw_rect(Rect2(control_pos, Vector2(0, 0)).grow(tangent_radius), right_tangent_color);
  742. }
  743. // Hover indicator.
  744. if (hovered_tangent_index == TANGENT_RIGHT || (hovered_tangent_index == TANGENT_LEFT && !shift_pressed && curve->get_point_right_mode(selected_index) != Curve::TANGENT_LINEAR)) {
  745. draw_rect(Rect2(control_pos, Vector2(0, 0)).grow(tangent_hover_radius - Math::round(3 * EDSCALE)), tangent_color, false, Math::round(1 * EDSCALE));
  746. }
  747. }
  748. }
  749. draw_rect(Rect2(get_view_pos(point_pos), Vector2(0, 0)).grow(point_radius), selected_point_color);
  750. }
  751. // Draw help text.
  752. if (selected_index > 0 && selected_index < curve->get_point_count() - 1 && selected_tangent_index == TANGENT_NONE && hovered_tangent_index != TANGENT_NONE && !shift_pressed) {
  753. float width = view_size.x - 50 * EDSCALE;
  754. text_color.a *= 0.4;
  755. draw_multiline_string(font, Vector2(25 * EDSCALE, font_height - Math::round(2 * EDSCALE)), TTR("Hold Shift to edit tangents individually"), HORIZONTAL_ALIGNMENT_CENTER, width, font_size, -1, text_color);
  756. } else if (selected_index != -1 && selected_tangent_index == TANGENT_NONE) {
  757. const Vector2 point_pos = curve->get_point_position(selected_index);
  758. float width = view_size.x - 50 * EDSCALE;
  759. text_color.a *= 0.8;
  760. draw_string(font, Vector2(25 * EDSCALE, font_height - Math::round(2 * EDSCALE)), vformat("(%.2f, %.2f)", point_pos.x, point_pos.y), HORIZONTAL_ALIGNMENT_CENTER, width, font_size, text_color);
  761. } else if (selected_index != -1 && selected_tangent_index != TANGENT_NONE) {
  762. float width = view_size.x - 50 * EDSCALE;
  763. text_color.a *= 0.8;
  764. real_t theta = Math::rad_to_deg(Math::atan(selected_tangent_index == TANGENT_LEFT ? -1 * curve->get_point_left_tangent(selected_index) : curve->get_point_right_tangent(selected_index)));
  765. draw_string(font, Vector2(25 * EDSCALE, font_height - Math::round(2 * EDSCALE)), String::num(theta, 1) + String::utf8(" °"), HORIZONTAL_ALIGNMENT_CENTER, width, font_size, text_color);
  766. }
  767. // Draw temporary constraints and snapping axes.
  768. draw_set_transform_matrix(_world_to_view);
  769. if (Input::get_singleton()->is_key_pressed(Key::ALT) && grabbing != GRAB_NONE && selected_tangent_index == TANGENT_NONE) {
  770. float prev_point_offset = (selected_index > 0) ? curve->get_point_position(selected_index - 1).x : curve->get_min_domain();
  771. float next_point_offset = (selected_index < curve->get_point_count() - 1) ? curve->get_point_position(selected_index + 1).x : curve->get_max_domain();
  772. draw_line(Vector2(prev_point_offset, curve->get_min_value()), Vector2(prev_point_offset, curve->get_max_value()), Color(point_color, 0.6));
  773. draw_line(Vector2(next_point_offset, curve->get_min_value()), Vector2(next_point_offset, curve->get_max_value()), Color(point_color, 0.6));
  774. }
  775. if (shift_pressed && grabbing != GRAB_NONE && selected_tangent_index == TANGENT_NONE) {
  776. draw_line(Vector2(initial_grab_pos.x, curve->get_min_value()), Vector2(initial_grab_pos.x, curve->get_max_value()), get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor)).darkened(0.4));
  777. draw_line(Vector2(curve->get_min_domain(), initial_grab_pos.y), Vector2(curve->get_max_domain(), initial_grab_pos.y), get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor)).darkened(0.4));
  778. }
  779. }
  780. ///////////////////////
  781. const int CurveEditor::DEFAULT_SNAP = 10;
  782. void CurveEditor::_set_snap_enabled(bool p_enabled) {
  783. curve_editor_rect->set_snap_enabled(p_enabled);
  784. snap_count_edit->set_visible(p_enabled);
  785. }
  786. void CurveEditor::_set_snap_count(int p_snap_count) {
  787. curve_editor_rect->set_snap_count(CLAMP(p_snap_count, 2, 100));
  788. }
  789. void CurveEditor::_on_preset_item_selected(int p_preset_id) {
  790. curve_editor_rect->use_preset(p_preset_id);
  791. }
  792. void CurveEditor::set_curve(const Ref<Curve> &p_curve) {
  793. curve_editor_rect->set_curve(p_curve);
  794. }
  795. void CurveEditor::_notification(int p_what) {
  796. switch (p_what) {
  797. case NOTIFICATION_THEME_CHANGED: {
  798. spacing = Math::round(BASE_SPACING * get_theme_default_base_scale());
  799. snap_button->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));
  800. PopupMenu *p = presets_button->get_popup();
  801. p->clear();
  802. p->add_icon_item(get_editor_theme_icon(SNAME("CurveConstant")), TTR("Constant"), CurveEdit::PRESET_CONSTANT);
  803. p->add_icon_item(get_editor_theme_icon(SNAME("CurveLinear")), TTR("Linear"), CurveEdit::PRESET_LINEAR);
  804. p->add_icon_item(get_editor_theme_icon(SNAME("CurveIn")), TTR("Ease In"), CurveEdit::PRESET_EASE_IN);
  805. p->add_icon_item(get_editor_theme_icon(SNAME("CurveOut")), TTR("Ease Out"), CurveEdit::PRESET_EASE_OUT);
  806. p->add_icon_item(get_editor_theme_icon(SNAME("CurveInOut")), TTR("Smoothstep"), CurveEdit::PRESET_SMOOTHSTEP);
  807. } break;
  808. case NOTIFICATION_READY: {
  809. Ref<Curve> curve = curve_editor_rect->get_curve();
  810. if (curve.is_valid()) {
  811. // Set snapping settings based on the curve's meta.
  812. snap_button->set_pressed(curve->get_meta("_snap_enabled", false));
  813. snap_count_edit->set_value(curve->get_meta("_snap_count", DEFAULT_SNAP));
  814. }
  815. } break;
  816. case NOTIFICATION_RESIZED:
  817. curve_editor_rect->update_minimum_size();
  818. break;
  819. }
  820. }
  821. CurveEditor::CurveEditor() {
  822. HFlowContainer *toolbar = memnew(HFlowContainer);
  823. add_child(toolbar);
  824. snap_button = memnew(Button);
  825. snap_button->set_tooltip_text(TTR("Toggle Grid Snap"));
  826. snap_button->set_toggle_mode(true);
  827. toolbar->add_child(snap_button);
  828. snap_button->connect(SceneStringName(toggled), callable_mp(this, &CurveEditor::_set_snap_enabled));
  829. toolbar->add_child(memnew(VSeparator));
  830. snap_count_edit = memnew(EditorSpinSlider);
  831. snap_count_edit->set_min(2);
  832. snap_count_edit->set_max(100);
  833. snap_count_edit->set_value(DEFAULT_SNAP);
  834. snap_count_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  835. toolbar->add_child(snap_count_edit);
  836. snap_count_edit->connect(SceneStringName(value_changed), callable_mp(this, &CurveEditor::_set_snap_count));
  837. presets_button = memnew(MenuButton);
  838. presets_button->set_text(TTR("Presets"));
  839. presets_button->set_switch_on_hover(true);
  840. presets_button->set_h_size_flags(SIZE_EXPAND | SIZE_SHRINK_END);
  841. toolbar->add_child(presets_button);
  842. presets_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &CurveEditor::_on_preset_item_selected));
  843. curve_editor_rect = memnew(CurveEdit);
  844. add_child(curve_editor_rect);
  845. // Some empty space below. Not a part of the curve editor so it can't draw in it.
  846. Control *empty_space = memnew(Control);
  847. empty_space->set_custom_minimum_size(Vector2(0, spacing));
  848. add_child(empty_space);
  849. set_mouse_filter(MOUSE_FILTER_STOP);
  850. _set_snap_enabled(snap_button->is_pressed());
  851. _set_snap_count(snap_count_edit->get_value());
  852. }
  853. ///////////////////////
  854. bool EditorInspectorPluginCurve::can_handle(Object *p_object) {
  855. return Object::cast_to<Curve>(p_object) != nullptr;
  856. }
  857. void EditorInspectorPluginCurve::parse_begin(Object *p_object) {
  858. Curve *curve = Object::cast_to<Curve>(p_object);
  859. ERR_FAIL_NULL(curve);
  860. Ref<Curve> c(curve);
  861. CurveEditor *editor = memnew(CurveEditor);
  862. editor->set_curve(c);
  863. add_custom_control(editor);
  864. }
  865. CurveEditorPlugin::CurveEditorPlugin() {
  866. Ref<EditorInspectorPluginCurve> plugin;
  867. plugin.instantiate();
  868. add_inspector_plugin(plugin);
  869. EditorInterface::get_singleton()->get_resource_previewer()->add_preview_generator(memnew(CurvePreviewGenerator));
  870. }
  871. ///////////////////////
  872. bool CurvePreviewGenerator::handles(const String &p_type) const {
  873. return p_type == "Curve";
  874. }
  875. Ref<Texture2D> CurvePreviewGenerator::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  876. Ref<Curve> curve = p_from;
  877. if (curve.is_null()) {
  878. return Ref<Texture2D>();
  879. }
  880. Ref<Image> img_ref;
  881. img_ref.instantiate();
  882. Image &im = **img_ref;
  883. im.initialize_data(p_size.x, p_size.y, false, Image::FORMAT_RGBA8);
  884. Color line_color = EditorInterface::get_singleton()->get_editor_theme()->get_color(SceneStringName(font_color), EditorStringName(Editor));
  885. // Set the first pixel of the thumbnail.
  886. float v = (curve->sample_baked(curve->get_min_domain()) - curve->get_min_value()) / curve->get_value_range();
  887. int y = CLAMP(im.get_height() - v * im.get_height(), 0, im.get_height() - 1);
  888. im.set_pixel(0, y, line_color);
  889. // Plot a line towards the next point.
  890. int prev_y = y;
  891. for (int x = 1; x < im.get_width(); ++x) {
  892. float t = static_cast<float>(x) / im.get_width() * curve->get_domain_range() + curve->get_min_domain();
  893. v = (curve->sample_baked(t) - curve->get_min_value()) / curve->get_value_range();
  894. y = CLAMP(im.get_height() - v * im.get_height(), 0, im.get_height() - 1);
  895. Vector<Point2i> points = Geometry2D::bresenham_line(Point2i(x - 1, prev_y), Point2i(x, y));
  896. for (Point2i point : points) {
  897. im.set_pixelv(point, line_color);
  898. }
  899. prev_y = y;
  900. }
  901. return ImageTexture::create_from_image(img_ref);
  902. }