diff --git a/front_end/src/state.rs b/front_end/src/state.rs index f2fbfb7..009c890 100644 --- a/front_end/src/state.rs +++ b/front_end/src/state.rs @@ -1,24 +1,7 @@ -use std::cell::{Cell, RefCell}; -use std::collections::VecDeque; +use std::cell::Cell; use std::rc::Rc; use yew::prelude::*; -#[derive(Clone, Debug, PartialEq, Eq, Default)] -pub struct MessageBus { - queued: VecDeque, -} - -impl MessageBus { - pub fn get_next_msg(&mut self) -> Option { - self.queued.pop_front() - } - - pub fn push_msg(&mut self, msg: String) -> Result<(), String> { - self.queued.push_back(msg); - Ok(()) - } -} - #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum BoardState { Empty, @@ -41,8 +24,7 @@ pub enum Turn { #[derive(Clone, Debug, PartialEq)] pub struct SharedState { pub board: [Rc>; 56], - pub bus: Rc>, - pub turn: Turn, + pub turn: Cell, pub info_text_ref: NodeRef, } @@ -108,8 +90,7 @@ impl Default for SharedState { Rc::new(Cell::new(BoardState::default())), Rc::new(Cell::new(BoardState::default())), ], - bus: Rc::new(RefCell::new(MessageBus::default())), - turn: Turn::CyanPlayer, + turn: Cell::new(Turn::CyanPlayer), info_text_ref: NodeRef::default(), } } diff --git a/front_end/src/yew_components.rs b/front_end/src/yew_components.rs index 2dd6308..e0127d6 100644 --- a/front_end/src/yew_components.rs +++ b/front_end/src/yew_components.rs @@ -1,6 +1,6 @@ use crate::constants::{COLS, INFO_TEXT_HEIGHT, INFO_TEXT_MAX_ITEMS}; -use crate::state::{BoardState, MessageBus, SharedState, Turn}; -use std::cell::{Cell, RefCell}; +use crate::state::{BoardState, SharedState, Turn}; +use std::cell::Cell; use std::rc::Rc; use yew::prelude::*; @@ -14,7 +14,6 @@ pub enum SlotMessage { pub struct SlotProperties { idx: u8, state: Rc>, - bus: Rc>, } impl Component for Slot { @@ -45,26 +44,14 @@ impl Component for Slot { fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { SlotMessage::Press(idx) => { - let (shared, _) = ctx - .link() - .context::(Callback::noop()) - .expect("shared to be set"); - - let result = shared.bus.borrow_mut().push_msg(format!("pressed {idx}")); - if let Err(e) = result { - log::error!("Error pushing msg to bus: {}", e); - } else { - // DEBUG - //log::info!("Pushed \"pressed {idx}\" msg to bus"); + // notify Wrapper with message + let msg = format!("pressed {idx}"); + if let Some(p) = ctx.link().get_parent() { + p.clone().downcast::().send_message(msg); } } } - // notify Wrapper with message - if let Some(p) = ctx.link().get_parent() { - p.clone().downcast::().send_message(()); - } - true } } @@ -72,7 +59,7 @@ impl Component for Slot { pub struct Wrapper {} impl Component for Wrapper { - type Message = (); + type Message = String; type Properties = (); fn create(_ctx: &Context) -> Self { @@ -86,62 +73,62 @@ impl Component for Wrapper { .expect("state to be set"); html! {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
@@ -149,79 +136,81 @@ impl Component for Wrapper { } } - fn rendered(&mut self, ctx: &Context, _first_render: bool) { + fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { let (shared, _) = ctx .link() .context::(Callback::noop()) .expect("state to be set"); + let split_str: Vec<&str> = msg.split_whitespace().collect(); + if split_str.len() == 2 { + if split_str[0] == "pressed" { + if let Ok(idx) = split_str[1].parse::() { + let output_str: String = format!("Got {idx} pressed."); + shared.board[idx as usize].replace(match shared.board[idx as usize].get() { + BoardState::Empty => BoardState::Cyan, + BoardState::Cyan => BoardState::Magenta, + BoardState::Magenta => BoardState::Empty, + }); + + // DEBUG + log::info!("{} is {:?}", idx, shared.board[idx as usize].get()); + + // DEBUG + //log::info!("{}", &output_str); + + if let Some(info_text) = shared.info_text_ref.cast::() + { + // create the new text to be appended in the output + let window = web_sys::window().expect("no window exists"); + let document = window.document().expect("window should have a document"); + let p = document + .create_element("p") + .expect("document should be able to create

"); + p.set_text_content(Some(&output_str)); + + // check if scrolled to top - while let Some(msg) = shared.bus.borrow_mut().get_next_msg() { - let split_str: Vec<&str> = msg.split_whitespace().collect(); - if split_str.len() == 2 { - if split_str[0] == "pressed" { - if let Ok(idx) = split_str[1].parse::() { - let output_str: String = format!("Got {idx} pressed."); // DEBUG - //log::info!("{}", &output_str); - if let Some(info_text) = - shared.info_text_ref.cast::() - { - // create the new text to be appended in the output - let window = web_sys::window().expect("no window exists"); - let document = - window.document().expect("window should have a document"); - let p = document - .create_element("p") - .expect("document should be able to create

"); - p.set_text_content(Some(&output_str)); + //log::info!( + // "pre: scroll top is {}, scroll height is {}", + // info_text.scroll_top(), + // info_text.scroll_height() + //); + let at_top: bool = + info_text.scroll_top() <= INFO_TEXT_HEIGHT - info_text.scroll_height(); - // check if scrolled to top - - // DEBUG - //log::info!( - // "pre: scroll top is {}, scroll height is {}", - // info_text.scroll_top(), - // info_text.scroll_height() - //); - let at_top: bool = info_text.scroll_top() - <= INFO_TEXT_HEIGHT - info_text.scroll_height(); - - // append text to output + // append text to output + info_text + .append_with_node_1(&p) + .expect("should be able to append to info_text"); + while info_text.child_element_count() > INFO_TEXT_MAX_ITEMS { info_text - .append_with_node_1(&p) - .expect("should be able to append to info_text"); - while info_text.child_element_count() > INFO_TEXT_MAX_ITEMS { - info_text - .remove_child(&info_text.first_child().unwrap()) - .expect("should be able to limit items in info_text"); - } - - // scroll to bottom only if at bottom - - // DEBUG - //log::info!("at_top is {}", if at_top { "true" } else { "false" }); - - if at_top { - info_text - .set_scroll_top(INFO_TEXT_HEIGHT - info_text.scroll_height()); - } - - // DEBUG - //log::info!( - // "post: scroll top is {}, scroll height is {}", - // info_text.scroll_top(), - // info_text.scroll_height() - //); - } else { - log::warn!("Failed to get \"info_text\""); + .remove_child(&info_text.first_child().unwrap()) + .expect("should be able to limit items in info_text"); } - } - } - } - } - } - fn update(&mut self, _ctx: &Context, _msg: Self::Message) -> bool { + // scroll to bottom only if at bottom + + // DEBUG + //log::info!("at_top is {}", if at_top { "true" } else { "false" }); + + if at_top { + info_text.set_scroll_top(INFO_TEXT_HEIGHT - info_text.scroll_height()); + } + + // DEBUG + //log::info!( + // "post: scroll top is {}, scroll height is {}", + // info_text.scroll_top(), + // info_text.scroll_height() + //); + } else { + log::warn!("Failed to get \"info_text\""); + } + } // let Ok(idx) = split_str[1].parse::() + } // split_str[0] == "pressed" + } // split_str.len() == 2 + true } }