Added controller support

This commit is contained in:
antopilo
2024-12-01 20:52:34 -05:00
parent 82459a6e4c
commit d93d69d059
8 changed files with 47427 additions and 611 deletions

View File

@@ -215,6 +215,50 @@ namespace Nuake
#pragma endregion
bool Input::IsControllerPresent(int id)
{
if (id > GLFW_JOYSTICK_LAST)
{
return 0;
}
return glfwJoystickPresent(GLFW_JOYSTICK_1 + id);
}
std::string Input::GetControllerName(int id)
{
if (IsControllerPresent(id))
{
return glfwGetJoystickName(id);
}
return "Controller Disconnected";
}
bool Input::IsControllerButtonPressed(int id, ControllerInput input)
{
GLFWgamepadstate state;
if (glfwGetGamepadState(GLFW_JOYSTICK_1 + id, &state))
{
if (state.buttons[static_cast<int>(input)])
{
return true;
}
}
return false;
}
float Input::GetControllerAxis(int id, ControllerAxis axis)
{
GLFWgamepadstate state;
if (glfwGetGamepadState(GLFW_JOYSTICK_1 + id, &state))
{
return state.axes[static_cast<int>(axis)];
}
return 0.0f;
}
bool Input::Init()
{
//auto window = Application::Get().GetWindow()->GetNative();

View File

@@ -1,6 +1,7 @@
#pragma once
#include "src/Core/Maths.h"
#include <string>
#include <map>
#include <utility>
@@ -132,6 +133,35 @@ namespace Nuake
MENU = 348
};
enum class ControllerInput
{
A = 0,
B,
X,
Y,
LEFT_BUMPER,
RIGHT_BUMPER,
BACK,
START,
GUIDE,
LEFT_THUMB,
RIGHT_THUMB,
DPAD_UP,
DPAD_RIGHT,
DPAD_DOWN,
DPAD_LEFT
};
enum class ControllerAxis
{
LEFT_X = 0,
LEFT_Y,
RIGHT_X,
RIGHT_Y,
LEFT_TRIGGER,
RIGHT_TRIGGER
};
class Input
{
private:
@@ -172,6 +202,12 @@ namespace Nuake
static Vector2 GetViewportSize();
static void SetViewportDimensions(const Vector2& pos, const Vector2& size);
// Controller
static bool IsControllerPresent(int id);
static std::string GetControllerName(int id);
static bool IsControllerButtonPressed(int id, ControllerInput input);
static float GetControllerAxis(int id, ControllerAxis axis);
static bool Init();
static void Update();

File diff suppressed because it is too large Load Diff

View File

@@ -14,6 +14,9 @@ namespace Nuake {
extern const std::string Resources_resources_rc_path;
extern unsigned int Resources_resources_rc_len;
extern unsigned char Resources_resources_rc[];
extern const std::string Resources_Data_gamecontrollerdb_txt_path;
extern unsigned int Resources_Data_gamecontrollerdb_txt_len;
extern unsigned char Resources_Data_gamecontrollerdb_txt[];
extern const std::string Resources_Fonts_fa_regular_400_ttf_path;
extern unsigned int Resources_Fonts_fa_regular_400_ttf_len;
extern unsigned char Resources_Fonts_fa_regular_400_ttf[];

View File

@@ -81,7 +81,7 @@ namespace Nuake
}
if (controlled && Input::IsMouseButtonDown(1))
if (controlled && Input::IsMouseButtonDown(1) || Input::IsControllerPresent(0))
{
// Should probably not have speed binding in here.
if (Input::YScroll != 0)
@@ -114,15 +114,52 @@ namespace Nuake
if (Input::IsKeyDown(Key::A))
movement += Right * (Speed * ts);
const float deadZone = 0.2f;
if (Input::IsControllerPresent(0))
{
float axis = Input::GetControllerAxis(0, ControllerAxis::LEFT_X);
if (glm::abs(axis) < deadZone)
{
axis = 0.0f;
}
movement -= Right * (Speed * ts * axis);
}
if (Input::IsKeyDown(Key::W))
movement += Direction * (Speed * ts);
if (Input::IsKeyDown(Key::S))
movement -= Direction * (Speed * ts);
if (Input::IsControllerPresent(0))
{
float axis = Input::GetControllerAxis(0, ControllerAxis::LEFT_Y);
if (glm::abs(axis) < deadZone)
{
axis = 0.0f;
}
movement -= Direction * (Speed * ts * axis);
}
if (Input::IsKeyDown(Key::LEFT_SHIFT))
movement -= Up * (Speed * ts);
if (Input::IsKeyDown(Key::SPACE))
movement += Up * (Speed * ts);
float rightTrigger = (Input::GetControllerAxis(0, ControllerAxis::RIGHT_TRIGGER) + 1.0f) / 2.0f;
if (glm::abs(rightTrigger) < 0.1f)
{
rightTrigger = 0.0f;
}
movement += Up * (Speed * ts * rightTrigger);
float leftTrigger = (Input::GetControllerAxis(0, ControllerAxis::LEFT_TRIGGER) + 1.0f) / 2.0f;
if (glm::abs(leftTrigger) < 0.1f)
{
leftTrigger = 0.0f;
}
movement -= Up * (Speed * ts * leftTrigger);
Translation += Vector3(movement);
}
@@ -145,6 +182,20 @@ namespace Nuake
TargetYaw += diffx;
TargetPitch += diffy;
float rightAxis = Input::GetControllerAxis(0, ControllerAxis::RIGHT_X);
float upAxis = Input::GetControllerAxis(0, ControllerAxis::RIGHT_Y);
if (glm::abs(rightAxis) < 0.1f)
{
rightAxis = 0.0f;
}
if (glm::abs(upAxis) < 0.1f)
{
upAxis = 0.0f;
}
TargetYaw += rightAxis;
TargetPitch -= upAxis;
}
if (TargetPitch > 89.0f)

