First real iteration of the API.

This commit is contained in:
2025-03-17 22:45:04 -04:00
parent c4c7b99f4c
commit 6b10887599
3 changed files with 63 additions and 7 deletions

View File

@@ -29,6 +29,14 @@ namespace jstick {
private:
};
class XBoxJoystick : public Joystick {
public:
protected:
private:
};
enum class ControllerType {
XBox,
PS3, PS4, PS5,
@@ -52,9 +60,17 @@ namespace jstick {
ThumbR = 10,
};
/// This event is fired when a new joystick device is detected.
// TODO: Call JoystickConnected for each device found on library runtime start.
Event<int> JoystickConnected;
/// This event is fired when a joystick device is unplugged or otherwise dropped.
Event<int> JoystickDisconnected;
/// This event is fired when a joystick button is pressed.
Event<XBoxButton> ButtonPressed;
/// This event is fired when a xbox button is pressed.
Event<XBoxButton> ButtonReleased;
Event<Vector2> LeftThumbstickMoved;
Event<Vector2> RightThumbstickMoved;

View File

@@ -230,6 +230,46 @@ void disconnect_device() {
int main(int argc, char *argv[])
{
std::cout << jstick::NumJoysticksDetected() << std::endl;
bool success = jstick::Connect(0);
jstick::ButtonPressed += [](jstick::XBoxButton btn){
std::cout << std::format("Button Pressed: {}", (int)btn) << std::endl;
};
jstick::ButtonReleased += [](jstick::XBoxButton btn) {
std::cout << std::format("Button Released: {}", (int)btn) << std::endl;
};
jstick::DPadMoved += [](Vector2 v) {
std::cout << std::format("DPad Moved {},{}", v.x, v.y) << std::endl;
};
jstick::LeftThumbstickMoved += [](Vector2 v)
{
std::cout << std::format("LThumb Moved {},{}", v.x, v.y) << std::endl;
};
jstick::RightThumbstickMoved += [](Vector2 v)
{
std::cout << std::format("RThumb Moved {},{}", v.x, v.y) << std::endl;
};
jstick::LeftTriggerMoved += [] (short val)
{
std::cout << std::format("LTrigger Moved {}", val) << std::endl;
};
jstick::RightTriggerMoved += [](short val)
{
std::cout << std::format("RTrigger Moved {}", val) << std::endl;
};
while(success)
{
jstick::ReadEventLoop();
}
return 0;
/*
@@ -281,8 +321,6 @@ int main(int argc, char *argv[])
close(js);
js = -1;
}
}
had = has_file;

View File

@@ -15,7 +15,6 @@ struct axis_state {
short x, y;
};
int js_handle;
bool js_connected;
struct js_event event;
@@ -29,7 +28,6 @@ short r_trigger;
Vector2 dpad;
/// Reads a joystick event from the joystick device.
/// @returns 0 on success. Otherwise -1 is returned.
int read_event(int fd, struct js_event *event) {
@@ -78,6 +76,7 @@ bool jstick::Connect(int hwid) {
}
js_connected = true;
JoystickConnected.Invoke(js_handle);
return true;
}
@@ -87,6 +86,7 @@ bool jstick::Disconnect(int hwid) {
return false;
}
close(js_handle);
JoystickDisconnected.Invoke(js_handle);
js_connected = false;
return true;
}
@@ -136,11 +136,13 @@ void ProcessAxisEvent(uint8_t axis, short value) {
if (axis == 6) dpad.x = value;
if (axis == 7) dpad.y = value;
if (l_thumb.DistanceSq(prev_lthumb) > 1.f)
// TODO: Compare against the l_thumb from the last time the event was called.
if (l_thumb.DistanceSq(prev_lthumb) > 0.1f)
jstick::LeftThumbstickMoved.Invoke(l_thumb);
if (r_thumb.DistanceSq(prev_rthumb) > 1.f)
if (r_thumb.DistanceSq(prev_rthumb) > 0.1f)
jstick::RightThumbstickMoved.Invoke(r_thumb);
if (dpad.DistanceSq(prev_dp) > 1.f)
if (dpad.DistanceSq(prev_dp) > 0.1f)
jstick::DPadMoved.Invoke(dpad);
if (Math::Abs(l_trigger) - Math::Abs(prev_ltrigger) > 1.f)