Skip to content

Commit b384c83

Browse files
committed
Add controls for changing speed
1 parent 839eb28 commit b384c83

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This program is a 2D screensaver which recreates the Pipes screensaver from old
66
- Custom piece sets are supported.
77
- **Depth mode** - in this mode several layers of pipes are drawn, and when a new layer is created,
88
old pipes are made darker which gives a sense of depth. Usable only with RGB palette.
9+
- Background color setting (by default transparent).
910
- Each pipe has its own color; available palettes are: none (colorless), base colors (16 colors
1011
defined by your terminal) and RGB.
1112
- There is a gradient mode for use with RGB palette.
@@ -43,13 +44,17 @@ default cargo dir will be) is in the `PATH` environment variable.
4344
To see all available options, pass `-h` or `--help`.
4445

4546
## Controls
46-
| Key | Action |
47-
|----------------------|-----------------------------|
48-
| `q` / `Q` / `Escape` | Quit |
49-
| `Space` | Pause |
50-
| `c` | Clear screen |
51-
| `s` | Show stats widget |
52-
| `l` | Clear and redraw everything |
47+
| Key | Action |
48+
|---------------------------------|-----------------------------|
49+
| `q` / `Q` / `Escape` / `Ctrl-C` | Quit |
50+
| `Space` | Pause |
51+
| `c` | Clear screen |
52+
| `s` | Show stats widget |
53+
| `l` | Clear and redraw everything |
54+
| `,` | Change speed by -1 |
55+
| `.` | Change speed by +1 |
56+
| `<` | Change speed by -10 |
57+
| `>` | Change speed by +10 |
5358

5459
## Piece Sets
5560

TODO.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
- [x] Add background color setting.
44
- [x] Lighten rather than darken if the pipe color is < background color.
5-
- [ ] Add keybinding for changing speed.
5+
- [x] Add keybinding for changing speed.
66

77
## Release v1.2.0
88

src/screensaver.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub struct Screensaver {
9999
darken_min: SrgbaTuple,
100100
bg_color: Option<SrgbaTuple>,
101101
stats_canv: Canvas,
102+
delay: Duration,
102103
cfg: Config,
103104
}
104105

@@ -123,7 +124,7 @@ impl Screensaver {
123124
},
124125
bg_color: {
125126
if let Some(c) = &cfg.bg_color {
126-
let hc = HexColor::parse_rgb(&c)?;
127+
let hc = HexColor::parse_rgb(c)?;
127128

128129
Some(SrgbaTuple(
129130
hc.r as f32 / 255.0,
@@ -142,6 +143,7 @@ impl Screensaver {
142143
},
143144
(scr_size.0, 3),
144145
),
146+
delay: Screensaver::calculate_delay(cfg.fps),
145147
cfg,
146148
});
147149

@@ -279,6 +281,7 @@ impl Screensaver {
279281
self.draw_bg();
280282
}
281283

284+
/// Fill the screen with background color.
282285
fn draw_bg(&mut self) {
283286
if let Some(c) = self.bg_color {
284287
self.canv
@@ -314,10 +317,8 @@ impl Screensaver {
314317
/// Run the main loop in the current thread until an external event is received (a key press or
315318
/// signal) or some internal error is occurred.
316319
pub fn run(&mut self) -> Result<()> {
317-
let delay = Duration::from_millis(1000 / self.cfg.fps as u64);
318-
319320
while !self.state.quit {
320-
self.handle_events(delay)?;
321+
self.handle_events(self.delay)?;
321322

322323
if !self.state.pause {
323324
self.gen_next_piece();
@@ -334,6 +335,10 @@ impl Screensaver {
334335
Ok(())
335336
}
336337

338+
fn calculate_delay(fps: i64) -> Duration {
339+
Duration::from_millis(1000 / fps as u64)
340+
}
341+
337342
/// Handle input and incoming events.
338343
fn handle_events(&mut self, delay: Duration) -> Result<()> {
339344
// The poll_input function blocks the thread if the argument is nonzero, so we can use it
@@ -361,6 +366,28 @@ impl Screensaver {
361366
KeyCode::Char('c') => self.clear(),
362367
KeyCode::Char('l') => self.redraw()?,
363368
KeyCode::Char('s') => self.cfg.show_stats = !self.cfg.show_stats,
369+
KeyCode::Char(',') => {
370+
self.cfg.fps -= 1;
371+
self.cfg.fps = self.cfg.fps.clamp(1, i64::MAX);
372+
373+
self.delay = Self::calculate_delay(self.cfg.fps)
374+
}
375+
KeyCode::Char('.') => {
376+
self.cfg.fps = self.cfg.fps.saturating_add(1);
377+
378+
self.delay = Self::calculate_delay(self.cfg.fps)
379+
}
380+
KeyCode::Char('<') => {
381+
self.cfg.fps -= 10;
382+
self.cfg.fps = self.cfg.fps.clamp(1, i64::MAX);
383+
384+
self.delay = Self::calculate_delay(self.cfg.fps)
385+
}
386+
KeyCode::Char('>') => {
387+
self.cfg.fps = self.cfg.fps.saturating_add(10);
388+
389+
self.delay = Self::calculate_delay(self.cfg.fps)
390+
}
364391
_ => {}
365392
},
366393
InputEvent::Key(KeyEvent {
@@ -433,15 +460,16 @@ impl Screensaver {
433460
});
434461

435462
let s = format!(
436-
"pcs. drawn: {}, lpcs. drawn: {}, c. pcs. drawn: {}, pps. drawn: {}, pcs. rem: {}, l. drawn: {}, pps. len: {}, pipe color: {}",
463+
"pcs. drawn: {}, lpcs. drawn: {}, c. pcs. drawn: {}, pps. drawn: {}, pcs. rem: {}, l. drawn: {}, pps. len: {}, pipe color: {}, fps: {}",
437464
self.state.pieces_total,
438465
self.state.layer_pieces_total,
439466
self.state.currently_drawn_pieces,
440467
self.state.pipes_total,
441468
self.state.pieces_remaining,
442469
self.state.layers_drawn,
443470
pipe_len,
444-
color
471+
color,
472+
self.cfg.fps,
445473
);
446474

447475
self.stats_canv.put_str(s);

0 commit comments

Comments
 (0)