uzor 1.0.20

Core UI engine — geometry, interaction, input state
Documentation
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
//! Electric border animation with perlin-like noise displacement
//!
//! Animates a border path with randomized displacement along the perimeter,
//! creating an electric/lightning effect. Uses multi-octave noise for
//! organic-looking animation.

use std::f32::consts::PI;

/// Electric border animator
///
/// Generates displaced points along a rounded rectangle border path,
/// creating an animated electric effect using noise displacement.
#[derive(Debug, Clone)]
pub struct ElectricBorder {
    /// Border width in pixels
    pub width: f32,

    /// Border height in pixels
    pub height: f32,

    /// Border radius for rounded corners (pixels)
    pub border_radius: f32,

    /// Animation speed multiplier (1.0 = normal)
    pub speed: f32,

    /// Chaos level - displacement amplitude (0.0 = none, 0.2 = high)
    pub chaos: f32,

    /// Current animation time (seconds)
    time: f32,

    /// Number of sample points along border
    sample_count: usize,

    /// Displacement scale in pixels
    displacement: f32,
}

impl Default for ElectricBorder {
    fn default() -> Self {
        Self::new(400.0, 300.0)
    }
}

impl ElectricBorder {
    /// Create a new electric border
    pub fn new(width: f32, height: f32) -> Self {
        let perimeter = 2.0 * (width + height);
        let sample_count = (perimeter / 2.0) as usize;

        Self {
            width,
            height,
            border_radius: 24.0,
            speed: 1.0,
            chaos: 0.12,
            time: 0.0,
            sample_count,
            displacement: 60.0,
        }
    }

    /// Set border radius
    pub fn with_radius(mut self, radius: f32) -> Self {
        self.border_radius = radius;
        self
    }

    /// Set animation speed
    pub fn with_speed(mut self, speed: f32) -> Self {
        self.speed = speed;
        self
    }

    /// Set chaos level (displacement amount)
    pub fn with_chaos(mut self, chaos: f32) -> Self {
        self.chaos = chaos;
        self
    }

    /// Update dimensions
    pub fn set_dimensions(&mut self, width: f32, height: f32) {
        self.width = width;
        self.height = height;

        // Recalculate sample count based on perimeter
        let perimeter = 2.0 * (width + height);
        self.sample_count = (perimeter / 2.0) as usize;
    }

    /// Update animation time
    pub fn update(&mut self, delta_time: f32) {
        self.time += delta_time * self.speed;
    }

    /// Set absolute time
    pub fn set_time(&mut self, time: f32) {
        self.time = time;
    }

    /// Get current animation time
    pub fn time(&self) -> f32 {
        self.time
    }

    /// Generate displaced border points
    ///
    /// Returns a vector of (x, y) coordinates along the border path
    /// with noise-based displacement applied.
    pub fn generate_points(&self) -> Vec<(f32, f32)> {
        let mut points = Vec::with_capacity(self.sample_count + 1);

        let max_radius = (self.width.min(self.height) / 2.0).min(self.border_radius);
        let radius = max_radius.max(0.0);

        for i in 0..=self.sample_count {
            let t = i as f32 / self.sample_count as f32;

            // Get base point on rounded rectangle
            let (base_x, base_y) = self.get_rounded_rect_point(t, radius);

            // Apply noise displacement
            let (dx, dy) = self.get_displacement(t);

            points.push((base_x + dx, base_y + dy));
        }

        points
    }

