curve_editor_plugin.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*************************************************************************/
  2. /* curve_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "curve_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/core_string_names.h"
  33. #include "core/os/input.h"
  34. #include "core/os/keyboard.h"
  35. CurveEditor::CurveEditor() {
  36. _selected_point = -1;
  37. _hover_point = -1;
  38. _selected_tangent = TANGENT_NONE;
  39. _hover_radius = 6;
  40. _tangents_length = 40;
  41. _dragging = false;
  42. _has_undo_data = false;
  43. set_focus_mode(FOCUS_ALL);
  44. set_clip_contents(true);
  45. _context_menu = memnew(PopupMenu);
  46. _context_menu->connect("id_pressed", this, "_on_context_menu_item_selected");
  47. add_child(_context_menu);
  48. _presets_menu = memnew(PopupMenu);
  49. _presets_menu->set_name("_presets_menu");
  50. _presets_menu->add_item(TTR("Flat0"), PRESET_FLAT0);
  51. _presets_menu->add_item(TTR("Flat1"), PRESET_FLAT1);
  52. _presets_menu->add_item(TTR("Linear"), PRESET_LINEAR);
  53. _presets_menu->add_item(TTR("Ease in"), PRESET_EASE_IN);
  54. _presets_menu->add_item(TTR("Ease out"), PRESET_EASE_OUT);
  55. _presets_menu->add_item(TTR("Smoothstep"), PRESET_SMOOTHSTEP);
  56. _presets_menu->connect("id_pressed", this, "_on_preset_item_selected");
  57. _context_menu->add_child(_presets_menu);
  58. }
  59. void CurveEditor::set_curve(Ref<Curve> curve) {
  60. if (curve == _curve_ref)
  61. return;
  62. if (_curve_ref.is_valid()) {
  63. _curve_ref->disconnect(CoreStringNames::get_singleton()->changed, this, "_curve_changed");
  64. _curve_ref->disconnect(Curve::SIGNAL_RANGE_CHANGED, this, "_curve_changed");
  65. }
  66. _curve_ref = curve;
  67. if (_curve_ref.is_valid()) {
  68. _curve_ref->connect(CoreStringNames::get_singleton()->changed, this, "_curve_changed");
  69. _curve_ref->connect(Curve::SIGNAL_RANGE_CHANGED, this, "_curve_changed");
  70. }
  71. _selected_point = -1;
  72. _hover_point = -1;
  73. _selected_tangent = TANGENT_NONE;
  74. update();
  75. // Note: if you edit a curve, then set another, and try to undo,
  76. // it will normally apply on the previous curve, but you won't see it
  77. }
  78. Size2 CurveEditor::get_minimum_size() const {
  79. return Vector2(64, 150) * EDSCALE;
  80. }
  81. void CurveEditor::_notification(int p_what) {
  82. if (p_what == NOTIFICATION_DRAW)
  83. _draw();
  84. }
  85. void CurveEditor::on_gui_input(const Ref<InputEvent> &p_event) {
  86. Ref<InputEventMouseButton> mb_ref = p_event;
  87. if (mb_ref.is_valid()) {
  88. const InputEventMouseButton &mb = **mb_ref;
  89. if (mb.is_pressed() && !_dragging) {
  90. Vector2 mpos = mb.get_position();
  91. _selected_tangent = get_tangent_at(mpos);
  92. if (_selected_tangent == TANGENT_NONE)
  93. set_selected_point(get_point_at(mpos));
  94. switch (mb.get_button_index()) {
  95. case BUTTON_RIGHT:
  96. _context_click_pos = mpos;
  97. open_context_menu(get_global_transform().xform(mpos));
  98. break;
  99. case BUTTON_MIDDLE:
  100. remove_point(_hover_point);
  101. break;
  102. case BUTTON_LEFT:
  103. _dragging = true;
  104. break;
  105. }
  106. }
  107. if (!mb.is_pressed() && _dragging && mb.get_button_index() == BUTTON_LEFT) {
  108. _dragging = false;
  109. if (_has_undo_data) {
  110. UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
  111. ur.create_action(_selected_tangent == TANGENT_NONE ? TTR("Modify Curve Point") : TTR("Modify Curve Tangent"));
  112. ur.add_do_method(*_curve_ref, "_set_data", _curve_ref->get_data());
  113. ur.add_undo_method(*_curve_ref, "_set_data", _undo_data);
  114. // Note: this will trigger one more "changed" signal even if nothing changes,
  115. // but it's ok since it would have fired every frame during the drag anyways
  116. ur.commit_action();
  117. _has_undo_data = false;
  118. }
  119. }
  120. }
  121. Ref<InputEventMouseMotion> mm_ref = p_event;
  122. if (mm_ref.is_valid()) {
  123. const InputEventMouseMotion &mm = **mm_ref;
  124. Vector2 mpos = mm.get_position();
  125. if (_dragging && _curve_ref.is_valid()) {
  126. Curve &curve = **_curve_ref;
  127. if (_selected_point != -1) {
  128. if (!_has_undo_data) {
  129. // Save full curve state before dragging points,
  130. // because this operation can modify their order
  131. _undo_data = curve.get_data();
  132. _has_undo_data = true;
  133. }
  134. if (_selected_tangent == TANGENT_NONE) {
  135. // Drag point
  136. Vector2 point_pos = get_world_pos(mpos);
  137. int i = curve.set_point_offset(_selected_point, point_pos.x);
  138. // The index may change if the point is dragged across another one
  139. set_hover_point_index(i);
  140. set_selected_point(i);
  141. // This is to prevent the user from losing a point out of view.
  142. if (point_pos.y < curve.get_min_value())
  143. point_pos.y = curve.get_min_value();
  144. else if (point_pos.y > curve.get_max_value())
  145. point_pos.y = curve.get_max_value();
  146. curve.set_point_value(_selected_point, point_pos.y);
  147. } else {
  148. // Drag tangent
  149. Vector2 point_pos = curve.get_point_position(_selected_point);
  150. Vector2 control_pos = get_world_pos(mpos);
  151. Vector2 dir = (control_pos - point_pos).normalized();
  152. real_t tangent;
  153. if (Math::abs(dir.x) > CMP_EPSILON)
  154. tangent = dir.y / dir.x;
  155. else
  156. tangent = 9999 * (dir.y >= 0 ? 1 : -1);
  157. bool link = !Input::get_singleton()->is_key_pressed(KEY_SHIFT);
  158. if (_selected_tangent == TANGENT_LEFT) {
  159. curve.set_point_left_tangent(_selected_point, tangent);
  160. // Note: if a tangent is set to linear, it shouldn't be linked to the other
  161. if (link && _selected_point != (curve.get_point_count() - 1) && curve.get_point_right_mode(_selected_point) != Curve::TANGENT_LINEAR)
  162. curve.set_point_right_tangent(_selected_point, tangent);
  163. } else {
  164. curve.set_point_right_tangent(_selected_point, tangent);
  165. if (link && _selected_point != 0 && curve.get_point_left_mode(_selected_point) != Curve::TANGENT_LINEAR)
  166. curve.set_point_left_tangent(_selected_point, tangent);
  167. }
  168. }
  169. }
  170. } else {
  171. set_hover_point_index(get_point_at(mpos));
  172. }
  173. }
  174. Ref<InputEventKey> key_ref = p_event;
  175. if (key_ref.is_valid()) {
  176. const InputEventKey &key = **key_ref;
  177. if (key.is_pressed() && _selected_point != -1) {
  178. if (key.get_scancode() == KEY_DELETE)
  179. remove_point(_selected_point);
  180. }
  181. }
  182. }
  183. void CurveEditor::on_preset_item_selected(int preset_id) {
  184. ERR_FAIL_COND(preset_id < 0 || preset_id >= PRESET_COUNT);
  185. ERR_FAIL_COND(_curve_ref.is_null());
  186. Curve &curve = **_curve_ref;
  187. Array previous_data = curve.get_data();
  188. curve.clear_points();
  189. switch (preset_id) {
  190. case PRESET_FLAT0:
  191. curve.add_point(Vector2(0, 0));
  192. curve.add_point(Vector2(1, 0));
  193. curve.set_point_right_mode(0, Curve::TANGENT_LINEAR);
  194. curve.set_point_left_mode(1, Curve::TANGENT_LINEAR);
  195. break;
  196. case PRESET_FLAT1:
  197. curve.add_point(Vector2(0, 1));
  198. curve.add_point(Vector2(1, 1));
  199. curve.set_point_right_mode(0, Curve::TANGENT_LINEAR);
  200. curve.set_point_left_mode(1, Curve::TANGENT_LINEAR);
  201. break;
  202. case PRESET_LINEAR:
  203. curve.add_point(Vector2(0, 0));
  204. curve.add_point(Vector2(1, 1));
  205. curve.set_point_right_mode(0, Curve::TANGENT_LINEAR);
  206. curve.set_point_left_mode(1, Curve::TANGENT_LINEAR);
  207. break;
  208. case PRESET_EASE_IN:
  209. curve.add_point(Vector2(0, 0));
  210. curve.add_point(Vector2(1, 1), (curve.get_max_value() - curve.get_min_value()) * 1.4, 0);
  211. break;
  212. case PRESET_EASE_OUT:
  213. curve.add_point(Vector2(0, 0), 0, (curve.get_max_value() - curve.get_min_value()) * 1.4);
  214. curve.add_point(Vector2(1, 1));
  215. break;
  216. case PRESET_SMOOTHSTEP:
  217. curve.add_point(Vector2(0, 0));
  218. curve.add_point(Vector2(1, 1));
  219. break;
  220. default:
  221. break;
  222. }
  223. UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
  224. ur.create_action(TTR("Load Curve Preset"));
  225. ur.add_do_method(&curve, "_set_data", curve.get_data());
  226. ur.add_undo_method(&curve, "_set_data", previous_data);
  227. ur.commit_action();
  228. }
  229. void CurveEditor::_curve_changed() {
  230. update();
  231. // Point count can change in case of undo
  232. if (_selected_point >= _curve_ref->get_point_count()) {
  233. set_selected_point(-1);
  234. }
  235. }
  236. void CurveEditor::on_context_menu_item_selected(int action_id) {
  237. switch (action_id) {
  238. case CONTEXT_ADD_POINT:
  239. add_point(_context_click_pos);
  240. break;
  241. case CONTEXT_REMOVE_POINT:
  242. remove_point(_selected_point);
  243. break;
  244. case CONTEXT_LINEAR:
  245. toggle_linear();
  246. break;
  247. case CONTEXT_LEFT_LINEAR:
  248. toggle_linear(TANGENT_LEFT);
  249. break;
  250. case CONTEXT_RIGHT_LINEAR:
  251. toggle_linear(TANGENT_RIGHT);
  252. break;
  253. }
  254. }
  255. void CurveEditor::open_context_menu(Vector2 pos) {
  256. _context_menu->set_position(pos);
  257. _context_menu->clear();
  258. if (_curve_ref.is_valid()) {
  259. _context_menu->add_item(TTR("Add point"), CONTEXT_ADD_POINT);
  260. if (_selected_point >= 0) {
  261. _context_menu->add_item(TTR("Remove point"), CONTEXT_REMOVE_POINT);
  262. if (_selected_tangent != TANGENT_NONE) {
  263. _context_menu->add_separator();
  264. _context_menu->add_check_item(TTR("Linear"), CONTEXT_LINEAR);
  265. bool is_linear = _selected_tangent == TANGENT_LEFT ?
  266. _curve_ref->get_point_left_mode(_selected_point) == Curve::TANGENT_LINEAR :
  267. _curve_ref->get_point_right_mode(_selected_point) == Curve::TANGENT_LINEAR;
  268. _context_menu->set_item_checked(_context_menu->get_item_index(CONTEXT_LINEAR), is_linear);
  269. } else {
  270. if (_selected_point > 0 || _selected_point + 1 < _curve_ref->get_point_count())
  271. _context_menu->add_separator();
  272. if (_selected_point > 0) {
  273. _context_menu->add_check_item(TTR("Left linear"), CONTEXT_LEFT_LINEAR);
  274. _context_menu->set_item_checked(_context_menu->get_item_index(CONTEXT_LEFT_LINEAR),
  275. _curve_ref->get_point_left_mode(_selected_point) == Curve::TANGENT_LINEAR);
  276. }
  277. if (_selected_point + 1 < _curve_ref->get_point_count()) {
  278. _context_menu->add_check_item(TTR("Right linear"), CONTEXT_RIGHT_LINEAR);
  279. _context_menu->set_item_checked(_context_menu->get_item_index(CONTEXT_RIGHT_LINEAR),
  280. _curve_ref->get_point_right_mode(_selected_point) == Curve::TANGENT_LINEAR);
  281. }
  282. }
  283. }
  284. _context_menu->add_separator();
  285. }
  286. _context_menu->add_submenu_item(TTR("Load preset"), _presets_menu->get_name());
  287. _context_menu->set_size(Size2(0, 0));
  288. _context_menu->popup();
  289. }
  290. int CurveEditor::get_point_at(Vector2 pos) const {
  291. if (_curve_ref.is_null())
  292. return -1;
  293. const Curve &curve = **_curve_ref;
  294. const float r = _hover_radius * _hover_radius;
  295. for (int i = 0; i < curve.get_point_count(); ++i) {
  296. Vector2 p = get_view_pos(curve.get_point_position(i));
  297. if (p.distance_squared_to(pos) <= r) {
  298. return i;
  299. }
  300. }
  301. return -1;
  302. }
  303. CurveEditor::TangentIndex CurveEditor::get_tangent_at(Vector2 pos) const {
  304. if (_curve_ref.is_null() || _selected_point < 0)
  305. return TANGENT_NONE;
  306. if (_selected_point != 0) {
  307. Vector2 control_pos = get_tangent_view_pos(_selected_point, TANGENT_LEFT);
  308. if (control_pos.distance_to(pos) < _hover_radius) {
  309. return TANGENT_LEFT;
  310. }
  311. }
  312. if (_selected_point != _curve_ref->get_point_count() - 1) {
  313. Vector2 control_pos = get_tangent_view_pos(_selected_point, TANGENT_RIGHT);
  314. if (control_pos.distance_to(pos) < _hover_radius) {
  315. return TANGENT_RIGHT;
  316. }
  317. }
  318. return TANGENT_NONE;
  319. }
  320. void CurveEditor::add_point(Vector2 pos) {
  321. ERR_FAIL_COND(_curve_ref.is_null());
  322. UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
  323. ur.create_action(TTR("Remove Curve Point"));
  324. Vector2 point_pos = get_world_pos(pos);
  325. if (point_pos.y < 0.0)
  326. point_pos.y = 0.0;
  327. else if (point_pos.y > 1.0)
  328. point_pos.y = 1.0;
  329. // Small trick to get the point index to feed the undo method
  330. int i = _curve_ref->add_point(point_pos);
  331. _curve_ref->remove_point(i);
  332. ur.add_do_method(*_curve_ref, "add_point", point_pos);
  333. ur.add_undo_method(*_curve_ref, "remove_point", i);
  334. ur.commit_action();
  335. }
  336. void CurveEditor::remove_point(int index) {
  337. ERR_FAIL_COND(_curve_ref.is_null());
  338. UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
  339. ur.create_action(TTR("Remove Curve Point"));
  340. Curve::Point p = _curve_ref->get_point(index);
  341. ur.add_do_method(*_curve_ref, "remove_point", index);
  342. ur.add_undo_method(*_curve_ref, "add_point", p.pos, p.left_tangent, p.right_tangent, p.left_mode, p.right_mode);
  343. if (index == _selected_point)
  344. set_selected_point(-1);
  345. ur.commit_action();
  346. }
  347. void CurveEditor::toggle_linear(TangentIndex tangent) {
  348. ERR_FAIL_COND(_curve_ref.is_null());
  349. UndoRedo &ur = *EditorNode::get_singleton()->get_undo_redo();
  350. ur.create_action(TTR("Toggle Curve Linear Tangent"));
  351. if (tangent == TANGENT_NONE)
  352. tangent = _selected_tangent;
  353. if (tangent == TANGENT_LEFT) {
  354. bool is_linear = _curve_ref->get_point_left_mode(_selected_point) == Curve::TANGENT_LINEAR;
  355. Curve::TangentMode prev_mode = _curve_ref->get_point_left_mode(_selected_point);
  356. Curve::TangentMode mode = is_linear ? Curve::TANGENT_FREE : Curve::TANGENT_LINEAR;
  357. ur.add_do_method(*_curve_ref, "set_point_left_mode", _selected_point, mode);
  358. ur.add_undo_method(*_curve_ref, "set_point_left_mode", _selected_point, prev_mode);
  359. } else {
  360. bool is_linear = _curve_ref->get_point_right_mode(_selected_point) == Curve::TANGENT_LINEAR;
  361. Curve::TangentMode prev_mode = _curve_ref->get_point_right_mode(_selected_point);
  362. Curve::TangentMode mode = is_linear ? Curve::TANGENT_FREE : Curve::TANGENT_LINEAR;
  363. ur.add_do_method(*_curve_ref, "set_point_right_mode", _selected_point, mode);
  364. ur.add_undo_method(*_curve_ref, "set_point_right_mode", _selected_point, prev_mode);
  365. }
  366. ur.commit_action();
  367. }
  368. void CurveEditor::set_selected_point(int index) {
  369. if (index != _selected_point) {
  370. _selected_point = index;
  371. update();
  372. }
  373. }
  374. void CurveEditor::set_hover_point_index(int index) {
  375. if (index != _hover_point) {
  376. _hover_point = index;
  377. update();
  378. }
  379. }
  380. void CurveEditor::update_view_transform() {
  381. Vector2 control_size = get_size();
  382. const real_t margin = 24;
  383. float min_y = 0;
  384. float max_y = 1;
  385. if (_curve_ref.is_valid()) {
  386. min_y = _curve_ref->get_min_value();
  387. max_y = _curve_ref->get_max_value();
  388. }
  389. Rect2 world_rect = Rect2(Curve::MIN_X, min_y, Curve::MAX_X, max_y - min_y);
  390. Vector2 wm = Vector2(margin, margin) / control_size;
  391. wm.y *= (max_y - min_y);
  392. world_rect.position -= wm;
  393. world_rect.size += 2.0 * wm;
  394. _world_to_view = Transform2D();
  395. _world_to_view.translate(-world_rect.position - Vector2(0, world_rect.size.y));
  396. _world_to_view.scale(Vector2(control_size.x, -control_size.y) / world_rect.size);
  397. }
  398. Vector2 CurveEditor::get_tangent_view_pos(int i, TangentIndex tangent) const {
  399. Vector2 dir;
  400. if (tangent == TANGENT_LEFT)
  401. dir = -Vector2(1, _curve_ref->get_point_left_tangent(i));
  402. else
  403. dir = Vector2(1, _curve_ref->get_point_right_tangent(i));
  404. Vector2 point_pos = get_view_pos(_curve_ref->get_point_position(i));
  405. Vector2 control_pos = get_view_pos(_curve_ref->get_point_position(i) + dir);
  406. return point_pos + _tangents_length * (control_pos - point_pos).normalized();
  407. }
  408. Vector2 CurveEditor::get_view_pos(Vector2 world_pos) const {
  409. return _world_to_view.xform(world_pos);
  410. }
  411. Vector2 CurveEditor::get_world_pos(Vector2 view_pos) const {
  412. return _world_to_view.affine_inverse().xform(view_pos);
  413. }
  414. // Uses non-baked points, but takes advantage of ordered iteration to be faster
  415. template <typename T>
  416. static void plot_curve_accurate(const Curve &curve, float step, T plot_func) {
  417. if (curve.get_point_count() <= 1) {
  418. // Not enough points to make a curve, so it's just a straight line
  419. float y = curve.interpolate(0);
  420. plot_func(Vector2(0, y), Vector2(1.f, y), true);
  421. } else {
  422. Vector2 first_point = curve.get_point_position(0);
  423. Vector2 last_point = curve.get_point_position(curve.get_point_count() - 1);
  424. // Edge lines
  425. plot_func(Vector2(0, first_point.y), first_point, false);
  426. plot_func(Vector2(Curve::MAX_X, last_point.y), last_point, false);
  427. // Draw section by section, so that we get maximum precision near points.
  428. // It's an accurate representation, but slower than using the baked one.
  429. for (int i = 1; i < curve.get_point_count(); ++i) {
  430. Vector2 a = curve.get_point_position(i - 1);
  431. Vector2 b = curve.get_point_position(i);
  432. Vector2 pos = a;
  433. Vector2 prev_pos = a;
  434. float len = b.x - a.x;
  435. for (float x = step; x < len; x += step) {
  436. pos.x = a.x + x;
  437. pos.y = curve.interpolate_local_nocheck(i - 1, x);
  438. plot_func(prev_pos, pos, true);
  439. prev_pos = pos;
  440. }
  441. plot_func(prev_pos, b, true);
  442. }
  443. }
  444. }
  445. struct CanvasItemPlotCurve {
  446. CanvasItem &ci;
  447. Color color1;
  448. Color color2;
  449. CanvasItemPlotCurve(CanvasItem &p_ci, Color p_color1, Color p_color2) :
  450. ci(p_ci),
  451. color1(p_color1),
  452. color2(p_color2) {}
  453. void operator()(Vector2 pos0, Vector2 pos1, bool in_definition) {
  454. ci.draw_line(pos0, pos1, in_definition ? color1 : color2);
  455. }
  456. };
  457. void CurveEditor::_draw() {
  458. if (_curve_ref.is_null())
  459. return;
  460. Curve &curve = **_curve_ref;
  461. update_view_transform();
  462. // Background
  463. Vector2 view_size = get_rect().size;
  464. draw_style_box(get_stylebox("bg", "Tree"), Rect2(Point2(), view_size));
  465. // Grid
  466. draw_set_transform_matrix(_world_to_view);
  467. Vector2 min_edge = get_world_pos(Vector2(0, view_size.y));
  468. Vector2 max_edge = get_world_pos(Vector2(view_size.x, 0));
  469. const Color grid_color0 = Color(1.0, 1.0, 1.0, 0.15);
  470. const Color grid_color1 = Color(1.0, 1.0, 1.0, 0.07);
  471. draw_line(Vector2(min_edge.x, curve.get_min_value()), Vector2(max_edge.x, curve.get_min_value()), grid_color0);
  472. draw_line(Vector2(max_edge.x, curve.get_max_value()), Vector2(min_edge.x, curve.get_max_value()), grid_color0);
  473. draw_line(Vector2(0, min_edge.y), Vector2(0, max_edge.y), grid_color0);
  474. draw_line(Vector2(1, max_edge.y), Vector2(1, min_edge.y), grid_color0);
  475. float curve_height = (curve.get_max_value() - curve.get_min_value());
  476. const Vector2 grid_step(0.25, 0.5 * curve_height);
  477. for (real_t x = 0; x < 1.0; x += grid_step.x) {
  478. draw_line(Vector2(x, min_edge.y), Vector2(x, max_edge.y), grid_color1);
  479. }
  480. for (real_t y = curve.get_min_value(); y < curve.get_max_value(); y += grid_step.y) {
  481. draw_line(Vector2(min_edge.x, y), Vector2(max_edge.x, y), grid_color1);
  482. }
  483. // Markings
  484. draw_set_transform_matrix(Transform2D());
  485. Ref<Font> font = get_font("font", "Label");
  486. float font_height = font->get_height();
  487. Color text_color = get_color("font_color", "Editor");
  488. {
  489. // X axis
  490. float y = curve.get_min_value();
  491. Vector2 off(0, font_height - 1);
  492. draw_string(font, get_view_pos(Vector2(0, y)) + off, "0.0", text_color);
  493. draw_string(font, get_view_pos(Vector2(0.25, y)) + off, "0.25", text_color);
  494. draw_string(font, get_view_pos(Vector2(0.5, y)) + off, "0.5", text_color);
  495. draw_string(font, get_view_pos(Vector2(0.75, y)) + off, "0.75", text_color);
  496. draw_string(font, get_view_pos(Vector2(1, y)) + off, "1.0", text_color);
  497. }
  498. {
  499. // Y axis
  500. float m0 = curve.get_min_value();
  501. float m1 = 0.5 * (curve.get_min_value() + curve.get_max_value());
  502. float m2 = curve.get_max_value();
  503. Vector2 off(1, -1);
  504. draw_string(font, get_view_pos(Vector2(0, m0)) + off, String::num(m0, 2), text_color);
  505. draw_string(font, get_view_pos(Vector2(0, m1)) + off, String::num(m1, 2), text_color);
  506. draw_string(font, get_view_pos(Vector2(0, m2)) + off, String::num(m2, 3), text_color);
  507. }
  508. // Draw tangents for current point
  509. if (_selected_point >= 0) {
  510. const Color tangent_color = get_color("accent_color", "Editor");
  511. int i = _selected_point;
  512. Vector2 pos = curve.get_point_position(i);
  513. if (i != 0) {
  514. Vector2 control_pos = get_tangent_view_pos(i, TANGENT_LEFT);
  515. draw_line(get_view_pos(pos), control_pos, tangent_color);
  516. draw_rect(Rect2(control_pos, Vector2(1, 1)).grow(2), tangent_color);
  517. }
  518. if (i != curve.get_point_count() - 1) {
  519. Vector2 control_pos = get_tangent_view_pos(i, TANGENT_RIGHT);
  520. draw_line(get_view_pos(pos), control_pos, tangent_color);
  521. draw_rect(Rect2(control_pos, Vector2(1, 1)).grow(2), tangent_color);
  522. }
  523. }
  524. // Draw lines
  525. draw_set_transform_matrix(_world_to_view);
  526. const Color line_color = get_color("highlight_color", "Editor");
  527. const Color edge_line_color = get_color("font_color", "Editor");
  528. CanvasItemPlotCurve plot_func(*this, line_color, edge_line_color);
  529. plot_curve_accurate(curve, 4.f / view_size.x, plot_func);
  530. // Draw points
  531. draw_set_transform_matrix(Transform2D());
  532. const Color point_color = get_color("font_color", "Editor");
  533. const Color selected_point_color = get_color("accent_color", "Editor");
  534. for (int i = 0; i < curve.get_point_count(); ++i) {
  535. Vector2 pos = curve.get_point_position(i);
  536. draw_rect(Rect2(get_view_pos(pos), Vector2(1, 1)).grow(3), i == _selected_point ? selected_point_color : point_color);
  537. // TODO Circles are prettier. Needs a fix! Or a texture
  538. //draw_circle(pos, 2, point_color);
  539. }
  540. // Hover
  541. if (_hover_point != -1) {
  542. const Color hover_color = line_color;
  543. Vector2 pos = curve.get_point_position(_hover_point);
  544. stroke_rect(Rect2(get_view_pos(pos), Vector2(1, 1)).grow(_hover_radius), hover_color);
  545. }
  546. // Help text
  547. if (_selected_point > 0 && _selected_point + 1 < curve.get_point_count()) {
  548. text_color.a *= 0.4;
  549. draw_string(font, Vector2(50, font_height), TTR("Hold Shift to edit tangents individually"), text_color);
  550. }
  551. }
  552. // TODO That should be part of the drawing API...
  553. void CurveEditor::stroke_rect(Rect2 rect, Color color) {
  554. // a---b
  555. // | |
  556. // c---d
  557. Vector2 a(rect.position);
  558. Vector2 b(rect.position.x + rect.size.x, rect.position.y);
  559. Vector2 c(rect.position.x, rect.position.y + rect.size.y);
  560. Vector2 d(rect.position + rect.size);
  561. draw_line(a, b, color);
  562. draw_line(b, d, color);
  563. draw_line(d, c, color);
  564. draw_line(c, a, color);
  565. }
  566. void CurveEditor::_bind_methods() {
  567. ClassDB::bind_method(D_METHOD("_gui_input"), &CurveEditor::on_gui_input);
  568. ClassDB::bind_method(D_METHOD("_on_preset_item_selected"), &CurveEditor::on_preset_item_selected);
  569. ClassDB::bind_method(D_METHOD("_curve_changed"), &CurveEditor::_curve_changed);
  570. ClassDB::bind_method(D_METHOD("_on_context_menu_item_selected"), &CurveEditor::on_context_menu_item_selected);
  571. }
  572. //---------------
  573. bool EditorInspectorPluginCurve::can_handle(Object *p_object) {
  574. return Object::cast_to<Curve>(p_object) != NULL;
  575. }
  576. void EditorInspectorPluginCurve::parse_begin(Object *p_object) {
  577. Curve *curve = Object::cast_to<Curve>(p_object);
  578. ERR_FAIL_COND(!curve);
  579. Ref<Curve> c(curve);
  580. CurveEditor *editor = memnew(CurveEditor);
  581. editor->set_curve(curve);
  582. add_custom_control(editor);
  583. }
  584. CurveEditorPlugin::CurveEditorPlugin(EditorNode *p_node) {
  585. Ref<EditorInspectorPluginCurve> curve_plugin;
  586. curve_plugin.instance();
  587. EditorInspector::add_inspector_plugin(curve_plugin);
  588. get_editor_interface()->get_resource_previewer()->add_preview_generator(memnew(CurvePreviewGenerator));
  589. }
  590. //-----------------------------------
  591. // Preview generator
  592. bool CurvePreviewGenerator::handles(const String &p_type) const {
  593. return p_type == "Curve";
  594. }
  595. Ref<Texture> CurvePreviewGenerator::generate(const Ref<Resource> &p_from, const Size2 p_size) const {
  596. Ref<Curve> curve_ref = p_from;
  597. ERR_FAIL_COND_V(curve_ref.is_null(), Ref<Texture>());
  598. Curve &curve = **curve_ref;
  599. // FIXME: Should be ported to use p_size as done in b2633a97
  600. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  601. thumbnail_size *= EDSCALE;
  602. Ref<Image> img_ref;
  603. img_ref.instance();
  604. Image &im = **img_ref;
  605. im.create(thumbnail_size, thumbnail_size / 2, 0, Image::FORMAT_RGBA8);
  606. im.lock();
  607. Color bg_color(0.1, 0.1, 0.1, 1.0);
  608. for (int i = 0; i < thumbnail_size; i++) {
  609. for (int j = 0; j < thumbnail_size / 2; j++) {
  610. im.set_pixel(i, j, bg_color);
  611. }
  612. }
  613. Color line_color(0.8, 0.8, 0.8, 1.0);
  614. float range_y = curve.get_max_value() - curve.get_min_value();
  615. int prev_y = 0;
  616. for (int x = 0; x < im.get_width(); ++x) {
  617. float t = static_cast<float>(x) / im.get_width();
  618. float v = (curve.interpolate_baked(t) - curve.get_min_value()) / range_y;
  619. int y = CLAMP(im.get_height() - v * im.get_height(), 0, im.get_height());
  620. // Plot point
  621. if (y >= 0 && y < im.get_height()) {
  622. im.set_pixel(x, y, line_color);
  623. }
  624. // Plot vertical line to fix discontinuity (not 100% correct but enough for a preview)
  625. if (x != 0 && Math::abs(y - prev_y) > 1) {
  626. int y0, y1;
  627. if (y < prev_y) {
  628. y0 = y;
  629. y1 = prev_y;
  630. } else {
  631. y0 = prev_y;
  632. y1 = y;
  633. }
  634. for (int ly = y0; ly < y1; ++ly) {
  635. im.set_pixel(x, ly, line_color);
  636. }
  637. }
  638. prev_y = y;
  639. }
  640. im.unlock();
  641. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  642. ptex->create_from_image(img_ref, 0);
  643. return ptex;
  644. }