Skip to content

Commit

Permalink
event cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollier committed Dec 29, 2023
1 parent 2c5b39c commit 1c13027
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 51 deletions.
7 changes: 5 additions & 2 deletions src/bin/solitaire-tui/component.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use ratatui::{prelude::Rect, Frame};

use crate::{error::Result, event::Event};
use crate::{
error::Result,
event::{Event, EventResult},
};

pub mod app;
pub mod game;

pub trait Component {
fn handle_event(&mut self, event: &Event) -> Result<()>;
fn handle_event(&mut self, event: &Event) -> EventResult;

fn handle_tick(&mut self, dt: &std::time::Duration) -> Result<()>;

Expand Down
8 changes: 3 additions & 5 deletions src/bin/solitaire-tui/component/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ pub struct AppComponent<RNG: rand::Rng> {
}

impl<RNG: rand::Rng> Component for AppComponent<RNG> {
fn handle_event(&mut self, event: &Event) -> Result<()> {
self.game.handle_event(event)?;
Ok(())
fn handle_event(&mut self, event: &Event) -> EventResult {
self.game.handle_event(event)
}

fn handle_tick(&mut self, dt: &std::time::Duration) -> Result<()> {
self.game.handle_tick(dt)?;
Ok(())
self.game.handle_tick(dt)
}

fn render(&self, f: &mut Frame, rect: Rect) {
Expand Down
72 changes: 30 additions & 42 deletions src/bin/solitaire-tui/component/game/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::time::Duration;
use crossterm::event::{KeyCode, KeyModifiers};
use ratatui::{
prelude::*,
symbols::line,
widgets::{
block::{Position, Title},
Block, BorderType, Borders,
Expand All @@ -21,7 +20,7 @@ use crate::{
Component,
},
error::Result,
event::Event,
event::{Event, EventResult, EventState},
};

pub struct GameComponent<RNG: rand::Rng> {
Expand All @@ -31,20 +30,28 @@ pub struct GameComponent<RNG: rand::Rng> {
}

impl<RNG: rand::Rng> Component for GameComponent<RNG> {
fn handle_event(&mut self, event: &Event) -> Result<()> {
fn handle_event(&mut self, event: &Event) -> EventResult {
match event {
Event::KeyPress(KeyCode::Up, m)
| Event::KeyPress(KeyCode::Char('w'), m)
| Event::KeyPress(KeyCode::Char('W'), m) => self.handle_up(*m),
| Event::KeyPress(KeyCode::Char('W'), m) => {
self.handle_direction(ui_state::Direction::Up, *m)
}
Event::KeyPress(KeyCode::Down, m)
| Event::KeyPress(KeyCode::Char('s'), m)
| Event::KeyPress(KeyCode::Char('S'), m) => self.handle_down(*m),
| Event::KeyPress(KeyCode::Char('S'), m) => {
self.handle_direction(ui_state::Direction::Down, *m)
}
Event::KeyPress(KeyCode::Left, m)
| Event::KeyPress(KeyCode::Char('a'), m)
| Event::KeyPress(KeyCode::Char('A'), m) => self.handle_left(*m),
| Event::KeyPress(KeyCode::Char('A'), m) => {
self.handle_direction(ui_state::Direction::Left, *m)
}
Event::KeyPress(KeyCode::Right, m)
| Event::KeyPress(KeyCode::Char('d'), m)
| Event::KeyPress(KeyCode::Char('D'), m) => self.handle_right(*m),
| Event::KeyPress(KeyCode::Char('D'), m) => {
self.handle_direction(ui_state::Direction::Right, *m)
}
Event::KeyPress(KeyCode::Enter, _) | Event::KeyPress(KeyCode::Char(' '), _) => {
self.handle_interact()
}
Expand All @@ -57,7 +64,7 @@ impl<RNG: rand::Rng> Component for GameComponent<RNG> {
Event::KeyPress(KeyCode::Char('r'), _) | Event::KeyPress(KeyCode::Char('R'), _) => {
self.handle_reset()
}
_ => Ok(()),
_ => Ok(EventState::NotConsumed),
}
}

Expand Down Expand Up @@ -117,54 +124,35 @@ impl<RNG: rand::Rng> GameComponent<RNG> {
}
}

fn handle_up(&mut self, modifier: KeyModifiers) -> Result<()> {
self.ui_state =
self.ui_state
.handle_direction(ui_state::Direction::Up, modifier, &self.state);
Ok(())
}

fn handle_down(&mut self, modifier: KeyModifiers) -> Result<()> {
self.ui_state =
self.ui_state
.handle_direction(ui_state::Direction::Down, modifier, &self.state);
Ok(())
}

fn handle_left(&mut self, modifier: KeyModifiers) -> Result<()> {
self.ui_state =
self.ui_state
.handle_direction(ui_state::Direction::Left, modifier, &self.state);
Ok(())
}

fn handle_right(&mut self, modifier: KeyModifiers) -> Result<()> {
self.ui_state =
self.ui_state
.handle_direction(ui_state::Direction::Right, modifier, &self.state);
Ok(())
fn handle_direction(
&mut self,
dir: ui_state::Direction,
modifier: KeyModifiers,
) -> EventResult {
self.ui_state = self.ui_state.handle_direction(dir, modifier, &self.state);
Ok(EventState::Consumed)
}

fn handle_interact(&mut self) -> Result<()> {
fn handle_interact(&mut self) -> EventResult {
self.ui_state = self.ui_state.handle_interact(&mut self.state);
Ok(())
Ok(EventState::Consumed)
}

fn handle_goto(&mut self, c: u32) -> Result<()> {
fn handle_goto(&mut self, c: u32) -> EventResult {
self.ui_state = self.ui_state.handle_goto(c as u8);
Ok(())
Ok(EventState::Consumed)
}

fn handle_cancel(&mut self) -> Result<()> {
fn handle_cancel(&mut self) -> EventResult {
self.ui_state = self.ui_state.handle_cancel();
Ok(())
Ok(EventState::Consumed)
}

fn handle_reset(&mut self) -> Result<()> {
fn handle_reset(&mut self) -> EventResult {
self.state = klondike::GameStateOption::from(klondike::InitialGameState::new_with_rng(
&mut self.rng,
));
self.ui_state = UIState::Dealing(DealingState::new());
Ok(())
Ok(EventState::Consumed)
}
}
10 changes: 10 additions & 0 deletions src/bin/solitaire-tui/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{sync, thread, time::Instant};

use crossterm::event;

use crate::error::Error;

#[derive(Copy, Clone)]
pub enum Event {
KeyPress(event::KeyCode, event::KeyModifiers),
Expand Down Expand Up @@ -29,6 +31,14 @@ pub enum Message {
Tick(std::time::Duration),
}

#[derive(Copy, Clone)]
pub enum EventState {
Consumed,
NotConsumed,
}

pub type EventResult = Result<EventState, Error>;

pub struct Events {
rx: sync::mpsc::Receiver<Message>,
}
Expand Down
8 changes: 6 additions & 2 deletions src/bin/solitaire-tui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ fn main() -> Result<()> {
match events.next()? {
Message::Event(Event::KeyPress(KeyCode::Char('q'), _))
| Message::Event(Event::KeyPress(KeyCode::Char('c'), KeyModifiers::CONTROL)) => break,
Message::Event(event) => app.handle_event(&event)?,
Message::Tick(dt) => app.handle_tick(&dt)?,
Message::Event(event) => {
app.handle_event(&event)?;
}
Message::Tick(dt) => {
app.handle_tick(&dt)?;
}
}
}

Expand Down

0 comments on commit 1c13027

Please sign in to comment.