diff --git a/examples/font.rs b/examples/font.rs index 0ddfad1..22b4cc5 100644 --- a/examples/font.rs +++ b/examples/font.rs @@ -7,41 +7,29 @@ use FontDirection::{LeftRight, TopDown}; fn main() { let context = Context::create(); context.save(); - context.translate(70., 50.); - // context.scale(8.); let font = AsteroidFont::new(); let line = font.get_text("Hey there!".to_string(), 12., LeftRight); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.draw_line_string(&line_string![(x : 0., y:14.), (x: 1000. , y : 14.)]); context.translate(0.0, 18.); let line = font.get_text("How are you?".to_string(), 22., LeftRight); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.translate(0.0, 28.); let line = font.get_text("Here a bitter text!".to_string(), 50., LeftRight); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.translate(0.0, 56.); let line = font.get_text("0123456789".to_string(), 50., LeftRight); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.restore(); context.translate(30., 50.); let line = font.get_text("0123456789".to_string(), 30., TopDown); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.render(); } diff --git a/src/bin/asteroids.rs b/src/bin/asteroids.rs index fc023f2..9f01cc2 100644 --- a/src/bin/asteroids.rs +++ b/src/bin/asteroids.rs @@ -3,10 +3,10 @@ use geo::algorithm::rotate::Rotate; use geo::prelude::Translate; use geo::rotate::RotatePoint; use geo::LineString; -use geo::{Coordinate, MultiPolygon, Point, Polygon}; +use geo::{polygon, Coordinate, MultiPolygon, Point, Polygon}; use geo_clipper::Clipper; -use polygon_art::MultiPolygonExt; use polygon_art::{load_multipolygon_from_svg_include, Context}; +use polygon_art::{AsteroidFont, FontDirection, MultiPolygonExt}; use rand::prelude::StdRng; use rand::Rng; @@ -65,9 +65,11 @@ fn main() { include_str!("../../pool/asteroids/ship3.svg"), ]; let chosen_ship = rng.gen_range(0..3); - let ship = load_multipolygon_from_svg_include(ship_yard[chosen_ship]) + let raw_ship = load_multipolygon_from_svg_include(ship_yard[chosen_ship]) .unwrap() - .rotate(90.) // rotate to scale ship with width + // rotate to scale ship with width + .rotate(90.); + let ship = raw_ship .scale_to_width(ship_length) .translate_center() .rotate(rotation as f64); @@ -76,9 +78,50 @@ fn main() { context.draw_multipolygon(&ship); context.restore(); - // drawing and clipping + // draw score board + let font = AsteroidFont::new(); + let random_seed_string = context.seed.to_string(); + let font_size = 20.; + let (x_font_size, y_font_size) = font.get_text_scale_factor(font_size); + let score_board_letters = random_seed_string.len(); let window = context.frame(frame_offset, frame_offset, frame_offset, frame_offset); - context.draw_polygon(&window); + let window_height = frame_offset + y_font_size; + let window_width = frame_offset + score_board_letters as f64 * x_font_size; + let score_board: Polygon = polygon![(x:0., y:0.), (x: window_width, y:0.), (x: window_width, y: window_height), (x: 0., y: window_height)]; + let score_board_lines = font.get_text(random_seed_string, font_size, FontDirection::LeftRight); + context.save(); + context.translate(frame_offset, frame_offset); + context.draw_line_strings(&score_board_lines); + context.restore(); + + // draw life board + let life = rng.gen_range(2..7) as f64; + let life_board: Polygon = polygon![ + (x:width, y:0.), + (x: width - frame_offset - life * x_font_size - 3., y:0.), + (x: width - frame_offset - life * x_font_size - 3., y:frame_offset + y_font_size + 3.), + (x: width , y:frame_offset + y_font_size + 3.) + ]; + // todo translate_top + // todo translate_left + let life_ship = raw_ship + .scale_to_width(font_size) + .rotate(-90.) + .translate_center_y(); + for l in 1..(1 + life as i32) { + context.save(); + context.translate( + width - frame_offset - (x_font_size * l as f64) as f64, + frame_offset + y_font_size / 2., + ); + context.draw_multipolygon(&life_ship); + context.restore(); + } + + // draw window + let window = window.difference(&score_board, 100.); + let window = window.difference(&life_board, 100.); + context.draw_multipolygon(&window); for asteroid in asteroids.iter() { context.draw_multipolygon(&asteroid.intersection(&window, 100.)); diff --git a/src/context.rs b/src/context.rs index 195dc0e..5b46bec 100644 --- a/src/context.rs +++ b/src/context.rs @@ -19,6 +19,7 @@ pub struct Context { pub width: f64, pub line_size: f64, pub pseudo_random_number_generator: Box, + pub seed: u64, } impl Context { @@ -64,6 +65,12 @@ impl Context { context.stroke(); } + pub fn draw_line_strings(&self, line_strings: &Vec>) { + for lines in line_strings { + self.draw_line_string(&lines); + } + } + /// restore state which was saved before by the context pub fn restore(&self) { self.render_container.context().restore(); @@ -171,6 +178,7 @@ impl Context { line_size, pseudo_random_number_generator: Box::new(rng), render_container, + seed: random_seed, } } } diff --git a/src/font.rs b/src/font.rs index 2b61642..34e5915 100644 --- a/src/font.rs +++ b/src/font.rs @@ -101,6 +101,12 @@ impl AsteroidFont { polygons } + /// size of space (per letter) + pub fn get_text_scale_factor(&self, size: f64) -> (f64, f64) { + let scale_factor = size / 12.; + (10.2 * scale_factor, 14.2 * scale_factor) + } + pub fn get_text( &self, text: String, @@ -111,13 +117,14 @@ impl AsteroidFont { let mut count = 0.; for char in text.chars() { let letter = self.get_letter(&char, size); + let (x_translate_factor, y_translate_factor) = self.get_text_scale_factor(size); for character in letter.iter() { match direction { FontDirection::LeftRight => { - letters.push(character.translate(count * 10.0 * size / 12., 0.)) + letters.push(character.translate(count * x_translate_factor, 0.)) } FontDirection::TopDown => { - letters.push(character.translate(0., count * 14.0 * size / 12.)) + letters.push(character.translate(0., count * y_translate_factor)) } } }