tetanes 0.14.0

A cross-platform NES Emulator written in Rust using wgpu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use crate::nes::{
    action::Action,
    config::Config,
    event::{ConfigEvent, NesEventProxy},
    input::{Gamepads, Input},
    renderer::gui::lib::ViewportOptions,
};
use egui::{
    Align2, Button, CentralPanel, Context, Grid, ScrollArea, Ui, Vec2, ViewportClass, ViewportId,
};
use parking_lot::Mutex;
use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};
use tetanes_core::input::Player;
use uuid::Uuid;
use winit::event::ElementState;

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub enum Tab {
    #[default]
    Shortcuts,
    Joypad(Player),
}

#[derive(Debug)]
#[must_use]
pub struct State {
    tx: NesEventProxy,
    tab: Tab,
    pending_input: Option<PendingInput>,
    gamepad_unassign_confirm: Option<(Player, Player, Uuid)>,
}

#[derive(Debug)]
#[must_use]
pub struct Keybinds {
    pub id: ViewportId,
    open: Arc<AtomicBool>,
    state: Arc<Mutex<State>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PendingInput {
    action: Action,
    input: Option<Input>,
    binding: usize,
    conflict: Option<Action>,
}

#[derive(Debug)]
#[must_use]
pub struct GamepadState {
    input_events: Vec<(Input, ElementState)>,
    connected: Option<Vec<ConnectedGamepad>>,
}

#[derive(Debug, PartialEq, Eq)]
#[must_use]
pub struct ConnectedGamepad {
    uuid: Uuid,
    name: String,
    assignment: Option<Player>,
}

impl Keybinds {
    const TITLE: &'static str = "TetaNES - Keybinds";

    pub fn new(tx: NesEventProxy) -> Self {
        Self {
            id: ViewportId::from_hash_of(Self::TITLE),
            open: Arc::new(AtomicBool::new(false)),
            state: Arc::new(Mutex::new(State {
                tx,
                tab: Tab::default(),
                pending_input: None,
                gamepad_unassign_confirm: None,
            })),
        }
    }

    pub fn wants_input(&self) -> bool {
        self.state.try_lock().is_some_and(|state| {
            state.pending_input.is_some() || state.gamepad_unassign_confirm.is_some()
        })
    }

    pub fn open(&self) -> bool {
        self.open.load(Ordering::Acquire)
    }

    pub fn set_open(&self, open: bool, ctx: &Context) {
        self.open.store(open, Ordering::Release);
        if !self.open() {
            ctx.send_viewport_cmd_to(self.id, egui::ViewportCommand::Close);
        }
    }

    pub fn toggle_open(&self, ctx: &Context) {
        let _ = self
            .open
            .fetch_update(Ordering::Release, Ordering::Acquire, |open| Some(!open));
        if !self.open() {
            ctx.send_viewport_cmd_to(self.id, egui::ViewportCommand::Close);
        }
    }

    pub fn show(&mut self, ctx: &Context, opts: ViewportOptions, cfg: Config, gamepads: &Gamepads) {
        if !self.open() {
            return;
        }

        let open = Arc::clone(&self.open);
        let state = Arc::clone(&self.state);

        let mut viewport_builder = egui::ViewportBuilder::default().with_title(Self::TITLE);
        if opts.always_on_top {
            viewport_builder = viewport_builder.with_always_on_top();
        }
        let gamepad_state = GamepadState {
            input_events: gamepads
                .events()
                .filter_map(|event| gamepads.input_from_event(event, &cfg))
                .collect::<Vec<_>>(),
            connected: gamepads.list().map(|gamepad_list| {
                gamepad_list
                    .map(|(_, gamepad)| {
                        let uuid = Gamepads::create_uuid(&gamepad);
                        ConnectedGamepad {
                            uuid,
                            name: gamepad.name().to_string(),
                            assignment: cfg.input.gamepad_assignment(&uuid),
                        }
                    })
                    .collect::<Vec<_>>()
            }),
        };

        ctx.show_viewport_deferred(self.id, viewport_builder, move |ctx, class| {
            if class == ViewportClass::Embedded {
                let mut window_open = open.load(Ordering::Acquire);
                egui::Window::new(Keybinds::TITLE)
                    .open(&mut window_open)
                    .default_rect(ctx.available_rect().shrink(16.0))
                    .show(ctx, |ui| {
                        state.lock().ui(ui, opts.enabled, &cfg, &gamepad_state);
                    });
                open.store(window_open, Ordering::Release);
            } else {
                CentralPanel::default().show(ctx, |ui| {
                    state.lock().ui(ui, opts.enabled, &cfg, &gamepad_state);
                });
                if ctx.input(|i| i.viewport().close_requested()) {
                    open.store(false, Ordering::Release);
                }
            }
            if !open.load(Ordering::Acquire) {
                let mut state = state.lock();
                state.pending_input = None;
                state.gamepad_unassign_confirm = None;
            }
        });
    }
}

impl State {
    fn ui(&mut self, ui: &mut Ui, enabled: bool, cfg: &Config, gamepad_state: &GamepadState) {
        self.show_set_keybind_window(ui.ctx(), cfg, &gamepad_state.input_events);
        self.show_gamepad_unassign_window(ui.ctx());

        ui.add_enabled_ui(enabled, |ui| {
            ui.horizontal(|ui| {
                ui.selectable_value(&mut self.tab, Tab::Shortcuts, "Shortcuts");
                ui.selectable_value(&mut self.tab, Tab::Joypad(Player::One), "Player1");
                ui.selectable_value(&mut self.tab, Tab::Joypad(Player::Two), "Player2");
                ui.selectable_value(&mut self.tab, Tab::Joypad(Player::Three), "Player3");
                ui.selectable_value(&mut self.tab, Tab::Joypad(Player::Four), "Player4");
            });

            ui.separator();

            match self.tab {
                Tab::Shortcuts => self.list(ui, None, cfg, gamepad_state.connected.as_deref()),
                Tab::Joypad(player) => {
                    self.list(ui, Some(player), cfg, gamepad_state.connected.as_deref())
                }
            }
        });
    }

