Support emulating low battery

This commit is contained in:
Simon Gellis 2026-08-15 00:52:40 -04:00
parent 3a00497942
commit 762f43fd42
No known key found for this signature in database
GPG Key ID: DA576912FED9577B
4 changed files with 35 additions and 0 deletions

View File

@ -467,6 +467,9 @@ impl ApplicationHandler<UserEvent> for Application {
self.controllers.handle_gamepad_event(&event);
}
}
UserEvent::LowBattery(sim_id, pwr) => {
self.controllers.handle_low_battery(sim_id, pwr);
}
UserEvent::Quit(sim_id) => match sim_id {
SimId::Player1 => event_loop.exit(),
SimId::Player2 => self.app.close(ChildWindow::Player2),
@ -673,6 +676,7 @@ impl ViewportManager {
pub enum UserEvent {
GamepadEvent(gilrs::Event),
Quit(SimId),
LowBattery(SimId, bool),
RequestRedraw(ViewportId, Instant),
}

View File

@ -141,6 +141,8 @@ pub struct SimConfig {
#[serde(default = "default_audio_enabled")]
pub audio_enabled: bool,
#[serde(default)]
pub low_battery: bool,
#[serde(default)]
pub position: Option<Pos2>,
}
@ -154,6 +156,7 @@ impl SimConfig {
colors: COLOR_PRESETS[0],
dimensions: DisplayMode::Anaglyph.proportions() + Vec2::new(0.0, 22.0),
audio_enabled: true,
low_battery: false,
position: None,
}
}

View File

@ -77,6 +77,15 @@ impl Controller {
self.update_state(pressed, released)
}
pub fn low_battery(&mut self, pwr: bool) -> Option<VBKey> {
let (pressed, released) = if pwr {
(VBKey::PWR, VBKey::empty())
} else {
(VBKey::empty(), VBKey::PWR)
};
self.update_state(pressed, released)
}
fn update_state(&mut self, pressed: VBKey, released: VBKey) -> Option<VBKey> {
let old_state = self.state;
self.state = self.state.union(pressed).difference(released);
@ -133,6 +142,14 @@ impl ControllerManager {
}
}
}
pub fn handle_low_battery(&mut self, sim_id: SimId, pwr: bool) {
let controller = &mut self.controllers[sim_id.to_index()];
if let Some(pressed) = controller.low_battery(pwr) {
self.client
.send_command(EmulatorCommand::SetKeys(controller.sim_id, pressed));
}
}
}
fn axis_presses(value: f32, pair_value: f32, neg: VBKey, pos: VBKey) -> (VBKey, VBKey) {

View File

@ -72,6 +72,7 @@ impl GameWindow {
sim_id: SimId,
) -> Self {
let config = SimConfig::load(&persistence, sim_id);
let _ = proxy.send_event(UserEvent::LowBattery(sim_id, config.low_battery));
let toasts = Toasts::new()
.with_anchor(Anchor::BottomLeft)
.with_margin((10.0, 10.0).into())
@ -389,6 +390,16 @@ impl GameWindow {
{
pollster::block_on(self.take_screenshot());
}
ui.separator();
let low_battery = self.config.low_battery;
if ui.selectable_button(low_battery, "Low Battery").clicked() {
self.update_config(|c| {
c.low_battery = !low_battery;
});
let _ = self
.proxy
.send_event(UserEvent::LowBattery(self.sim_id, !low_battery));
}
});
ui.menu_button("Options", |ui| self.show_options_menu(ui));
ui.menu_button("Multiplayer", |ui| {