update theme editor to use new land generator implementation
authoralfadur
Thu, 01 Nov 2018 03:38:13 +0300
changeset 14054 3185fb34f3b5
parent 14053 38eb5937169e
child 14055 82956b3ba2ab
update theme editor to use new land generator implementation
rust/integral-geometry/src/lib.rs
rust/landgen/src/lib.rs
rust/landgen/src/template_based.rs
rust/theme-editor/src/main.rs
--- a/rust/integral-geometry/src/lib.rs	Thu Nov 01 01:07:26 2018 +0100
+++ b/rust/integral-geometry/src/lib.rs	Thu Nov 01 03:38:13 2018 +0300
@@ -167,6 +167,16 @@
     pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
         Self { x, y, width, height }
     }
+
+    #[inline]
+    pub fn size(&self) -> Size {
+        Size::new(self.width as usize, self.height as usize)
+    }
+
+    #[inline]
+    pub fn area(&self) -> usize {
+        self.size().area()
+    }
 }
 
 
--- a/rust/landgen/src/lib.rs	Thu Nov 01 01:07:26 2018 +0100
+++ b/rust/landgen/src/lib.rs	Thu Nov 01 03:38:13 2018 +0300
@@ -1,4 +1,4 @@
-mod template_based;
+pub mod template_based;
 
 extern crate integral_geometry;
 extern crate land2d;
@@ -9,6 +9,12 @@
     basic: T,
 }
 
+impl <T: Copy + PartialEq> LandGenerationParameters<T> {
+    pub fn new(zero: T, basic: T) -> Self {
+        Self { zero, basic }
+    }
+}
+
 pub trait LandGenerator {
     fn generate_land<T: Copy + PartialEq, I: Iterator<Item = u32>>(
         &self,
--- a/rust/landgen/src/template_based.rs	Thu Nov 01 01:07:26 2018 +0100
+++ b/rust/landgen/src/template_based.rs	Thu Nov 01 03:38:13 2018 +0300
@@ -46,7 +46,7 @@
     }
 }
 
-struct OutlineTemplate {
+pub struct OutlineTemplate {
     islands: Vec<Vec<Rect>>,
     fill_points: Vec<Point>,
     size: Size,
@@ -56,7 +56,54 @@
     is_negative: bool,
 }
 
-struct TemplatedLandGenerator {
+impl OutlineTemplate {
+    pub fn new(size: Size) -> Self {
+        OutlineTemplate {
+            size,
+            islands: Vec::new(),
+            fill_points: Vec::new(),
+            can_flip: false,
+            can_invert: false,
+            can_mirror: false,
+            is_negative: false
+        }
+    }
+
+    pub fn flippable(self) -> Self {
+        Self { can_flip: true, ..self }
+    }
+
+    pub fn mirrorable(self) -> Self {
+        Self { can_mirror: true, ..self }
+    }
+
+    pub fn invertable(self) -> Self {
+        Self { can_invert: true, ..self }
+    }
+
+    pub fn negative(self) -> Self {
+        Self { is_negative: true, ..self }
+    }
+
+    pub fn with_fill_points(self, fill_points: Vec<Point>) -> Self
+    {
+        Self { fill_points, ..self }
+    }
+
+    pub fn with_islands(mut self, islands: Vec<Vec<Rect>>) -> Self {
+        Self { islands, ..self }
+    }
+
+    pub fn add_fill_points(mut self, points: &[Point]) -> Self {
+        self.fill_points.extend_from_slice(points); self
+    }
+
+    pub fn add_island(mut self, island: &[Rect]) -> Self {
+        self.islands.push(island.into()); self
+    }
+}
+
+pub struct TemplatedLandGenerator {
     outline_template: OutlineTemplate,
 }
 
--- a/rust/theme-editor/src/main.rs	Thu Nov 01 01:07:26 2018 +0100
+++ b/rust/theme-editor/src/main.rs	Thu Nov 01 03:38:13 2018 +0300
@@ -7,7 +7,7 @@
     }
 };
 
-use integral_geometry::{Point, Size};
+use integral_geometry::{Point, Size, Rect};
 
 use rand::{
     thread_rng, RngCore, Rng,
@@ -15,6 +15,10 @@
 };
 
 use landgen::{
+    template_based::{
+        OutlineTemplate,
+        TemplatedLandGenerator
+    },
     LandGenerator,
     LandGenerationParameters
 };
@@ -35,6 +39,7 @@
             generator
         }
     }
+
     fn next(&mut self, parameters: LandGenerationParameters<u32>) -> Land2D<u32> {
         self.generator.generate_land(parameters, &mut self.rnd)
     }
@@ -66,6 +71,30 @@
 const HEIGHT: u32 = 512;
 const SIZE: Size = Size {width: 512, height: 512};
 
+fn point() -> Point {
+    Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32))
+}
+fn rect() -> Rect {
+    Rect::new(rnd(WIDTH as i32), rnd(HEIGHT as i32), rnd(128), rnd(128))
+}
+
+fn init_source() -> LandSource<TemplatedLandGenerator> {
+    let template = OutlineTemplate::new(SIZE)
+        .with_fill_points((0..32).map(|_| point()).collect())
+        .with_islands((0..16).map(|_| vec![rect()]).collect());
+
+    let generator = TemplatedLandGenerator::new(template);
+    LandSource::new(generator)
+}
+
+fn draw_random_lines(land: &mut Land2D<u32>) {
+    for i in 0..32 {
+        land.draw_thick_line(point(), point(), rnd(5), u32::max_value());
+
+        land.fill_circle(point(), rnd(60), u32::max_value());
+    }
+}
+
 fn main() {
     let sdl = sdl2::init().unwrap();
     let _image = sdl2::image::init(sdl2::image::INIT_PNG).unwrap();
@@ -77,18 +106,11 @@
         .position_centered()
         .build().unwrap();
 
-    let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap();
-
-    fn point() -> Point {
-        Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32))
-    }
+    let mut source = init_source();
+    let mut land = source.next(
+        LandGenerationParameters::new(0, u32::max_value()));
 
-    let mut land = Land2D::new(SIZE, 0);
-    for i in 0..32 {
-        land.draw_thick_line(point(), point(), rnd(5), u32::max_value());
-
-        land.fill_circle(point(), rnd(60), u32::max_value());
-    }
+    let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap();
 
     fill_texture(&mut land_surf, &land);