From 2e85730892727a0552bf1cd0f6e360e1112ce35d Mon Sep 17 00:00:00 2001 From: sdasda7777 <17746796+sdasda7777@users.noreply.github.com> Date: Fri, 8 Dec 2023 13:45:59 +0100 Subject: [PATCH] Attempt to optimize WASM build --- Cargo.toml | 2 + src/main.rs | 342 ++++++++++++++++++++++++++-------------------------- 2 files changed, 171 insertions(+), 173 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 04101ec..ffa418f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,8 @@ web-time = "0.2" getrandom = { version = "0.2", features = ["js"] } wasm-bindgen = "=0.2.84" wasm-bindgen-futures = "0.4" +# Figure this out later +# ehttp = "0.3" [profile.release] opt-level = 2 # fast and small wasm diff --git a/src/main.rs b/src/main.rs index 376df67..e1f2ee2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ use eframe::{egui, emath::Align2}; use eframe::egui::{Button, containers::panel::TopBottomPanel, Key, KeyboardShortcut, menu, Modifiers, PointerButton, Response, RichText, Sense}; use eframe::epaint::{Color32, FontId, Pos2, Rect, Rounding, Shadow, Shape, Stroke}; -use std::{cmp::min, fs, env}; +use std::{cmp::min, fs}; use web_time::SystemTime; use toml::Table; @@ -73,6 +73,8 @@ fn main() -> Result<(), eframe::Error> { let options = eframe::NativeOptions { ..Default::default() }; + let config_content = fs::read_to_string("config.toml").unwrap_or_else(|_| "".into()); + eframe::run_native( "Minesweeper6D", options, @@ -83,7 +85,7 @@ fn main() -> Result<(), eframe::Error> { style.visuals.window_rounding = Rounding::ZERO; style.visuals.window_shadow = Shadow::NONE; }); - Box::::default() + Box::new(MinesweeperViewController::new(config_content)) }), ) } @@ -95,13 +97,22 @@ fn main() { eframe::WebLogger::init(log::LevelFilter::Debug).ok(); let web_options = eframe::WebOptions::default(); - + let config_content = "".into(); + wasm_bindgen_futures::spawn_local(async { eframe::WebRunner::new() .start( "the_canvas_id", // hardcode it web_options, - Box::new(|cc| Box::::default()), + Box::new(|cc| { + cc.egui_ctx.style_mut(|style| { + style.visuals.menu_rounding = Rounding::ZERO; + style.visuals.popup_shadow = Shadow::NONE; + style.visuals.window_rounding = Rounding::ZERO; + style.visuals.window_shadow = Shadow::NONE; + }); + Box::new(MinesweeperViewController::new(config_content)) + }), ) .await .expect("failed to start eframe"); @@ -148,168 +159,9 @@ struct MinesweeperViewController { } impl MinesweeperViewController { - fn reset(&mut self) { - self.game = None; - self.cursor_mode = CursorMode::ProbeAndMark; - } - - fn start(&mut self, initial: [usize; DIMENSIONS_COUNT]) { - self.start_time = Some(SystemTime::now()); - self.end_time = None; - if let Some(seed) = &self.current_initial_settings.seed { - self.game = Some(GameBoard::new(self.current_initial_settings.size, - self.current_initial_settings.wrap, - self.current_initial_settings.mines, - None, - u64::from_str_radix(&seed, 16).ok())); - self.game.as_mut().unwrap().probe_at(initial, true); - } else { - self.game = Some(GameBoard::new(self.current_initial_settings.size, - self.current_initial_settings.wrap, - self.current_initial_settings.mines, - Some(initial), - None)); - } - } - - // Translate and Scale from screen coordinates to cell coordinates - // Uses modular cutoff to decide in constant time whether mouse is over any cell: - // - // If current position modulo (x*cell+(x-1)*space_1+space_2) - // is larger than (x*cell+(x-1)*space_1), it must be outside a cell, otherwise repeat: - // | | | | | | | | | - // | cell | space_1 | cell | space_1 | cell | space_1 | cell | space_2 | - // | | | | | | | | | - fn get_coords(&self, pos: Pos2) -> Option<[usize; DIMENSIONS_COUNT]> { - if pos.x < self.view_origin.x || pos.y < self.view_origin.y { - return None; - } - - let [c_xx, c_yy, c_zz, c_uu, c_vv, c_ww] = self.current_initial_settings.size; - let [sp_xx, sp_yy, sp_zz, sp_uu, sp_vv, sp_ww] = self.tile_spacings; - - // Sizes of blocks (cells + inner spacing) - let x_block_size = c_xx as f32 * self.cell_edge + (c_xx - 1) as f32 * sp_xx; - let y_block_size = c_yy as f32 * self.cell_edge + (c_yy - 1) as f32 * sp_yy; - let z_block_size = c_zz as f32 * x_block_size + (c_zz - 1) as f32 * sp_zz; - let u_block_size = c_uu as f32 * y_block_size + (c_uu - 1) as f32 * sp_uu; - // let v_block_size = c_vv as f32 * z_block_size + (c_vv - 1) as f32 * sp_vv; - // let w_block_size = c_ww as f32 * u_block_size + (c_ww - 1) as f32 * sp_ww; - - // Get logical points - let (dx, dy) = ((pos.x - self.view_origin.x) / self.zoom_factor, - (pos.y - self.view_origin.y) / self.zoom_factor); - - // Modulo largest period - let (dx_m1, dy_m1) = (dx as f32 % (z_block_size + sp_vv), - dy as f32 % (u_block_size + sp_ww)); - if dx_m1 > z_block_size || dy_m1 > u_block_size { - return None; - } - // Modulo middle period - let (dx_m2, dy_m2) = (dx_m1 % (x_block_size + sp_zz), - dy_m1 % (y_block_size + sp_uu)); - if dx_m2 > x_block_size || dy_m2 > y_block_size { - return None; - } - // Modulo smallest period - let (dx_m3, dy_m3) = (dx_m2 % (self.cell_edge + sp_xx), - dy_m2 % (self.cell_edge + sp_yy)); - if dx_m3 > self.cell_edge || dy_m3 > self.cell_edge { - return None; - } - - // Is cell-like, get coords - let (xx, yy) = ((dx_m2 / (self.cell_edge + sp_xx)) as usize, - (dy_m2 / (self.cell_edge + sp_yy)) as usize); - let (zz, uu) = ((dx_m1 / (x_block_size + sp_zz)) as usize, - (dy_m1 / (y_block_size + sp_uu)) as usize); - let (vv, ww) = ((dx / (z_block_size + sp_vv)) as usize, - (dy / (u_block_size + sp_ww)) as usize); - - // Check if actual cell within grid bounds - if xx >= c_xx || yy >= c_yy || zz >= c_zz || uu >= c_uu || vv >= c_vv || ww >= c_ww { - return None; - } - - return Some([xx, yy, zz, uu, vv, ww]); - } - - // Scale and Translate from logical points to screen coordinates - fn sc_tr(&self, xx: f32, yy: f32) -> Pos2 { - Pos2::new(xx * self.zoom_factor, yy * self.zoom_factor) + self.view_origin.to_vec2() - } - - // Cumulative spacing in screen axis directions - fn cum_spc_x(&self, xx: usize, zz: usize, vv: usize) -> f32 { - let [c_xx, _, _, _, _, _] = self.current_initial_settings.size; - let [sp_xx, _, sp_zz, _, sp_vv, _] = self.tile_spacings; - return (xx+zz*(c_xx-1)+vv*(c_xx-1)) as f32*sp_xx + (zz+vv*(c_xx-1)) as f32*sp_zz + vv as f32*sp_vv; - } - fn cum_spc_y(&self, yy: usize, uu: usize, ww: usize) -> f32 { - let [_, c_yy, _, _, _, _] = self.current_initial_settings.size; - let [_, sp_yy, _, sp_uu, _, sp_ww] = self.tile_spacings; - return (yy+uu*(c_yy-1)+ww*(c_yy-1)) as f32*sp_yy + (uu+ww*(c_yy-1)) as f32*sp_uu + ww as f32*sp_ww; - } - - fn try_set_cursor(&mut self, mode: CursorMode) { - match mode { - CursorMode::ProbeAndMark => { - self.cursor_mode = CursorMode::ProbeAndMark; - }, - CursorMode::Highlighter => { - if self.game != None { - self.cursor_mode = CursorMode::Highlighter; - } - } - } - } - - fn reset_view(&mut self) { - self.view_origin = Pos2::new(0.0, 20.0); - self.zoom_factor = 1.0; - } - - fn zoom_to_fit(&mut self, screen_size: Pos2) { - let [c_xx, c_yy, c_zz, c_uu, c_vv, c_ww] = self.current_initial_settings.size; - let [sp_xx, sp_yy, sp_zz, sp_uu, sp_vv, sp_ww] = self.tile_spacings; - - // TODO: allow user to set the padding - let (padding_x, padding_y) = (5.0, 5.0); - - let x_block_size = c_xx as f32 * self.cell_edge + (c_xx - 1) as f32 * sp_xx; - let y_block_size = c_yy as f32 * self.cell_edge + (c_yy - 1) as f32 * sp_yy; - let z_block_size = c_zz as f32 * x_block_size + (c_zz - 1) as f32 * sp_zz; - let u_block_size = c_uu as f32 * y_block_size + (c_uu - 1) as f32 * sp_uu; - let v_block_size = c_vv as f32 * z_block_size + (c_vv - 1) as f32 * sp_vv; - let w_block_size = c_ww as f32 * u_block_size + (c_ww - 1) as f32 * sp_ww; - - let x_factor = (screen_size.x - 2.0*padding_x) / v_block_size; - let y_factor = (screen_size.y - 40.0 - 2.0*padding_y) / w_block_size; - - // Zoom to fit the larger side - if (x_factor > y_factor && w_block_size * x_factor <= screen_size.y - 40.0 - 2.0*padding_y) - || v_block_size * y_factor > screen_size.x - 2.0*padding_x { - self.zoom_factor = if self.unlimited_zoom {x_factor} else {x_factor.clamp(0.01, 5.0)}; - } else { - self.zoom_factor = if self.unlimited_zoom {y_factor} else {y_factor.clamp(0.01, 5.0)}; - } - - // Translate to center - self.view_origin.x = (screen_size.x - 10.0 - v_block_size*self.zoom_factor) / 2.0 + padding_x; - self.view_origin.y = (screen_size.y - 50.0 - w_block_size*self.zoom_factor) / 2.0 + 20.0 + padding_y; - } -} - -impl Default for MinesweeperViewController { - fn default() -> Self { + fn new(config_text: String) -> Self { // Sanity check //println!("{}", std::mem::size_of::()); - - match env::current_dir() { - Ok(p) => println!("The current directory is {}", p.display()), - Err(_) => println!("The current directory cannot be obtained") - }; let settings = InitialGameSettings { name: "Custom".into(), @@ -358,16 +210,8 @@ impl Default for MinesweeperViewController { shortcuts: Shortcuts::new(), }; - - let config_text = match fs::read_to_string("config.toml") { - Ok(s) => s, - Err(_) => { - println!("config could not be read"); - "".into() - } - }; //.unwrap_or_else(|_| "".into()); - let config_table: Table = config_text.parse::().expect("Invalid configuration file"); + let config_table: Table = config_text.parse::
().expect("Invalid configuration file"); // Load in presets if let Some(val) = config_table.get("preset"){ for e in val.as_array().unwrap() { @@ -491,6 +335,158 @@ impl Default for MinesweeperViewController { ret } + + fn reset(&mut self) { + self.game = None; + self.cursor_mode = CursorMode::ProbeAndMark; + } + + fn start(&mut self, initial: [usize; DIMENSIONS_COUNT]) { + self.start_time = Some(SystemTime::now()); + self.end_time = None; + if let Some(seed) = &self.current_initial_settings.seed { + self.game = Some(GameBoard::new(self.current_initial_settings.size, + self.current_initial_settings.wrap, + self.current_initial_settings.mines, + None, + u64::from_str_radix(&seed, 16).ok())); + self.game.as_mut().unwrap().probe_at(initial, true); + } else { + self.game = Some(GameBoard::new(self.current_initial_settings.size, + self.current_initial_settings.wrap, + self.current_initial_settings.mines, + Some(initial), + None)); + } + } + + // Translate and Scale from screen coordinates to cell coordinates + // Uses modular cutoff to decide in constant time whether mouse is over any cell: + // + // If current position modulo (x*cell+(x-1)*space_1+space_2) + // is larger than (x*cell+(x-1)*space_1), it must be outside a cell, otherwise repeat: + // | | | | | | | | | + // | cell | space_1 | cell | space_1 | cell | space_1 | cell | space_2 | + // | | | | | | | | | + fn get_coords(&self, pos: Pos2) -> Option<[usize; DIMENSIONS_COUNT]> { + if pos.x < self.view_origin.x || pos.y < self.view_origin.y { + return None; + } + + let [c_xx, c_yy, c_zz, c_uu, c_vv, c_ww] = self.current_initial_settings.size; + let [sp_xx, sp_yy, sp_zz, sp_uu, sp_vv, sp_ww] = self.tile_spacings; + + // Sizes of blocks (cells + inner spacing) + let x_block_size = c_xx as f32 * self.cell_edge + (c_xx - 1) as f32 * sp_xx; + let y_block_size = c_yy as f32 * self.cell_edge + (c_yy - 1) as f32 * sp_yy; + let z_block_size = c_zz as f32 * x_block_size + (c_zz - 1) as f32 * sp_zz; + let u_block_size = c_uu as f32 * y_block_size + (c_uu - 1) as f32 * sp_uu; + // let v_block_size = c_vv as f32 * z_block_size + (c_vv - 1) as f32 * sp_vv; + // let w_block_size = c_ww as f32 * u_block_size + (c_ww - 1) as f32 * sp_ww; + + // Get logical points + let (dx, dy) = ((pos.x - self.view_origin.x) / self.zoom_factor, + (pos.y - self.view_origin.y) / self.zoom_factor); + + // Modulo largest period + let (dx_m1, dy_m1) = (dx as f32 % (z_block_size + sp_vv), + dy as f32 % (u_block_size + sp_ww)); + if dx_m1 > z_block_size || dy_m1 > u_block_size { + return None; + } + // Modulo middle period + let (dx_m2, dy_m2) = (dx_m1 % (x_block_size + sp_zz), + dy_m1 % (y_block_size + sp_uu)); + if dx_m2 > x_block_size || dy_m2 > y_block_size { + return None; + } + // Modulo smallest period + let (dx_m3, dy_m3) = (dx_m2 % (self.cell_edge + sp_xx), + dy_m2 % (self.cell_edge + sp_yy)); + if dx_m3 > self.cell_edge || dy_m3 > self.cell_edge { + return None; + } + + // Is cell-like, get coords + let (xx, yy) = ((dx_m2 / (self.cell_edge + sp_xx)) as usize, + (dy_m2 / (self.cell_edge + sp_yy)) as usize); + let (zz, uu) = ((dx_m1 / (x_block_size + sp_zz)) as usize, + (dy_m1 / (y_block_size + sp_uu)) as usize); + let (vv, ww) = ((dx / (z_block_size + sp_vv)) as usize, + (dy / (u_block_size + sp_ww)) as usize); + + // Check if actual cell within grid bounds + if xx >= c_xx || yy >= c_yy || zz >= c_zz || uu >= c_uu || vv >= c_vv || ww >= c_ww { + return None; + } + + return Some([xx, yy, zz, uu, vv, ww]); + } + + // Scale and Translate from logical points to screen coordinates + fn sc_tr(&self, xx: f32, yy: f32) -> Pos2 { + Pos2::new(xx * self.zoom_factor, yy * self.zoom_factor) + self.view_origin.to_vec2() + } + + // Cumulative spacing in screen axis directions + fn cum_spc_x(&self, xx: usize, zz: usize, vv: usize) -> f32 { + let [c_xx, _, _, _, _, _] = self.current_initial_settings.size; + let [sp_xx, _, sp_zz, _, sp_vv, _] = self.tile_spacings; + return (xx+zz*(c_xx-1)+vv*(c_xx-1)) as f32*sp_xx + (zz+vv*(c_xx-1)) as f32*sp_zz + vv as f32*sp_vv; + } + fn cum_spc_y(&self, yy: usize, uu: usize, ww: usize) -> f32 { + let [_, c_yy, _, _, _, _] = self.current_initial_settings.size; + let [_, sp_yy, _, sp_uu, _, sp_ww] = self.tile_spacings; + return (yy+uu*(c_yy-1)+ww*(c_yy-1)) as f32*sp_yy + (uu+ww*(c_yy-1)) as f32*sp_uu + ww as f32*sp_ww; + } + + fn try_set_cursor(&mut self, mode: CursorMode) { + match mode { + CursorMode::ProbeAndMark => { + self.cursor_mode = CursorMode::ProbeAndMark; + }, + CursorMode::Highlighter => { + if self.game != None { + self.cursor_mode = CursorMode::Highlighter; + } + } + } + } + + fn reset_view(&mut self) { + self.view_origin = Pos2::new(0.0, 20.0); + self.zoom_factor = 1.0; + } + + fn zoom_to_fit(&mut self, screen_size: Pos2) { + let [c_xx, c_yy, c_zz, c_uu, c_vv, c_ww] = self.current_initial_settings.size; + let [sp_xx, sp_yy, sp_zz, sp_uu, sp_vv, sp_ww] = self.tile_spacings; + + // TODO: allow user to set the padding + let (padding_x, padding_y) = (5.0, 5.0); + + let x_block_size = c_xx as f32 * self.cell_edge + (c_xx - 1) as f32 * sp_xx; + let y_block_size = c_yy as f32 * self.cell_edge + (c_yy - 1) as f32 * sp_yy; + let z_block_size = c_zz as f32 * x_block_size + (c_zz - 1) as f32 * sp_zz; + let u_block_size = c_uu as f32 * y_block_size + (c_uu - 1) as f32 * sp_uu; + let v_block_size = c_vv as f32 * z_block_size + (c_vv - 1) as f32 * sp_vv; + let w_block_size = c_ww as f32 * u_block_size + (c_ww - 1) as f32 * sp_ww; + + let x_factor = (screen_size.x - 2.0*padding_x) / v_block_size; + let y_factor = (screen_size.y - 40.0 - 2.0*padding_y) / w_block_size; + + // Zoom to fit the larger side + if (x_factor > y_factor && w_block_size * x_factor <= screen_size.y - 40.0 - 2.0*padding_y) + || v_block_size * y_factor > screen_size.x - 2.0*padding_x { + self.zoom_factor = if self.unlimited_zoom {x_factor} else {x_factor.clamp(0.01, 5.0)}; + } else { + self.zoom_factor = if self.unlimited_zoom {y_factor} else {y_factor.clamp(0.01, 5.0)}; + } + + // Translate to center + self.view_origin.x = (screen_size.x - 10.0 - v_block_size*self.zoom_factor) / 2.0 + padding_x; + self.view_origin.y = (screen_size.y - 50.0 - w_block_size*self.zoom_factor) / 2.0 + 20.0 + padding_y; + } } impl eframe::App for MinesweeperViewController {