    /// Get point on rounded rectangle perimeter at parameter t (0.0 to 1.0)
    fn get_rounded_rect_point(&self, t: f32, radius: f32) -> (f32, f32) {
        let straight_width = self.width - 2.0 * radius;
        let straight_height = self.height - 2.0 * radius;
        let corner_arc = (PI * radius) / 2.0;

        let total_perimeter =
            2.0 * straight_width + 2.0 * straight_height + 4.0 * corner_arc;

        let distance = t * total_perimeter;
        let mut accumulated = 0.0;

        // Top edge
        if distance <= accumulated + straight_width {
            let progress = (distance - accumulated) / straight_width;
            return (radius + progress * straight_width, 0.0);
        }
        accumulated += straight_width;

        // Top-right corner
        if distance <= accumulated + corner_arc {
            let progress = (distance - accumulated) / corner_arc;
            return self.get_corner_point(
                self.width - radius,
                radius,
                radius,
                -PI / 2.0,
                progress,
            );
        }
        accumulated += corner_arc;

        // Right edge
        if distance <= accumulated + straight_height {
            let progress = (distance - accumulated) / straight_height;
            return (self.width, radius + progress * straight_height);
        }
        accumulated += straight_height;

        // Bottom-right corner
        if distance <= accumulated + corner_arc {
            let progress = (distance - accumulated) / corner_arc;
            return self.get_corner_point(
                self.width - radius,
                self.height - radius,
                radius,
                0.0,
                progress,
            );
        }
        accumulated += corner_arc;

        // Bottom edge
        if distance <= accumulated + straight_width {
            let progress = (distance - accumulated) / straight_width;
            return (
                self.width - radius - progress * straight_width,
                self.height,
            );
        }
        accumulated += straight_width;

        // Bottom-left corner
        if distance <= accumulated + corner_arc {
            let progress = (distance - accumulated) / corner_arc;
            return self.get_corner_point(
                radius,
                self.height - radius,
                radius,
                PI / 2.0,
                progress,
            );
        }
        accumulated += corner_arc;

        // Left edge
        if distance <= accumulated + straight_height {
            let progress = (distance - accumulated) / straight_height;
            return (0.0, self.height - radius - progress * straight_height);
        }
        accumulated += straight_height;

        // Top-left corner (remaining)
        let progress = (distance - accumulated) / corner_arc;
        self.get_corner_point(radius, radius, radius, PI, progress)
    }

    /// Get point on circular arc
    fn get_corner_point(
        &self,
        center_x: f32,
        center_y: f32,
        radius: f32,
        start_angle: f32,
        progress: f32,
    ) -> (f32, f32) {
        let angle = start_angle + progress * (PI / 2.0);
        (
            center_x + radius * angle.cos(),
            center_y + radius * angle.sin(),
        )
    }

    /// Get noise displacement at parameter t
    fn get_displacement(&self, t: f32) -> (f32, f32) {
        let octaves = 10;
        let lacunarity = 1.6;
        let gain = 0.7;
        let amplitude = self.chaos;
        let frequency = 10.0;

        let x_noise = self.octaved_noise(
            t * 8.0,
            octaves,
            lacunarity,
            gain,
            amplitude,
            frequency,
            self.time,
            0.0,
        );

        let y_noise = self.octaved_noise(
            t * 8.0,
            octaves,
            lacunarity,
            gain,
            amplitude,
            frequency,
            self.time,
            1.0,
        );

        let scale = self.displacement;
        (x_noise * scale, y_noise * scale)
    }

    /// Multi-octave noise function
    #[allow(clippy::too_many_arguments)]
    fn octaved_noise(
        &self,
        x: f32,
        octaves: usize,
        lacunarity: f32,
        gain: f32,
        base_amplitude: f32,
        base_frequency: f32,
        time: f32,
        seed: f32,
    ) -> f32 {
        let mut result = 0.0;
        let mut amplitude = base_amplitude;
        let mut frequency = base_frequency;

        for _i in 0..octaves {
            result += amplitude
                * self.noise_2d(
                    frequency * x + seed * 100.0,
                    time * frequency * 0.3,
                );
            frequency *= lacunarity;
            amplitude *= gain;
        }

        result
    }

    /// 2D noise function (simplified perlin-style)
    fn noise_2d(&self, x: f32, y: f32) -> f32 {
        let i = x.floor();
        let j = y.floor();
        let fx = x - i;
        let fy = y - j;

        // Sample grid corners
        let a = self.random(i + j * 57.0);
        let b = self.random(i + 1.0 + j * 57.0);
        let c = self.random(i + (j + 1.0) * 57.0);
        let d = self.random(i + 1.0 + (j + 1.0) * 57.0);

        // Smoothstep interpolation
        let ux = fx * fx * (3.0 - 2.0 * fx);
        let uy = fy * fy * (3.0 - 2.0 * fy);

        // Bilinear interpolation
        a * (1.0 - ux) * (1.0 - uy)
            + b * ux * (1.0 - uy)
            + c * (1.0 - ux) * uy
            + d * ux * uy
    }

