added cursor image loading

This commit is contained in:
Turánszki János
2024-10-28 07:00:21 +01:00
parent 61987641e1
commit 162c21eed7
6 changed files with 147 additions and 27 deletions
@@ -1863,6 +1863,9 @@ Query input devices
- IsGamepadButton(int button) : bool -- returns whether that button code is a gamepad button or not
- StringToButton(string str) : int -- returns button code for a given string name
- ButtonToString(int button, opt int preference = CONTROLLER_PREFERENCE_GENERIC) : string -- returns string name for the given button code. You can set a preference for controller type which can modify the string returned
- SetCursor(int cursor) -- sets the current cursor type. Values can be of the cursor values, see below
- SetCursorFromFile(int cursor, string filename) -- sets the specified cursor type to an image from a cursor file
- ResetCursor(int cursor) -- resets the specified cursor to the default one
#### ControllerFeedback
Describes controller feedback such as touch and LED color which can be replayed on a controller
@@ -2004,6 +2007,18 @@ Playstation button codes:
- [outer]CONTROLLER_PREFERENCE_PLAYSTATION : int
- [outer]CONTROLLER_PREFERENCE_XBOX : int
#### Cursor codes:
- [outer]CURSOR_DEFAULT : int
- [outer]CURSOR_TEXTINPUT : int
- [outer]CURSOR_RESIZEALL : int
- [outer]CURSOR_RESIZE_NS : int
- [outer]CURSOR_RESIZE_EW : int
- [outer]CURSOR_RESIZE_NESW : int
- [outer]CURSOR_RESIZE_NWSE : int
- [outer]CURSOR_HAND : int
- [outer]CURSOR_NOTALLOWED : int
- [outer]CURSOR_CROSS : int
### Physics
- [outer]physics : Physics -- use this global object to access physics functions
+68 -26
View File
@@ -42,9 +42,41 @@ namespace wi::input
bool double_click = false;
wi::Timer doubleclick_timer;
XMFLOAT2 doubleclick_prevpos = XMFLOAT2(0, 0);
CURSOR cursor_current = CURSOR_CROSS; // something that's not default, because at least once code should change it to default
CURSOR cursor_current = CURSOR_COUNT; // something that's not default, because at least once code should change it to default
CURSOR cursor_next = CURSOR_DEFAULT;
#ifdef PLATFORM_WINDOWS_DESKTOP
static const HCURSOR cursor_table_original[] = {
::LoadCursor(nullptr, IDC_ARROW),
::LoadCursor(nullptr, IDC_IBEAM),
::LoadCursor(nullptr, IDC_SIZEALL),
::LoadCursor(nullptr, IDC_SIZENS),
::LoadCursor(nullptr, IDC_SIZEWE),
::LoadCursor(nullptr, IDC_SIZENESW),
::LoadCursor(nullptr, IDC_SIZENWSE),
::LoadCursor(nullptr, IDC_HAND),
::LoadCursor(nullptr, IDC_NO),
::LoadCursor(nullptr, IDC_CROSS),
};
static HCURSOR cursor_table[arraysize(cursor_table_original)] = {};
#endif // PLATFORM_WINDOWS_DESKTOP
#ifdef SDL2
static const SDL_Cursor* cursor_table_original[] = {
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR),
};
static SDL_Cursor* cursor_table[arraysize(cursor_table_original)] = {};
#endif // SDL2
const KeyboardState& GetKeyboardState() { return keyboard; }
const MouseState& GetMouseState() { return mouse; }
@@ -93,6 +125,11 @@ namespace wi::input
wi::input::ps5::Initialize();
#endif // PLATFORM_PS5
for (int i = 0; i < arraysize(cursor_table); ++i)
{
cursor_table[i] = cursor_table_original[i];
}
wi::backlog::post("wi::input Initialized (" + std::to_string((int)std::round(timer.elapsed())) + " ms)");
initialized.store(true);
}
@@ -354,34 +391,10 @@ namespace wi::input
if(cursor_next != cursor_current || cursor_next != CURSOR_DEFAULT)
{
#ifdef PLATFORM_WINDOWS_DESKTOP
static HCURSOR cursor_table[] = {
::LoadCursor(nullptr, IDC_ARROW),
::LoadCursor(nullptr, IDC_IBEAM),
::LoadCursor(nullptr, IDC_SIZEALL),
::LoadCursor(nullptr, IDC_SIZENS),
::LoadCursor(nullptr, IDC_SIZEWE),
::LoadCursor(nullptr, IDC_SIZENESW),
::LoadCursor(nullptr, IDC_SIZENWSE),
::LoadCursor(nullptr, IDC_HAND),
::LoadCursor(nullptr, IDC_NO),
::LoadCursor(nullptr, IDC_CROSS),
};
::SetCursor(cursor_table[cursor_next]);
#endif // PLATFORM_WINDOWS_DESKTOP
#ifdef SDL2
static SDL_Cursor* cursor_table[] = {
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO),
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR),
};
SDL_SetCursor(cursor_table[cursor_next] ? cursor_table[cursor_next] : cursor_table[CURSOR_DEFAULT]);
#endif // SDL2
@@ -735,7 +748,14 @@ namespace wi::input
BUTTON WhatIsPressed(int playerindex)
{
for (int i = DIGIT_RANGE_START; i < BUTTON_ENUM_SIZE; ++i)
// First go through predefined enums:
for (int i = GAMEPAD_RANGE_START + 1; i < BUTTON_ENUM_SIZE; ++i)
{
if (Press((BUTTON)i, playerindex))
return (BUTTON)i;
}
// Go through remaining digits, letters and undefined:
for (int i = DIGIT_RANGE_START; i < GAMEPAD_RANGE_START; ++i)
{
if (Press((BUTTON)i, playerindex))
return (BUTTON)i;
@@ -809,6 +829,28 @@ namespace wi::input
cursor_next = cursor;
}
void SetCursorFromFile(CURSOR cursor, const char* filename)
{
#ifdef PLATFORM_WINDOWS_DESKTOP
wchar_t wfilename[1024] = {};
wi::helper::StringConvert(filename, wfilename);
cursor_table[cursor] = LoadCursorFromFile(wfilename);
#endif // PLATFORM_WINDOWS_DESKTOP
// refresh in case we set the current one:
cursor_next = cursor_current;
cursor_current = CURSOR_COUNT;
}
void ResetCursor(CURSOR cursor)
{
cursor_table[cursor] = cursor_table_original[cursor];
// refresh in case we set the current one:
cursor_next = cursor_current;
cursor_current = CURSOR_COUNT;
}
BUTTON StringToButton(const char* str)
{
if (str == nullptr)
+9
View File
@@ -252,9 +252,18 @@ namespace wi::input
CURSOR_HAND,
CURSOR_NOTALLOWED,
CURSOR_CROSS,
CURSOR_COUNT
};
void SetCursor(CURSOR cursor);
// Replaces the specified cursor from a cursor image file
// (file type must be a .cur or .ani on Windows)
void SetCursorFromFile(CURSOR cursor, const char* filename);
// Resets specified cursor to the original:
void ResetCursor(CURSOR cursor);
BUTTON StringToButton(const char* str);
enum CONTROLLER_PREFERENCE
+51
View File
@@ -20,6 +20,9 @@ namespace wi::lua
lunamethod(Input_BindLua, IsGamepadButton),
lunamethod(Input_BindLua, ButtonToString),
lunamethod(Input_BindLua, StringToButton),
lunamethod(Input_BindLua, SetCursor),
lunamethod(Input_BindLua, SetCursorFromFile),
lunamethod(Input_BindLua, ResetCursor),
{ NULL, NULL }
};
Luna<Input_BindLua>::PropertyType Input_BindLua::properties[] = {
@@ -253,6 +256,43 @@ namespace wi::lua
wi::lua::SError(L, "StringToButton(string str): not enough parameters!");
return 0;
}
int Input_BindLua::SetCursor(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
wi::input::CURSOR cursor = (wi::input::CURSOR)wi::lua::SGetInt(L, 1);
wi::input::SetCursor(cursor);
return 0;
}
wi::lua::SError(L, "SetCursor(int cursor) not enough arguments!");
return 0;
}
int Input_BindLua::SetCursorFromFile(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 1)
{
wi::input::CURSOR cursor = (wi::input::CURSOR)wi::lua::SGetInt(L, 1);
const char* filename = wi::lua::SGetString(L, 2);
wi::input::SetCursorFromFile(cursor, filename);
return 0;
}
wi::lua::SError(L, "SetCursorFromFile(int cursor, string filename) not enough arguments!");
return 0;
}
int Input_BindLua::ResetCursor(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
wi::input::CURSOR cursor = (wi::input::CURSOR)wi::lua::SGetInt(L, 1);
wi::input::ResetCursor(cursor);
return 0;
}
wi::lua::SError(L, "ResetCursor(int cursor) not enough arguments!");
return 0;
}
void Input_BindLua::Bind()
{
@@ -393,6 +433,17 @@ KEYBOARD_BUTTON_INSERT = 564
CONTROLLER_PREFERENCE_GENERIC = 0
CONTROLLER_PREFERENCE_PLAYSTATION = 1
CONTROLLER_PREFERENCE_XBOX = 2
CURSOR_DEFAULT = 0
CURSOR_TEXTINPUT = 1
CURSOR_RESIZEALL = 2
CURSOR_RESIZE_NS = 3
CURSOR_RESIZE_EW = 4
CURSOR_RESIZE_NESW = 5
CURSOR_RESIZE_NWSE = 6
CURSOR_HAND = 7
CURSOR_NOTALLOWED = 8
CURSOR_CROSS = 9
)");
}
+3
View File
@@ -30,6 +30,9 @@ namespace wi::lua
int IsGamepadButton(lua_State* L);
int ButtonToString(lua_State* L);
int StringToButton(lua_State* L);
int SetCursor(lua_State* L);
int SetCursorFromFile(lua_State* L);
int ResetCursor(lua_State* L);
static void Bind();
};
+1 -1
View File
@@ -9,7 +9,7 @@ namespace wi::version
// minor features, major updates, breaking compatibility changes
const int minor = 71;
// minor bug fixes, alterations, refactors, updates
const int revision = 606;
const int revision = 607;
const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);