From a6079103a4e824a139d68ee9880408c8358e0037 Mon Sep 17 00:00:00 2001 From: Tao Tien <29749622+taotien@users.noreply.github.com> Date: Mon, 12 Jan 2026 21:07:34 -0800 Subject: [PATCH] big ass input system --- src/game/hand.rs | 3 +- src/tui/console.rs | 19 ------------- src/tui/menu.rs | 21 -------------- src/tui/mod.rs | 60 ++++++++++++++++++++++++++++++++++++++-- src/tui/render/hand.rs | 12 ++++++++ src/tui/render/ingame.rs | 2 +- 6 files changed, 73 insertions(+), 44 deletions(-) diff --git a/src/game/hand.rs b/src/game/hand.rs index 4daa915..1984d5b 100644 --- a/src/game/hand.rs +++ b/src/game/hand.rs @@ -30,7 +30,8 @@ pub(crate) fn deal_hands( Ok(()) } -pub(crate) fn sort_hand( +#[allow(clippy::type_complexity)] +pub fn sort_hands( mut commands: Commands, tiles: Populated<&Tile>, handtiles_entity: Single>, diff --git a/src/tui/console.rs b/src/tui/console.rs index 0d4ac88..f668331 100644 --- a/src/tui/console.rs +++ b/src/tui/console.rs @@ -1,8 +1,5 @@ -use bevy::input::keyboard::Key; use bevy::prelude::*; use bevy_ratatui::RatatuiContext; -use bevy_ratatui::event::KeyMessage; -use ratatui::crossterm::event::KeyCode; use ratatui::widgets::Block; use tui_logger::TuiLoggerWidget; @@ -24,22 +21,6 @@ impl std::ops::Not for ConsoleState { } } -pub(crate) fn toggle_console( - // input: Res>, - mut messages: MessageReader, - curr_state: Res>, - mut next_state: ResMut>, -) { - // if input.just_pressed(Key::Character("`".into())) { - // next_state.set(!*curr_state.get()); - // } - for message in messages.read() { - if let KeyCode::Char('`') = message.code { - next_state.set(!*curr_state.get()); - } - } -} - pub(crate) fn draw_console(mut tui_ctx: ResMut) -> Result { tui_ctx.draw(|frame| { let block = Block::bordered().title("console"); diff --git a/src/tui/menu.rs b/src/tui/menu.rs index 65da70b..3ee27e5 100644 --- a/src/tui/menu.rs +++ b/src/tui/menu.rs @@ -27,24 +27,3 @@ pub(crate) fn draw_mainmenu( } }); } - -pub(crate) fn mainmenu_input( - // input: Res>, - mut messages: MessageReader, - mut next_tuistate: ResMut>, - mut next_gamestate: ResMut>, - mut exit: MessageWriter, -) { - for message in messages.read() { - match message.code { - KeyCode::Char('p') => { - next_tuistate.set(TuiState::InGame); - next_gamestate.set(GameState::Setup); - } - KeyCode::Char('q') => { - exit.write_default(); - } - _ => {} - } - } -} diff --git a/src/tui/mod.rs b/src/tui/mod.rs index 5ccd9bf..cde1cf2 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -2,17 +2,20 @@ use std::time::Duration; use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin}; use bevy_ratatui::RatatuiPlugins; +use bevy_ratatui::event::KeyMessage; use ratatui::{text::ToSpan, widgets::Paragraph}; use jong::game::GameState; use jong::game::wall::InWall; +use crate::tui::console::ConsoleState; + mod console; mod menu; mod render; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)] -enum TuiState { +pub(crate) enum TuiState { #[default] MainMenu, InGame, @@ -56,9 +59,10 @@ impl Plugin for RiichiTui { // general setup .init_state::() .add_computed_state::() + .add_systems(Update, input_system) // main menu - .add_systems(Update, (menu::draw_mainmenu, menu::mainmenu_input).run_if(in_state(TuiState::MainMenu))) + .add_systems(Update, menu::draw_mainmenu.run_if(in_state(TuiState::MainMenu))) // gaming .init_resource::() @@ -70,3 +74,55 @@ impl Plugin for RiichiTui { } } +#[allow(clippy::too_many_arguments)] +pub(crate) fn input_system( + mut messages: MessageReader, + + curr_tuistate: Res>, + curr_consolestate: Res>, + curr_gamestate: Res>, + + mut next_tuistate: ResMut>, + mut next_consolestate: ResMut>, + mut next_gamestate: ResMut>, + + mut exit: MessageWriter, +) { + use bevy_ratatui::crossterm::event::KeyCode; + + let (ts, cs, gs) = (curr_tuistate.get(), curr_consolestate.get(), curr_gamestate.get()); + + for message in messages.read() { + if let KeyCode::Char('`') = message.code { + next_consolestate.set(!*curr_consolestate.get()); + continue + } + + match ts { + TuiState::MainMenu => match message.code { + KeyCode::Char('p') => { + next_tuistate.set(TuiState::InGame); + next_gamestate.set(GameState::Setup); + } + KeyCode::Char('q') => { + exit.write_default(); + } + _ => {} + }, + TuiState::InGame => match gs { + GameState::Setup => match message.code { + _ => {} + }, + GameState::Play => match message.code { + KeyCode::Char('q') => { + exit.write_default(); + } + _ => {} + }, + GameState::Score => todo!(), + _ => unreachable!("TuiState::InGame but GameState invalid") + }, + } + } +} + diff --git a/src/tui/render/hand.rs b/src/tui/render/hand.rs index 9c76106..712fba6 100644 --- a/src/tui/render/hand.rs +++ b/src/tui/render/hand.rs @@ -14,6 +14,18 @@ pub(crate) fn render_changed_hand( tiles: Populated<&Tile>, mut target: ResMut, ) -> Result { + let mut rendered = vec![]; + + for hand in hands { + let hand_tiles = hand + .iter() + .map(|inhand| -> Result<_> { Ok(tiles.get(inhand).map(tiles::draw_tile)?) }) + .collect::>>()?; + rendered.push(hand_tiles); + } + + target.0 = rendered; + trace!("render_changed_hand"); render_hand(hand, tiles, target)?; diff --git a/src/tui/render/ingame.rs b/src/tui/render/ingame.rs index 3d407f8..6a30d4e 100644 --- a/src/tui/render/ingame.rs +++ b/src/tui/render/ingame.rs @@ -11,7 +11,7 @@ pub(crate) fn draw_ingame( use ratatui::prelude::*; tui_ctx.draw(|frame| { - debug!("{}", frame.area()); + // debug!("{}", frame.area()); let layout = Layout::horizontal(vec![Constraint::Max(5); 13]).flex(Flex::Start); let mut area = frame.area();