    /// Pseudo-random function
    fn random(&self, x: f32) -> f32 {
        ((x * 12.9898).sin() * 43_758.547) % 1.0
    }

    /// Get number of sample points
    pub fn sample_count(&self) -> usize {
        self.sample_count
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_electric_border_creation() {
        let border = ElectricBorder::new(400.0, 300.0);
        assert_eq!(border.width, 400.0);
        assert_eq!(border.height, 300.0);
        assert!(border.sample_count > 0);
    }

    #[test]
    fn test_time_update() {
        let mut border = ElectricBorder::new(400.0, 300.0);

        border.update(0.1);
        assert!((border.time() - 0.1).abs() < 0.01);

        border.update(0.1);
        assert!((border.time() - 0.2).abs() < 0.01);
    }

    #[test]
    fn test_speed_multiplier() {
        let mut border = ElectricBorder::new(400.0, 300.0).with_speed(2.0);

        border.update(0.1);
        assert!((border.time() - 0.2).abs() < 0.01); // 0.1 * 2.0
    }

    #[test]
    fn test_generate_points() {
        let border = ElectricBorder::new(400.0, 300.0);
        let points = border.generate_points();

        // Should have sample_count + 1 points (including closing point)
        assert_eq!(points.len(), border.sample_count() + 1);

        // Points should be roughly within bounds (plus displacement margin)
        for (x, y) in points.iter() {
            assert!(
                x >= &-border.displacement && x <= &(border.width + border.displacement)
            );
            assert!(
                y >= &-border.displacement && y <= &(border.height + border.displacement)
            );
        }
    }

    #[test]
    fn test_rounded_rect_corners() {
        let border = ElectricBorder::new(400.0, 300.0).with_radius(24.0);

        // Test corner points (approximately)
        let radius = border.border_radius;

        // Top-left corner region (t near 0)
        let (x, y) = border.get_rounded_rect_point(0.0, radius);
        assert!(x >= radius - 1.0 && x <= radius + 1.0);
        assert!(y < 1.0);

        // Closing point should match start
        let (x_end, y_end) = border.get_rounded_rect_point(1.0, radius);
        assert!((x - x_end).abs() < 10.0);
        assert!((y - y_end).abs() < 10.0);
    }

    #[test]
    fn test_noise_consistency() {
        let border = ElectricBorder::new(400.0, 300.0);

        // Same input should produce same output
        let noise1 = border.noise_2d(1.5, 2.5);
        let noise2 = border.noise_2d(1.5, 2.5);
        assert_eq!(noise1, noise2);
    }

    #[test]
    fn test_random_function() {
        let border = ElectricBorder::new(400.0, 300.0);

        // Random should be deterministic
        let r1 = border.random(42.0);
        let r2 = border.random(42.0);
        assert_eq!(r1, r2);

        // Different inputs should give different outputs
        let r3 = border.random(43.0);
        assert_ne!(r1, r3);
    }

    #[test]
    fn test_dimensions_update() {
        let mut border = ElectricBorder::new(400.0, 300.0);
        let old_count = border.sample_count();

        border.set_dimensions(800.0, 600.0);
        assert_eq!(border.width, 800.0);
        assert_eq!(border.height, 600.0);

        // Sample count should increase with perimeter
        assert!(border.sample_count() > old_count);
    }

    #[test]
    fn test_displacement_changes_over_time() {
        let mut border = ElectricBorder::new(400.0, 300.0);

        let points_t0 = border.generate_points();

        border.update(1.0);
        let points_t1 = border.generate_points();

        // Points should be different due to time-varying noise
        let mut different = false;
        for i in 0..points_t0.len().min(points_t1.len()) {
            if (points_t0[i].0 - points_t1[i].0).abs() > 0.1
                || (points_t0[i].1 - points_t1[i].1).abs() > 0.1
            {
                different = true;
                break;
            }
        }
        assert!(different, "Points should change over time");
    }
}