smol cleanup
This commit is contained in:
parent
2447e60f16
commit
441e729dee
6 changed files with 90 additions and 78 deletions
|
|
@ -1,34 +0,0 @@
|
|||
use bevy::prelude::*;
|
||||
use ratatui::widgets::{Block, Clear};
|
||||
use tui_logger::TuiLoggerWidget;
|
||||
|
||||
use crate::tui::render::WidgetStack;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
|
||||
pub(crate) enum ConsoleState {
|
||||
#[default]
|
||||
Closed,
|
||||
Open,
|
||||
}
|
||||
|
||||
impl std::ops::Not for ConsoleState {
|
||||
type Output = Self;
|
||||
|
||||
fn not(self) -> Self::Output {
|
||||
match self {
|
||||
ConsoleState::Open => ConsoleState::Closed,
|
||||
ConsoleState::Closed => ConsoleState::Open,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn draw_console(mut widgets: ResMut<WidgetStack>) {
|
||||
widgets.0.push(Box::new(|frame| {
|
||||
let block = Block::bordered().title("console");
|
||||
frame.render_widget(Clear, frame.area());
|
||||
frame.render_widget(
|
||||
TuiLoggerWidget::default().block(block),
|
||||
frame.area(), /* .inner(Margin { horizontal: 8, vertical: 8 }) */
|
||||
);
|
||||
}));
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ use bevy_ratatui::event::KeyMessage;
|
|||
|
||||
use jong::game::GameState;
|
||||
|
||||
use crate::tui::{TuiState, ZenState, console::ConsoleState};
|
||||
use crate::tui::states::*;
|
||||
|
||||
// TODO change this to handle console open request, esc for menu, etc, then
|
||||
// route other messages to other systems
|
||||
|
|
|
|||
|
|
@ -6,41 +6,11 @@ use bevy_ratatui::RatatuiPlugins;
|
|||
use jong::game::GameState;
|
||||
|
||||
use crate::tui::render::{WidgetStack, menu::Splash};
|
||||
// use jong::game::wall::InWall;
|
||||
use states::*;
|
||||
|
||||
|
||||
mod console;
|
||||
mod render;
|
||||
mod input;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)]
|
||||
pub(crate) enum TuiState {
|
||||
#[default]
|
||||
MainMenu,
|
||||
InGame,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||
struct InGame;
|
||||
|
||||
impl ComputedStates for InGame {
|
||||
type SourceStates = TuiState;
|
||||
|
||||
fn compute(sources: Self::SourceStates) -> Option<Self> {
|
||||
match sources {
|
||||
TuiState::InGame => Some(Self),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(SubStates, Default, Clone, Copy, PartialEq, Eq, Hash, Debug,)]
|
||||
#[source(TuiState = TuiState::MainMenu)]
|
||||
pub(crate) enum ZenState {
|
||||
#[default]
|
||||
Menu,
|
||||
Zen,
|
||||
}
|
||||
mod states;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RiichiTui;
|
||||
|
|
@ -65,8 +35,8 @@ impl Plugin for RiichiTui {
|
|||
.init_resource::<WidgetStack>()
|
||||
.add_sub_state::<ZenState>()
|
||||
.init_resource::<Splash>()
|
||||
.init_state::<console::ConsoleState>()
|
||||
.add_systems(PostUpdate, console::draw_console.run_if(in_state(console::ConsoleState::Open)))
|
||||
.init_state::<ConsoleState>()
|
||||
.add_systems(PostUpdate, render::draw_console.run_if(in_state(ConsoleState::Open)))
|
||||
|
||||
// input
|
||||
.add_systems(PreUpdate, input::keyboard::input_system)
|
||||
|
|
@ -82,7 +52,7 @@ impl Plugin for RiichiTui {
|
|||
// gaming
|
||||
.init_resource::<render::hand::RenderedHand>()
|
||||
.add_systems(Update, render::hand::render_hands.run_if(in_state(InGame).and(in_state(GameState::Play))))
|
||||
.add_systems(Update, render::ingame::draw_ingame.run_if(in_state(InGame)))
|
||||
// .add_systems(Update, render::ingame::draw_ingame.run_if(in_state(InGame)))
|
||||
|
||||
// render
|
||||
.add_systems(Last, render::draw_system.run_if(not(in_state(InGame))))
|
||||
|
|
|
|||
|
|
@ -5,12 +5,25 @@ use jong::game::player::{MainPlayer, Player};
|
|||
use jong::tile::Tile;
|
||||
use ratatui::widgets::{Block, Borders};
|
||||
|
||||
use crate::tui::render::WidgetStack;
|
||||
use crate::tui::render::tile::draw_tile;
|
||||
use crate::tui::{
|
||||
input::mouse::PickRegion,
|
||||
render::{Hovered, hand, tile::RenderedTile},
|
||||
};
|
||||
|
||||
pub(crate) fn draw_system(
|
||||
mut tui_ctx: ResMut<RatatuiContext>,
|
||||
mut widgets: ResMut<WidgetStack>,
|
||||
) -> Result {
|
||||
tui_ctx.draw(|frame| {
|
||||
for widget in widgets.0.drain(..) {
|
||||
widget(frame)
|
||||
}
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn draw_ingame(
|
||||
mut commands: Commands,
|
||||
mut tui_ctx: ResMut<RatatuiContext>,
|
||||
|
|
@ -115,18 +128,18 @@ pub(crate) fn draw_ingame(
|
|||
this_drawn.width = 5;
|
||||
this_drawn.x += 2;
|
||||
let mut widget = draw_tile(tiles.get(tile.0).unwrap());
|
||||
let mut hitbox = this_drawn;
|
||||
if hovered_entity.contains(tile.0) {
|
||||
widget = widget.add_modifier(Modifier::BOLD);
|
||||
if let Some(val) = this_drawn.y.checked_sub(1) {
|
||||
this_drawn.y = val
|
||||
if let Some(val) = hitbox.y.checked_sub(1) {
|
||||
hitbox.y = val
|
||||
} else {
|
||||
// FIXME show error
|
||||
panic!("terminal too small!");
|
||||
}
|
||||
hitbox.height += 1;
|
||||
}
|
||||
commands
|
||||
.entity(tile.0)
|
||||
.insert(PickRegion { area: this_drawn });
|
||||
commands.entity(tile.0).insert(PickRegion { area: hitbox });
|
||||
frame.render_widget(widget, this_drawn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy_ratatui::RatatuiContext;
|
||||
use ratatui::Frame;
|
||||
use ratatui::{
|
||||
Frame,
|
||||
widgets::{Block, Clear},
|
||||
};
|
||||
use tui_logger::TuiLoggerWidget;
|
||||
|
||||
pub(crate) mod hand;
|
||||
pub(crate) mod ingame;
|
||||
|
|
@ -13,12 +17,23 @@ pub(crate) struct WidgetStack(pub(crate) Vec<Box<dyn FnOnce(&mut Frame) + Send +
|
|||
#[derive(Component)]
|
||||
pub(crate) struct Hovered;
|
||||
|
||||
pub(crate) fn draw_console(mut widgets: ResMut<WidgetStack>) {
|
||||
widgets.0.push(Box::new(|frame| {
|
||||
let block = Block::bordered().title("console");
|
||||
frame.render_widget(Clear, frame.area());
|
||||
frame.render_widget(
|
||||
TuiLoggerWidget::default().block(block),
|
||||
frame.area(), /* .inner(Margin { horizontal: 8, vertical: 8 }) */
|
||||
);
|
||||
}));
|
||||
}
|
||||
|
||||
pub(crate) fn draw_system(
|
||||
mut tui_ctx: ResMut<RatatuiContext>,
|
||||
mut widgetstack: ResMut<WidgetStack>,
|
||||
mut widgets: ResMut<WidgetStack>,
|
||||
) -> Result {
|
||||
tui_ctx.draw(|frame| {
|
||||
for widget in widgetstack.0.drain(..) {
|
||||
for widget in widgets.0.drain(..) {
|
||||
widget(frame)
|
||||
}
|
||||
})?;
|
||||
|
|
|
|||
48
src/tui/states.rs
Normal file
48
src/tui/states.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)]
|
||||
pub(crate) enum TuiState {
|
||||
#[default]
|
||||
MainMenu,
|
||||
InGame,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||
pub(crate) struct InGame;
|
||||
|
||||
impl ComputedStates for InGame {
|
||||
type SourceStates = TuiState;
|
||||
|
||||
fn compute(sources: Self::SourceStates) -> Option<Self> {
|
||||
match sources {
|
||||
TuiState::InGame => Some(Self),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(SubStates, Default, Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
||||
#[source(TuiState = TuiState::MainMenu)]
|
||||
pub(crate) enum ZenState {
|
||||
#[default]
|
||||
Menu,
|
||||
Zen,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
|
||||
pub(crate) enum ConsoleState {
|
||||
#[default]
|
||||
Closed,
|
||||
Open,
|
||||
}
|
||||
|
||||
impl std::ops::Not for ConsoleState {
|
||||
type Output = Self;
|
||||
|
||||
fn not(self) -> Self::Output {
|
||||
match self {
|
||||
ConsoleState::Open => ConsoleState::Closed,
|
||||
ConsoleState::Closed => ConsoleState::Open,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue