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
//! The core module of `Iced Audio`.
//!
//! This module holds basic types that can be reused and re-exported in
//! different runtime implementations.

pub mod math;
pub mod param;
pub mod range;
pub mod text_marks;
pub mod tick_marks;

pub use param::*;
pub use range::*;
pub use text_marks::*;
pub use tick_marks::*;

/// An `f32` value that is gauranteed to be constrained to the range of
///
/// `0.0 >= value >= 1.0`
///
/// # Example
///
/// ```
/// use iced_audio::Normal;
///
/// let mut normal = Normal::new(-1.0);
/// assert_eq!(normal.value(), 0.0);
///
/// normal.set(3.0);
/// assert_eq!(normal.value(), 1.0);
///
/// normal.set(0.5);
/// assert_eq!(normal.value(), 0.5);
/// ```
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Normal {
    value: f32,
}

impl Default for Normal {
    fn default() -> Self {
        Self { value: 0.0 }
    }
}

impl Normal {
    /// Creates a new `Normal`.
    ///
    /// # Arguments
    ///
    /// * `value` - the value to initialize the `Normal` with
    ///
    /// if `value < 0.0`, then `normal.value` is set to `0.0`
    ///
    /// else if `value > 1.0`, then `normal.value` is set to `1.0`
    ///
    /// else `normal.value` is set to `value`
    pub fn new(value: f32) -> Self {
        Self {
            value: {
                if value < 0.0 {
                    0.0
                } else if value > 1.0 {
                    1.0
                } else {
                    value
                }
            },
        }
    }

    /// Returns a `Normal` with the value `0.0`.
    pub fn min() -> Self {
        Self { value: 0.0 }
    }

    /// Returns a `Normal` with the value `1.0`.
    pub fn max() -> Self {
        Self { value: 1.0 }
    }

    /// Returns a `Normal` with the value `0.5`.
    pub fn center() -> Self {
        Self { value: 0.5 }
    }

    /// Set a value for the `Normal`.
    ///
    /// # Arguments
    ///
    /// * `value` - the value to set the `Normal` with
    ///
    /// if `value < 0.0`, then `normal.value` is set to `0.0`
    ///
    /// else if `value > 1.0`, then `normal.value` is set to `1.0`
    ///
    /// else `normal.value` is set to `value`
    pub fn set(&mut self, value: f32) {
        self.value = {
            if value < 0.0 {
                0.0
            } else if value > 1.0 {
                1.0
            } else {
                value
            }
        }
    }

    /// Returns the value of the `Normal`
    pub fn value(&self) -> f32 {
        self.value
    }

    /// Returns the inverse value (`1.0 - value`) of the `Normal`
    pub fn inv(&self) -> f32 {
        1.0 - self.value()
    }

    /// Returns the value of the `Normal` times the `scalar`
    pub fn scale(&self, scalar: f32) -> f32 {
        self.value * scalar
    }

    /// Returns the inverse value (`1.0 - value`) of the `Normal`
    /// times the `scalar`
    pub fn scale_inv(&self, scalar: f32) -> f32 {
        (1.0 - self.value()) * scalar
    }
}

impl From<f32> for Normal {
    fn from(value: f32) -> Self {
        Normal::new(value)
    }
}

impl From<Normal> for f32 {
    fn from(normal: Normal) -> f32 {
        normal.value
    }
}

/// The texture padding around a bounding rectangle. This is useful when the
/// texture is larger than the intended bounds of the widget, such as a glowing
/// button texture or a slider with a drop shadow, etc.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TexturePadding {
    /// Padding above the bounding rectangle in pixels
    pub top: u16,
    /// Padding below the bounding rectangle in pixels
    pub bottom: u16,
    /// Padding to the left of the bounding rectangle in pixels
    pub left: u16,
    /// Padding to the right of the bounding rectangle in pixels
    pub right: u16,
}

impl Default for TexturePadding {
    fn default() -> Self {
        Self {
            top: 0,
            bottom: 0,
            left: 0,
            right: 0,
        }
    }
}

impl TexturePadding {
    /// Creates a new `TexturePadding` with `top`, `bottom`, `left`, and `right`
    /// all set to `padding`.
    pub fn from_single(padding: u16) -> Self {
        Self {
            top: padding,
            bottom: padding,
            left: padding,
            right: padding,
        }
    }

    /// Creates a new `TexturePadding`
    ///
    /// # Arguments
    /// * `vertical_pad` - padding for `top` and `bottom`
    /// * `horizontal_pad` - padding for `left` and `right`
    pub fn from_v_h(vertical_pad: u16, horizontal_pad: u16) -> Self {
        Self {
            top: vertical_pad,
            bottom: vertical_pad,
            left: horizontal_pad,
            right: horizontal_pad,
        }
    }
}

