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
//! Particle system management
use super::particle::Particle;
use super::presets;
use super::{PresetEffect, PresetEffectOptions};
use std::time::Instant;
/// Particle system that manages all particles for an effect
pub struct ParticleSystem {
particles: Vec<Particle>,
effect: PresetEffect,
options: PresetEffectOptions,
width: f32,
height: f32,
last_update: Instant,
time: f32,
active: bool,
/// Delay before effect starts (in seconds)
start_delay: f32,
/// Time elapsed since system creation
elapsed_since_creation: f32,
/// Whether particles have been initialized after delay
initialized: bool,
}
/// Default delay before effect starts (500ms)
const DEFAULT_START_DELAY: f32 = 0.5;
impl ParticleSystem {
pub fn new(
effect: PresetEffect,
options: PresetEffectOptions,
width: f32,
height: f32,
) -> Self {
Self::with_delay(effect, options, width, height, DEFAULT_START_DELAY)
}
/// Create a particle system with a custom start delay
pub fn with_delay(
effect: PresetEffect,
options: PresetEffectOptions,
width: f32,
height: f32,
delay: f32,
) -> Self {
Self {
particles: Vec::new(),
effect,
options,
width,
height,
last_update: Instant::now(),
time: 0.0,
active: true,
start_delay: delay,
elapsed_since_creation: 0.0,
initialized: false,
}
}
/// Create a particle system with no delay (starts immediately)
pub fn immediate(
effect: PresetEffect,
options: PresetEffectOptions,
width: f32,
height: f32,
) -> Self {
let mut system = Self::with_delay(effect, options, width, height, 0.0);
system.initialize();
system.initialized = true;
system
}
/// Initialize particles based on the effect type
fn initialize(&mut self) {
let count = self.options.particle_count.unwrap_or_else(|| self.auto_particle_count());
self.particles.clear();
self.particles.reserve(count);
for i in 0..count {
let particle = self.spawn_particle(i as f32 / count as f32);
self.particles.push(particle);
}
}
/// Calculate automatic particle count based on window size and effect type
fn auto_particle_count(&self) -> usize {
match self.effect {
PresetEffect::RotatingHalo => {
// More particles for a dense glowing halo
let circumference = std::f32::consts::PI * self.width.min(self.height);
let base_count = (circumference / 5.0) as usize; // One particle every 5 pixels
((base_count as f32 * self.options.intensity * 3.0) as usize).max(30)
}
PresetEffect::ElectricSpark => {
// More particles - 1.5x more than before
let circumference = std::f32::consts::PI * self.width.min(self.height);
let base_count = (circumference / 10.0) as usize; // One particle every 10 pixels
((base_count as f32 * self.options.intensity * 1.5) as usize).clamp(15, 60)
}
PresetEffect::SilkRibbon => {
// 120 segments per ribbon × ribbon_count
120 * self.options.ribbon_count.max(1)
}
_ => {
let perimeter = 2.0 * (self.width + self.height);
let base_count = (perimeter / 10.0) as usize;
(base_count as f32 * self.options.intensity * 2.0) as usize
}
}
}
/// Spawn a new particle at the given position along the edge (0.0 - 1.0)
fn spawn_particle(&self, edge_position: f32) -> Particle {
match self.effect {
PresetEffect::RotatingHalo => {
presets::rotating_halo::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::PulseRipple => {
presets::pulse_ripple::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::FlowingLight => {
presets::flowing_light::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::StardustScatter => presets::stardust_scatter::spawn(
edge_position,
&self.options,
self.width,
self.height,
),
PresetEffect::ElectricSpark => presets::electric_spark::spawn(
edge_position,
&self.options,
self.width,
self.height,
),
PresetEffect::SmokeWisp => {
presets::smoke_wisp::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::RainDrop => {
presets::rain_drop::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::LaserBeam => {
presets::laser_beam::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::LightningArc => {
presets::lightning_arc::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::MeteorShower => {
presets::meteor_shower::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::SonarPulse => {
presets::sonar_pulse::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::MatrixRain => {
presets::matrix_rain::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::AuroraWave => {
presets::aurora_wave::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::OrbitRings => {
presets::orbit_rings::spawn(edge_position, &self.options, self.width, self.height)
}
PresetEffect::HeartbeatPulse => presets::heartbeat_pulse::spawn(
edge_position,
&self.options,
self.width,
self.height,
),
PresetEffect::CosmicStrings => presets::cosmic_strings::spawn(
edge_position,
&self.options,
self.width,
self.height,
),
PresetEffect::SilkRibbon => {
presets::silk_ribbon::spawn(edge_position, &self.options, self.width, self.height)
}
}
}
/// Update all particles
pub fn update(&mut self) {
if !self.active {
return;
}
let now = Instant::now();
let dt = now.duration_since(self.last_update).as_secs_f32();
self.last_update = now;
// Track time since creation for delay
self.elapsed_since_creation += dt;
// Check if we should initialize particles after delay
if !self.initialized && self.elapsed_since_creation >= self.start_delay {
self.initialize();
self.initialized = true;
}
// Don't update particles until initialized
if !self.initialized {
return;
}
self.time += dt;
// Update each particle based on effect type
for particle in &mut self.particles {
match self.effect {
PresetEffect::RotatingHalo => {
presets::rotating_halo::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::PulseRipple => {
presets::pulse_ripple::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::FlowingLight => {
presets::flowing_light::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::StardustScatter => {
presets::stardust_scatter::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::ElectricSpark => {
presets::electric_spark::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::SmokeWisp => {
presets::smoke_wisp::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::RainDrop => {
presets::rain_drop::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::LaserBeam => {
presets::laser_beam::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::LightningArc => {
presets::lightning_arc::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::MeteorShower => {
presets::meteor_shower::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::SonarPulse => {
presets::sonar_pulse::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::MatrixRain => {
presets::matrix_rain::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::AuroraWave => {
presets::aurora_wave::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::OrbitRings => {
presets::orbit_rings::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::HeartbeatPulse => {
presets::heartbeat_pulse::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::CosmicStrings => {
presets::cosmic_strings::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
PresetEffect::SilkRibbon => {
presets::silk_ribbon::update(
particle,
dt,
self.time,
&self.options,
self.width,
self.height,
);
}
}
}
// Respawn dead particles if looping
if self.options.loop_effect {
for i in 0..self.particles.len() {
if !self.particles[i].is_alive() {
let edge_pos = rand::random::<f32>();
self.particles[i] = self.spawn_particle(edge_pos);
}
}
} else {
// Remove dead particles
self.particles.retain(|p| p.is_alive());
}
}
/// Get all particles for rendering
pub fn particles(&self) -> &[Particle] {
&self.particles
}
/// Set window dimensions
pub fn set_size(&mut self, width: f32, height: f32) {
self.width = width;
self.height = height;
}
/// Start the effect
pub fn start(&mut self) {
self.active = true;
self.last_update = Instant::now();
}
/// Stop the effect
pub fn stop(&mut self) {
self.active = false;
}
/// Check if effect is active
pub fn is_active(&self) -> bool {
self.active
}
/// Reset the effect
pub fn reset(&mut self) {
self.time = 0.0;
self.elapsed_since_creation = 0.0;
self.initialized = false;
self.last_update = Instant::now();
self.particles.clear();
}
/// Set the start delay
pub fn set_delay(&mut self, delay: f32) {
self.start_delay = delay;
}
/// Get the start delay
pub fn delay(&self) -> f32 {
self.start_delay
}
/// Check if the effect has started (after delay)
pub fn has_started(&self) -> bool {
self.initialized
}
}