Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamakaky committed May 8, 2024
1 parent 7126c31 commit 639b4d8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
7 changes: 6 additions & 1 deletion src/backend/sdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ use anyhow::{bail, Result};
use cgmath::{vec2, Vector3};
use hid_gamepad_types::{Acceleration, JoyKey, Motion, RotationSpeed};
use sdl2::{
self, controller::{Axis, Button, GameController}, event::Event, keyboard::Keycode, sensor::SensorType, GameControllerSubsystem, Sdl
self,
controller::{Axis, Button, GameController},
event::Event,
keyboard::Keycode,
sensor::SensorType,
GameControllerSubsystem, Sdl,
};

use crate::{
Expand Down
24 changes: 18 additions & 6 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,28 @@ impl Engine {
ExtAction::GyroOn(ClickType::Click) | ExtAction::GyroOff(ClickType::Click) => {
eprintln!("Warning: event type Click has no effect on gyro on/off");
}
ExtAction::KeyPress(c, ClickType::Click) => self.mouse.enigo().key(c, Direction::Click)?,
ExtAction::KeyPress(c, ClickType::Press) => self.mouse.enigo().key(c, Direction::Press)?,
ExtAction::KeyPress(c, ClickType::Release) => self.mouse.enigo().key(c, Direction::Release)?,
ExtAction::KeyPress(c, ClickType::Click) => {
self.mouse.enigo().key(c, Direction::Click)?
}
ExtAction::KeyPress(c, ClickType::Press) => {
self.mouse.enigo().key(c, Direction::Press)?
}
ExtAction::KeyPress(c, ClickType::Release) => {
self.mouse.enigo().key(c, Direction::Release)?
}
ExtAction::KeyPress(_, ClickType::Toggle) => {
// TODO: Implement key press toggle
eprintln!("Warning: key press toggle is not implemented");
}
ExtAction::MousePress(c, ClickType::Click) => self.mouse.enigo().button(c, Direction::Click)?,
ExtAction::MousePress(c, ClickType::Press) => self.mouse.enigo().button(c, Direction::Press)?,
ExtAction::MousePress(c, ClickType::Release) => self.mouse.enigo().button(c, Direction::Release)?,
ExtAction::MousePress(c, ClickType::Click) => {
self.mouse.enigo().button(c, Direction::Click)?
}
ExtAction::MousePress(c, ClickType::Press) => {
self.mouse.enigo().button(c, Direction::Press)?
}
ExtAction::MousePress(c, ClickType::Release) => {
self.mouse.enigo().button(c, Direction::Release)?
}
ExtAction::MousePress(_, ClickType::Toggle) => {
// TODO: Implement mouse click toggle
eprintln!("Warning: mouse click toggle is not implemented");
Expand Down
5 changes: 4 additions & 1 deletion src/joystick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,10 @@ impl Stick for ScrollStick {
let delta = (angle - *last).normalize_signed() / settings.stick.scroll.sens + *acc;
let delta_rounded = delta.round();
*acc = delta - delta_rounded;
mouse.enigo().scroll(delta_rounded as i32, Axis::Vertical).unwrap();
mouse
.enigo()
.scroll(delta_rounded as i32, Axis::Vertical)
.unwrap();
*last = angle;
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ pub enum ClickType {

fn main() {
// https://github.com/rust-cli/human-panic/issues/77
human_panic::setup_panic!(human_panic::Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
.authors(env!("CARGO_PKG_AUTHORS").replace(":", ", "))
.homepage(env!("CARGO_PKG_REPOSITORY"))
);
human_panic::setup_panic!(human_panic::Metadata::new(
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
)
.authors(env!("CARGO_PKG_AUTHORS").replace(":", ", "))
.homepage(env!("CARGO_PKG_REPOSITORY")));

if let Err(e) = do_main() {
eprintln!("Error: {:?}", e);
Expand Down
16 changes: 5 additions & 11 deletions src/mapping.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use enigo::{Key, Button};
use enigo::{Button, Key};
use enum_map::{Enum, EnumArray, EnumMap};
use hid_gamepad_types::JoyKey;
use std::{
collections::HashMap,
fmt::Display,
time::Duration,
};
use std::{collections::HashMap, fmt::Display, time::Duration};
use std::{convert::TryInto, time::Instant};

use crate::ClickType;
Expand Down Expand Up @@ -150,7 +146,7 @@ const VIRTKEY_SIZE: usize = <VirtualKey as Enum>::LENGTH;
const MAP_KEY_SIZE: usize = JOYKEY_SIZE + VIRTKEY_SIZE;

impl Enum for MapKey {
const LENGTH: usize = MAP_KEY_SIZE;
const LENGTH: usize = MAP_KEY_SIZE;

fn from_usize(value: usize) -> Self {
if value < JOYKEY_SIZE {
Expand All @@ -170,7 +166,7 @@ impl Enum for MapKey {
}
}

impl<T> EnumArray<T> for MapKey{
impl<T> EnumArray<T> for MapKey {
type Array = [T; MAP_KEY_SIZE];
}

Expand Down Expand Up @@ -219,9 +215,7 @@ impl Buttons {
}

pub fn tick(&mut self, now: Instant) -> impl Iterator<Item = ExtAction> + '_ {
for key in (0..<MapKey as Enum>::LENGTH)
.map(<MapKey as Enum>::from_usize)
{
for key in (0..<MapKey as Enum>::LENGTH).map(<MapKey as Enum>::from_usize) {
let binding = self.find_binding(key);
match self.state[key].status {
KeyStatus::Down => {
Expand Down
9 changes: 6 additions & 3 deletions src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub struct Mouse {
error_accumulator: Vector2<f64>,
}


impl Mouse {
pub fn new() -> anyhow::Result<Self> {
#[allow(unused_mut)]
Expand Down Expand Up @@ -70,13 +69,17 @@ impl Mouse {
if let Some(rounded) = rounded.cast::<i32>() {
if rounded != Vector2::zero() {
// In enigo, +y is toward the bottom
self.enigo.move_mouse(rounded.x, rounded.y, Coordinate::Rel).unwrap();
self.enigo
.move_mouse(rounded.x, rounded.y, Coordinate::Rel)
.unwrap();
}
}
}

pub fn mouse_move_absolute_pixel(&mut self, offset: Vector2<i32>) {
self.enigo.move_mouse(offset.x, offset.y, Coordinate::Abs).unwrap();
self.enigo
.move_mouse(offset.x, offset.y, Coordinate::Abs)
.unwrap();
}

pub fn enigo(&mut self) -> &mut Enigo {
Expand Down

0 comments on commit 639b4d8

Please sign in to comment.