Skip to content

Commit

Permalink
refactor: moved around platform code to condense it
Browse files Browse the repository at this point in the history
  • Loading branch information
lukexor committed Jun 27, 2024
1 parent 0b6f794 commit 0f18928
Show file tree
Hide file tree
Showing 16 changed files with 476 additions and 373 deletions.
1 change: 1 addition & 0 deletions tetanes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn main() -> anyhow::Result<()> {

let opts = opts::Opts::parse();
tracing::debug!("CLI Options: {opts:?}");

opts.load()?
}
}
Expand Down
31 changes: 16 additions & 15 deletions tetanes/src/nes/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
feature,
nes::{
action::{Action, Debug, DebugStep, Debugger, Feature, Setting, Ui},
config::Config,
Expand All @@ -11,7 +12,7 @@ use crate::{
rom::RomData,
Nes, Running, State,
},
platform::{self, open_file_dialog},
platform::open_file_dialog,
};
use anyhow::anyhow;
use egui::ViewportId;
Expand Down Expand Up @@ -246,7 +247,7 @@ impl Nes {
match &event {
Event::Resumed => {
let state = if let State::Running(state) = &mut self.state {
if platform::supports(platform::Feature::Suspend) {
if feature!(Suspend) {
state.renderer.recreate_window(event_loop);
}
state
Expand Down Expand Up @@ -303,9 +304,9 @@ impl Nes {
#[cfg(feature = "profiling")]
puffin::set_scopes_on(false);

// Wasm should never be able to exit
#[cfg(target_arch = "wasm32")]
panic!("exited unexpectedly");
if feature!(AbortOnExit) && !matches!(self.state, State::Running(_)) {
panic!("exited unexpectedly");
}
}
_ => (),
}
Expand Down Expand Up @@ -350,7 +351,7 @@ impl Running {

match event {
Event::Suspended => {
if platform::supports(platform::Feature::Suspend) {
if feature!(Suspend) {
if let Err(err) = self.renderer.drop_window() {
error!("failed to suspend window: {err:?}");
event_loop.exit();
Expand Down Expand Up @@ -614,9 +615,9 @@ impl Running {
}
self.renderer.destroy();

// Wasm should never be able to exit
#[cfg(target_arch = "wasm32")]
panic!("exited unexpectedly");
if feature!(AbortOnExit) {
panic!("exited unexpectedly");
}
}
_ => (),
}
Expand Down Expand Up @@ -849,7 +850,7 @@ impl Running {
Action::Menu(menu) if released => self.event(RendererEvent::Menu(menu)),
Action::Feature(feature) if is_root_window => match feature {
Feature::ToggleReplayRecording if released => {
if platform::supports(platform::Feature::Filesystem) {
if feature!(Filesystem) {
if self.renderer.rom_loaded() {
self.replay_recording = !self.replay_recording;
self.event(EmulationEvent::ReplayRecord(self.replay_recording));
Expand All @@ -862,7 +863,7 @@ impl Running {
}
}
Feature::ToggleAudioRecording if released => {
if platform::supports(platform::Feature::Filesystem) {
if feature!(Filesystem) {
if self.renderer.rom_loaded() {
self.audio_recording = !self.audio_recording;
self.event(EmulationEvent::AudioRecord(self.audio_recording));
Expand All @@ -875,7 +876,7 @@ impl Running {
}
}
Feature::TakeScreenshot if released => {
if platform::supports(platform::Feature::Filesystem) {
if feature!(Filesystem) {
if self.renderer.rom_loaded() {
self.event(EmulationEvent::Screenshot);
}
Expand Down Expand Up @@ -991,7 +992,7 @@ impl Running {
| DeckAction::ZapperAimOffscreen
| DeckAction::ZapperTrigger => (),
DeckAction::SetSaveSlot(slot) if released => {
if platform::supports(platform::Feature::Storage) {
if feature!(Storage) {
if self.cfg.emulation.save_slot != slot {
self.cfg.emulation.save_slot = slot;
self.renderer.add_message(
Expand All @@ -1007,7 +1008,7 @@ impl Running {
}
}
DeckAction::SaveState if released && is_root_window => {
if platform::supports(platform::Feature::Storage) {
if feature!(Storage) {
self.event(EmulationEvent::SaveState(self.cfg.emulation.save_slot));
} else {
self.renderer.add_message(
Expand All @@ -1017,7 +1018,7 @@ impl Running {
}
}
DeckAction::LoadState if released && is_root_window => {
if platform::supports(platform::Feature::Storage) {
if feature!(Storage) {
self.event(EmulationEvent::LoadState(self.cfg.emulation.save_slot));
} else {
self.renderer.add_message(
Expand Down
70 changes: 47 additions & 23 deletions tetanes/src/nes/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use crate::{
feature,
nes::{
config::Config,
event::{
ConfigEvent, EmulationEvent, NesEvent, NesEventProxy, RendererEvent, RunState, UiEvent,
},
input::Gamepads,
renderer::{
gui::{lib::key_from_keycode, Gui, MessageType},
gui::{
lib::{is_paste_command, key_from_keycode},
Gui, MessageType,
},
shader::Shader,
texture::Texture,
},
},
platform::{self, BuilderExt, Initialize},
platform::{BuilderExt, Initialize},
thread,
};
use anyhow::Context;
Expand Down Expand Up @@ -189,7 +193,7 @@ impl Renderer {
// Platforms like wasm don't easily support multiple viewports, and even if it could spawn
// multiple canvases for each viewport, the async requirements of wgpu would make it
// impossible to render until wasm-bindgen gets proper non-blocking async/await support.
if platform::supports(platform::Feature::Viewports) {
if feature!(Viewports) {
ctx.set_embed_viewports(cfg.renderer.embed_viewports);
}

Expand Down Expand Up @@ -395,7 +399,7 @@ impl Renderer {
}

pub fn set_fullscreen(&mut self, fullscreen: bool, embed_viewports: bool) {
if platform::supports(platform::Feature::Viewports) {
if feature!(Viewports) {
self.ctx.set_embed_viewports(fullscreen || embed_viewports);
}
self.ctx
Expand Down Expand Up @@ -451,12 +455,12 @@ impl Renderer {
});
}
ConfigEvent::EmbedViewports(embed) => {
if platform::supports(platform::Feature::Viewports) {
if feature!(Viewports) {
self.ctx.set_embed_viewports(*embed);
}
}
ConfigEvent::Fullscreen(fullscreen) => {
if platform::supports(platform::Feature::Viewports) {
if feature!(Viewports) {
self.ctx
.set_embed_viewports(*fullscreen || cfg.renderer.embed_viewports);
}
Expand Down Expand Up @@ -565,21 +569,11 @@ impl Renderer {

let modifiers = self.ctx.input(|i| i.modifiers);

#[cfg(target_arch = "wasm32")]
{
fn is_paste_command(modifiers: egui::Modifiers, keycode: Key) -> bool {
keycode == Key::Paste
|| (modifiers.command && keycode == Key::V)
|| (cfg!(target_os = "windows")
&& modifiers.shift
&& keycode == Key::Insert)
}
if is_paste_command(modifiers, key) {
return EventResponse {
consumed: true,
repaint: true,
};
}
if feature!(ConsumePaste) && is_paste_command(modifiers, key) {
return EventResponse {
consumed: true,
repaint: true,
};
}

if matches!(key, Key::Plus | Key::Equals | Key::Minus | Key::Num0)
Expand Down Expand Up @@ -1068,6 +1062,37 @@ impl Renderer {
};
}

#[cfg(target_arch = "wasm32")]
pub fn set_clipboard_text(state: &Rc<RefCell<State>>, text: String) -> EventResponse {
let State { viewports, .. } = &mut *state.borrow_mut();
let egui_state = viewports
.get_mut(&egui::ViewportId::ROOT)
.and_then(|viewport| viewport.egui_state.as_mut());
match egui_state {
Some(egui_state) => {
// Requires creating an event and setting the clipboard
// here because egui_winit internally tries to manage a
// fallback clipboard for platforms not supported by the
// clipboard crates being used.
//
// This has associated behavior in the renderer to prevent
// sending 'paste events' (ctrl/cmd+V) to egui_state to
// bypass its internal clipboard handling.
egui_state
.egui_input_mut()
.events
.push(egui::Event::Paste(text.clone()));
egui_state.set_clipboard_text(text);
EventResponse {
consumed: true,
repaint: true,
}
}
_ => EventResponse::default(),
}
}

#[cfg(target_arch = "wasm32")]
pub fn process_input(
ctx: &egui::Context,
state: &Rc<RefCell<State>>,
Expand Down Expand Up @@ -1105,8 +1130,7 @@ impl Renderer {

let copied_text = std::mem::take(&mut output.platform_output.copied_text);
if !copied_text.is_empty() {
#[cfg(target_arch = "wasm32")]
platform::set_clipboard_text(&copied_text);
crate::platform::set_clipboard_text(&copied_text);
}

return EventResponse {
Expand Down
Loading

0 comments on commit 0f18928

Please sign in to comment.