Skip to content

Commit

Permalink
Rework input devices
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Sep 16, 2024
1 parent 657141a commit 9d18fb6
Show file tree
Hide file tree
Showing 13 changed files with 878 additions and 346 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions truncate_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
eframe = "0.26.2"
eframe = { version = "0.26.2", features = ["serde"] }
egui = { version = "*", features = ["serde"] }
epaint = { version = "0.26.2", features = ["color-hex"] }
color-hex = "0.2.0"
futures-util = { version = "0.3", default-features = false, features = [
Expand All @@ -33,7 +34,7 @@ tracing = "0.1.40"
tokio = { version = "1", features = ["full"] }
tokio-tungstenite = { version = "0.18", features = ["rustls-tls-webpki-roots"] }
tungstenite = { version = "0.18", default-features = false }
gilrs = "0.10"
gilrs = { version = "0.10", features = ["serde-serialize"] }

# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
31 changes: 23 additions & 8 deletions truncate_client/src/app_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,21 @@ pub fn render(outer: &mut OuterApplication, ui: &mut egui::Ui, current_time: Dur
outer.tx_player.try_send(msg).unwrap();
};

let mut switchboard = outer.switchboard.lock().unwrap();

match &mut outer.game_status {
GameStatus::None(_, _) => { /* handled above */ }
GameStatus::Generator(generator) => {
generator.render(ui, &outer.theme, current_time);
generator.render(ui, &mut switchboard, &outer.theme, current_time);
}
GameStatus::Tutorial(tutorial) => {
for msg in tutorial.render(ui, outer.map_texture.clone(), &outer.theme, current_time) {
for msg in tutorial.render(
ui,
outer.map_texture.clone(),
&outer.theme,
&mut switchboard,
current_time,
) {
send(msg);
}
}
Expand Down Expand Up @@ -296,6 +304,7 @@ pub fn render(outer: &mut OuterApplication, ui: &mut egui::Ui, current_time: Dur
&outer.theme,
current_time,
&outer.backchannel,
&mut switchboard,
&outer.logged_in_as,
) {
send(msg);
Expand All @@ -308,6 +317,7 @@ pub fn render(outer: &mut OuterApplication, ui: &mut egui::Ui, current_time: Dur
&outer.theme,
current_time,
&outer.backchannel,
&mut switchboard,
&outer.logged_in_as,
) {
send(msg);
Expand Down Expand Up @@ -379,18 +389,23 @@ pub fn render(outer: &mut OuterApplication, ui: &mut egui::Ui, current_time: Dur
}
}
GameStatus::Active(game) => {
if let Some(msg) = game.render(ui, current_time, None) {
for msg in game.render(ui, current_time, &mut switchboard, None) {
// Online games can only play as the logged in player
debug_assert!(msg.player_id.is_none());
send(msg.message);
}
}
GameStatus::Concluded(game, _winner) => {
if let Some(AssignedPlayerMessage {
message: PlayerMessage::Rematch,
..
}) = game.render(ui, current_time, None)
{
let msgs = game.render(ui, current_time, &mut switchboard, None);
if msgs.iter().any(|m| {
matches!(
m,
AssignedPlayerMessage {
message: PlayerMessage::Rematch,
..
}
)
}) {
send(PlayerMessage::Rematch);
}
}
Expand Down
5 changes: 4 additions & 1 deletion truncate_client/src/app_outer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::OnceLock;
use std::sync::{Arc, Mutex, OnceLock};

use futures::channel::mpsc::{Receiver, Sender};
use serde::{Deserialize, Serialize};
Expand All @@ -8,6 +8,7 @@ type S = Sender<PlayerMessage>;

use super::utils::Theme;
use crate::app_inner::AppInnerStorage;
use crate::utils::control_devices::Switchboard;
use crate::{app_inner, utils::glyph_utils::Glypher};
use eframe::egui::{self, Frame, Margin, TextureOptions};
#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -116,6 +117,7 @@ pub struct OuterApplication {
pub log_frames: bool,
pub frames: debug::FrameHistory,
pub event_dispatcher: EventDispatcher,
pub switchboard: Arc<Mutex<Switchboard>>,
}

impl OuterApplication {
Expand Down Expand Up @@ -282,6 +284,7 @@ impl OuterApplication {
tx_player,
sent: vec![],
},
switchboard: Arc::new(Mutex::new(Switchboard::load())),
}
}
}
Expand Down
36 changes: 11 additions & 25 deletions truncate_client/src/regions/active_game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use hashbrown::HashMap;
use crate::{
lil_bits::{BoardUI, DictionaryUI},
utils::{
control_devices,
control_devices::{self, Switchboard},
depot::{
AestheticDepot, AudioDepot, BoardDepot, GameplayDepot, InteractionDepot, RegionDepot,
TimingDepot, TruncateDepot, UIStateDepot,
Expand All @@ -34,7 +34,7 @@ use crate::{
};

#[cfg(not(target_arch = "wasm32"))]
use crate::utils::control_devices::gamepad::GamepadInput;
use crate::utils::control_devices::gamepad::GamepadManager;

mod actions_menu;
mod control_strip;
Expand Down Expand Up @@ -76,8 +76,6 @@ pub struct ActiveGame {
pub turn_reports: Vec<Vec<Change>>,
pub location: GameLocation,
pub dictionary_ui: Option<DictionaryUI>,
#[cfg(not(target_arch = "wasm32"))]
pub gamepad_input: Arc<Mutex<GamepadInput>>,
}

impl ActiveGame {
Expand Down Expand Up @@ -171,8 +169,6 @@ impl ActiveGame {
turn_reports: vec![],
location,
dictionary_ui: None,
#[cfg(not(target_arch = "wasm32"))]
gamepad_input: Arc::new(Mutex::new(GamepadInput::new())),
}
}
}
Expand All @@ -182,9 +178,11 @@ impl ActiveGame {
&mut self,
ui: &mut egui::Ui,
current_time: Duration,
switchboard: &mut Switchboard,
game_ref: Option<&truncate_core::game::Game>,
) -> Option<AssignedPlayerMessage> {
) -> Vec<AssignedPlayerMessage> {
self.depot.timing.current_time = current_time;
let mut msgs = vec![];
let cur_tick = get_qs_tick(current_time);
if cur_tick > self.depot.aesthetics.qs_tick {
self.depot.aesthetics.qs_tick = cur_tick;
Expand All @@ -206,20 +204,7 @@ impl ActiveGame {
}
}

let kb_msg = control_devices::keyboard::handle_input(
ui.ctx(),
&self.board,
&self.hands,
&mut self.depot,
);

#[cfg(not(target_arch = "wasm32"))]
let pad_msg = self.gamepad_input.lock().unwrap().handle_input(
ui.ctx(),
&self.board,
&self.hands,
&mut self.depot,
);
msgs.extend(switchboard.operate(ui.ctx(), &self.board, &self.hands, &mut self.depot));

if !self.depot.ui_state.is_touch {
// If we ever receive any touch event,
Expand Down Expand Up @@ -323,10 +308,11 @@ impl ActiveGame {
.or(dict_player_message.map(|message| wrap(message)))
.or(sidebar_player_message.map(|message| wrap(message)));

#[cfg(not(target_arch = "wasm32"))]
return kb_msg.or(pad_msg).or(player_message);
#[cfg(target_arch = "wasm32")]
kb_msg.or(player_message)
if let Some(player_message) = player_message {
msgs.push(player_message);
}

msgs
}

pub fn apply_new_timing(&mut self, state_message: GameStateMessage) {
Expand Down
Loading

0 comments on commit 9d18fb6

Please sign in to comment.