Reset button

This commit is contained in:
Guilleag01
2023-08-07 16:39:20 +02:00
parent 8c810f4b31
commit 5afa7eaecc
4 changed files with 48 additions and 27 deletions

View File

@@ -1,16 +1,14 @@
use serde::{Deserialize, Serialize};
// use serde_wasm_bindgen::to_value;
use wasm_bindgen::prelude::*;
use web_sys::AddEventListenerOptions;
// use wasm_bindgen_futures::spawn_local;
use yew::{prelude::*, html::Scope};
use log::info;
use wasm_bindgen::JsValue;
// use log::info;
// use wasm_bindgen::JsValue;
use minesweeper_ui::components::{button::Button, board::BoardComponent};
use minesweeper_ui::components::board::BoardComponent;
use minesweeper_ui::minesweeper::{cell::Cell, board::Board, Game};
use minesweeper_ui::minesweeper::{cell::Cell, Game};
#[wasm_bindgen]
extern "C" {
@@ -20,12 +18,16 @@ extern "C" {
pub enum Msg {
Discover{ cell: Cell },
Flag{ cell: Cell }
Flag{ cell: Cell },
Reset
}
pub struct App {
link: Scope<Self>,
game: Game
game: Game,
height: usize,
width: usize,
num_mines: usize
}
impl Component for App {
@@ -34,32 +36,47 @@ impl Component for App {
type Properties = ();
fn create(ctx: &Context<Self>) -> Self {
let height = 100;
let width = 41;
let height = 30;
let width = 30;
let num_mines =(height * width / 10) as usize;
let mut game = Game::new(height, width, num_mines);
let mut game = Game::new(height, width, 5);
game.start_board();
Self {
link: ctx.link().clone(),
game
game,
height,
width,
num_mines
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
fn view(&self, _ctx: &Context<Self>) -> Html {
let b = self.game.get_board().clone();
html!{
<main class="container">
// Disable context menu
// <button class="restart-button">Restart</button>
<script>{"document.addEventListener('contextmenu', event => event.preventDefault());"}</script>
<BoardComponent onsignal={self.link.callback(|cell| Msg::Discover{cell})} flagsignal={self.link.callback(|cell| Msg::Flag{cell})} board={b}/>
<script>
{"document.addEventListener('contextmenu', event => event.preventDefault());"}
</script>
<div class="upper-menu">
<button class="button-reset"
onclick={self.link.callback(|_| Msg::Reset)}>{"Reset"}</button>
</div>
<div class="game">
</div>
<BoardComponent
onsignal={self.link.callback(|cell| Msg::Discover{cell})}
flagsignal={self.link.callback(|cell| Msg::Flag{cell})}
board={b}/>
</main>
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Discover {cell} => {
self.game.show(cell.get_pos());
@@ -67,6 +84,10 @@ impl Component for App {
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();
}
}
true
}

View File

@@ -4,8 +4,8 @@ use crate::minesweeper::{board::Board, cell::Cell};
use crate::components::button::Button;
use log::info;
use wasm_bindgen::JsValue;
// use log::info;
// use wasm_bindgen::JsValue;
pub struct BoardComponent {
link: Scope<Self>,
@@ -62,7 +62,7 @@ impl Component for BoardComponent {
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Discover {cell} => {
self.onsignal.emit(cell);

View File

@@ -1,8 +1,8 @@
use yew::{prelude::*, html::Scope};
use crate::minesweeper::cell::Cell;
use log::info;
use wasm_bindgen::JsValue;
// use log::info;
// use wasm_bindgen::JsValue;
pub struct Button {
link: Scope<Self>,
@@ -36,7 +36,7 @@ impl Component for Button {
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
fn view(&self, _ctx: &Context<Self>) -> Html {
let mut style = String::new();
if !self.cell.is_hidden() {
style = format!("background-color: #2f2f2f; color: #ffffff; transition: background-color 0.5s, color 0.5s; transition-delay: {}s, {}s;", self.cell.get_delay(), self.cell.get_delay());
@@ -46,7 +46,7 @@ impl Component for Button {
<button
class={if self.cell.is_hidden() {"button-hidden"} else {"button-shown"}}
onclick={self.link.callback(|_| Msg::Clicked)}
oncontextmenu={self.link.callback(|e: MouseEvent| {Msg::RightClicked})}
oncontextmenu={self.link.callback(|_| {Msg::RightClicked})}
style={style}>
if self.cell.is_flagged() {
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-pennant-filled" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
@@ -70,7 +70,7 @@ impl Component for Button {
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Clicked => {
self.onsignal.emit(self.cell);

View File

@@ -6,8 +6,8 @@ use cell::Cell;
use rand::Rng;
// use getrandom::getrandom;
use log::info;
use wasm_bindgen::JsValue;
// use log::info;
// use wasm_bindgen::JsValue;
pub struct Game {
board: Board,