Skip to content

Commit

Permalink
Client: separated configs definitions into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
DvaMishkiLapa committed Nov 12, 2024
1 parent 3e2b428 commit 6ec3b67
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 230 deletions.
223 changes: 3 additions & 220 deletions client/packages/vita_virtual_device/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use rstar::RTree;
use serde::{Deserialize, Serialize};
use std::ffi::OsString;

mod virtual_button;
pub use virtual_button::{Button, DpadDirection};

mod virtual_config;
mod virtual_touch;
pub use virtual_touch::{Point, TouchAction, TouchZone};

pub use virtual_touch::Point;

// Error handling that includes platform-specific errors
#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -34,221 +32,6 @@ pub trait VitaVirtualDevice<ConfigSetter: ?Sized>: Sized {
fn send_report(&mut self, report: vita_reports::MainReport) -> Result<()>;
}

/// Configuration for touch inputs, can be zones or a touchpad.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[doc(hidden)]
pub enum TouchConfig {
Zones(RTree<TouchZone>),
Touchpad,
}

impl TouchConfig {
/// Creates a `TouchConfig` with specified zones.
pub fn zones<I: IntoIterator<Item = TouchZone>>(it: I) -> Self {
TouchConfig::Zones(RTree::bulk_load(it.into_iter().collect()))
}

/// Creates a `TouchConfig` representing a touchpad.
#[inline]
pub fn touchpad() -> Self {
TouchConfig::Touchpad
}
}

/// Configuration for trigger inputs.
#[derive(Clone, Debug, Copy, Deserialize, Serialize)]
pub enum TriggerConfig {
Shoulder,
Trigger,
}

impl Default for TriggerConfig {
#[inline]
fn default() -> Self {
TriggerConfig::Shoulder
}
}

