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
use crate::prelude::*;
use crate::{scalar, Point};
use skia_bindings as sb;
use skia_bindings::{SkInterpolator, SkInterpolatorBase_Result, SkUnitCubicInterp};
use std::time::Duration;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(i32)]
pub enum Result {
Normal = SkInterpolatorBase_Result::kNormal_Result as _,
FreezeStart = SkInterpolatorBase_Result::kFreezeStart_Result as _,
FreezeEnd = SkInterpolatorBase_Result::kFreezeEnd_Result as _,
}
impl NativeTransmutable<SkInterpolatorBase_Result> for Result {}
#[test]
fn test_interpolator_result_layout() {
Result::test_layout();
}
pub type Interpolator = Handle<SkInterpolator>;
impl NativeDrop for SkInterpolator {
fn drop(&mut self) {
unsafe {
sb::C_SkInterpolator_destruct(self);
}
}
}
impl Default for Handle<SkInterpolator> {
fn default() -> Self {
Handle::from_native(unsafe { SkInterpolator::new() })
}
}
impl Handle<SkInterpolator> {
pub fn duration(&self) -> Option<(Duration, Duration)> {
let mut start_time = 0;
let mut end_time = 0;
unsafe {
self.native()
._base
.getDuration(&mut start_time, &mut end_time)
}
.if_true_then_some(|| {
(
Duration::from_millis(start_time.try_into().unwrap()),
Duration::from_millis(end_time.try_into().unwrap()),
)
})
}
pub fn set_mirror(&mut self, mirror: bool) -> &mut Self {
unsafe { sb::C_SkInterpolator_setMirror(self.native_mut(), mirror) }
self
}
pub fn set_repeat_count(&mut self, repeat_count: scalar) -> &mut Self {
unsafe { sb::C_SkInterpolator_setRepeatCount(self.native_mut(), repeat_count) }
self
}
pub fn set_reset(&mut self, reset: bool) -> &mut Self {
unsafe { sb::C_SkInterpolator_setReset(self.native_mut(), reset) }
self
}
pub fn time_to_t(&self, time: Duration) -> (Result, TimeToT) {
let mut t = 0.0;
let mut index = 0;
let mut exact = false;
let r = Result::from_native(unsafe {
self.native()._base.timeToT(
time.as_millis().try_into().unwrap(),
&mut t,
&mut index,
&mut exact,
)
});
(
r,
TimeToT {
t,
index: index.try_into().unwrap(),
exact,
},
)
}
}
impl Handle<SkInterpolator> {
pub fn new(elem_count: usize, frame_count: usize) -> Self {
Handle::from_native(unsafe {
SkInterpolator::new1(
elem_count.try_into().unwrap(),
frame_count.try_into().unwrap(),
)
})
}
pub fn reset(&mut self, elem_count: usize, frame_count: usize) -> &mut Self {
unsafe {
self.native_mut().reset(
elem_count.try_into().unwrap(),
frame_count.try_into().unwrap(),
)
}
self
}
pub fn set_key_frame<'a>(
&mut self,
index: usize,
time: Duration,
values: &[scalar],
blend: impl Into<Option<&'a [scalar; 4]>>,
) -> bool {
assert_eq!(values.len(), self.elem_count());
unsafe {
self.native_mut().setKeyFrame(
index.try_into().unwrap(),
time.as_millis().try_into().unwrap(),
values.as_ptr(),
blend.into().as_ptr_or_null() as _,
)
}
}
pub fn time_to_values<'a>(
&self,
time: Duration,
values: impl Into<Option<&'a mut [scalar]>>,
) -> Result {
let mut values = values.into();
if let Some(ref values) = values {
assert_eq!(values.len(), self.elem_count());
};
Result::from_native(unsafe {
self.native().timeToValues(
time.as_millis().try_into().unwrap(),
values.as_ptr_or_null_mut(),
)
})
}
}
impl Handle<SkInterpolator> {
pub fn elem_count(&self) -> usize {
self.native()._base.fElemCount.try_into().unwrap()
}
}
pub struct TimeToT {
pub t: scalar,
pub index: usize,
pub exact: bool,
}
pub fn unit_cubic_interp(value: scalar, b: impl Into<Point>, c: impl Into<Point>) -> scalar {
let b = b.into();
let c = c.into();
unsafe { SkUnitCubicInterp(value, b.x, b.y, c.x, c.y) }
}