diff --git a/src/app.rs b/src/app.rs index e2c2daf..72ddbd9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,7 +3,7 @@ use wasm_bindgen::prelude::*; use web_sys::HtmlInputElement; // use wasm_bindgen_futures::spawn_local; -use yew::{prelude::*, html::Scope}; +use yew::{html::Scope, prelude::*}; // use log::info; use wasm_bindgen::JsValue; @@ -21,9 +21,9 @@ extern "C" { } pub enum Msg { - Discover{ cell: Cell }, - Flag{ cell: Cell }, - Reset, + Discover { cell: Cell }, + Flag { cell: Cell }, + Reset, ToggleSettings, UpdateHeight, UpdateWidth, @@ -36,7 +36,7 @@ pub struct App { height: usize, width: usize, num_mines: usize, - show_settings: bool + show_settings: bool, } impl Component for App { @@ -52,38 +52,42 @@ impl Component for App { let mut game = Game::new(height, width, 5); game.start_board(); - Self { + Self { link: ctx.link().clone(), game, height, width, num_mines, - show_settings: false + show_settings: false, } } fn view(&self, _ctx: &Context) -> Html { let b = self.game.get_board().clone(); - let style = format!("height: {}px; transition: height 1s;", if !self.show_settings {0} else {98}).to_string(); + let style = format!( + "height: {}px; transition: height 1s;", + if !self.show_settings { 0 } else { 98 } + ) + .to_string(); - html!{ + html! {
// Disable context menu - +
-
@@ -133,9 +137,17 @@ impl Component for App { let d = web_sys::window().unwrap().document().unwrap(); let get_mines = || { - let text = d.get_element_by_id("mines-input").unwrap().dyn_into::().unwrap().value(); + let text = d + .get_element_by_id("mines-input") + .unwrap() + .dyn_into::() + .unwrap() + .value(); - let re = Regex::new("^((0+)|((0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)))$").unwrap(); + let re = Regex::new( + "^((0+)|((0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)))$", + ) + .unwrap(); let mut value: usize = self.num_mines; if re.is_match(&text) { value = text.parse().unwrap(); @@ -144,23 +156,33 @@ impl Component for App { }; match msg { - Msg::Discover {cell} => { + Msg::Discover { cell } => { self.game.show(cell.get_pos()); - }, - Msg::Flag {cell} => { - self.game.set_flag(cell.get_pos(), !self.game.get_cell(cell.get_pos()).is_flagged()); - }, + } + Msg::Flag { cell } => { + self.game.set_flag( + cell.get_pos(), + !self.game.get_cell(cell.get_pos()).is_flagged(), + ); + } Msg::Reset => { self.game = Game::new(self.height, self.width, self.num_mines); self.game.start_board(); - }, + } Msg::ToggleSettings => { self.show_settings = !self.show_settings; - }, + } Msg::UpdateHeight => { - let text = d.get_element_by_id("height-input").unwrap().dyn_into::().unwrap().value(); + let text = d + .get_element_by_id("height-input") + .unwrap() + .dyn_into::() + .unwrap() + .value(); - let re = Regex::new("^(0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)$").unwrap(); + let re = + Regex::new("^(0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)$") + .unwrap(); if re.is_match(&text) { self.height = text.parse().unwrap(); let mines = get_mines(); @@ -168,11 +190,18 @@ impl Component for App { self.game = Game::new(self.height, self.width, self.num_mines); self.game.start_board(); } - }, + } Msg::UpdateWidth => { - let text = d.get_element_by_id("width-input").unwrap().dyn_into::().unwrap().value(); + let text = d + .get_element_by_id("width-input") + .unwrap() + .dyn_into::() + .unwrap() + .value(); - let re = Regex::new("^(0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)$").unwrap(); + let re = + Regex::new("^(0*)(1|[2-9]|[1-9][0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-8]|999)$") + .unwrap(); if re.is_match(&text) { self.width = text.parse().unwrap(); let mines = get_mines(); @@ -180,7 +209,7 @@ impl Component for App { self.game = Game::new(self.height, self.width, self.num_mines); self.game.start_board(); } - }, + } Msg::UpdateMines => { let mines = get_mines(); if mines <= self.height * self.width { @@ -192,4 +221,4 @@ impl Component for App { } true } -} \ No newline at end of file +} diff --git a/src/components/board.rs b/src/components/board.rs index 2d1fa5d..ade1db2 100644 --- a/src/components/board.rs +++ b/src/components/board.rs @@ -1,4 +1,4 @@ -use yew::{prelude::*, html::Scope}; +use yew::{html::Scope, prelude::*}; use crate::minesweeper::{board::Board, cell::Cell}; @@ -11,19 +11,19 @@ pub struct BoardComponent { link: Scope, board: Board, onsignal: Callback, - flagsignal: Callback + flagsignal: Callback, } pub enum Msg { - Discover{ cell: Cell }, - Flag{ cell: Cell } + Discover { cell: Cell }, + Flag { cell: Cell }, } #[derive(Clone, PartialEq, Properties)] pub struct Props { pub board: Board, pub onsignal: Callback, - pub flagsignal: Callback + pub flagsignal: Callback, } impl Component for BoardComponent { @@ -36,12 +36,11 @@ impl Component for BoardComponent { link: ctx.link().clone(), board: ctx.props().board.clone(), onsignal: ctx.props().onsignal.clone(), - flagsignal: ctx.props().flagsignal.clone() + flagsignal: ctx.props().flagsignal.clone(), } } fn view(&self, ctx: &Context) -> Html { - let b = ctx.props().board.get_board().clone(); let height = self.board.get_height(); @@ -54,13 +53,13 @@ impl Component for BoardComponent { <> {row.into_iter().map(|c| { html! { -