aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs55
-rw-r--r--src/shuffle.rs99
-rw-r--r--src/state.rs0
-rw-r--r--src/text.rs28
-rw-r--r--src/time.rs24
-rw-r--r--src/ui.rs23
6 files changed, 206 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs
index d87720a..c997be1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,57 +1,80 @@
use sdl2::ttf;
-use sdl2::event::Event;
+use sdl2::event::{Event};
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
+use sdl2::rect::Rect;
+use sdl2::render::{Texture, TextureCreator};
pub mod time;
+pub mod shuffle;
+pub mod text;
+
+const WIDTH: u32 = 640;
+const HEIGHT: u32 = 480;
+const TITLE: &str = "rutikmer";
fn main() {
let sdl = sdl2::init().unwrap();
- let video_subsys = sdl.video().unwrap();
let ttf = ttf::init().unwrap();
- let font = ttf.load_font("font/FiraMono-Regular.ttf", 40).unwrap();
-
+ let video_subsys = sdl.video().unwrap();
let window = video_subsys
- .window("rutikmer", 640, 480)
+ .window(TITLE, WIDTH, HEIGHT)
+ .resizable()
.build()
.unwrap();
-
- let mut timer = time::Timer::new();
+ let font = ttf.load_font("font/FiraMono-Regular.ttf", 40).unwrap();
let mut canvas = window.into_canvas().build().unwrap();
- let texture_creator = canvas.texture_creator();
let mut event_pump = sdl.event_pump().unwrap();
+ let tex_creator = canvas.texture_creator();
+
+ let mut timer = time::Timer::new();
+ // let mut ui = ui::UI::new(WIDTH, HEIGHT);
+ //
+ let timer_rect = Rect::new(10, 10, 100, 40);
'running: loop {
for e in event_pump.poll_iter() {
match e {
Event::Quit {..} => break 'running,
- Event::KeyDown { keycode: Some(Keycode::Space), .. } => {
+ Event::KeyDown { keycode: Some(Keycode::Space), repeat: false, .. } => {
match timer.state {
time::State::Active => timer.stop(),
time::State::Inactive => timer.idle(),
time::State::Idle => {},
}
}
- Event::KeyUp { keycode: Some(Keycode::Space), .. } => {
+ Event::KeyUp { keycode: Some(Keycode::Space), repeat: false, .. } => {
if timer.state == time::State::Idle {
timer.start();
}
},
+
+ // Event::Window { win_event: WindowEvent::Resized(w, h), .. } =>
+ // ui.set_layout(w as u32, h as u32),
_ => {}
}
}
- match timer.state {
- time::State::Idle => canvas.set_draw_color(Color::RGB(100, 100, 0)),
- time::State::Active => canvas.set_draw_color(Color::RGB(0, 100, 0)),
- time::State::Inactive => canvas.set_draw_color(Color::RGB(0, 0, 0)),
- }
+ let bg_color = match timer.state {
+ time::State::Idle => Color::RGB(100, 100, 0),
+ time::State::Active => Color::RGB(0, 100, 0),
+ time::State::Inactive => Color::RGB(0, 0, 0),
+ };
+ canvas.set_draw_color(bg_color);
canvas.clear();
if timer.state != time::State::Idle {
- canvas.copy(&timer.to_texture(&font, &texture_creator), None, None).unwrap();
+ canvas.copy(&timer.to_texture(&font, &tex_creator, &bg_color),
+ None,
+ timer_rect)
+ .unwrap();
}
+ let s = shuffle::Move::string_sequence(10);
+ let shuff_tex = text::to_texture(&s, &font, &tex_creator, &bg_color);
+ canvas.copy(&shuff_tex, None, Rect::new(10, 100, 500, 40)).unwrap();
+
+
canvas.present();
std::thread::sleep(std::time::Duration::new(0, 3_000_000));
}
diff --git a/src/shuffle.rs b/src/shuffle.rs
index e69de29..8d421b6 100644
--- a/src/shuffle.rs
+++ b/src/shuffle.rs
@@ -0,0 +1,99 @@
+use rand::{distributions::{Distribution, Standard}, Rng};
+
+#[derive(PartialEq)]
+enum MoveDirection {
+ Front,
+ Back,
+ Down,
+ Up,
+ Right,
+ Left,
+}
+
+enum MoveModifier {
+ None,
+ Twice,
+ Prime,
+}
+
+pub struct Move {
+ direction: MoveDirection,
+ modifier: MoveModifier,
+}
+
+
+// https://stackoverflow.com/questions/48490049
+impl Distribution<MoveDirection> for Standard {
+ fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MoveDirection {
+ use MoveDirection::*;
+
+ match rng.gen_range(0, 6) {
+ 0 => Front,
+ 1 => Back,
+ 2 => Down,
+ 3 => Up,
+ 4 => Right,
+ _ => Left,
+ }
+ }
+}
+
+impl Distribution<MoveModifier> for Standard {
+ fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MoveModifier {
+ use MoveModifier::*;
+
+ match rng.gen_range(0, 3) {
+ 0 => None,
+ 1 => Twice,
+ _ => Prime,
+ }
+ }
+}
+
+impl Move {
+ pub fn sequence(n: usize) -> Vec<Move> {
+ let mut sequence: Vec<Move> = Vec::with_capacity(n);
+
+ while sequence.len() != n {
+ let direction = rand::random::<MoveDirection>();
+ let modifier = rand::random::<MoveModifier>();
+
+ if let Some(l) = sequence.last() {
+ if l.direction == direction {
+ continue;
+ }
+ }
+ sequence.push(Move { direction, modifier });
+ }
+ sequence
+ }
+
+ pub fn string_sequence(n: usize) -> String {
+ let seq = Move::sequence(n);
+ seq.iter().map(|m| m.to_string() + " ").collect::<Vec<String>>().join(" ")
+ }
+}
+
+use std::fmt;
+
+impl fmt::Display for Move {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ use MoveDirection::*;
+ use MoveModifier::*;
+
+ let letter = match self.direction {
+ Front => "F",
+ Back => "B",
+ Down => "D",
+ Up => "U",
+ Right => "R",
+ Left => "L",
+ };
+ let modifier = match self.modifier {
+ None => "",
+ Twice => "2",
+ Prime => "'",
+ };
+ write!(f, "{}{}", letter, modifier)
+ }
+}
diff --git a/src/state.rs b/src/state.rs
deleted file mode 100644
index e69de29..0000000
--- a/src/state.rs
+++ /dev/null
diff --git a/src/text.rs b/src/text.rs
new file mode 100644
index 0000000..10ca700
--- /dev/null
+++ b/src/text.rs
@@ -0,0 +1,28 @@
+use sdl2::ttf;
+use sdl2::render::{Texture, TextureCreator};
+use sdl2::pixels::Color;
+
+struct Factory<'a, T> {
+ font: ttf::Font<'a, 'a>,
+ creator: &'a TextureCreator<T>,
+ bg: Color,
+}
+
+impl Factory<'a, T> {
+ pub fn new(font: &ttf::Font<'a, 'a>, creator: &'a TextureCreator<T>, bg: Color) -> Factory {
+ Factory { font, creator, bg }
+ }
+}
+
+pub fn to_texture<'a, T>(
+ s: &'a String,
+ font: &ttf::Font,
+ tex_creator: &'a TextureCreator<T>,
+ bg: &Color
+) -> Texture<'a>
+{
+ let surface = font.render(s).shaded(Color::RGB(255, 255, 255), *bg).unwrap();
+ tex_creator.create_texture_from_surface(&surface).unwrap()
+}
+
+// pub fn width(s: &String
diff --git a/src/time.rs b/src/time.rs
index 2cdd768..eba7507 100644
--- a/src/time.rs
+++ b/src/time.rs
@@ -39,12 +39,22 @@ impl Timer {
self.state = State::Idle;
}
- pub fn to_texture<'a, T>(&'a self, font: &ttf::Font, texture_creator: &'a TextureCreator<T>) -> Texture {
- let rendered = if self.state == State::Active { self.time.elapsed().unwrap() } else { self.result };
-
- let surface = font.render(&rendered.as_millis().to_string())
- .solid(Color::RGB(255, 255, 255))
- .unwrap();
- texture_creator.create_texture_from_surface(&surface).unwrap()
+ pub fn to_texture<'a, T>(
+ &'a self,
+ font: &ttf::Font,
+ tex_creator: &'a TextureCreator<T>,
+ bg: &Color
+ ) -> Texture
+ {
+ let current = if self.state == State::Active {
+ self.time.elapsed().unwrap()
+ } else {
+ self.result
+ }.as_millis();
+
+ let s = format!("{}.{}", current / 1000, current % 1000);
+
+ let surface = font.render(&s).shaded(Color::RGB(255, 255, 255), *bg).unwrap();
+ tex_creator.create_texture_from_surface(&surface).unwrap()
}
}
diff --git a/src/ui.rs b/src/ui.rs
new file mode 100644
index 0000000..011e5ed
--- /dev/null
+++ b/src/ui.rs
@@ -0,0 +1,23 @@
+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);
+ }
+}