fyrox_ui/curve/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::message::CursorIcon;
use crate::style::resource::StyleResourceExt;
use crate::style::Style;
use crate::{
brush::Brush,
core::{
algebra::{Matrix3, Point2, SimdPartialOrd, Vector2, Vector3},
color::Color,
math::{
cubicf,
curve::{Curve, CurveKeyKind},
lerpf, wrap_angle, Rect,
},
parking_lot::{MappedMutexGuard, Mutex, MutexGuard},
pool::Handle,
reflect::prelude::*,
type_traits::prelude::*,
uuid_provider,
visitor::prelude::*,
},
curve::key::{CurveKeyView, CurveKeyViewContainer},
define_constructor,
draw::{CommandTexture, Draw, DrawingContext},
formatted_text::{FormattedText, FormattedTextBuilder},
grid::{Column, GridBuilder, Row},
menu::{ContextMenuBuilder, MenuItemBuilder, MenuItemContent, MenuItemMessage},
message::{ButtonState, KeyCode, MessageDirection, MouseButton, UiMessage},
numeric::{NumericUpDownBuilder, NumericUpDownMessage},
popup::PopupBuilder,
stack_panel::StackPanelBuilder,
text::TextBuilder,
widget::{Widget, WidgetBuilder, WidgetMessage},
BuildContext, Control, RcUiNodeHandle, Thickness, UiNode, UserInterface, VerticalAlignment,
};
use fxhash::FxHashSet;
use fyrox_graph::constructor::{ConstructorProvider, GraphNodeConstructor};
use fyrox_graph::BaseSceneGraph;
use std::{
cell::{Cell, RefCell},
ops::{Deref, DerefMut},
sync::mpsc::Sender,
};
pub mod key;
#[derive(Debug, Clone, PartialEq)]
pub enum CurveEditorMessage {
SyncBackground(Vec<Curve>),
Sync(Vec<Curve>),
Colorize(Vec<(Uuid, Brush)>),
ViewPosition(Vector2<f32>),
Zoom(Vector2<f32>),
ZoomToFit {
/// Should the zoom to fit be performed on some of the next update cycle (up to 10 frames delay), or immediately when
/// processing the message.
after_layout: bool,
},
HighlightZones(Vec<HighlightZone>),
// Internal messages. Use only when you know what you're doing.
// These are internal because you must use Sync message to request changes
// in the curve editor.
ChangeSelectedKeysKind(CurveKeyKind),
ChangeSelectedKeysValue(f32),
ChangeSelectedKeysLocation(f32),
RemoveSelection,
CopySelection,
PasteSelection,
// Position in screen coordinates.
AddKey(Vector2<f32>),
}
impl CurveEditorMessage {
define_constructor!(CurveEditorMessage:SyncBackground => fn sync_background(Vec<Curve>), layout: false);
define_constructor!(CurveEditorMessage:Sync => fn sync(Vec<Curve>), layout: false);
define_constructor!(CurveEditorMessage:Colorize => fn colorize(Vec<(Uuid, Brush)>), layout: false);
define_constructor!(CurveEditorMessage:ViewPosition => fn view_position(Vector2<f32>), layout: false);
define_constructor!(CurveEditorMessage:Zoom => fn zoom(Vector2<f32>), layout: false);
define_constructor!(CurveEditorMessage:ZoomToFit => fn zoom_to_fit(after_layout: bool), layout: true);
define_constructor!(CurveEditorMessage:HighlightZones => fn hightlight_zones(Vec<HighlightZone>), layout: false);
// Internal. Use only when you know what you're doing.
define_constructor!(CurveEditorMessage:RemoveSelection => fn remove_selection(), layout: false);
define_constructor!(CurveEditorMessage:ChangeSelectedKeysKind => fn change_selected_keys_kind(CurveKeyKind), layout: false);
define_constructor!(CurveEditorMessage:ChangeSelectedKeysValue => fn change_selected_keys_value(f32), layout: false);
define_constructor!(CurveEditorMessage:ChangeSelectedKeysLocation => fn change_selected_keys_location(f32), layout: false);
define_constructor!(CurveEditorMessage:AddKey => fn add_key(Vector2<f32>), layout: false);
define_constructor!(CurveEditorMessage:CopySelection => fn copy_selection(), layout: false);
define_constructor!(CurveEditorMessage:PasteSelection => fn paste_selection(), layout: false);
}
/// Highlight zone in values space.
#[derive(Clone, Debug, PartialEq, Visit, Reflect, Default)]
pub struct HighlightZone {
pub rect: Rect<f32>,
pub brush: Brush,
}
#[derive(Debug, Default)]
pub struct CurveTransformCell(Mutex<CurveTransform>);
impl Clone for CurveTransformCell {
fn clone(&self) -> Self {
Self(Mutex::new(self.0.lock().clone()))
}
}
impl CurveTransformCell {
/// Position of the center of the curve editor in the curve coordinate space.
pub fn position(&self) -> Vector2<f32> {
self.0.lock().position
}
/// Scape of the curve editor: multiply curve space units by this to get screen space units.
pub fn scale(&self) -> Vector2<f32> {
self.0.lock().scale
}
/// Location of the curve editor on the screen, in screen space units.
pub fn bounds(&self) -> Rect<f32> {
self.0.lock().bounds
}
/// Modify the current position. Call this when the center of the curve view should change.
pub fn set_position(&self, position: Vector2<f32>) {
self.0.lock().position = position
}
/// Modify the current zoom of the curve view.
pub fn set_scale(&self, scale: Vector2<f32>) {
self.0.lock().scale = scale;
}
/// Update the bounds of the curve view. Call this to ensure the CurveTransform accurately
/// reflects the actual size of the widget being drawn.
pub fn set_bounds(&self, bounds: Rect<f32>) {
self.0.lock().bounds = bounds;
}
/// Just like [CurveTransformCell::y_step_iter] but for x-coordinates.
/// Iterate through a list of x-coordinates across the width of the bounds.
/// The x-coordinates are in curve-space, but their distance apart should be
/// at least `grid_size` in screen-space.
///
/// This iterator indates where grid lines should be drawn to make a curve easier
/// to read for the user.
pub fn x_step_iter(&self, grid_size: f32) -> StepIterator {
self.0.lock().x_step_iter(grid_size)
}
/// Just like [CurveTransformCell::x_step_iter] but for y-coordinates.
/// Iterate through a list of y-coordinates across the width of the bounds.
/// The y-coordinates are in curve-space, but their distance apart should be
/// at least `grid_size` in screen-space.
///
/// This iterator indates where grid lines should be drawn to make a curve easier
/// to read for the user.
pub fn y_step_iter(&self, grid_size: f32) -> StepIterator {
self.0.lock().y_step_iter(grid_size)
}
/// Construct the transformation matrices to reflect the current position, scale, and bounds.
pub fn update_transform(&self) {
self.0.lock().update_transform();
}
/// Transform a point on the curve into a point in the local coordinate space of the widget.
pub fn curve_to_local(&self) -> MappedMutexGuard<Matrix3<f32>> {
MutexGuard::map(self.0.lock(), |t| &mut t.curve_to_local)
}
/// Transform a point on the curve into a point on the screen.
pub fn curve_to_screen(&self) -> MappedMutexGuard<Matrix3<f32>> {
MutexGuard::map(self.0.lock(), |t| &mut t.curve_to_screen)
}
/// Transform a point in the local coordinate space of the widget into a point in the coordinate space of the curve.
/// After the transformation, the x-coordinate could be a key location and the y-coordinate could be a key value.
/// Y-coordinates are flipped so that positive-y becomes the up direction.
pub fn local_to_curve(&self) -> MappedMutexGuard<Matrix3<f32>> {
MutexGuard::map(self.0.lock(), |t| &mut t.local_to_curve)
}
/// Transform a point on the screen into a point in the coordinate space of the curve.
/// After the transformation, the x-coordinate could be a key location and the y-coordinate could be a key value.
/// Y-coordinates are flipped so that positive-y becomes the up direction.
pub fn screen_to_curve(&self) -> MappedMutexGuard<Matrix3<f32>> {
MutexGuard::map(self.0.lock(), |t| &mut t.screen_to_curve)
}
}
/// Default grid size when not otherwise specified
pub const STANDARD_GRID_SIZE: f32 = 50.0;
/// Step sizes are more readable when they are easily recognizable.
/// This is a list of step sizes to round up to when choosing a step size.
/// The values in this list are meaningless; they are just intended to be convenient and round and easy to read.
const STANDARD_STEP_SIZES: [f32; 10] = [0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0, 25.0, 50.0, 100.0];
/// Round the given step size up to the next standard step size, if possible.
fn standardize_step(step: f32) -> f32 {
STANDARD_STEP_SIZES
.iter()
.copied()
.find(|x| step <= *x)
.unwrap_or(step)
}
/// This object represents the transformation curve coordinates into
/// the local coordinates of the curve editor widget and into the coordinates
/// of the screen. Using this object allows other widgets to align themselves
/// perfectly with the coordinates of a curve editor.
///
/// Since widgets are not mutable during layout and rendering, a CurveTransform
/// is intended to be used within a [CurveTransformCell] which provides interior mutability.
#[derive(Clone, Debug)]
pub struct CurveTransform {
/// Position of the center of the curve editor in the curve coordinate space.
pub position: Vector2<f32>,
/// Scape of the curve editor: multiply curve space units by this to get screen space units.
pub scale: Vector2<f32>,
/// Location of the curve editor on the screen, in screen space units.
pub bounds: Rect<f32>,
/// Transform a point on a curve into a point in the local space
/// of the curve editor. Before applying this transformation, (0,0) is
/// the origin of the curve, and positive-y is up.
/// After the transform, (0,0) is the top-left corner of the editor,
/// and positive-y is down.
pub curve_to_local: Matrix3<f32>,
/// The inverse of `curve_to_local`, transforming a point in the local space
/// of the editor widget into a point in the space of the curve.
pub local_to_curve: Matrix3<f32>,
/// Transform a point on the screen into a point in the space of the curve.
/// For example, a mouse click position would be transformed into the corresponding
/// (x,y) coordinates of a cure key that would result from the click.
pub screen_to_curve: Matrix3<f32>,
/// Transform a point on the curve into a point on the screen.
pub curve_to_screen: Matrix3<f32>,
}
impl Default for CurveTransform {
fn default() -> Self {
Self {
position: Vector2::default(),
scale: Vector2::new(1.0, 1.0),
bounds: Rect::default(),
curve_to_local: Matrix3::identity(),
local_to_curve: Matrix3::identity(),
screen_to_curve: Matrix3::identity(),
curve_to_screen: Matrix3::identity(),
}
}
}
impl CurveTransform {
/// Construct the transformations matrices for the current position, scale, and bounds.
pub fn update_transform(&mut self) {
let bounds = self.bounds;
let local_center = bounds.size.scale(0.5);
let mut curve_to_local = Matrix3::<f32>::identity();
// Translate the view position to be at (0,0) in the curve space.
curve_to_local.append_translation_mut(&-self.position);
// Scale from curve units into local space units.
curve_to_local.append_nonuniform_scaling_mut(&self.scale);
// Flip y-positive from pointing up to pointing down
curve_to_local.append_nonuniform_scaling_mut(&Vector2::new(1.0, -1.0));
// Translate (0,0) to the center of the widget in local space.
curve_to_local.append_translation_mut(&local_center);
// Find the inverse transform matrix automatically.
let local_to_curve = curve_to_local.try_inverse().unwrap_or_default();
let mut curve_to_screen = curve_to_local;
// Translate the local (0,0) to the top-left corner of the widget in screen coordinates.
curve_to_screen.append_translation_mut(&bounds.position);
// Find the screen-to-curve matrix automatically from the curve-to-screen matrix.
let screen_to_curve = curve_to_screen.try_inverse().unwrap_or_default();
*self = CurveTransform {
curve_to_local,
local_to_curve,
screen_to_curve,
curve_to_screen,
..*self
};
}
/// Just like [CurveTransform::y_step_iter] but for x-coordinates.
/// Iterate through a list of x-coordinates across the width of the bounds.
/// The x-coordinates are in curve-space, but their distance apart should be
/// at least `grid_size` in screen-space.
///
/// This iterator indates where grid lines should be drawn to make a curve easier
/// to read for the user.
pub fn x_step_iter(&self, grid_size: f32) -> StepIterator {
let zoom = self.scale;
let step_size = grid_size / zoom.x.clamp(0.001, 1000.0);
let screen_left = self.bounds.position.x;
let screen_right = self.bounds.position.x + self.bounds.size.x;
let left = self
.screen_to_curve
.transform_point(&Point2::new(screen_left, 0.0))
.x;
let right = self
.screen_to_curve
.transform_point(&Point2::new(screen_right, 0.0))
.x;
StepIterator::new(standardize_step(step_size), left, right)
}
/// Just like [CurveTransform::x_step_iter] but for y-coordinates.
/// Iterate through a list of y-coordinates across the width of the bounds.
/// The y-coordinates are in curve-space, but their distance apart should be
/// at least `grid_size` in screen-space.
///
/// This iterator indates where grid lines should be drawn to make a curve easier
/// to read for the user.
pub fn y_step_iter(&self, grid_size: f32) -> StepIterator {
let zoom = self.scale;
let step_size = grid_size / zoom.y.clamp(0.001, 1000.0);
let screen_top = self.bounds.position.y;
let screen_bottom = self.bounds.position.y + self.bounds.size.y;
let start = self
.screen_to_curve
.transform_point(&Point2::new(0.0, screen_bottom))
.y;
let end = self
.screen_to_curve
.transform_point(&Point2::new(0.0, screen_top))
.y;
StepIterator::new(standardize_step(step_size), start, end)
}
}
/// Iterate through f32 values stepping by `step_size`. Each value is
/// is a multiple of `step_size`.
#[derive(Debug, Clone)]
pub struct StepIterator {
pub step_size: f32,
index: isize,
end: isize,
}
impl StepIterator {
/// Construct an interator that starts at or before `start` and ends at or after `end`.
/// The intention is to cover the whole range of `start` to `end` at least.
pub fn new(step: f32, start: f32, end: f32) -> Self {
Self {
step_size: step,
index: (start / step).floor() as isize,
end: ((end / step).ceil() as isize).saturating_add(1),
}
}
}
impl Iterator for StepIterator {
type Item = f32;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.end {
return None;
}
let value = (self.index as f32) * self.step_size;
self.index += 1;
Some(value)
}
}
#[derive(Default, Clone, Visit, Reflect, Debug)]
pub struct CurvesContainer {
curves: Vec<CurveKeyViewContainer>,
}
impl CurvesContainer {
pub fn from_native(brush: Brush, curves: &[Curve]) -> Self {
Self {
curves: curves
.iter()
.map(|curve| CurveKeyViewContainer::new(curve, brush.clone()))
.collect::<Vec<_>>(),
}
}
pub fn to_native(&self) -> Vec<Curve> {
self.curves
.iter()
.map(|view| view.curve())
.collect::<Vec<_>>()
}
pub fn container_of(&self, key_id: Uuid) -> Option<&CurveKeyViewContainer> {
self.curves
.iter()
.find(|curve| curve.key_ref(key_id).is_some())
}
pub fn key_ref(&self, uuid: Uuid) -> Option<&CurveKeyView> {
// TODO: This will be slow for curves with lots of keys.
self.curves.iter().find_map(|keys| keys.key_ref(uuid))
}
pub fn key_mut(&mut self, uuid: Uuid) -> Option<&mut CurveKeyView> {
// TODO: This will be slow for curves with lots of keys.
self.curves.iter_mut().find_map(|keys| keys.key_mut(uuid))
}
pub fn remove(&mut self, uuid: Uuid) {
for curve in self.curves.iter_mut() {
curve.remove(uuid);
}
}
pub fn iter(&self) -> impl Iterator<Item = &CurveKeyViewContainer> {
self.curves.iter()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut CurveKeyViewContainer> {
self.curves.iter_mut()
}
}
#[derive(Default, Clone, Visit, Reflect, Debug, ComponentProvider)]
pub struct CurveEditor {
widget: Widget,
background_curves: CurvesContainer,
curves: CurvesContainer,
#[visit(skip)]
#[reflect(hidden)]
curve_transform: CurveTransformCell,
key_brush: Brush,
selected_key_brush: Brush,
key_size: f32,
grid_brush: Brush,
background_curve_brush: Brush,
#[visit(skip)]
#[reflect(hidden)]
operation_context: Option<OperationContext>,
#[visit(skip)]
#[reflect(hidden)]
selection: Option<Selection>,
handle_radius: f32,
context_menu: ContextMenu,
#[visit(skip)]
#[reflect(hidden)]
text: RefCell<FormattedText>,
view_bounds: Option<Rect<f32>>,
show_x_values: bool,
show_y_values: bool,
grid_size: Vector2<f32>,
min_zoom: Vector2<f32>,
max_zoom: Vector2<f32>,
highlight_zones: Vec<HighlightZone>,
#[visit(skip)]
#[reflect(hidden)]
zoom_to_fit_timer: Option<usize>,
#[visit(skip)]
#[reflect(hidden)]
clipboard: Vec<(Vector2<f32>, CurveKeyKind)>,
}
impl ConstructorProvider<UiNode, UserInterface> for CurveEditor {
fn constructor() -> GraphNodeConstructor<UiNode, UserInterface> {
GraphNodeConstructor::new::<Self>()
.with_variant("Curve Editor", |ui| {
CurveEditorBuilder::new(WidgetBuilder::new().with_name("Curve Editor"))
.build(&mut ui.build_ctx())
.into()
})
.with_group("Input")
}
}
crate::define_widget_deref!(CurveEditor);
#[derive(Default, Clone, Visit, Reflect, Debug)]
struct ContextMenu {
widget: RcUiNodeHandle,
add_key: Handle<UiNode>,
remove: Handle<UiNode>,
key: Handle<UiNode>,
make_constant: Handle<UiNode>,
make_linear: Handle<UiNode>,
make_cubic: Handle<UiNode>,
zoom_to_fit: Handle<UiNode>,
key_properties: Handle<UiNode>,
key_value: Handle<UiNode>,
key_location: Handle<UiNode>,
copy_keys: Handle<UiNode>,
paste_keys: Handle<UiNode>,
}
#[derive(Clone, Debug)]
struct DragEntry {
key_id: Uuid,
initial_position: Vector2<f32>,
}
#[derive(Clone, Debug)]
enum OperationContext {
DragKeys {
// In local coordinates.
initial_mouse_pos: Vector2<f32>,
entries: Vec<DragEntry>,
},
MoveView {
initial_mouse_pos: Vector2<f32>,
initial_view_pos: Vector2<f32>,
},
DragTangent {
key_id: Uuid,
left: bool,
},
BoxSelection {
// In local coordinates.
initial_mouse_pos: Vector2<f32>,
min: Cell<Vector2<f32>>,
max: Cell<Vector2<f32>>,
},
}
impl OperationContext {
fn is_dragging(&self) -> bool {
matches!(
self,
OperationContext::DragKeys { .. } | OperationContext::DragTangent { .. }
)
}
}
#[derive(Clone, Debug)]
enum Selection {
Keys { keys: FxHashSet<Uuid> },
LeftTangent { key_id: Uuid },
RightTangent { key_id: Uuid },
}
#[derive(Copy, Clone)]
enum PickResult {
Key(Uuid),
LeftTangent(Uuid),
RightTangent(Uuid),
}
impl Selection {
fn single_key(key: Uuid) -> Self {
let mut keys = FxHashSet::default();
keys.insert(key);
Self::Keys { keys }
}
}
uuid_provider!(CurveEditor = "5c7b087e-871e-498d-b064-187b604a37d8");
impl Control for CurveEditor {
fn draw(&self, ctx: &mut DrawingContext) {
ctx.transform_stack.push(Matrix3::identity());
self.curve_transform.set_bounds(self.screen_bounds());
self.curve_transform.update_transform();
self.draw_background(ctx);
self.draw_highlight_zones(ctx);
self.draw_grid(ctx);
self.draw_curves(&self.background_curves, ctx);
self.draw_curves(&self.curves, ctx);
self.draw_keys(ctx);
self.draw_operation(ctx);
ctx.transform_stack.pop();
}
fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
self.widget.handle_routed_message(ui, message);
if message.destination() == self.handle {
if let Some(msg) = message.data::<WidgetMessage>() {
match msg {
WidgetMessage::KeyUp(code) => match code {
KeyCode::Delete => self.remove_selection(ui),
KeyCode::KeyF => self.zoom_to_fit(&ui.sender()),
_ => (),
},
WidgetMessage::MouseMove { pos, state } => {
let is_dragging = self
.operation_context
.as_ref()
.is_some_and(|ctx| ctx.is_dragging());
if self.pick(*pos).is_some() || is_dragging {
if self.cursor.is_none() {
ui.send_message(WidgetMessage::cursor(
self.handle,
MessageDirection::ToWidget,
Some(if is_dragging {
CursorIcon::Grabbing
} else {
CursorIcon::Grab
}),
));
}
} else if self.cursor.is_some() {
ui.send_message(WidgetMessage::cursor(
self.handle,
MessageDirection::ToWidget,
None,
));
}
let curve_mouse_pos = self.screen_to_curve_space(*pos);
if let Some(operation_context) = self.operation_context.as_ref() {
match operation_context {
OperationContext::DragKeys {
entries,
initial_mouse_pos,
} => {
let local_delta = curve_mouse_pos - initial_mouse_pos;
for entry in entries {
if let Some(key) = self.curves.key_mut(entry.key_id) {
key.position = entry.initial_position + local_delta;
}
}
self.sort_keys();
}
OperationContext::MoveView {
initial_mouse_pos,
initial_view_pos,
} => {
let d = *pos - initial_mouse_pos;
let zoom = self.curve_transform.scale();
// Dragging left moves the position right. Dragging up moves the position down.
// Remember: up is negative-y in screen space, and up is positive-y in curve space.
let delta = Vector2::<f32>::new(-d.x / zoom.x, d.y / zoom.y);
ui.send_message(CurveEditorMessage::view_position(
self.handle,
MessageDirection::ToWidget,
initial_view_pos + delta,
));
}
OperationContext::DragTangent { key_id: key, left } => {
if let Some(key) = self.curves.key_mut(*key) {
let key_pos = key.position;
let screen_key_pos = self
.curve_transform
.curve_to_screen()
.transform_point(&Point2::from(key_pos))
.coords;
if let CurveKeyKind::Cubic {
left_tangent,
right_tangent,
} = &mut key.kind
{
let mut local_delta = pos - screen_key_pos;
if *left {
local_delta.x = local_delta.x.min(f32::EPSILON);
} else {
local_delta.x = local_delta.x.max(f32::EPSILON);
}
let tangent =
(local_delta.y / local_delta.x).clamp(-10e6, 10e6);
if *left {
*left_tangent = tangent;
} else {
*right_tangent = tangent;
}
} else {
unreachable!(
"attempt to edit tangents of non-cubic curve key!"
)
}
}
}
OperationContext::BoxSelection {
initial_mouse_pos,
min,
max,
..
} => {
min.set(curve_mouse_pos.inf(initial_mouse_pos));
max.set(curve_mouse_pos.sup(initial_mouse_pos));
}
}
} else if state.left == ButtonState::Pressed {
if let Some(selection) = self.selection.as_ref() {
match selection {
Selection::Keys { keys } => {
self.operation_context = Some(OperationContext::DragKeys {
entries: keys
.iter()
.map(|k| DragEntry {
key_id: *k,
initial_position: self
.curves
.key_ref(*k)
.map(|k| k.position)
.unwrap_or_default(),
})
.collect::<Vec<_>>(),
initial_mouse_pos: curve_mouse_pos,
});
}
Selection::LeftTangent { key_id: key } => {
self.operation_context =
Some(OperationContext::DragTangent {
key_id: *key,
left: true,
})
}
Selection::RightTangent { key_id: key } => {
self.operation_context =
Some(OperationContext::DragTangent {
key_id: *key,
left: false,
})
}
}
} else {
self.operation_context = Some(OperationContext::BoxSelection {
initial_mouse_pos: curve_mouse_pos,
min: Default::default(),
max: Default::default(),
})
}
if self.operation_context.is_some() {
ui.capture_mouse(self.handle);
}
}
}
WidgetMessage::MouseUp { .. } => {
if let Some(context) = self.operation_context.take() {
ui.release_mouse_capture();
// Send modified curve back to user.
match context {
OperationContext::DragKeys { .. }
| OperationContext::DragTangent { .. } => {
// Ensure that the order of keys is correct.
self.sort_keys();
self.send_curves(ui);
}
OperationContext::BoxSelection { min, max, .. } => {
let min = min.get();
let max = max.get();
let rect =
Rect::new(min.x, min.y, max.x - min.x, max.y - min.y);
let mut selection = FxHashSet::default();
for curve in self.curves.iter() {
for key in curve.keys() {
if rect.contains(key.position) {
selection.insert(key.id);
}
}
}
if !selection.is_empty() {
self.set_selection(
Some(Selection::Keys { keys: selection }),
ui,
);
}
}
_ => {}
}
}
}
WidgetMessage::MouseDown { pos, button } => match button {
MouseButton::Left => {
let pick_result = self.pick(*pos);
if let Some(picked) = pick_result {
match picked {
PickResult::Key(picked_key) => {
if let Some(picked_key_id) =
self.curves.key_ref(picked_key).map(|key| key.id)
{
if let Some(selection) = self.selection.as_mut() {
match selection {
Selection::Keys { keys } => {
if ui.keyboard_modifiers().control {
keys.insert(picked_key_id);
}
if !keys.contains(&picked_key_id) {
self.set_selection(
Some(Selection::single_key(
picked_key_id,
)),
ui,
);
}
}
Selection::LeftTangent { .. }
| Selection::RightTangent { .. } => self
.set_selection(
Some(Selection::single_key(
picked_key_id,
)),
ui,
),
}
} else {
self.set_selection(
Some(Selection::single_key(picked_key_id)),
ui,
);
}
}
}
PickResult::LeftTangent(picked_key) => {
self.set_selection(
Some(Selection::LeftTangent { key_id: picked_key }),
ui,
);
}
PickResult::RightTangent(picked_key) => {
self.set_selection(
Some(Selection::RightTangent { key_id: picked_key }),
ui,
);
}
}
} else {
self.set_selection(None, ui);
}
}
MouseButton::Middle => {
ui.capture_mouse(self.handle);
self.operation_context = Some(OperationContext::MoveView {
initial_mouse_pos: *pos,
initial_view_pos: self.curve_transform.position(),
});
}
_ => (),
},
WidgetMessage::MouseWheel { amount, .. } => {
let k = if *amount < 0.0 { 0.9 } else { 1.1 };
let zoom = self.curve_transform.scale();
let new_zoom = if ui.keyboard_modifiers().shift {
Vector2::new(zoom.x * k, zoom.y)
} else if ui.keyboard_modifiers.control {
Vector2::new(zoom.x, zoom.y * k)
} else {
zoom * k
};
ui.send_message(CurveEditorMessage::zoom(
self.handle,
MessageDirection::ToWidget,
new_zoom,
));
message.set_handled(true);
}
_ => {}
}
} else if let Some(msg) = message.data::<CurveEditorMessage>() {
if message.destination() == self.handle
&& message.direction() == MessageDirection::ToWidget
{
match msg {
CurveEditorMessage::SyncBackground(curves) => {
self.background_curves =
CurvesContainer::from_native(self.key_brush.clone(), curves);
for curve in self.background_curves.iter_mut() {
curve.brush = self.background_curve_brush.clone();
}
}
CurveEditorMessage::Sync(curves) => {
let color_map = self
.curves
.iter()
.map(|curve| (curve.id(), curve.brush.clone()))
.collect::<Vec<_>>();
self.curves =
CurvesContainer::from_native(self.key_brush.clone(), curves);
self.colorize(&color_map);
}
CurveEditorMessage::Colorize(color_map) => {
self.colorize(color_map);
}
CurveEditorMessage::ViewPosition(view_position) => {
self.set_view_position(*view_position);
ui.send_message(message.reverse());
}
CurveEditorMessage::Zoom(zoom) => {
self.curve_transform
.set_scale(zoom.simd_clamp(self.min_zoom, self.max_zoom));
ui.send_message(message.reverse());
}
CurveEditorMessage::RemoveSelection => {
self.remove_selection(ui);
}
CurveEditorMessage::ChangeSelectedKeysKind(kind) => {
self.change_selected_keys_kind(kind.clone(), ui);
}
CurveEditorMessage::AddKey(screen_pos) => {
let local_pos = self.screen_to_curve_space(*screen_pos);
let mut curves = Vec::new();
if let Some(selection) = self.selection.as_ref() {
if let Selection::Keys { keys } = selection {
curves.extend(self.curves.iter_mut().filter(|curve| {
for key in curve.keys() {
if keys.contains(&key.id) {
return true;
}
}
false
}));
}
} else {
curves.extend(self.curves.curves.iter_mut());
};
let mut added_keys = FxHashSet::default();
for curve in curves {
let id = Uuid::new_v4();
curve.add(CurveKeyView {
position: local_pos,
kind: CurveKeyKind::Linear,
id,
});
added_keys.insert(id);
}
self.set_selection(Some(Selection::Keys { keys: added_keys }), ui);
self.sort_keys();
self.send_curves(ui);
}
CurveEditorMessage::ZoomToFit { after_layout } => {
if *after_layout {
// TODO: Layout system could take up to 10 frames in worst cases. This is super hackish solution
// but when it works, who cares.
self.zoom_to_fit_timer = Some(10);
} else {
self.zoom_to_fit(&ui.sender);
}
}
CurveEditorMessage::ChangeSelectedKeysValue(value) => {
self.change_selected_keys_value(*value, ui);
}
CurveEditorMessage::ChangeSelectedKeysLocation(location) => {
self.change_selected_keys_location(*location, ui);
}
CurveEditorMessage::HighlightZones(zones) => {
self.highlight_zones.clone_from(zones);
}
CurveEditorMessage::CopySelection => {
if let Some(Selection::Keys { keys }) = self.selection.as_ref() {
let menu_pos =
ui.node(self.context_menu.widget.handle()).screen_position();
let local_menu_pos = self.screen_to_curve_space(menu_pos);
self.clipboard.clear();
for key in keys {
for curve in self.curves.iter() {
if let Some(key) = curve.key_ref(*key) {
self.clipboard.push((
key.position - local_menu_pos,
key.kind.clone(),
));
}
}
}
}
}
CurveEditorMessage::PasteSelection => {
if !self.clipboard.is_empty() {
let menu_pos =
ui.node(self.context_menu.widget.handle()).screen_position();
let local_menu_pos = self.screen_to_curve_space(menu_pos);
let mut selection = FxHashSet::default();
for (offset, kind) in self.clipboard.iter().cloned() {
for curve in self.curves.iter_mut() {
let id = Uuid::new_v4();
selection.insert(id);
curve.add(CurveKeyView {
position: local_menu_pos + offset,
kind: kind.clone(),
id,
});
}
}
self.set_selection(Some(Selection::Keys { keys: selection }), ui);
self.sort_keys();
self.send_curves(ui);
}
}
}
}
}
}
}
fn preview_message(&self, ui: &UserInterface, message: &mut UiMessage) {
if let Some(MenuItemMessage::Click) = message.data::<MenuItemMessage>() {
if message.destination() == self.context_menu.remove {
ui.send_message(CurveEditorMessage::remove_selection(
self.handle,
MessageDirection::ToWidget,
));
} else if message.destination() == self.context_menu.make_constant {
ui.send_message(CurveEditorMessage::change_selected_keys_kind(
self.handle,
MessageDirection::ToWidget,
CurveKeyKind::Constant,
));
} else if message.destination() == self.context_menu.make_linear {
ui.send_message(CurveEditorMessage::change_selected_keys_kind(
self.handle,
MessageDirection::ToWidget,
CurveKeyKind::Linear,
));
} else if message.destination() == self.context_menu.make_cubic {
ui.send_message(CurveEditorMessage::change_selected_keys_kind(
self.handle,
MessageDirection::ToWidget,
CurveKeyKind::Cubic {
left_tangent: 0.0,
right_tangent: 0.0,
},
));
} else if message.destination() == self.context_menu.add_key {
let screen_pos = ui.node(self.context_menu.widget.handle()).screen_position();
ui.send_message(CurveEditorMessage::add_key(
self.handle,
MessageDirection::ToWidget,
screen_pos,
));
} else if message.destination() == self.context_menu.zoom_to_fit {
ui.send_message(CurveEditorMessage::zoom_to_fit(
self.handle,
MessageDirection::ToWidget,
false,
));
} else if message.destination() == self.context_menu.copy_keys {
ui.send_message(CurveEditorMessage::copy_selection(
self.handle,
MessageDirection::ToWidget,
));
} else if message.destination() == self.context_menu.paste_keys {
ui.send_message(CurveEditorMessage::paste_selection(
self.handle,
MessageDirection::ToWidget,
));
}
} else if let Some(NumericUpDownMessage::<f32>::Value(value)) = message.data() {
if message.direction() == MessageDirection::FromWidget && !message.handled() {
if message.destination() == self.context_menu.key_value {
ui.send_message(CurveEditorMessage::change_selected_keys_value(
self.handle,
MessageDirection::ToWidget,
*value,
));
} else if message.destination() == self.context_menu.key_location {
ui.send_message(CurveEditorMessage::change_selected_keys_location(
self.handle,
MessageDirection::ToWidget,
*value,
));
}
}
}
}
fn update(&mut self, _dt: f32, ui: &mut UserInterface) {
if let Some(timer) = self.zoom_to_fit_timer.as_mut() {
*timer = timer.saturating_sub(1);
if *timer == 0 {
self.zoom_to_fit(&ui.sender);
self.zoom_to_fit_timer = None;
}
}
}
}
fn draw_cubic(
left_pos: Vector2<f32>,
left_tangent: f32,
right_pos: Vector2<f32>,
right_tangent: f32,
steps: usize,
ctx: &mut DrawingContext,
) {
let mut prev = left_pos;
for i in 0..steps {
let t = i as f32 / (steps - 1) as f32;
let middle_x = lerpf(left_pos.x, right_pos.x, t);
let middle_y = cubicf(left_pos.y, right_pos.y, t, left_tangent, right_tangent);
let pt = Vector2::new(middle_x, middle_y);
ctx.push_line(prev, pt, 1.0);
prev = pt;
}
}
fn round_to_step(x: f32, step: f32) -> f32 {
x - x % step
}
impl CurveEditor {
#[allow(clippy::let_and_return)] // Improves readability
fn set_view_position(&mut self, position: Vector2<f32>) {
self.curve_transform
.set_position(self.view_bounds.map_or(position, |bounds| {
let local_space_position = -position;
let clamped_local_space_position = Vector2::new(
local_space_position
.x
.clamp(bounds.position.x, bounds.position.x + 2.0 * bounds.size.x),
local_space_position
.y
.clamp(bounds.position.y, bounds.position.y + 2.0 * bounds.size.y),
);
let clamped_view_space = -clamped_local_space_position;
clamped_view_space
}));
}
fn colorize(&mut self, color_map: &[(Uuid, Brush)]) {
for (curve_id, brush) in color_map.iter() {
if let Some(curve) = self.curves.iter_mut().find(|curve| &curve.id() == curve_id) {
curve.brush = brush.clone();
}
}
}
fn zoom_to_fit(&mut self, sender: &Sender<UiMessage>) {
let mut min = Vector2::repeat(f32::MAX);
let mut max = Vector2::repeat(-f32::MAX);
for curve in self.curves.iter() {
let bounds = curve.curve().bounds();
if bounds.position.x < min.x {
min.x = bounds.position.x;
}
if bounds.position.y < min.y {
min.y = bounds.position.y;
}
let local_max = bounds.position + bounds.size;
if local_max.x > max.x {
max.x = local_max.x;
}
if local_max.y > max.y {
max.y = local_max.y;
}
}
let mut bounds = Rect {
position: min,
size: max - min,
};
// Prevent division by zero.
if bounds.size.x < 0.001 {
bounds.size.x = 0.001;
}
if bounds.size.y < 0.001 {
bounds.size.y = 0.001;
}
let center = bounds.center();
sender
.send(CurveEditorMessage::zoom(
self.handle,
MessageDirection::ToWidget,
Vector2::new(
self.actual_local_size().x / bounds.w(),
self.actual_local_size().y / bounds.h(),
),
))
.unwrap();
sender
.send(CurveEditorMessage::view_position(
self.handle,
MessageDirection::ToWidget,
center,
))
.unwrap();
}
/// Transforms a point to view space.
pub fn point_to_view_space(&self, point: Vector2<f32>) -> Vector2<f32> {
self.curve_transform
.local_to_curve()
.transform_point(&Point2::from(point))
.coords
}
/// Transforms a point to screen space.
pub fn point_to_screen_space(&self, point: Vector2<f32>) -> Vector2<f32> {
self.curve_transform
.curve_to_screen()
.transform_point(&Point2::from(point))
.coords
}
/// Transforms a vector to screen space.
pub fn vector_to_screen_space(&self, vector: Vector2<f32>) -> Vector2<f32> {
(*self.curve_transform.curve_to_screen() * Vector3::new(vector.x, vector.y, 0.0)).xy()
}
/// Transforms a screen position to a curve position.
pub fn screen_to_curve_space(&self, point: Vector2<f32>) -> Vector2<f32> {
self.curve_transform
.screen_to_curve()
.transform_point(&Point2::from(point))
.coords
}
fn sort_keys(&mut self) {
for curve in self.curves.iter_mut() {
curve.sort_keys();
}
}
fn set_selection(&mut self, selection: Option<Selection>, ui: &UserInterface) {
self.selection = selection;
ui.send_message(WidgetMessage::enabled(
self.context_menu.remove,
MessageDirection::ToWidget,
self.selection.is_some(),
));
ui.send_message(WidgetMessage::enabled(
self.context_menu.key,
MessageDirection::ToWidget,
self.selection.is_some(),
));
ui.send_message(WidgetMessage::enabled(
self.context_menu.key_properties,
MessageDirection::ToWidget,
self.selection.is_some(),
));
if let Some(Selection::Keys { keys }) = self.selection.as_ref() {
if let Some(first) = keys.iter().next() {
if let Some(key) = self.curves.key_ref(*first) {
ui.send_message(
NumericUpDownMessage::value(
self.context_menu.key_location,
MessageDirection::ToWidget,
key.position.x,
)
.with_handled(true),
);
ui.send_message(
NumericUpDownMessage::value(
self.context_menu.key_value,
MessageDirection::ToWidget,
key.position.y,
)
.with_handled(true),
);
}
}
}
}
fn remove_selection(&mut self, ui: &mut UserInterface) {
if let Some(Selection::Keys { keys }) = self.selection.as_ref() {
for &id in keys {
self.curves.remove(id);
}
self.set_selection(None, ui);
// Send modified curve back to user.
self.send_curves(ui);
}
}
fn change_selected_keys_kind(&mut self, kind: CurveKeyKind, ui: &mut UserInterface) {
if let Some(Selection::Keys { keys }) = self.selection.as_ref() {
for key in keys {
if let Some(key) = self.curves.key_mut(*key) {
key.kind = kind.clone();
}
}
self.send_curves(ui);
}
}
fn change_selected_keys_value(&mut self, value: f32, ui: &mut UserInterface) {
if let Some(Selection::Keys { keys }) = self.selection.as_ref() {
let mut modified = false;
for key in keys {
if let Some(key) = self.curves.key_mut(*key) {
let key_value = &mut key.position.y;
if (*key_value).ne(&value) {
*key_value = value;
modified = true;
}
}
}
if modified {
self.send_curves(ui);
}
}
}
fn change_selected_keys_location(&mut self, location: f32, ui: &mut UserInterface) {
if let Some(Selection::Keys { keys }) = self.selection.as_ref() {
let mut modified = false;
for key in keys {
if let Some(key) = self.curves.key_mut(*key) {
let key_location = &mut key.position.x;
if (*key_location).ne(&location) {
*key_location = location;
modified = true;
}
}
}
if modified {
self.send_curves(ui);
}
}
}
/// `pos` must be in screen space.
fn pick(&self, pos: Vector2<f32>) -> Option<PickResult> {
for curve in self.curves.iter() {
// Linear search is fine here, having a curve with thousands of
// points is insane anyway.
for key in curve.keys().iter() {
let screen_pos = self.point_to_screen_space(key.position);
let bounds = Rect::new(
screen_pos.x - self.key_size * 0.5,
screen_pos.y - self.key_size * 0.5,
self.key_size,
self.key_size,
);
if bounds.contains(pos) {
return Some(PickResult::Key(key.id));
}
// Check tangents.
if let CurveKeyKind::Cubic {
left_tangent,
right_tangent,
} = key.kind
{
let left_handle_pos = self.tangent_screen_position(
wrap_angle(left_tangent.atan()) + std::f32::consts::PI,
key.position,
);
if (left_handle_pos - pos).norm() <= self.key_size * 0.5 {
return Some(PickResult::LeftTangent(key.id));
}
let right_handle_pos = self
.tangent_screen_position(wrap_angle(right_tangent.atan()), key.position);
if (right_handle_pos - pos).norm() <= self.key_size * 0.5 {
return Some(PickResult::RightTangent(key.id));
}
}
}
}
None
}
fn tangent_screen_position(&self, angle: f32, key_position: Vector2<f32>) -> Vector2<f32> {
self.point_to_screen_space(key_position)
+ Vector2::new(angle.cos(), angle.sin()).scale(self.handle_radius)
}
fn send_curves(&self, ui: &UserInterface) {
ui.send_message(CurveEditorMessage::sync(
self.handle,
MessageDirection::FromWidget,
self.curves.to_native(),
));
}
fn draw_background(&self, ctx: &mut DrawingContext) {
let screen_bounds = self.screen_bounds();
// Draw background.
ctx.push_rect_filled(&screen_bounds, None);
ctx.commit(
self.clip_bounds(),
self.background(),
CommandTexture::None,
None,
);
}
fn draw_highlight_zones(&self, ctx: &mut DrawingContext) {
for zone in self.highlight_zones.iter() {
let left_top_corner = self.point_to_screen_space(zone.rect.left_top_corner());
let bottom_right_corner = self.point_to_screen_space(zone.rect.right_bottom_corner());
ctx.push_rect_filled(
&Rect::new(
left_top_corner.x,
left_top_corner.y,
bottom_right_corner.x - left_top_corner.x,
bottom_right_corner.y - left_top_corner.y,
),
None,
);
ctx.commit(
self.clip_bounds(),
zone.brush.clone(),
CommandTexture::None,
None,
);
}
}
fn draw_grid(&self, ctx: &mut DrawingContext) {
let screen_bounds = self.screen_bounds();
let zoom = self.curve_transform.scale();
let step_size_x = self.grid_size.x / zoom.x;
let step_size_y = self.grid_size.y / zoom.y;
let mut local_left_bottom = self.screen_to_curve_space(screen_bounds.left_top_corner());
let local_left_bottom_n = local_left_bottom;
local_left_bottom.x = round_to_step(local_left_bottom.x, step_size_x);
local_left_bottom.y = round_to_step(local_left_bottom.y, step_size_y);
let mut local_right_top = self.screen_to_curve_space(screen_bounds.right_bottom_corner());
local_right_top.x = round_to_step(local_right_top.x, step_size_x);
local_right_top.y = round_to_step(local_right_top.y, step_size_y);
for y in self.curve_transform.y_step_iter(self.grid_size.y) {
ctx.push_line(
self.point_to_screen_space(Vector2::new(local_left_bottom.x - step_size_x, y)),
self.point_to_screen_space(Vector2::new(local_right_top.x + step_size_x, y)),
1.0,
);
}
for x in self.curve_transform.x_step_iter(self.grid_size.x) {
ctx.push_line(
self.point_to_screen_space(Vector2::new(x, local_left_bottom.y + step_size_y)),
self.point_to_screen_space(Vector2::new(x, local_right_top.y - step_size_y)),
1.0,
);
}
// Draw main axes.
let vb = self.point_to_screen_space(Vector2::new(0.0, -10e6));
let ve = self.point_to_screen_space(Vector2::new(0.0, 10e6));
ctx.push_line(vb, ve, 2.0);
let hb = self.point_to_screen_space(Vector2::new(-10e6, 0.0));
let he = self.point_to_screen_space(Vector2::new(10e6, 0.0));
ctx.push_line(hb, he, 2.0);
ctx.commit(
self.clip_bounds(),
self.grid_brush.clone(),
CommandTexture::None,
None,
);
// Draw values.
let mut text = self.text.borrow_mut();
if self.show_y_values {
for y in self.curve_transform.y_step_iter(self.grid_size.y) {
text.set_text(format!("{y:.1}")).build();
ctx.draw_text(
self.clip_bounds(),
self.point_to_screen_space(Vector2::new(local_left_bottom_n.x, y)),
&text,
);
}
}
if self.show_x_values {
for x in self.curve_transform.x_step_iter(self.grid_size.x) {
text.set_text(format!("{x:.1}")).build();
ctx.draw_text(
self.clip_bounds(),
self.point_to_screen_space(Vector2::new(x, local_left_bottom_n.y)),
&text,
);
}
}
}
fn draw_curves(&self, curves: &CurvesContainer, ctx: &mut DrawingContext) {
let screen_bounds = self.screen_bounds();
for curve in curves.iter() {
let draw_keys = curve.keys();
if let Some(first) = draw_keys.first() {
let screen_pos = self.point_to_screen_space(first.position);
ctx.push_line(Vector2::new(0.0, screen_pos.y), screen_pos, 1.0);
}
if let Some(last) = draw_keys.last() {
let screen_pos = self.point_to_screen_space(last.position);
ctx.push_line(
screen_pos,
Vector2::new(screen_bounds.x() + screen_bounds.w(), screen_pos.y),
1.0,
);
}
for pair in draw_keys.windows(2) {
let left = &pair[0];
let right = &pair[1];
let left_pos = self.point_to_screen_space(left.position);
let right_pos = self.point_to_screen_space(right.position);
let steps = ((right_pos.x - left_pos.x).abs() / 2.0) as usize;
match (&left.kind, &right.kind) {
// Constant-to-any is depicted as two straight lines.
(CurveKeyKind::Constant, CurveKeyKind::Constant)
| (CurveKeyKind::Constant, CurveKeyKind::Linear)
| (CurveKeyKind::Constant, CurveKeyKind::Cubic { .. }) => {
ctx.push_line(left_pos, Vector2::new(right_pos.x, left_pos.y), 1.0);
ctx.push_line(Vector2::new(right_pos.x, left_pos.y), right_pos, 1.0);
}
// Linear-to-any is depicted as a straight line.
(CurveKeyKind::Linear, CurveKeyKind::Constant)
| (CurveKeyKind::Linear, CurveKeyKind::Linear)
| (CurveKeyKind::Linear, CurveKeyKind::Cubic { .. }) => {
ctx.push_line(left_pos, right_pos, 1.0)
}
// Cubic-to-constant and cubic-to-linear is depicted as Hermite spline with right tangent == 0.0.
(
CurveKeyKind::Cubic {
right_tangent: left_tangent,
..
},
CurveKeyKind::Constant,
)
| (
CurveKeyKind::Cubic {
right_tangent: left_tangent,
..
},
CurveKeyKind::Linear,
) => draw_cubic(left_pos, *left_tangent, right_pos, 0.0, steps, ctx),
// Cubic-to-cubic is depicted as Hermite spline.
(
CurveKeyKind::Cubic {
right_tangent: left_tangent,
..
},
CurveKeyKind::Cubic {
left_tangent: right_tangent,
..
},
) => draw_cubic(
left_pos,
*left_tangent,
right_pos,
*right_tangent,
steps,
ctx,
),
}
}
ctx.commit(
self.clip_bounds(),
curve.brush.clone(),
CommandTexture::None,
None,
);
}
}
fn draw_keys(&self, ctx: &mut DrawingContext) {
for curve in self.curves.iter() {
let keys_to_draw = curve.keys();
for key in keys_to_draw.iter() {
let origin = self.point_to_screen_space(key.position);
let size = Vector2::new(self.key_size, self.key_size);
let half_size = size.scale(0.5);
ctx.push_rect_filled(
&Rect::new(
origin.x - half_size.x,
origin.y - half_size.x,
size.x,
size.y,
),
None,
);
let mut selected = false;
if let Some(selection) = self.selection.as_ref() {
match selection {
Selection::Keys { keys } => {
selected = keys.contains(&key.id);
}
Selection::LeftTangent { key_id } | Selection::RightTangent { key_id } => {
selected = key.id == *key_id;
}
}
}
// Show tangents for Cubic keys.
if selected {
let (show_left, show_right) =
match self.curves.container_of(key.id).and_then(|container| {
container
.key_position(key.id)
.and_then(|i| keys_to_draw.get(i.wrapping_sub(1)))
}) {
Some(left) => match (&left.kind, &key.kind) {
(CurveKeyKind::Cubic { .. }, CurveKeyKind::Cubic { .. }) => {
(true, true)
}
(CurveKeyKind::Linear, CurveKeyKind::Cubic { .. })
| (CurveKeyKind::Constant, CurveKeyKind::Cubic { .. }) => {
(false, true)
}
_ => (false, false),
},
None => match key.kind {
CurveKeyKind::Cubic { .. } => (false, true),
_ => (false, false),
},
};
if let CurveKeyKind::Cubic {
left_tangent,
right_tangent,
} = key.kind
{
if show_left {
let left_handle_pos = self.tangent_screen_position(
wrap_angle(left_tangent.atan()) + std::f32::consts::PI,
key.position,
);
ctx.push_line(origin, left_handle_pos, 1.0);
ctx.push_circle_filled(
left_handle_pos,
self.key_size * 0.5,
6,
Default::default(),
);
}
if show_right {
let right_handle_pos = self.tangent_screen_position(
wrap_angle(right_tangent.atan()),
key.position,
);
ctx.push_line(origin, right_handle_pos, 1.0);
ctx.push_circle_filled(
right_handle_pos,
self.key_size * 0.5,
6,
Default::default(),
);
}
}
}
ctx.commit(
self.clip_bounds(),
if selected {
self.selected_key_brush.clone()
} else {
self.key_brush.clone()
},
CommandTexture::None,
None,
);
}
}
}
fn draw_operation(&self, ctx: &mut DrawingContext) {
if let Some(OperationContext::BoxSelection { min, max, .. }) =
self.operation_context.as_ref()
{
let min = self.point_to_screen_space(min.get());
let max = self.point_to_screen_space(max.get());
let rect = Rect::new(min.x, min.y, max.x - min.x, max.y - min.y);
ctx.push_rect(&rect, 1.0);
ctx.commit(
self.clip_bounds(),
Brush::Solid(Color::WHITE),
CommandTexture::None,
None,
);
}
}
}
pub struct CurveEditorBuilder {
widget_builder: WidgetBuilder,
background_curves: Vec<Curve>,
curves: Vec<Curve>,
view_position: Vector2<f32>,
zoom: f32,
view_bounds: Option<Rect<f32>>,
show_x_values: bool,
show_y_values: bool,
grid_size: Vector2<f32>,
min_zoom: Vector2<f32>,
max_zoom: Vector2<f32>,
highlight_zones: Vec<HighlightZone>,
}
impl CurveEditorBuilder {
pub fn new(widget_builder: WidgetBuilder) -> Self {
Self {
widget_builder,
background_curves: Default::default(),
curves: Default::default(),
view_position: Default::default(),
zoom: 1.0,
view_bounds: None,
show_x_values: true,
show_y_values: true,
grid_size: Vector2::new(STANDARD_GRID_SIZE, STANDARD_GRID_SIZE),
min_zoom: Vector2::new(0.001, 0.001),
max_zoom: Vector2::new(1000.0, 1000.0),
highlight_zones: Default::default(),
}
}
pub fn with_background_curves(mut self, curves: Vec<Curve>) -> Self {
self.background_curves = curves;
self
}
pub fn with_curves(mut self, curves: Vec<Curve>) -> Self {
self.curves = curves;
self
}
pub fn with_zoom(mut self, zoom: f32) -> Self {
self.zoom = zoom;
self
}
pub fn with_view_position(mut self, view_position: Vector2<f32>) -> Self {
self.view_position = view_position;
self
}
pub fn with_show_x_values(mut self, show_x_values: bool) -> Self {
self.show_x_values = show_x_values;
self
}
pub fn with_show_y_values(mut self, show_y_values: bool) -> Self {
self.show_y_values = show_y_values;
self
}
/// View bounds in value-space.
pub fn with_view_bounds(mut self, bounds: Rect<f32>) -> Self {
self.view_bounds = Some(bounds);
self
}
pub fn with_grid_size(mut self, size: Vector2<f32>) -> Self {
self.grid_size = size;
self
}
pub fn with_min_zoom(mut self, min_zoom: Vector2<f32>) -> Self {
self.min_zoom = min_zoom;
self
}
pub fn with_max_zoom(mut self, max_zoom: Vector2<f32>) -> Self {
self.max_zoom = max_zoom;
self
}
pub fn with_highlight_zone(mut self, zones: Vec<HighlightZone>) -> Self {
self.highlight_zones = zones;
self
}
pub fn build(mut self, ctx: &mut BuildContext) -> Handle<UiNode> {
let background_curve_brush = ctx.style.get_or_default::<Brush>(Style::BRUSH_LIGHT);
let key_brush = Brush::Solid(Color::opaque(140, 140, 140));
let mut background_curves = CurvesContainer::from_native(key_brush.clone(), &self.curves);
for curve in background_curves.iter_mut() {
curve.brush = background_curve_brush.clone();
}
let curves = CurvesContainer::from_native(key_brush.clone(), &self.curves);
let add_key;
let remove;
let make_constant;
let make_linear;
let make_cubic;
let key;
let zoom_to_fit;
let key_properties;
let key_value;
let key_location;
let copy_keys;
let paste_keys;
let context_menu = ContextMenuBuilder::new(
PopupBuilder::new(WidgetBuilder::new()).with_content(
StackPanelBuilder::new(
WidgetBuilder::new()
.with_child({
key_properties = GridBuilder::new(
WidgetBuilder::new()
.with_enabled(false)
.with_child(
TextBuilder::new(
WidgetBuilder::new()
.with_vertical_alignment(VerticalAlignment::Center)
.with_margin(Thickness::uniform(1.0))
.on_row(0)
.on_column(0),
)
.with_text("Location")
.build(ctx),
)
.with_child({
key_location = NumericUpDownBuilder::<f32>::new(
WidgetBuilder::new()
.with_margin(Thickness::uniform(1.0))
.on_row(0)
.on_column(1),
)
.build(ctx);
key_location
})
.with_child(
TextBuilder::new(
WidgetBuilder::new()
.with_vertical_alignment(VerticalAlignment::Center)
.with_margin(Thickness::uniform(1.0))
.on_row(1)
.on_column(0),
)
.with_text("Value")
.build(ctx),
)
.with_child({
key_value = NumericUpDownBuilder::<f32>::new(
WidgetBuilder::new()
.with_margin(Thickness::uniform(1.0))
.on_row(1)
.on_column(1),
)
.build(ctx);
key_value
}),
)
.add_column(Column::auto())
.add_column(Column::stretch())
.add_row(Row::strict(22.0))
.add_row(Row::strict(22.0))
.build(ctx);
key_properties
})
.with_child({
add_key = MenuItemBuilder::new(WidgetBuilder::new())
.with_content(MenuItemContent::text("Add Key"))
.build(ctx);
add_key
})
.with_child({
remove = MenuItemBuilder::new(WidgetBuilder::new().with_enabled(false))
.with_content(MenuItemContent::text("Remove"))
.build(ctx);
remove
})
.with_child({
key = MenuItemBuilder::new(WidgetBuilder::new().with_enabled(false))
.with_content(MenuItemContent::text("Key..."))
.with_items(vec![
{
make_constant = MenuItemBuilder::new(WidgetBuilder::new())
.with_content(MenuItemContent::text("Constant"))
.build(ctx);
make_constant
},
{
make_linear = MenuItemBuilder::new(WidgetBuilder::new())
.with_content(MenuItemContent::text("Linear"))
.build(ctx);
make_linear
},
{
make_cubic = MenuItemBuilder::new(WidgetBuilder::new())
.with_content(MenuItemContent::text("Cubic"))
.build(ctx);
make_cubic
},
])
.build(ctx);
key
})
.with_child({
zoom_to_fit = MenuItemBuilder::new(WidgetBuilder::new())
.with_content(MenuItemContent::text("Zoom To Fit"))
.build(ctx);
zoom_to_fit
})
.with_child({
copy_keys = MenuItemBuilder::new(WidgetBuilder::new())
.with_content(MenuItemContent::text("Copy Selected Keys"))
.build(ctx);
copy_keys
})
.with_child({
paste_keys = MenuItemBuilder::new(WidgetBuilder::new())
.with_content(MenuItemContent::text("Paste Keys"))
.build(ctx);
paste_keys
}),
)
.build(ctx),
),
)
.build(ctx);
let context_menu = RcUiNodeHandle::new(context_menu, ctx.sender());
if self.widget_builder.foreground.is_none() {
self.widget_builder.foreground = Some(ctx.style.property(Style::BRUSH_BRIGHT))
}
let editor = CurveEditor {
widget: self
.widget_builder
.with_context_menu(context_menu.clone())
.with_preview_messages(true)
.with_need_update(true)
.build(ctx),
background_curves,
curves,
curve_transform: Default::default(),
key_brush,
selected_key_brush: Brush::Solid(Color::opaque(220, 220, 220)),
key_size: 8.0,
handle_radius: 36.0,
operation_context: None,
grid_brush: Brush::Solid(Color::from_rgba(110, 110, 110, 50)),
selection: None,
text: RefCell::new(
FormattedTextBuilder::new(ctx.default_font())
.with_brush(Brush::Solid(Color::opaque(100, 100, 100)))
.build(),
),
context_menu: ContextMenu {
widget: context_menu,
add_key,
remove,
make_constant,
make_linear,
make_cubic,
key,
zoom_to_fit,
key_properties,
key_value,
key_location,
copy_keys,
paste_keys,
},
view_bounds: self.view_bounds,
show_x_values: self.show_x_values,
show_y_values: self.show_y_values,
grid_size: self.grid_size,
min_zoom: self.min_zoom,
max_zoom: self.max_zoom,
highlight_zones: self.highlight_zones,
zoom_to_fit_timer: None,
clipboard: Default::default(),
background_curve_brush,
};
ctx.add_node(UiNode::new(editor))
}
}
#[cfg(test)]
mod test {
use crate::{curve::CurveEditorBuilder, test::test_widget_deletion, widget::WidgetBuilder};
#[test]
fn test_curve_editor_deletion() {
test_widget_deletion(|ctx| CurveEditorBuilder::new(WidgetBuilder::new()).build(ctx));
}
}