diff options
| -rw-r--r-- | Cargo.lock | 8 | ||||
| -rw-r--r-- | src/history.rs | 14 | ||||
| -rw-r--r-- | src/main.rs | 147 | ||||
| -rw-r--r-- | src/scramble.rs | 12 | ||||
| -rw-r--r-- | src/text.rs | 64 | ||||
| -rw-r--r-- | src/time.rs | 14 | ||||
| -rw-r--r-- | src/ui.rs | 23 |
7 files changed, 161 insertions, 121 deletions
@@ -106,9 +106,9 @@ checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" [[package]] name = "num-integer" -version = "0.1.42" +version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" +checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" dependencies = [ "autocfg", "num-traits", @@ -116,9 +116,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" +checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" dependencies = [ "autocfg", ] diff --git a/src/history.rs b/src/history.rs index 6b02b85..3238543 100644 --- a/src/history.rs +++ b/src/history.rs @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* . */ +/* history.rs / \ */ +/* / \ */ +/* By: charles <charles.cabergs@gmail.com> /o o \ */ +/* / v \ */ +/* Created: 2020/06/25 13:24:10 by charles / _ \ */ +/* Updated: 2020/06/25 13:24:12 by charles '-----------' */ +/* */ +/* ************************************************************************** */ + use std::time::Duration; use std::str; use std::num; @@ -71,7 +83,7 @@ impl History { writter.flush().unwrap(); } - pub fn summary(&self, n: usize) -> Vec<String> { + pub fn summarize(&self, n: usize) -> Vec<String> { self.0.iter().skip(self.0.len() - n).map(|Entry{time, ..}| time.to_string()).collect() } } diff --git a/src/main.rs b/src/main.rs index e106cb2..eab790e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* . */ +/* main.rs / \ */ +/* / \ */ +/* By: charles <charles.cabergs@gmail.com> /o o \ */ +/* / v \ */ +/* Created: 2020/06/25 11:42:17 by charles / _ \ */ +/* Updated: 2020/06/25 13:24:00 by charles '-----------' */ +/* */ +/* ************************************************************************** */ + use sdl2::ttf; -use sdl2::event::{Event}; +use sdl2::event::{Event, WindowEvent}; use sdl2::keyboard::Keycode; use sdl2::pixels::Color; use sdl2::rect::Rect; +use sdl2::render::{Texture, TextureCreator}; pub mod time; pub mod scramble; -pub mod text; pub mod history; use scramble::Scramble; @@ -15,33 +27,43 @@ use history::History; const WIDTH: u32 = 640; const HEIGHT: u32 = 480; const TITLE: &str = "rutikmer"; -const FONT_SIZE: u32 = 40; +const FONT_SIZE: u32 = 20; + +const CENTER_Y: u32 = HEIGHT / 2; +const CENTER_X: u32 = WIDTH / 2; +const WHITE: Color = Color::RGB(0xff, 0xff, 0xff); const GREEN: Color = Color::RGB(0x1B, 0x5E, 0x20); const ORANGE: Color = Color::RGB(0xEF, 0x6C, 0x00); const BLACK: Color = Color::RGB(0x00, 0x00, 0x00); +fn str_to_tex<'a, T>(s: &str, font: &ttf::Font, creator: &'a TextureCreator<T>, bg: Color) -> Texture<'a> { + let surface = font.render(s).shaded(WHITE, bg).unwrap(); + creator.create_texture_from_surface(&surface).unwrap() +} + +fn pad_rect(rect: &mut Rect, pad: u32) { + rect.resize(rect.width() - pad, rect.height() - pad); + rect.offset(pad as i32 / 2, pad as i32 / 2); +} + fn main() { let sdl = sdl2::init().unwrap(); let ttf = ttf::init().unwrap(); let video_subsys = sdl.video().unwrap(); let window = video_subsys .window(TITLE, WIDTH, HEIGHT) - .resizable() .build() .unwrap(); let font = ttf.load_font("font/FiraMono-Regular.ttf", FONT_SIZE as u16).unwrap(); - let hist = History::from_csv("history.csv"); - let mut canvas = window.into_canvas().build().unwrap(); let mut event_pump = sdl.event_pump().unwrap(); let tex_creator = canvas.texture_creator(); + let hist = History::from_csv("history.csv"); let mut timer = time::Timer::new(); - let timer_rect = Rect::new(10, 10, 100, 40); - let mut text_factory = text::Factory::new(&font, &tex_creator, BLACK, FONT_SIZE); - - let mut scramble_str = Scramble::new_rand(10).to_string(); + let mut scramble_tex = str_to_tex(&Scramble::new_rand(10).to_string(), &font, &tex_creator, BLACK); + let mut timer_tex = str_to_tex(&timer.to_string(), &font, &tex_creator, BLACK); 'running: loop { for e in event_pump.poll_iter() { @@ -49,12 +71,14 @@ fn main() { Event::Quit {..} => break 'running, Event::KeyDown { keycode: Some(Keycode::Space), repeat: false, .. } => { match timer.state { - time::State::Idle => {}, - time::State::Active => timer.stop(), time::State::Inactive => { timer.idle(); - scramble_str = Scramble::new_rand(10).to_string(); + scramble_tex = str_to_tex(&Scramble::new_rand(10).to_string(), + &font, &tex_creator, BLACK); + timer_tex = str_to_tex(&timer.to_string(), &font, &tex_creator, BLACK); }, + time::State::Idle => {}, + time::State::Active => timer.stop(), } } Event::KeyUp { keycode: Some(Keycode::Space), repeat: false, .. } => { @@ -62,34 +86,101 @@ fn main() { timer.start(); } }, - - // Event::Window { win_event: WindowEvent::Resized(w, h), .. } => - // ui.set_layout(w as u32, h as u32), _ => {} } } let bg_color = match timer.state { + time::State::Inactive => BLACK, time::State::Idle => ORANGE, time::State::Active => GREEN, - time::State::Inactive => BLACK, }; canvas.set_draw_color(bg_color); - text_factory.set_bg(bg_color); canvas.clear(); - if timer.state != time::State::Idle { - canvas.copy(&text_factory.from_string(&timer.to_string()), None, timer_rect).unwrap(); - } + match timer.state { + time::State::Inactive => { - if timer.state == time::State::Inactive { - canvas.copy(&text_factory.from_string(&scramble_str), None, Rect::new(10, 100, 500, 40)).unwrap(); - let sum = hist.summary(3); - for (i, s) in sum.iter().enumerate() { - canvas.copy(&text_factory.from_string(&s), None, Rect::new(10, 200 + 40 * (i as i32), 200, 30)).unwrap(); - } + let mut history_rect = Rect::new(0, 0, WIDTH/ 3, HEIGHT); + let mut scramble_rect = Rect::new(WIDTH as i32 / 3, 0, 2 * WIDTH / 3, HEIGHT / 3); + let mut timer_rect = Rect::new(WIDTH as i32 / 3, HEIGHT as i32 / 3, + 2 * WIDTH / 3, 2 * HEIGHT / 3); + canvas.set_draw_color(WHITE); + canvas.draw_rect(history_rect).unwrap(); + canvas.draw_rect(scramble_rect).unwrap(); + canvas.draw_rect(timer_rect).unwrap(); + + pad_rect(&mut history_rect, 10); + pad_rect(&mut scramble_rect, 20); + pad_rect(&mut timer_rect, 40); + scramble_rect.set_height(FONT_SIZE * 2); + timer_rect.set_height(FONT_SIZE * 2); + timer_rect.set_width(timer.to_string().len() as u32 * FONT_SIZE); + + canvas.copy(&scramble_tex, None, scramble_rect).unwrap(); + canvas.copy(&timer_tex, None, timer_rect).unwrap(); + + let mut entry_rect = history_rect; + entry_rect.set_height(entry_rect.height() / 5); + for entry in hist.summarize(5) { + let entry_tex = str_to_tex(&entry.to_string(), &font, &tex_creator, BLACK); + canvas.copy(&entry_tex, None, entry_rect).unwrap(); + entry_rect.set_y(entry_rect.y() + history_rect.height() as i32 / 5); + } + }, + time::State::Idle => {}, + time::State::Active => { + let s = timer.to_string(); + timer_tex = str_to_tex(&s, &font, &tex_creator, GREEN); + let h: u32 = FONT_SIZE * 2; + let w: u32 = s.len() as u32 * FONT_SIZE; + let dest = Rect::new((CENTER_X - w / 2) as i32, (CENTER_Y - h / 2) as i32, w, h); + canvas.copy(&timer_tex, None, dest).unwrap(); + }, } canvas.present(); - std::thread::sleep(std::time::Duration::new(0, 30_000_000)); + if timer.state != time::State::Active { + std::thread::sleep(std::time::Duration::new(0, 40_000_000)); + } else { + std::thread::sleep(std::time::Duration::new(0, 10_000_000)); + } } } + +/* inactive (all black) + * +------------+---------------------+ + * | history | scramble | + * | | | + * | | | + * | | | + * | |---------------------| + * | | | + * | | timer | + * | | | + * | | | + * +------------+---------------------+ + * idle (all yellow) + * +------------+---------------------+ + * | | + * | | + * | | + * | | + * | | + * | | + * | | + * | | + * | | + * +------------+---------------------+ + * active (all green) + * +------------+---------------------+ + * | | + * | | + * | | + * | timer | + * | | + * | | + * | | + * | | + * | | + * +------------+---------------------+ + */ diff --git a/src/scramble.rs b/src/scramble.rs index abee6a6..061341e 100644 --- a/src/scramble.rs +++ b/src/scramble.rs @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* . */ +/* scramble.rs / \ */ +/* / \ */ +/* By: charles <charles.cabergs@gmail.com> /o o \ */ +/* / v \ */ +/* Created: 2020/06/25 13:24:17 by charles / _ \ */ +/* Updated: 2020/06/25 13:24:18 by charles '-----------' */ +/* */ +/* ************************************************************************** */ + use std::fmt; use std::str; diff --git a/src/text.rs b/src/text.rs deleted file mode 100644 index e62db3d..0000000 --- a/src/text.rs +++ /dev/null @@ -1,64 +0,0 @@ -use sdl2::ttf; -use sdl2::render::{Canvas, Texture, TextureCreator}; -use sdl2::pixels::Color; -use sdl2::rect::Rect; - -const WHITE: Color = Color::RGB(255, 255, 255); - -pub struct Factory<'a, T> { - font: &'a ttf::Font<'a, 'a>, - creator: &'a TextureCreator<T>, - bg: Color, - font_size: u32, -} - -impl<'a, T> Factory<'a, T> { - pub fn new(font: &'a ttf::Font, - creator: &'a TextureCreator<T>, - bg: Color, - font_size: u32 - ) -> Factory<'a, T> - { - Factory { font, creator, bg, font_size } - } - - pub fn from_string(&self, s: &str) -> Texture - { - let surface = self.font.render(s).shaded(WHITE, self.bg).unwrap(); - self.creator.create_texture_from_surface(&surface).unwrap() - } - - pub fn set_bg(&mut self, bg: Color) { - self.bg = bg; - } -} - -struct Frame<'a> { - width: u32, - height: u32, - texture: Texture<'a>, -} - -impl<'a> Frame<'a> { - fn new<T>(s: &'a str, factory: &'a Factory<T>) -> Frame<'a> { - Frame{ - width: s.len() as u32 * factory.font_size, - height: factory.font_size, - texture: factory.from_string(s) - } - } - - fn to_rect(&self, x: i32, y: i32) -> Rect { - Rect::new(x, y, self.width, self.height) - } -} - -trait Framable { - fn to_frame<'a>(&'a self) -> Frame<'a>; - - fn put_canvas<T: sdl2::render::RenderTarget>(&self, canvas: &mut Canvas<T>, x: i32, y: i32) { - let frame = self.to_frame(); - let rect = frame.to_rect(x, y); - canvas.copy(&frame.texture, None, rect); - } -} diff --git a/src/time.rs b/src/time.rs index 791ff2e..e9fabe9 100644 --- a/src/time.rs +++ b/src/time.rs @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* . */ +/* time.rs / \ */ +/* / \ */ +/* By: charles <charles.cabergs@gmail.com> /o o \ */ +/* / v \ */ +/* Created: 2020/06/25 13:24:24 by charles / _ \ */ +/* Updated: 2020/06/25 13:24:24 by charles '-----------' */ +/* */ +/* ************************************************************************** */ + use std::time::{SystemTime, Duration}; #[derive(PartialEq)] @@ -47,6 +59,6 @@ impl fmt::Display for Timer { self.result }.as_millis(); - write!(f, "{}.{}", current / 1000, current % 1000) + write!(f, "{:0>2}.{:0>3}", current / 1000, current % 1000) } } diff --git a/src/ui.rs b/src/ui.rs deleted file mode 100644 index 011e5ed..0000000 --- a/src/ui.rs +++ /dev/null @@ -1,23 +0,0 @@ -use sdl2::rect::Rect; - -pub struct UI { - pub history_rect: Rect, - pub shuffle_rect: Rect, - pub timer_rect: Rect, -} - -impl UI { - pub fn new(width: u32, height: u32) -> UI { - let default = Rect::new(0, 0, 0, 0); - let mut ret = UI {history_rect: default, shuffle_rect: default, timer_rect:default}; - ret.set_layout(width, height); - ret - } - - pub fn set_layout(&mut self, width: u32, height: u32) { - self.history_rect = Rect::new(0, 0, width / 3, height); - self.shuffle_rect = Rect::new((width / 3) as i32, 0, width - width / 3, height / 4); - self.timer_rect = Rect::new((width / 3) as i32, (height / 4 + (height - height / 4) / 2) as i32, - 100, 40); - } -} |
