Draw terrains within their relevant positions on the hex grid #22

Merged
savanni merged 3 commits from terrain-tiling into main 2023-02-12 03:24:54 +00:00
1 changed files with 21 additions and 10 deletions
Showing only changes of commit 017733bfec - Show all commits

View File

@ -197,14 +197,19 @@ impl ObjectImpl for HexGridWindowPrivate {
context.set_line_width(2.); context.set_line_width(2.);
let clip_path = hexagon_path(context, 100., 88.); context.save().unwrap();
context.set_source_pixbuf(&image_deep_water, 0., 0.); context.append_path(&hexagon_path(context, 0., 0., 100., 88.));
context.append_path(&clip_path);
context.clip(); context.clip();
context.set_source_pixbuf(&image_deep_water, 0., 0.);
context.paint().expect("paint should succeed"); context.paint().expect("paint should succeed");
context.restore().unwrap();
context.save().unwrap();
context.append_path(&hexagon_path(context, 150., 0., 100., 88.));
context.clip();
context.set_source_pixbuf(&image_shallow_water, 150., 0.); context.set_source_pixbuf(&image_shallow_water, 150., 0.);
context.paint().expect("paint should succeed"); context.paint().expect("paint should succeed");
context.restore().unwrap();
context.set_source_pixbuf(&image_grasslands, 300., 0.); context.set_source_pixbuf(&image_grasslands, 300., 0.);
context.paint().expect("paint should succeed"); context.paint().expect("paint should succeed");
@ -275,15 +280,21 @@ fn draw_hexagon(context: &Context, center_x: f64, center_y: f64, radius: f64) {
context.close_path(); context.close_path();
} }
fn hexagon_path(context: &Context, width: f64, height: f64) -> Path { fn hexagon_path(
context: &Context,
translate_x: f64,
translate_y: f64,
width: f64,
height: f64,
) -> Path {
context.new_path(); context.new_path();
let points = hexagon(width, height); let points = hexagon(width, height);
context.move_to(points[0].0, points[0].1); context.move_to(translate_x + points[0].0, translate_y + points[0].1);
context.line_to(points[1].0, points[1].1); context.line_to(translate_x + points[1].0, translate_y + points[1].1);
context.line_to(points[2].0, points[2].1); context.line_to(translate_x + points[2].0, translate_y + points[2].1);
context.line_to(points[3].0, points[3].1); context.line_to(translate_x + points[3].0, translate_y + points[3].1);
context.line_to(points[4].0, points[4].1); context.line_to(translate_x + points[4].0, translate_y + points[4].1);
context.line_to(points[5].0, points[5].1); context.line_to(translate_x + points[5].0, translate_y + points[5].1);
context.copy_path().expect("to successfully copy a path") context.copy_path().expect("to successfully copy a path")
} }