curve_editor_plugin.cpp 43 KB

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