Add better support for screen resizing

This commit is contained in:
Emi Simpson 2021-11-16 22:15:41 -05:00
parent 2033cb8156
commit e96e413c39
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
1 changed files with 84 additions and 23 deletions

View File

@ -1,7 +1,7 @@
pub mod util;
use macroquad::prelude::set_camera;
use macroquad::prelude::DVec2;
use macroquad::prelude::Rect;
use macroquad::prelude::Camera2D;
use core::mem::swap;
use std::collections::HashMap;
@ -39,12 +39,22 @@ const HILITE: bool = false;
#[macroquad::main("BasicShapes")]
async fn main() {
let mut dd = DelaunayDemo::default();
let mut camera = dd.get_camera();
set_camera(&camera);
let mut iter = 0;
loop {
if iter == 100 {
iter = 0;
dd.resize((screen_width()/screen_height()) as f64);
camera = dd.get_camera();
set_camera(&mut camera);
}
if is_mouse_button_pressed(MouseButton::Left) {
dd.click(mouse_position().into())
dd.click(camera.screen_to_world(mouse_position().into()));
}
if is_mouse_button_pressed(MouseButton::Right) {
let mouse_position: Vec2 = mouse_position().into();
let mouse_position: Vec2 = camera.screen_to_world(mouse_position().into());
println!("{}", mouse_position);
@ -59,9 +69,13 @@ async fn main() {
}
}
}
clear_background(BG);
dd.update();
//dd.draw_delaunay();
dd.draw_voronoi();
iter += 1;
next_frame().await;
}
}
@ -71,7 +85,9 @@ pub struct DelaunayDemo {
pub triangles: Vec<Triangle>,
adjacency: HashMap<Edge, (NeighborOne, NeighborTwo)>,
poisoned: bool,
random_state: u128
random_state: u128,
width: f64,
height: f64,
}
#[derive(Debug)]
@ -123,13 +139,13 @@ impl NeighborTwo {
impl Default for DelaunayDemo {
fn default() -> Self {
let height = screen_height() as f64;
let width = screen_width() as f64;
const HEIGHT: f64 = 900.0;
const WIDTH: f64 = 1200.0;
let nodes: Vec<NodeRef> = vec![
Arc::new(Cell::new((-5.0 - BOING_RADIUS, 0.0 - BOING_RADIUS).into())),
Arc::new(Cell::new((0.0 - BOING_RADIUS, height + BOING_RADIUS).into())),
Arc::new(Cell::new((width + BOING_RADIUS, 0.0 - BOING_RADIUS).into())),
Arc::new(Cell::new((width + BOING_RADIUS, height + BOING_RADIUS).into())),
Arc::new(Cell::new((0.0 - BOING_RADIUS, HEIGHT + BOING_RADIUS).into())),
Arc::new(Cell::new((WIDTH + BOING_RADIUS, 0.0 - BOING_RADIUS).into())),
Arc::new(Cell::new((WIDTH + BOING_RADIUS, HEIGHT + BOING_RADIUS).into())),
];
let edges = (
Edge(nodes[2].clone(), nodes[0].clone()),
@ -153,8 +169,14 @@ impl Default for DelaunayDemo {
(edges.3, (Border, (&triangles[1]).into())),
(edges.4, (Border, (&triangles[1]).into())),
].into();
let mut dd = DelaunayDemo {
nodes, adjacency, triangles, random_state: 1312_1312_1312, poisoned: false,
let dd = DelaunayDemo {
nodes,
adjacency,
triangles,
height: HEIGHT,
width: WIDTH,
random_state: 1312_1312_1312,
poisoned: false,
};
/*let n_v_nodes = ((height / 50.0) - 1.0).round();
@ -443,8 +465,50 @@ impl fmt::Debug for Triangle {
}
impl DelaunayDemo {
pub fn get_camera(&self) -> Camera2D {
let width = self.width as f32;
let height = self.height as f32;
Camera2D {
target: Vec2::new(width / 2.0, height / 2.0),
zoom: Vec2::new(2./width, 2./height),
.. Default::default()
}
}
pub fn resize(&mut self, aspect_ratio: f64) {
let minimum_dimension = f64::min(self.width, self.height);
let (new_width, new_height) = if aspect_ratio < 1.0 {
(minimum_dimension, minimum_dimension / aspect_ratio)
} else {
(minimum_dimension * aspect_ratio, minimum_dimension)
};
let resize_factor = DVec2::new(
new_width / self.width,
new_height / self.height,
);
if resize_factor != DVec2::ONE {
for node in &self.nodes {
let mut node_inner = node.get();
node_inner.0 *= resize_factor;
node.set(node_inner);
}
self.width = new_width;
self.height = new_height;
if let Err(msg) = self.re_delaunize() {
eprintln!("POISONED: {}", msg);
self.poisoned = true;
}
}
}
fn draw_delaunay(&self) {
clear_background(BG);
let mut highlight_segments = Vec::new();
for triangle in &self.triangles {
let (c, r) = triangle.circumcenter();
@ -467,10 +531,10 @@ impl DelaunayDemo {
let edge_1 = edge.1.get().as_f32();
if
edge_0.x < 0.0 || edge_0.x > screen_width()
|| edge_0.y < 0.0 || edge_0.y > screen_height()
|| edge_1.x < 0.0 || edge_1.x > screen_width()
|| edge_1.y < 0.0 || edge_1.y > screen_height()
edge_0.x < 0.0 || edge_0.x > self.width as f32
|| edge_0.y < 0.0 || edge_0.y > self.height as f32
|| edge_1.x < 0.0 || edge_1.x > self.width as f32
|| edge_1.y < 0.0 || edge_1.y > self.height as f32
{
continue; //don't draw container nodes
}
@ -489,7 +553,6 @@ impl DelaunayDemo {
}
fn draw_voronoi(&self) {
clear_background(BG);
for neighbors in self.adjacency.values() {
if let (Occupant(tri1), Friend(tri2)) = neighbors {
let (center1, _) = tri1.circumcenter();
@ -677,18 +740,16 @@ impl DelaunayDemo {
}
// Check for a bounce
let height = screen_height() as f64;
let width = screen_width() as f64;
if node_inner.0.y > height {
node_inner.0.y = 2.0 * height - node_inner.0.y;
if node_inner.0.y > self.height {
node_inner.0.y = 2.0 * self.height - node_inner.0.y;
node_inner.1.y = -node_inner.1.y;
}
if node_inner.0.y < 0.0 {
node_inner.0.y = -node_inner.0.y;
node_inner.1.y = -node_inner.1.y;
}
if node_inner.0.x > width {
node_inner.0.x = 2.0 * width - node_inner.0.x;
if node_inner.0.x > self.width {
node_inner.0.x = 2.0 * self.width - node_inner.0.x;
node_inner.1.x = -node_inner.1.x;
}
if node_inner.0.x < 0.0 {