impl Config {
#[inline]
pub fn builder() -> ConfigBuilder {
ConfigBuilder::default()
}

#[inline]
pub fn rear_rl2_front_rl3() -> Self {
Config {
rear_touch_config: Some(TouchConfig::zones([
TouchZone::new(
(
REAR_TOUCHPAD_RECT.0,
Point((REAR_TOUCHPAD_RECT.1).0 / 2, (REAR_TOUCHPAD_RECT.1).1),
),
Some(TouchAction::Button(Button::ShoulderLeft)),
),
TouchZone::new(
(
Point((REAR_TOUCHPAD_RECT.1).0 / 2, (REAR_TOUCHPAD_RECT.0).0),
REAR_TOUCHPAD_RECT.1,
),
Some(TouchAction::Button(Button::ShoulderRight)),
),
])),
front_touch_config: Some(TouchConfig::zones([
TouchZone::new(
(
FRONT_TOUCHPAD_RECT.0,
Point((FRONT_TOUCHPAD_RECT.1).0 / 2, (FRONT_TOUCHPAD_RECT.1).1),
),
Some(TouchAction::Button(Button::ThumbLeft)),
),
TouchZone::new(
(
Point((FRONT_TOUCHPAD_RECT.1).0 / 2, (FRONT_TOUCHPAD_RECT.0).0),
FRONT_TOUCHPAD_RECT.1,
),
Some(TouchAction::Button(Button::ThumbRight)),
),
])),
trigger_config: TriggerConfig::Trigger,
}
}

#[inline]
pub fn rear_rl1_front_rl3_vitatriggers_rl2() -> Self {
Config {
rear_touch_config: Some(TouchConfig::zones([
TouchZone::new(
(
REAR_TOUCHPAD_RECT.0,
Point((REAR_TOUCHPAD_RECT.1).0 / 2, (REAR_TOUCHPAD_RECT.1).1),
),
Some(TouchAction::Button(Button::TriggerLeft)),
),
TouchZone::new(
(
Point((REAR_TOUCHPAD_RECT.1).0 / 2, (REAR_TOUCHPAD_RECT.0).0),
REAR_TOUCHPAD_RECT.1,
),
Some(TouchAction::Button(Button::TriggerRight)),
),
])),
front_touch_config: Some(TouchConfig::zones([
TouchZone::new(
(
FRONT_TOUCHPAD_RECT.0,
Point((FRONT_TOUCHPAD_RECT.1).0 / 2, (FRONT_TOUCHPAD_RECT.1).1),
),
Some(TouchAction::Button(Button::ThumbLeft)),
),
TouchZone::new(
(
Point((FRONT_TOUCHPAD_RECT.1).0 / 2, (FRONT_TOUCHPAD_RECT.0).0),
FRONT_TOUCHPAD_RECT.1,
),
Some(TouchAction::Button(Button::ThumbRight)),
),
])),
trigger_config: TriggerConfig::Shoulder,
}
}

#[inline]
pub fn front_top_rl2_bottom_rl3_rear_touchpad() -> Self {
Config {
front_touch_config: Some(TouchConfig::zones([
TouchZone::new(
(
FRONT_TOUCHPAD_RECT.0,
Point(FRONT_TOUCHPAD_RECT.1.x() / 2, FRONT_TOUCHPAD_RECT.1.y() / 2),
),
Some(TouchAction::Button(Button::ShoulderLeft)),
),
TouchZone::new(
(
Point(FRONT_TOUCHPAD_RECT.1.x() / 2, FRONT_TOUCHPAD_RECT.0.y()),
Point(FRONT_TOUCHPAD_RECT.1.x(), FRONT_TOUCHPAD_RECT.1.y() / 2),
),
Some(TouchAction::Button(Button::ShoulderRight)),
),
TouchZone::new(
(
Point(FRONT_TOUCHPAD_RECT.0.x(), FRONT_TOUCHPAD_RECT.1.y() / 2),
Point(FRONT_TOUCHPAD_RECT.1.x() / 2, FRONT_TOUCHPAD_RECT.1.y()),
),
Some(TouchAction::Button(Button::ThumbLeft)),
),
TouchZone::new(
(
Point(FRONT_TOUCHPAD_RECT.1.x() / 2, FRONT_TOUCHPAD_RECT.1.y() / 2),
FRONT_TOUCHPAD_RECT.1,
),
Some(TouchAction::Button(Button::ThumbRight)),
),
])),
rear_touch_config: Some(TouchConfig::Touchpad),
trigger_config: TriggerConfig::Trigger,
}
}

#[inline]
pub fn rear_top_rl2_bottom_rl3_front_touchpad() -> Self {
Config {
front_touch_config: Some(TouchConfig::Touchpad),
rear_touch_config: Some(TouchConfig::zones([
TouchZone::new(
(
REAR_TOUCHPAD_RECT.0,
Point(REAR_TOUCHPAD_RECT.1.x() / 2, REAR_TOUCHPAD_RECT.1.y() / 2),
),
Some(TouchAction::Button(Button::ShoulderLeft)),
),
TouchZone::new(
(
Point(REAR_TOUCHPAD_RECT.1.x() / 2, REAR_TOUCHPAD_RECT.0.y()),
Point(REAR_TOUCHPAD_RECT.1.x(), REAR_TOUCHPAD_RECT.1.y() / 2),
),
Some(TouchAction::Button(Button::ShoulderRight)),
),
TouchZone::new(
(
Point(REAR_TOUCHPAD_RECT.0.x(), REAR_TOUCHPAD_RECT.1.y() / 2),
Point(REAR_TOUCHPAD_RECT.1.x() / 2, REAR_TOUCHPAD_RECT.1.y()),
),
Some(TouchAction::Button(Button::ThumbLeft)),
),
TouchZone::new(
(
Point(REAR_TOUCHPAD_RECT.1.x() / 2, REAR_TOUCHPAD_RECT.1.y() / 2),
REAR_TOUCHPAD_RECT.1,
),
Some(TouchAction::Button(Button::ThumbRight)),
),
])),
trigger_config: TriggerConfig::Trigger,
}
}
}

/// Overall configuration for the virtual device.
#[derive(Clone, Debug, Deserialize, Serialize, derive_builder::Builder)]
pub struct Config {
pub front_touch_config: Option<TouchConfig>,
pub rear_touch_config: Option<TouchConfig>,
pub trigger_config: TriggerConfig,
}

impl Default for Config {
#[inline]
fn default() -> Self {
Config {
front_touch_config: Some(TouchConfig::Touchpad),
rear_touch_config: None,
trigger_config: TriggerConfig::default(),
}
}
}

/// Helper function to convert a `f32` value to `i16` within specified bounds.
#[inline]
pub fn f32_to_i16(value: f32, min_value: f32, max_value: f32) -> i16 {
Expand Down
Loading

0 comments on commit 6ec3b67

Please sign in to comment.