    fn list(
        &mut self,
        ui: &mut Ui,
        player: Option<Player>,
        cfg: &Config,
        connected_gamepads: Option<&[ConnectedGamepad]>,
    ) {
        ui.set_min_height(ui.available_height());

        if let Some(player) = player {
            self.player_gamepad_combo(ui, player, connected_gamepads);

            ui.separator();
        }

        ScrollArea::both().auto_shrink(false).show(ui, |ui| {
            let grid = Grid::new("keybind_list")
                .num_columns(4)
                .spacing([10.0, 6.0]);
            grid.show(ui, |ui| {
                ui.heading("Action");
                ui.heading("Binding #1");
                ui.heading("Binding #2");
                ui.heading("Binding #3");
                ui.end_row();

                let keybinds = match player {
                    None => &cfg.input.shortcuts,
                    Some(player) => &cfg.input.joypads[player as usize],
                };

                let mut clear_bind = None;
                for (action, bind) in keybinds {
                    ui.strong(action.to_string());
                    for (slot, input) in bind.bindings.iter().enumerate() {
                        let button = Button::new(input.map(Input::fmt).unwrap_or_default())
                            // Make enough room for larger inputs like controller joysticks
                            .min_size(Vec2::new(135.0, 0.0));
                        let res = ui
                            .add(button)
                            .on_hover_text("Click to set. Right-click to unset.");
                        if res.clicked() {
                            self.pending_input = Some(PendingInput {
                                action: *action,
                                input: None,
                                binding: slot,
                                conflict: None,
                            });
                        } else if res.secondary_clicked()
                            && let Some(input) = input
                        {
                            clear_bind = Some(input)
                        }
                    }
                    ui.end_row();
                }
                if let Some(input) = clear_bind.take() {
                    self.tx.event(ConfigEvent::ActionBindingClear(*input));
                }
            });
        });
    }

    fn player_gamepad_combo(
        &mut self,
        ui: &mut Ui,
        player: Player,
        connected_gamepads: Option<&[ConnectedGamepad]>,
    ) {
        ui.horizontal(|ui| {
            let gamepad_label = "🎮 Assigned Gamepad:";

            let unassigned = "Unassigned".to_string();
            match connected_gamepads {
                Some(gamepads) => {
                    if gamepads.is_empty() {
                        ui.add_enabled_ui(false, |ui| {
                            let combo = egui::ComboBox::from_label(gamepad_label)
                                .selected_text("No Gamepads Connected");
                            combo.show_ui(ui, |_| {});
                        });
                    } else {
                        let mut assigned = gamepads
                            .iter()
                            .find(|gamepad| gamepad.assignment == Some(player));
                        let previous_assigned = assigned;
                        let combo = egui::ComboBox::from_label(gamepad_label).selected_text(
                            assigned
                                .as_ref()
                                .map_or(&unassigned, |assignment| &assignment.name),
                        );
                        combo.show_ui(ui, |ui| {
                            ui.selectable_value(&mut assigned, None, unassigned);
                            for assignment in gamepads {
                                ui.selectable_value(
                                    &mut assigned,
                                    Some(assignment),
                                    &assignment.name,
                                );
                            }
                        });
                        if previous_assigned != assigned {
                            match &assigned {
                                Some(gamepad) => {
                                    match assigned.as_ref().and_then(|gamepad| gamepad.assignment) {
                                        Some(player) => {
                                            self.gamepad_unassign_confirm =
                                                Some((player, player, gamepad.uuid));
                                        }
                                        None => {
                                            self.tx.event(ConfigEvent::GamepadAssign((
                                                player,
                                                gamepad.uuid,
                                            )));
                                        }
                                    }
                                }
                                None => self.tx.event(ConfigEvent::GamepadUnassign(player)),
                            }
                        }
                    }
                }
                None => {
                    ui.add_enabled_ui(false, |ui| {
                        let combo = egui::ComboBox::from_label(gamepad_label)
                            .selected_text("Gamepads not supported");
                        combo.show_ui(ui, |_| {});
                    });
                }
            }
        });
    }

