Skip to content

Commit

Permalink
Added gamepad text input functions
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Dec 23, 2024
1 parent d858fc3 commit 5712b7a
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.DS_Store
Thumbs.db
/.github
/.editor_settings
42 changes: 42 additions & 0 deletions steam/src/steam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ static const luaL_reg Module_methods[] = {
{ "utils_get_image_size", SteamUtils_GetImageSize },
{ "utils_get_image_rgba", SteamUtils_GetImageRGBA },
{ "utils_get_server_real_time", SteamUtils_GetServerRealTime },
{ "utils_show_floating_gamepad_text_input", SteamUtils_ShowFloatingGamepadTextInput },
{ "utils_show_gamepad_text_input", SteamUtils_ShowGamepadTextInput },


// USERSTATS - stats
Expand Down Expand Up @@ -245,6 +247,46 @@ static void LuaInit(lua_State* L)
lua_pushnumber(L, (lua_Number) val); \
lua_setfield(L, -2, #name);

//
// EFloatingGamepadTextInputMode
//
/**
* Enter dismisses the keyboard
* @field EFloatingGamepadTextInputModeModeSingleLine
*/
SETCONSTANT(EFloatingGamepadTextInputModeModeSingleLine, k_EFloatingGamepadTextInputModeModeSingleLine);
/**
* User needs to explicitly dismiss the keyboard
* @field EFloatingGamepadTextInputModeModeMultipleLines
*/
SETCONSTANT(EFloatingGamepadTextInputModeModeMultipleLines, k_EFloatingGamepadTextInputModeModeMultipleLines);
/**
* Keyboard is displayed in a special mode that makes it easier to enter emails
* @field EFloatingGamepadTextInputModeModeEmail
*/
SETCONSTANT(EFloatingGamepadTextInputModeModeEmail, k_EFloatingGamepadTextInputModeModeEmail);
/**
* Numeric keypad is shown
* @field EFloatingGamepadTextInputModeModeNumeric
*/
SETCONSTANT(EFloatingGamepadTextInputModeModeNumeric, k_EFloatingGamepadTextInputModeModeNumeric);


//
// EGamepadTextInputMode
//
/**
* Normal text input
* @field EGamepadTextInputModeNormal
*/
SETCONSTANT(EGamepadTextInputModeNormal, k_EGamepadTextInputModeNormal);
/**
* Password text input
* @field EGamepadTextInputModePassword
*/
SETCONSTANT(EGamepadTextInputModePassword, k_EGamepadTextInputModePassword);


//
// ELeaderboardDataRequest
//
Expand Down
117 changes: 117 additions & 0 deletions steam/src/steam_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,72 @@

#include <dmsdk/sdk.h>
#include "steam_api.h"
#include "steam_listener.h"

const uint32 MAX_INPUT_LENGTH = 1024;
const uint32 FOO = MAX_INPUT_LENGTH + 1;


int SteamUtils_OnFloatingGamepadTextInputDismissed(lua_State* L, void* data)
{
FloatingGamepadTextInputDismissed_t* s = (FloatingGamepadTextInputDismissed_t*)data;
lua_pushstring(L, "FloatingGamepadTextInputDismissed_t");
return 1;
}

int SteamUtils_OnGamepadTextInputDismissed(lua_State* L, void* data)
{
GamepadTextInputDismissed_t* s = (GamepadTextInputDismissed_t*)data;
// The user canceled,
if (!s->m_bSubmitted) {
lua_pushnil(L);
return 1;
}

const uint32 length = SteamUtils()->GetEnteredGamepadTextLength();
char* szTextInput = (char*)malloc(length);
bool success = SteamUtils()->GetEnteredGamepadTextInput( szTextInput, length );

if ( !success ) {
// Log an error. This should only ever happen if length is > MaxInputLength
free(szTextInput);
lua_pushnil(L);
return 1;
}

lua_pushstring(L, "GamepadTextInputDismissed_t");

lua_newtable(L);
lua_pushstring(L, "m_szTextInput");
lua_pushstring(L, szTextInput);
lua_settable(L, -3);

free(szTextInput);
return 2;
}

class SteamUtilsCallbacks
{
public:
SteamUtilsCallbacks();
STEAM_CALLBACK(SteamUtilsCallbacks, OnGamepadTextInputDismissed, GamepadTextInputDismissed_t, m_CallbackGamepadTextInputDismissed);
STEAM_CALLBACK(SteamUtilsCallbacks, OnFloatingGamepadTextInputDismissed, FloatingGamepadTextInputDismissed_t, m_CallbackFloatingGamepadTextInputDismissed);
};
SteamUtilsCallbacks::SteamUtilsCallbacks() :
m_CallbackGamepadTextInputDismissed(this, &SteamUtilsCallbacks::OnGamepadTextInputDismissed),
m_CallbackFloatingGamepadTextInputDismissed(this, &SteamUtilsCallbacks::OnFloatingGamepadTextInputDismissed)
{}
void SteamUtilsCallbacks::OnGamepadTextInputDismissed(GamepadTextInputDismissed_t *s)
{
SteamListener_Invoke(SteamUtils_OnGamepadTextInputDismissed, s);
}
void SteamUtilsCallbacks::OnFloatingGamepadTextInputDismissed(FloatingGamepadTextInputDismissed_t *s)
{
SteamListener_Invoke(SteamUtils_OnFloatingGamepadTextInputDismissed, s);
}


static SteamUtilsCallbacks* g_SteamUtilsCallbacks = new SteamUtilsCallbacks();

static ISteamUtils* g_SteamUtils = 0;

Expand Down Expand Up @@ -139,4 +205,55 @@ int SteamUtils_GetServerRealTime(lua_State* L)
}



/** Opens a floating keyboard over the game content and sends OS keyboard keys directly to the game.
* @name utils_show_floating_gamepad_text_input
* @number mode EFloatingGamepadTextInputMode
* @number x Text field x position
* @number y Text field y position
* @number width Text field width
* @number height Text field height
* @treturn bool result True if the floating keyboard was shown, otherwise, false.
*/
int SteamUtils_ShowFloatingGamepadTextInput(lua_State* L)
{
if (!g_SteamUtils) return 0;
DM_LUA_STACK_CHECK(L, 1);

EFloatingGamepadTextInputMode eKeyboardMode = (EFloatingGamepadTextInputMode)luaL_checknumber(L, 1);
int nTextFieldXPosition = luaL_checknumber(L, 2);
int nTextFieldYPosition = luaL_checknumber(L, 3);
int nTextFieldWidth = luaL_checknumber(L, 4);
int nTextFieldHeight = luaL_checknumber(L, 5);

bool was_shown = g_SteamUtils->ShowFloatingGamepadTextInput(eKeyboardMode, nTextFieldXPosition, nTextFieldYPosition, nTextFieldWidth, nTextFieldHeight);
lua_pushboolean(L, was_shown);
return 1;
}


/** Activates the Big Picture text input dialog which only supports gamepad input.
* @name utils_show_gamepad_text_input
* @number input_mode EGamepadTextInputMode
* @number line_input_mode EGamepadTextInputLineMode
* @string description Sets the description that should inform the user what the input dialog is for
* @string existing_text Sets the preexisting text which the user can edit.
* @treturn bool result True if the big picture overlay is running; otherwise, false
*/
int SteamUtils_ShowGamepadTextInput(lua_State* L)
{
if (!g_SteamUtils) return 0;
DM_LUA_STACK_CHECK(L, 1);

EGamepadTextInputMode eInputMode = (EGamepadTextInputMode)luaL_checknumber(L, 1);
EGamepadTextInputLineMode eLineInputMode = (EGamepadTextInputLineMode)luaL_checknumber(L, 2);
const char *pchDescription = luaL_checkstring(L, 3);
uint32 unCharMax = MAX_INPUT_LENGTH;
const char *pchExistingText = luaL_checkstring(L, 4);

bool big_picture = g_SteamUtils->ShowGamepadTextInput(eInputMode, eLineInputMode, pchDescription, unCharMax, pchExistingText);
lua_pushboolean(L, big_picture);
return 1;
}

#endif
5 changes: 5 additions & 0 deletions steam/src/steam_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

#include <dmsdk/sdk.h>

int SteamUtils_OnGamepadTextInputDismissed(lua_State* L, void* data);
int SteamUtils_OnFloatingGamepadTextInputDismissed(lua_State* L, void* data);

int SteamUtils_Init(lua_State* L);
int SteamUtils_GetAppId(lua_State* L);
int SteamUtils_GetSecondsSinceAppActive(lua_State* L);
int SteamUtils_IsSteamRunningOnSteamDeck(lua_State* L);
int SteamUtils_GetImageSize(lua_State* L);
int SteamUtils_GetImageRGBA(lua_State* L);
int SteamUtils_GetServerRealTime(lua_State* L);
int SteamUtils_ShowFloatingGamepadTextInput(lua_State* L);
int SteamUtils_ShowGamepadTextInput(lua_State* L);

#endif

Expand Down

0 comments on commit 5712b7a

Please sign in to comment.