static PI_OVER_180: f32 = std::f32::consts::PI / 180.0;

/// 2.0 * pi
pub static TAU: f32 = std::f32::consts::PI * 2.0;

/// The default minimum angle of a rotating widget such as a Knob
pub static DEFAULT_ANGLE_MIN: f32 = 30.0 * PI_OVER_180;
/// The default maximum angle of a rotating widget such as a Knob
pub static DEFAULT_ANGLE_MAX: f32 = (360.0 - 30.0) * PI_OVER_180;

/// The range between the minimum and maximum angle (in radians) the knob
/// will rotate.
///
/// `0.0` radians points straight down at the bottom of the knob, with the
/// angles rotating clockwise towards `TAU` (`2*PI`).
///
/// Values < `0.0` and >= `TAU` are not allowed.
///
/// The default minimum (converted to degrees) is `30` degrees, and the default
/// maximum is `330` degrees, giving a span of `300` degrees, and a halfway
/// point pointing strait up.
#[derive(Debug, Clone)]
pub struct KnobAngleRange {
    min: f32,
    max: f32,
}

impl std::default::Default for KnobAngleRange {
    fn default() -> Self {
        Self {
            min: DEFAULT_ANGLE_MIN,
            max: DEFAULT_ANGLE_MAX,
        }
    }
}

impl KnobAngleRange {
    /// The range between the `min` and `max` angle (in degrees) the knob
    /// will rotate.
    ///
    /// `0.0` degrees points straight down at the bottom of the knob, with the
    /// angles rotating clockwise towards `360` degrees.
    ///
    /// Values < `0.0` and >= `360.0` will be set to `0.0`.
    ///
    /// The default minimum is `30` degrees, and the default maximum is `330`
    /// degrees, giving a span of `300` degrees, and a halfway point pointing
    /// strait up.
    ///
    /// # Panics
    ///
    /// This will panic if `min` > `max`.
    pub fn from_deg(min: f32, max: f32) -> Self {
        let min_rad = min * PI_OVER_180;
        let max_rad = max * PI_OVER_180;

        Self::from_rad(min_rad, max_rad)
    }

    /// The span between the `min` and `max` angle (in radians) the knob
    /// will rotate.
    ///
    /// `0.0` radians points straight down at the bottom of the knob, with the
    /// angles rotating clockwise towards `TAU` (`2*PI`) radians.
    ///
    /// Values < `0.0` and >= `TAU` will be set to `0.0`.
    ///
    /// The default minimum (converted to degrees) is `30` degrees, and the
    /// default maximum is `330` degrees, giving a span of `300` degrees, and
    /// a halfway point pointing strait up.
    ///
    /// # Panics
    ///
    /// This will panic if `min` > `max`.
    pub fn from_rad(min: f32, max: f32) -> Self {
        debug_assert!(min <= max);

        let mut min = min;
        let mut max = max;

        if min < 0.0 || min >= TAU {
            min = 0.0;
        }
        if max < 0.0 || max >= TAU {
            max = 0.0;
        }

        Self { min, max }
    }

    /// returns the minimum angle (between `0.0` and `TAU` in radians)
    pub fn min(&self) -> f32 {
        self.min
    }
    /// returns the maximum angle (between `0.0` and `TAU` in radians)
    pub fn max(&self) -> f32 {
        self.max
    }
}

/// The state of a modulation range
#[derive(Debug, Copy, Clone)]
pub struct ModulationRange {
    /// Where the modulation range starts.
    /// `0.0.into()` is all the way minimum, and `1.0.into()` is all the way maximum.
    pub start: Normal,
    /// Where the modulation range ends.
    /// `0.0.into()` is all the way minimum, and `1.0.into()` is all the way maximum.
    pub end: Normal,
    /// Whether the modulation range is visible or not.
    pub visible: bool,
    /// Whether the filled portion of the modulation range is visible or not, while keeping
    /// the empty portion visible. This will have no effect if the `visible` field is false.
    pub filled_visible: bool,
}

impl ModulationRange {
    /// Creates a new `ModulationRange`
    ///
    /// * start - Where the modulation range starts.
    /// `0.0.into()` is all the way minimum, and `1.0.into()` is all the way maximum.
    /// * ends - Where the modulation range ends.
    /// `0.0.into()` is all the way minimum, and `1.0.into()` is all the way maximum.
    pub fn new(start: Normal, end: Normal) -> Self {
        Self {
            start,
            end,
            visible: true,
            filled_visible: true,
        }
    }
}

impl Default for ModulationRange {
    fn default() -> Self {
        Self {
            start: 0.0.into(),
            end: 0.0.into(),
            visible: true,
            filled_visible: true,
        }
    }
}