    pub fn show_set_keybind_window(
        &mut self,
        ctx: &Context,
        cfg: &Config,
        gamepad_events: &[(Input, ElementState)],
    ) {
        if self.pending_input.is_none() {
            return;
        }

        let mut set_keybind_open = self.pending_input.is_some();
        let res = egui::Window::new("🖮 Set Keybind")
            .anchor(Align2::CENTER_CENTER, Vec2::ZERO)
            .collapsible(false)
            .resizable(false)
            .open(&mut set_keybind_open)
            .show(ctx, |ui| self.set_keybind(ui, cfg, gamepad_events));
        if let Some(ref res) = res {
            // Force on-top focus when embedded
            if set_keybind_open {
                ctx.move_to_top(res.response.layer_id);
                res.response.request_focus();
            } else {
                ctx.memory_mut(|m| m.surrender_focus(res.response.id));
            }
        }
        if !set_keybind_open {
            self.pending_input = None;
        }
    }

    pub fn set_keybind(
        &mut self,
        ui: &mut Ui,
        cfg: &Config,
        gamepad_events: &[(Input, ElementState)],
    ) {
        let Some(PendingInput {
            action,
            binding,
            mut input,
            mut conflict,
            ..
        }) = self.pending_input
        else {
            return;
        };

        if let Some(action) = conflict {
            ui.label(format!("Conflict with {action}."));
            ui.horizontal(|ui| {
                if ui.button("Overwrite").clicked() {
                    conflict = None;
                }
                if ui.button("Cancel").clicked() {
                    self.pending_input = None;
                    input = None;
                }
            });
        } else {
            ui.label(format!(
                "Press any key on your keyboard or controller to set a new binding for {action}.",
            ));
        }

        match input {
            Some(input) => {
                if conflict.is_none() {
                    self.pending_input = None;
                    self.tx
                        .event(ConfigEvent::ActionBindingSet((action, input, binding)));
                }
            }
            None => {
                if let Some(keybind) = &mut self.pending_input {
                    let input = ui.input(|i| {
                        use egui::Event;

                        // Find first released key/button event
                        for event in &i.events {
                            match *event {
                                Event::Key {
                                    physical_key: Some(key),
                                    pressed: false,
                                    modifiers,
                                    ..
                                } => {
                                    // TODO: Ignore unsupported key mappings for now as egui supports less
                                    // overall than winit
                                    return Input::try_from((key, modifiers)).ok();
                                }
                                Event::PointerButton {
                                    button,
                                    pressed: false,
                                    ..
                                } => {
                                    return Some(Input::from(button));
                                }
                                _ => (),
                            }
                        }
                        for (input, state) in gamepad_events {
                            if *state == ElementState::Released {
                                return Some(*input);
                            }
                        }
                        None
                    });

                    if let Some(input) = input {
                        keybind.input = Some(input);
                        let binds = cfg
                            .input
                            .shortcuts
                            .iter()
                            .chain(cfg.input.joypads.iter().flatten());
                        for (action, bind) in binds {
                            if bind
                                .bindings
                                .iter()
                                .any(|b| b == &Some(input) && *action != keybind.action)
                            {
                                keybind.conflict = Some(*action);
                            }
                        }
                    }
                }
            }
        }
    }

    fn show_gamepad_unassign_window(&mut self, ctx: &Context) {
        if self.gamepad_unassign_confirm.is_none() {
            return;
        }

        let mut gamepad_unassign_open = self.gamepad_unassign_confirm.is_some();
        let res = egui::Window::new("🎮 Unassign Gamepad")
            .anchor(Align2::CENTER_CENTER, Vec2::ZERO)
            .collapsible(false)
            .resizable(false)
            .open(&mut gamepad_unassign_open)
            .show(ctx, |ui| self.gamepad_unassign_confirm(ui));
        if let Some(ref res) = res {
            // Force on-top focus when embedded
            if gamepad_unassign_open {
                ctx.move_to_top(res.response.layer_id);
                res.response.request_focus();
            } else {
                ctx.memory_mut(|m| m.surrender_focus(res.response.id));
            }
        }
        if !gamepad_unassign_open {
            self.gamepad_unassign_confirm = None;
        }
    }

    fn gamepad_unassign_confirm(&mut self, ui: &mut Ui) {
        if let Some((existing_player, new_player, uuid)) = self.gamepad_unassign_confirm {
            ui.label(format!("Unassign gamepad from Player {existing_player}?"));
            ui.horizontal(|ui| {
                if ui.button("Yes").clicked() {
                    self.tx.event(ConfigEvent::GamepadUnassign(existing_player));
                    self.tx
                        .event(ConfigEvent::GamepadAssign((new_player, uuid)));
                    self.gamepad_unassign_confirm = None;
                }
                if ui.button("Cancel").clicked() {
                    self.gamepad_unassign_confirm = None;
                }
            });
        }
    }
}