draw queue
This commit is contained in:
parent
3b026c73cd
commit
2447e60f16
6 changed files with 90 additions and 41 deletions
|
|
@ -6,7 +6,7 @@ pub struct Tile {
|
||||||
pub suit: Suit,
|
pub suit: Suit,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(MapEntities, Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
|
#[derive(/* MapEntities, */ Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
|
||||||
pub enum Suit {
|
pub enum Suit {
|
||||||
Man(Rank),
|
Man(Rank),
|
||||||
Pin(Rank),
|
Pin(Rank),
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_ratatui::RatatuiContext;
|
use ratatui::widgets::{Block, Clear};
|
||||||
use ratatui::{layout::Margin, widgets::Block};
|
|
||||||
use tui_logger::TuiLoggerWidget;
|
use tui_logger::TuiLoggerWidget;
|
||||||
|
|
||||||
|
use crate::tui::render::WidgetStack;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
|
||||||
pub(crate) enum ConsoleState {
|
pub(crate) enum ConsoleState {
|
||||||
#[default]
|
#[default]
|
||||||
|
|
@ -21,14 +22,13 @@ impl std::ops::Not for ConsoleState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn draw_console(mut tui_ctx: ResMut<RatatuiContext>) -> Result {
|
pub(crate) fn draw_console(mut widgets: ResMut<WidgetStack>) {
|
||||||
tui_ctx.draw(|frame| {
|
widgets.0.push(Box::new(|frame| {
|
||||||
let block = Block::bordered().title("console");
|
let block = Block::bordered().title("console");
|
||||||
|
frame.render_widget(Clear, frame.area());
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
TuiLoggerWidget::default().block(block),
|
TuiLoggerWidget::default().block(block),
|
||||||
frame.area(), /* .inner(Margin { horizontal: 8, vertical: 8 }) */
|
frame.area(), /* .inner(Margin { horizontal: 8, vertical: 8 }) */
|
||||||
);
|
);
|
||||||
})?;
|
}));
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ pub(crate) fn input_system(
|
||||||
curr_tuistate: Res<State<TuiState>>,
|
curr_tuistate: Res<State<TuiState>>,
|
||||||
curr_consolestate: Res<State<ConsoleState>>,
|
curr_consolestate: Res<State<ConsoleState>>,
|
||||||
curr_gamestate: Res<State<GameState>>,
|
curr_gamestate: Res<State<GameState>>,
|
||||||
curr_zenstate: Res<State<ZenState>>,
|
curr_zenstate: Option<Res<State<ZenState>>>,
|
||||||
|
|
||||||
mut next_tuistate: ResMut<NextState<TuiState>>,
|
mut next_tuistate: ResMut<NextState<TuiState>>,
|
||||||
mut next_consolestate: ResMut<NextState<ConsoleState>>,
|
mut next_consolestate: ResMut<NextState<ConsoleState>>,
|
||||||
|
|
@ -60,10 +60,14 @@ pub(crate) fn input_system(
|
||||||
next_tuistate.set(TuiState::InGame);
|
next_tuistate.set(TuiState::InGame);
|
||||||
next_gamestate.set(GameState::Setup);
|
next_gamestate.set(GameState::Setup);
|
||||||
}
|
}
|
||||||
KeyCode::Char('z') => match curr_zenstate.get() {
|
KeyCode::Char('z') => {
|
||||||
ZenState::Menu => next_zenstate.set(ZenState::Zen),
|
if let Some(ref curr_zenstate) = curr_zenstate {
|
||||||
ZenState::Zen => next_zenstate.set(ZenState::Menu),
|
match curr_zenstate.get() {
|
||||||
},
|
ZenState::Menu => next_zenstate.set(ZenState::Zen),
|
||||||
|
ZenState::Zen => next_zenstate.set(ZenState::Menu),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
KeyCode::Char('q') => {
|
KeyCode::Char('q') => {
|
||||||
exit.write_default();
|
exit.write_default();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,12 @@ use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin};
|
||||||
use bevy_ratatui::RatatuiPlugins;
|
use bevy_ratatui::RatatuiPlugins;
|
||||||
|
|
||||||
use jong::game::GameState;
|
use jong::game::GameState;
|
||||||
|
|
||||||
|
use crate::tui::render::{WidgetStack, menu::Splash};
|
||||||
// use jong::game::wall::InWall;
|
// use jong::game::wall::InWall;
|
||||||
|
|
||||||
|
|
||||||
mod console;
|
mod console;
|
||||||
mod menu;
|
|
||||||
mod render;
|
mod render;
|
||||||
mod input;
|
mod input;
|
||||||
|
|
||||||
|
|
@ -58,25 +59,33 @@ impl Plugin for RiichiTui {
|
||||||
))
|
))
|
||||||
.add_plugins(StatesPlugin)
|
.add_plugins(StatesPlugin)
|
||||||
|
|
||||||
// console
|
|
||||||
.init_state::<console::ConsoleState>()
|
|
||||||
.add_systems(Last, console::draw_console.run_if(in_state(console::ConsoleState::Open)))
|
|
||||||
|
|
||||||
// general setup
|
// general setup
|
||||||
.init_state::<TuiState>()
|
.init_state::<TuiState>()
|
||||||
.add_computed_state::<InGame>()
|
.add_computed_state::<InGame>()
|
||||||
|
.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)))
|
||||||
|
|
||||||
|
// input
|
||||||
.add_systems(PreUpdate, input::keyboard::input_system)
|
.add_systems(PreUpdate, input::keyboard::input_system)
|
||||||
.add_systems(PreUpdate, input::mouse::input_system.chain())
|
.add_systems(PreUpdate, input::mouse::input_system)
|
||||||
|
|
||||||
// main menu
|
// main menu
|
||||||
.add_sub_state::<ZenState>()
|
.add_systems(PostStartup, render::menu::init_splash)
|
||||||
.add_systems(Update, menu::draw_splash.run_if(in_state(TuiState::MainMenu)))
|
.insert_resource(Time::<Fixed>::from_seconds(1.0))
|
||||||
.add_systems(Update, menu::draw_mainmenu.after(menu::draw_splash).run_if(in_state(TuiState::MainMenu).and(in_state(ZenState::Menu))))
|
.add_systems(FixedUpdate, render::menu::render_splash.run_if(in_state(TuiState::MainMenu)))
|
||||||
|
.add_systems(Update, render::menu::draw_splash.run_if(in_state(TuiState::MainMenu)))
|
||||||
|
.add_systems(Update, render::menu::draw_mainmenu.after(render::menu::draw_splash).run_if(in_state(TuiState::MainMenu).and(in_state(ZenState::Menu))))
|
||||||
|
|
||||||
// gaming
|
// gaming
|
||||||
.init_resource::<render::hand::RenderedHand>()
|
.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::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))))
|
||||||
|
|
||||||
// semicolon stopper
|
// semicolon stopper
|
||||||
;
|
;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_ratatui::RatatuiContext;
|
use bevy_ratatui::RatatuiContext;
|
||||||
|
use rand::rng;
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
use ratatui::layout::Constraint;
|
use ratatui::layout::Constraint;
|
||||||
use ratatui::layout::Flex;
|
use ratatui::layout::Flex;
|
||||||
use ratatui::layout::Layout;
|
use ratatui::layout::Layout;
|
||||||
|
|
@ -9,6 +11,7 @@ use ratatui::widgets::{Block, Clear};
|
||||||
|
|
||||||
use jong::tile::Tile;
|
use jong::tile::Tile;
|
||||||
|
|
||||||
|
use crate::tui::render::WidgetStack;
|
||||||
use crate::tui::render::tile;
|
use crate::tui::render::tile;
|
||||||
|
|
||||||
const MAINMENU_OPTIONS: [&str; 2] = [
|
const MAINMENU_OPTIONS: [&str; 2] = [
|
||||||
|
|
@ -28,13 +31,13 @@ const MAINMENU_OPTIONS: [&str; 2] = [
|
||||||
",
|
",
|
||||||
];
|
];
|
||||||
|
|
||||||
pub(crate) fn draw_mainmenu(mut tui_ctx: ResMut<RatatuiContext>) -> Result {
|
pub(crate) fn draw_mainmenu(mut widgets: ResMut<WidgetStack>) {
|
||||||
let options = MAINMENU_OPTIONS;
|
let options = MAINMENU_OPTIONS;
|
||||||
let layout =
|
let layout =
|
||||||
Layout::vertical(vec![Constraint::Fill(1); options.len()]).flex(Flex::SpaceBetween);
|
Layout::vertical(vec![Constraint::Fill(1); options.len()]).flex(Flex::SpaceBetween);
|
||||||
let block = Block::bordered();
|
let block = Block::bordered();
|
||||||
|
|
||||||
tui_ctx.draw(|frame| {
|
widgets.0.push(Box::new(move |frame| {
|
||||||
let area = frame
|
let area = frame
|
||||||
.area()
|
.area()
|
||||||
.centered(Constraint::Max(55), Constraint::Max(19));
|
.centered(Constraint::Max(55), Constraint::Max(19));
|
||||||
|
|
@ -45,34 +48,49 @@ pub(crate) fn draw_mainmenu(mut tui_ctx: ResMut<RatatuiContext>) -> Result {
|
||||||
let para = Paragraph::new(opt);
|
let para = Paragraph::new(opt);
|
||||||
frame.render_widget(para, *area)
|
frame.render_widget(para, *area)
|
||||||
}
|
}
|
||||||
})?;
|
}));
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn draw_splash(mut tui_ctx: ResMut<RatatuiContext>, tiles: Populated<&Tile>) -> Result {
|
#[derive(Resource, Default)]
|
||||||
let mut tile_it = tiles.into_iter().cycle();
|
pub(crate) struct Splash(pub Vec<Paragraph<'static>>);
|
||||||
|
|
||||||
tui_ctx.draw(|frame| {
|
pub(crate) fn init_splash(mut commands: Commands, tiles: Populated<&Tile>) {
|
||||||
|
let tiles: Vec<_> = tiles
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.map(|tile| tile::draw_tile(&tile))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
commands.insert_resource(Splash(tiles));
|
||||||
|
trace!("init_splash")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn render_splash(mut splash: ResMut<Splash>) {
|
||||||
|
let mut rng = rng();
|
||||||
|
splash.0.shuffle(&mut rng);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn draw_splash(mut widgets: ResMut<WidgetStack>, splash: Res<Splash>) {
|
||||||
|
let tiles: Vec<_> = splash.0.clone();
|
||||||
|
widgets.0.push(Box::new(move |frame| {
|
||||||
let area = frame.area().outer(Margin {
|
let area = frame.area().outer(Margin {
|
||||||
horizontal: 1,
|
horizontal: 1,
|
||||||
vertical: 1,
|
vertical: 1,
|
||||||
});
|
});
|
||||||
let layout = Layout::horizontal(vec![Constraint::Max(5); (area.width / 5) as usize]);
|
let layout = Layout::horizontal(vec![Constraint::Length(5); (area.width / 5) as usize]);
|
||||||
let areas = layout.split(area);
|
let areas = layout.split(area);
|
||||||
|
let mut tile_chunks = tiles.chunks(areas.len()).cycle();
|
||||||
for area in areas.iter() {
|
for area in areas.iter() {
|
||||||
let layout = Layout::vertical(vec![Constraint::Max(4); (area.height / 4) as usize]);
|
let layout = Layout::vertical(vec![Constraint::Length(4); (area.height / 4) as usize]);
|
||||||
let areas = layout.split(*area);
|
let areas = layout.split(*area);
|
||||||
let tiles: Vec<_> = tile_it
|
// let tiles: Vec<_> = tile_it
|
||||||
.clone()
|
// .by_ref()
|
||||||
.take((area.height / 4 + 1) as usize)
|
// .take((area.height / 4 + 1) as usize)
|
||||||
.cloned()
|
// .map(|t| tile::draw_tile(&t))
|
||||||
.map(|t| tile::draw_tile(&t))
|
// .collect();
|
||||||
.collect();
|
for (tile, area) in tile_chunks.next().unwrap().iter().zip(areas.iter()) {
|
||||||
for (tile, area) in tiles.iter().zip(areas.into_iter()) {
|
|
||||||
frame.render_widget(tile, *area);
|
frame.render_widget(tile, *area);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})?;
|
}));
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,8 +1,26 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use bevy_ratatui::RatatuiContext;
|
||||||
|
use ratatui::Frame;
|
||||||
|
|
||||||
pub(crate) mod hand;
|
pub(crate) mod hand;
|
||||||
pub(crate) mod ingame;
|
pub(crate) mod ingame;
|
||||||
|
pub(crate) mod menu;
|
||||||
pub(crate) mod tile;
|
pub(crate) mod tile;
|
||||||
|
|
||||||
|
#[derive(Resource, Default)]
|
||||||
|
pub(crate) struct WidgetStack(pub(crate) Vec<Box<dyn FnOnce(&mut Frame) + Send + Sync>>);
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub(crate) struct Hovered;
|
pub(crate) struct Hovered;
|
||||||
|
|
||||||
|
pub(crate) fn draw_system(
|
||||||
|
mut tui_ctx: ResMut<RatatuiContext>,
|
||||||
|
mut widgetstack: ResMut<WidgetStack>,
|
||||||
|
) -> Result {
|
||||||
|
tui_ctx.draw(|frame| {
|
||||||
|
for widget in widgetstack.0.drain(..) {
|
||||||
|
widget(frame)
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue