add an existing template to the theme editor
authoralfadur
Fri, 02 Nov 2018 23:33:22 +0300
changeset 14104 7845beb87cb5
parent 14103 a8e194ae26e8
child 14105 4d22be35cfa2
add an existing template to the theme editor
rust/theme-editor/src/main.rs
--- a/rust/theme-editor/src/main.rs	Fri Nov 02 22:55:15 2018 +0300
+++ b/rust/theme-editor/src/main.rs	Fri Nov 02 23:33:22 2018 +0300
@@ -65,39 +65,80 @@
     thread_rng().gen_range(T::default(), max)
 }
 
-const WIDTH: u32 = 512;
-const HEIGHT: u32 = 512;
-const SIZE: Size = Size {width: 512, height: 512};
+const WINDOW_WIDTH: u32 = 800;
+const WINDOW_HEIGHT: u32 = 600;
+const WINDOW_SIZE: Size = Size {width: WINDOW_WIDTH as usize, height: WINDOW_HEIGHT as usize};
+
+const PLAY_WIDTH: u32 = 3072;
+const PLAY_HEIGHT: u32 = 1424;
+const PLAY_SIZE: Size = Size {width: PLAY_WIDTH as usize, height: PLAY_HEIGHT as usize};
+
+const LAND_WIDTH: u32 = 4096;
+const LAND_HEIGHT: u32 = 2048;
+const LAND_SIZE: Size = Size {width: LAND_WIDTH as usize, height: LAND_HEIGHT as usize};
 
 fn point() -> Point {
-    Point::new(rnd(WIDTH as i32), rnd(HEIGHT as i32))
+    Point::new(rnd(LAND_WIDTH as i32), rnd(LAND_HEIGHT as i32))
 }
 fn rect() -> Rect {
-    Rect::new(rnd(WIDTH as i32), rnd(HEIGHT as i32), rnd(120) + 8, rnd(120) + 8)
+    Rect::new(rnd(LAND_WIDTH as i32), rnd(LAND_HEIGHT as i32), rnd(120) + 8, rnd(120) + 8)
 }
 
 fn land_rect() -> Rect {
-    Rect::at_origin(SIZE)
+    Rect::at_origin(PLAY_SIZE)
 }
 
-fn test_template() -> OutlineTemplate {
-    OutlineTemplate::new(SIZE)
+fn basic_template() -> OutlineTemplate {
+    OutlineTemplate::new(PLAY_SIZE)
         .with_fill_points(vec![land_rect().center()])
         .add_island(&land_rect().split_at(land_rect().center()))
 }
 
+macro_rules! pseudo_yaml {
+    [$({x: $x: tt, y: $y: tt, w: $w: tt, h: $h: tt}),*] => {
+        [$(Rect::new($x, $y, $w, $h)),*]
+    }
+}
+
+fn test_template() -> OutlineTemplate {
+    let island = pseudo_yaml![
+        {x: 810, y: 1424, w: 1, h: 1},
+        {x: 560, y: 1160, w: 130, h: 170},
+        {x: 742, y: 1106, w: 316, h: 150},
+        {x: 638, y: 786, w: 270, h: 180},
+        {x: 646, y: 576, w: 242, h: 156},
+        {x: 952, y: 528, w: 610, h: 300},
+        {x: 1150, y: 868, w: 352, h: 324},
+        {x: 1050, y: 1424, w: 500, h: 1},
+        {x: 1650, y: 1500, w: 1, h: 1},
+        {x: 1890, y: 1424, w: 1, h: 1},
+        {x: 1852, y: 1304, w: 74, h: 12},
+        {x: 1648, y: 975, w: 68, h: 425},
+        {x: 1826, y: 992, w: 140, h: 142},
+        {x: 1710, y: 592, w: 150, h: 350},
+        {x: 1988, y: 594, w: 148, h: 242},
+        {x: 2018, y: 872, w: 276, h: 314},
+        {x: 2110, y: 1250, w: 130, h: 86},
+        {x: 2134, y: 1424, w: 1, h: 1}
+    ];
+
+    OutlineTemplate::new(PLAY_SIZE)
+        .add_island(&island)
+        .add_fill_points(&[Point::new(1023, 0)])
+}
+
+
 fn init_source() -> LandSource<TemplatedLandGenerator> {
     let template = test_template();
-
     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(Line::new(point(), point()), rnd(5), u32::max_value());
+        land.draw_thick_line(Line::new(point(), point()), rnd(5), 128);
 
-        land.fill_circle(point(), rnd(60), u32::max_value());
+        land.fill_circle(point(), rnd(60), 128);
     }
 }
 
@@ -108,21 +149,23 @@
 
     let mut pump = sdl.event_pump().unwrap();
     let video = sdl.video().unwrap();
-    let window = video.window("Theme Editor", WIDTH, HEIGHT)
+    let window = video.window("Theme Editor", WINDOW_WIDTH, WINDOW_HEIGHT)
         .position_centered()
         .build().unwrap();
 
     let mut source = init_source();
     let mut land = source.next(
         LandGenerationParameters::new(0, u32::max_value()));
+    draw_random_lines(&mut land);
 
-    let mut land_surf = Surface::new(WIDTH, HEIGHT, PixelFormatEnum::ARGB8888).unwrap();
+
+    let mut land_surf = Surface::new(LAND_WIDTH, LAND_HEIGHT, PixelFormatEnum::ARGB8888).unwrap();
 
     fill_texture(&mut land_surf, &land);
 
     let mut win_surf = window.surface(&pump).unwrap();
-    let win_rect = win_surf.rect();
-    land_surf.blit(land_surf.rect(), &mut win_surf, win_rect).unwrap();
+    let dest_rect = win_surf.rect();
+    land_surf.blit_scaled(land_surf.rect(), &mut win_surf, dest_rect).unwrap();
     win_surf.update_window();
 
     'pool: loop {