From 7a8097d035bdbba653c01e81d0d2d1ddff91f3a4 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 23 Oct 2022 18:14:12 -0700 Subject: [PATCH] make the background get redder as it progresses --- Cargo.lock | 3 +- Cargo.toml | 5 ++- README.md | 2 +- VERSION | 2 +- src/main.rs | 2 +- src/timer/eframe_app.rs | 67 ++++++++++++++++++++++++++++++----------- src/timer/gui.rs | 2 +- src/timer/mod.rs | 9 +++++- src/util.rs | 2 +- 9 files changed, 66 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f41b0e..7fb3ca8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1162,11 +1162,10 @@ dependencies = [ [[package]] name = "katabastird" -version = "1.6.0" +version = "1.6.1" dependencies = [ "clap", "eframe", - "egui", "egui_extras", "rodio", ] diff --git a/Cargo.toml b/Cargo.toml index 2c5af59..c7d9315 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "katabastird" -version = "1.6.0" +version = "1.6.1" edition = "2021" rust-version = "1.61" @@ -13,8 +13,7 @@ license-file = "LICENSE.md" [dependencies] clap = { version = "4", features = ["derive", "env", "unicode", "suggestions", "usage"] } eframe = { version = "0.19", features = ["wgpu"] } -egui = { version = "0.19", features = ["default"] } egui_extras = "0.19" -# naga = { version = "0.10", features = ["spv-out", "wgsl-out", "wgsl-in"] } rodio = { version = "0.16" } +# naga = { version = "0.10", features = ["spv-out", "wgsl-out", "wgsl-in"] } # wgpu = { version = "0.14", features = ["naga", "spirv"] } diff --git a/README.md b/README.md index 2daf69a..2052af2 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Options: -V, --version Print version information ``` -![a Predator's view of the timer while it's paused](./predator_timer_small.png "a Predator's view of the timer while it's paused") +![a Predator's view of the timer while it's paused](./predator_timer_small.png "a Predator's view of rthe timer while it's paused") "[Katabasis](https://en.wikipedia.org/wiki/Katabasis)" is the descent into the Underworld. diff --git a/VERSION b/VERSION index 810ee4e..4213d88 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.6 +1.61 diff --git a/src/main.rs b/src/main.rs index 9703072..1e0a529 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use egui::Vec2; +use eframe::egui::Vec2; use katabastird::timer::Timer; fn main() { diff --git a/src/timer/eframe_app.rs b/src/timer/eframe_app.rs index fc21789..16f064c 100644 --- a/src/timer/eframe_app.rs +++ b/src/timer/eframe_app.rs @@ -1,28 +1,61 @@ use std::time::Instant; +use eframe::{ + egui::{self, Frame}, + epaint::Color32, + Frame as EFrame, +}; + use super::{state::*, Timer}; use crate::{MAX_REPAINT, MIN_REPAINT}; +const STARTING_COLOR: &[f32; 3] = &[10.0, 4.0, 4.0]; +const UNIT_COLOR: &[f32; 3] = &[0.986, 0.154, 0.055]; + impl eframe::App for Timer { - fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { + fn update(&mut self, ctx: &egui::Context, frame: &mut EFrame) { ctx.request_repaint_after(MAX_REPAINT); let height = ctx.input().screen_rect().height(); - egui::CentralPanel::default().show(ctx, |ui| { - match self.state { - TimerState::Unstarted => self.unstarted(ui, height), - TimerState::Running(cs) => { - let dur = Instant::now() - cs.updated; - let dur = MIN_REPAINT.saturating_sub(dur); - ctx.request_repaint_after(dur); - self.running(ui, height, cs); + + let color = get_color(self.done); + + egui::CentralPanel::default() + .frame(Frame::none().fill(color)) + .show(ctx, |ui| { + match self.state { + TimerState::Unstarted => self.unstarted(ui, height), + TimerState::Running(cs) => { + let dur = Instant::now() - cs.updated; + let dur = MIN_REPAINT.saturating_sub(dur); + ctx.request_repaint_after(dur); + self.running(ui, height, cs); + } + TimerState::Paused(cs) => self.paused(ui, height, cs), + TimerState::Finished => self.finished(ui, height), } - TimerState::Paused(cs) => self.paused(ui, height, cs), - TimerState::Finished => self.finished(ui, height), - } - // check for quit key - if ui.input().key_pressed(egui::Key::Q) || ui.input().key_pressed(egui::Key::Escape) { - frame.close(); - } - }); + // check for quit key + if ui.input().key_pressed(egui::Key::Q) || ui.input().key_pressed(egui::Key::Escape) + { + frame.close(); + } + }); } } + +fn get_color(t: f32) -> Color32 { + let sr = STARTING_COLOR[0]; + let sg = STARTING_COLOR[1]; + let sb = STARTING_COLOR[2]; + + let ur = UNIT_COLOR[0]; + let ug = UNIT_COLOR[1]; + let ub = UNIT_COLOR[2]; + + let mag = t * 162.0; + let (r, g, b) = ( + (sr + (mag * ur).round()) as u8, + (sg + (mag * ug).round()) as u8, + (sb + (mag * ub).round()) as u8, + ); + Color32::from_rgb(r, g, b) +} diff --git a/src/timer/gui.rs b/src/timer/gui.rs index 35960d0..dbfdc70 100644 --- a/src/timer/gui.rs +++ b/src/timer/gui.rs @@ -1,6 +1,6 @@ use std::{sync::mpsc::Sender, time::Duration}; -use egui::{Color32, Direction, Layout, RichText, Ui}; +use eframe::egui::{Color32, Direction, Layout, RichText, Ui}; use egui_extras::{Size, StripBuilder}; use super::state::NextTimerState; diff --git a/src/timer/mod.rs b/src/timer/mod.rs index ffebd1f..2afe5d5 100644 --- a/src/timer/mod.rs +++ b/src/timer/mod.rs @@ -2,7 +2,7 @@ use std::sync::mpsc::channel; use std::time::{Duration, Instant}; use clap::Parser; -use egui::{Color32, FontId, RichText, Ui}; +use eframe::egui::{self, Color32, FontId, RichText, Ui}; use crate::{cli::Cli, util::*, AIRHORN, DIGIT_FACTOR, MAX_REPAINT, PREDATOR_FONT, TEXT_FACTOR}; @@ -25,6 +25,7 @@ pub struct Timer { state: TimerState, tstart: Instant, // so we can blink alarm: Option>, + done: f32, } impl Timer { @@ -74,6 +75,7 @@ impl Timer { state: TimerState::Unstarted, tstart: updated, alarm, + done: 0.0, }; if cli.running { let cs = ChronoState { @@ -92,6 +94,7 @@ impl Timer { .font(FontId::monospace(tsize)) .color(Color32::WHITE) .background_color(Color32::LIGHT_GREEN); + self.done = 0.0; let (sender, rx) = channel(); one_row(ui, &[(start, NextTimerState::Running)], sender); @@ -119,9 +122,13 @@ impl Timer { std::thread::spawn(move || alarm(alarm_sound)); } self.state = TimerState::Finished; + self.done = 1.0; return; } + self.done = + (1.0 - (remaining.as_millis() as f32) / (self.duration.as_millis() as f32)).powf(3.1); + let (sender, rx) = channel(); { // if we're counting up, do the right thing diff --git a/src/util.rs b/src/util.rs index e065cb4..9e26ebe 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,6 @@ use std::{io::Cursor, time::Duration}; -use egui::{Color32, Direction, FontId, Layout, RichText}; +use eframe::egui::{Color32, Direction, FontId, Layout, RichText}; use egui_extras::{Size, Strip}; use rodio::{source::Source, Decoder, OutputStream};