View File

@@ -3,6 +3,7 @@
#include "src/Core/Input.h"
#include <Coral/Array.hpp>
#include <Coral/String.hpp>
namespace Nuake {
@@ -39,6 +40,26 @@ namespace Nuake {
return Coral::Array<float>::New({ mousePosition.x, mousePosition.y });
}
bool IsControllerConnected(int id)
{
return Input::IsControllerPresent(id);
}
Coral::String GetControllerName(int id)
{
std::string controllerName = Input::GetControllerName(id);
return Coral::String::New(controllerName);
}
bool IsControllerButtonPressed(int id, int button)
{
return Input::IsControllerButtonPressed(id, (ControllerInput)button);
}
float GetControllerAxis(int id, int axis)
{
return Input::GetControllerAxis(id, (ControllerAxis)axis);
}
void InputNetAPI::RegisterMethods()
{
@@ -48,6 +69,12 @@ namespace Nuake {
RegisterMethod("Input.IsMouseButtonDownIcall", &IsMouseButtonDown);
RegisterMethod("Input.GetMousePositionIcall", &GetMousePosition);
RegisterMethod("Input.IsControllerConnectedIcall", &IsControllerConnected);
RegisterMethod("Input.GetControllerNameIcall", &GetControllerName);
RegisterMethod("Input.IsControllerButtonPressedIcall", &IsControllerButtonPressed);
RegisterMethod("Input.GetControllerAxisIcall", &GetControllerAxis);
}
}

View File

@@ -696,4 +696,3 @@ namespace Nuake {
}
}

View File

@@ -144,6 +144,35 @@ namespace Nuake.Net
BUTTON_8 = 7,
}
public enum ControllerInput
{
A = 0,
B,
X,
Y,
LEFT_BUMPER,
RIGHT_BUMPER,
BACK,
START,
GUIDE,
LEFT_THUMB,
RIGHT_THUMB,
DPAD_UP,
DPAD_RIGHT,
DPAD_DOWN,
DPAD_LEFT
};
public enum ControllerAxis
{
LEFT_X = 0,
LEFT_Y,
RIGHT_X,
RIGHT_Y,
LEFT_TRIGGER,
RIGHT_TRIGGER
};
public class Input
{
internal static unsafe delegate*<bool, void> ShowMouseIcall;
@@ -151,6 +180,11 @@ namespace Nuake.Net
internal static unsafe delegate*<int, bool> IsKeyPressedIcall;
internal static unsafe delegate*<int, bool> IsMouseButtonDownIcall;
internal static unsafe delegate*<NativeArray<float>> GetMousePositionIcall;
internal static unsafe delegate*<int, bool> IsControllerConnectedIcall;
internal static unsafe delegate*<int, NativeString> GetControllerNameIcall;
internal static unsafe delegate*<int, int, bool> IsControllerButtonPressedIcall;
internal static unsafe delegate*<int, int, float> GetControllerAxisIcall;
public static bool IsMouseButtonDown(MouseButton button)
{
@@ -179,5 +213,34 @@ namespace Nuake.Net
return new Vector2(result[0], result[1]);
}
public static bool IsControllerConnected(int id)
{
unsafe
{
return IsControllerConnectedIcall(id);
}
}
public static string GetControllerName(int id)
{
unsafe { return GetControllerNameIcall(id).ToString(); ; }
}
public static bool IsControllerButtonPressed(int id, ControllerInput button)
{
unsafe
{
return IsControllerButtonPressedIcall(id, (int)button);
}
}
public static float GetControllerAxis(int id, ControllerAxis axis)
{
unsafe
{
return GetControllerAxisIcall(id, (int)axis);
}
}
}
}