Skip to main content

fyrox_ui/curve/
key.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::{
22    brush::Brush,
23    core::{
24        algebra::Vector2,
25        math::curve::{Curve, CurveKey, CurveKeyKind},
26        reflect::prelude::*,
27        uuid::Uuid,
28        visitor::prelude::*,
29    },
30};
31use std::cmp::Ordering;
32
33#[derive(Default, Clone, Debug, Visit, Reflect)]
34pub struct CurveKeyView {
35    pub position: Vector2<f32>,
36    pub kind: CurveKeyKind,
37    pub id: Uuid,
38}
39
40impl From<&CurveKey> for CurveKeyView {
41    fn from(key: &CurveKey) -> Self {
42        Self {
43            position: Vector2::new(key.location(), key.value),
44            kind: key.kind.clone(),
45            id: key.id,
46        }
47    }
48}
49
50#[derive(Default, Clone, Visit, Reflect, Debug)]
51pub struct CurveKeyViewContainer {
52    id: Uuid,
53    pub brush: Brush,
54    keys: Vec<CurveKeyView>,
55}
56
57impl CurveKeyViewContainer {
58    pub fn new(curve: &Curve, brush: Brush) -> Self {
59        Self {
60            keys: curve
61                .keys()
62                .iter()
63                .map(CurveKeyView::from)
64                .collect::<Vec<_>>(),
65            brush,
66            id: curve.id(),
67        }
68    }
69
70    pub fn add(&mut self, key: CurveKeyView) {
71        self.keys.push(key)
72    }
73
74    pub fn remove(&mut self, id: Uuid) -> Option<CurveKeyView> {
75        if let Some(position) = self.keys.iter().position(|k| k.id == id) {
76            Some(self.keys.remove(position))
77        } else {
78            None
79        }
80    }
81
82    pub fn id(&self) -> Uuid {
83        self.id
84    }
85
86    pub fn key_ref(&self, id: Uuid) -> Option<&CurveKeyView> {
87        self.keys.iter().find(|k| k.id == id)
88    }
89
90    pub fn key_mut(&mut self, id: Uuid) -> Option<&mut CurveKeyView> {
91        self.keys.iter_mut().find(|k| k.id == id)
92    }
93
94    pub fn key_position(&self, id: Uuid) -> Option<usize> {
95        self.keys.iter().position(|key| key.id == id)
96    }
97
98    pub fn key_index_ref(&self, index: usize) -> Option<&CurveKeyView> {
99        self.keys.get(index)
100    }
101
102    pub fn key_index_mut(&mut self, index: usize) -> Option<&mut CurveKeyView> {
103        self.keys.get_mut(index)
104    }
105
106    pub fn keys(&self) -> &[CurveKeyView] {
107        &self.keys
108    }
109
110    pub fn keys_mut(&mut self) -> &mut [CurveKeyView] {
111        &mut self.keys
112    }
113
114    pub fn sort_keys(&mut self) {
115        self.keys.sort_by(|a, b| {
116            if a.position.x < b.position.x {
117                Ordering::Less
118            } else if a.position.x > b.position.x {
119                Ordering::Greater
120            } else {
121                Ordering::Equal
122            }
123        })
124    }
125
126    pub fn curve(&self) -> Curve {
127        let mut curve = Curve::from(
128            self.keys
129                .iter()
130                .map(|k| {
131                    let mut key = CurveKey::new(k.position.x, k.position.y, k.kind.clone());
132                    key.id = k.id;
133                    key
134                })
135                .collect::<Vec<_>>(),
136        );
137        curve.set_id(self.id);
138        curve
139    }
140}