Untitled
unknown
rust
4 years ago
3.1 kB
17
Indexable
#![warn(clippy::all)]
use eframe::{egui::*, epi};
use wasm_bindgen::prelude::*;
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
const RECT_SIZE: f32 = 30.0;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[derive(Copy, Clone, Debug)]
pub enum Cell {
Empty,
Wall,
Start,
End,
}
pub struct App {
grid: Vec<Cell>,
lines: Vec<Vec<Pos2>>,
}
impl Default for App {
fn default() -> Self {
Self {
grid: vec![],
lines: vec![vec![]],
}
}
}
impl epi::App for App {
fn name(&self) -> &str {
"Pathypath"
}
fn update(&mut self, ctx: &CtxRef, frame: &mut epi::Frame<'_>) {
CentralPanel::default().show(ctx, |ui| {
let available_size = ui.available_size();
let (mut painter_response, painter) =
ui.allocate_painter(available_size, Sense::drag());
let cell_count = (available_size.x * available_size.y) as usize;
let available_size =
Vec2::new(available_size.x / RECT_SIZE, available_size.y / RECT_SIZE);
if self.grid.len() < cell_count {
log("Initializing Grid Vector");
for _ in 0..=cell_count {
self.grid.push(Cell::Empty);
}
}
if let Some(pointer_pos) = painter_response.interact_pointer_pos() {
let index = ((pointer_pos.x / RECT_SIZE) * (pointer_pos.y / RECT_SIZE)) as usize;
log(&format!(
"{} x: {} y: {} {:?}",
index, pointer_pos.x, pointer_pos.y, pointer_pos
));
self.grid[index] = Cell::Wall;
painter_response.mark_changed();
}
let mut shapes = vec![];
let (mut x, mut y, mut i) = (0.0, 0.0, 0);
while y < available_size.y {
while x < available_size.x {
let rect = Rect::from_two_pos(
Pos2::new(x * RECT_SIZE, y * RECT_SIZE),
Pos2::new(x * RECT_SIZE + RECT_SIZE, y * RECT_SIZE + RECT_SIZE),
);
let shape = match self.grid[i] {
Cell::Wall => Shape::rect_filled(rect, 1.0, Color32::LIGHT_BLUE),
Cell::Empty | _ => {
Shape::rect_stroke(rect, 1.0, Stroke::new(0.1, Color32::LIGHT_BLUE))
}
};
shapes.push(shape);
x += 1.0;
i += 1;
}
y += 1.0;
x = 0.0;
}
painter.extend(shapes);
});
}
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn start(canvas_id: &str) -> Result<(), JsValue> {
let app = App::default();
eframe::start_web(canvas_id, Box::new(app))
}
Editor is loading...