Skip to content

Commit

Permalink
Merge pull request ceifa#113 from dfabulich/show-floating-gamepad-tex…
Browse files Browse the repository at this point in the history
…t-input

Add utils.showFloatingGamepadTextInput()
  • Loading branch information
ceifa authored Jul 14, 2024
2 parents bd30b38 + 4c047c7 commit 5a1f44f
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
18 changes: 18 additions & 0 deletions client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ export namespace utils {
export function getAppId(): number
export function getServerRealTime(): number
export function isSteamRunningOnSteamDeck(): boolean
export const enum GamepadTextInputMode {
Normal = 0,
Password = 1
}
export const enum GamepadTextInputLineMode {
SingleLine = 0,
MultipleLines = 1
}
/** @returns the entered text, or null if cancelled or could not show the input */
export function showGamepadTextInput(inputMode: GamepadTextInputMode, inputLineMode: GamepadTextInputLineMode, description: string, maxCharacters: number, existingText?: string | undefined | null): Promise<string | null>
export const enum FloatingGamepadTextInputMode {
SingleLine = 0,
MultipleLines = 1,
Email = 2,
Numeric = 3
}
/** @returns true if the floating keyboard was shown, otherwise, false */
export function showFloatingGamepadTextInput(keyboardMode: FloatingGamepadTextInputMode, x: number, y: number, width: number, height: number): Promise<boolean>
}
export namespace workshop {
export interface UgcResult {
Expand Down
112 changes: 112 additions & 0 deletions src/api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use napi_derive::napi;

#[napi]
pub mod utils {
use napi::bindgen_prelude::{FromNapiValue, ToNapiValue};
use steamworks::FloatingGamepadTextInputMode as kFloatingGamepadTextInputMode;
use steamworks::GamepadTextInputLineMode as kGamepadTextInputLineMode;
use steamworks::GamepadTextInputMode as kGamepadTextInputMode;
use tokio::sync::oneshot;

#[napi]
pub fn get_app_id() -> u32 {
let client = crate::client::get_client();
Expand All @@ -19,4 +25,110 @@ pub mod utils {
let client = crate::client::get_client();
client.utils().is_steam_running_on_steam_deck()
}

#[napi]
pub enum GamepadTextInputMode {
Normal,
Password,
}

#[napi]
pub enum GamepadTextInputLineMode {
SingleLine,
MultipleLines,
}

/// @returns the entered text, or null if cancelled or could not show the input
#[napi]
pub async fn show_gamepad_text_input(
input_mode: GamepadTextInputMode,
input_line_mode: GamepadTextInputLineMode,
description: String,
max_characters: u32,
existing_text: Option<String>,
) -> Option<String> {
let client = crate::client::get_client();

let (tx, rx) = oneshot::channel();
let mut tx = Some(tx);

let opened = client.utils().show_gamepad_text_input(
match input_mode {
GamepadTextInputMode::Normal => kGamepadTextInputMode::Normal,
GamepadTextInputMode::Password => kGamepadTextInputMode::Password,
},
match input_line_mode {
GamepadTextInputLineMode::SingleLine => kGamepadTextInputLineMode::SingleLine,
GamepadTextInputLineMode::MultipleLines => kGamepadTextInputLineMode::MultipleLines,
},
&description,
max_characters,
existing_text.as_deref(),
move |dismissed_data| {
if let Some(tx) = tx.take() {
let text = client
.utils()
.get_entered_gamepad_text_input(&dismissed_data);
tx.send(text).unwrap();
}
},
);

if opened {
rx.await.unwrap()
} else {
None
}
}

#[napi]
pub enum FloatingGamepadTextInputMode {
SingleLine,
MultipleLines,
Email,
Numeric,
}

/// @returns true if the floating keyboard was shown, otherwise, false
#[napi]
pub async fn show_floating_gamepad_text_input(
keyboard_mode: FloatingGamepadTextInputMode,
x: i32,
y: i32,
width: i32,
height: i32,
) -> bool {
let client = crate::client::get_client();

let (tx, rx) = oneshot::channel();
let mut tx = Some(tx);

let opened = client.utils().show_floating_gamepad_text_input(
match keyboard_mode {
FloatingGamepadTextInputMode::SingleLine => {
kFloatingGamepadTextInputMode::SingleLine
}
FloatingGamepadTextInputMode::MultipleLines => {
kFloatingGamepadTextInputMode::MultipleLines
}
FloatingGamepadTextInputMode::Email => kFloatingGamepadTextInputMode::Email,
FloatingGamepadTextInputMode::Numeric => kFloatingGamepadTextInputMode::Numeric,
},
x,
y,
width,
height,
move || {
if let Some(tx) = tx.take() {
tx.send(true).unwrap();
}
},
);

if opened {
rx.await.unwrap()
} else {
false
}
}
}

0 comments on commit 5a1f44f

Please sign in to comment.