载入中...
搜索中...
未找到
Joystick.cpp
浏览该文件的文档.
1#include "joystick/Joystick.h"
3#include "joystick/Pad.h"
4
5#include <cmath>
6#include <simplesquirrel/simplesquirrel.hpp>
7
8namespace eve::joystick {
9
11
13
14float clampAxis(float x) {
15 if (std::fabs(x) < 0.01f) return 0.0f;
16 if (x < -0.99f) return -1.0f;
17 if (x > 0.99f) return 1.0f;
18 return x;
19}
20
21void Joystick::expose(ssq::Table& table) {
22 auto cls = table.addClass(name, Joystick::create, false);
23 expose(cls);
24
25 // Pad instances are owned by the Joystick module (created/removed by the
26 // SDL event sink), so they are exposed non-owning: the script constructor
27 // returns null and pushed pads are never deleted by the GC.
28 auto pad = table.addClass<Pad>(
29 "Pad", std::function<Pad*()>([]() -> Pad* { return nullptr; }), true);
30 pad.addFunc("getName", &Pad::getName);
31 pad.addFunc("getAxisCount", &Pad::getAxisCount);
32 pad.addFunc("getButtonCount", &Pad::getButtonCount);
33 pad.addFunc("getHatCount", &Pad::getHatCount);
34 pad.addFunc("getAxis", &Pad::getAxis);
35 pad.addFunc("getAxes", &Pad::getAxes);
36 pad.addFunc("getHat", &Pad::getHat);
37 pad.addFunc("isDown", static_cast<bool (Pad::*)(int) const>(&Pad::isDown));
38 pad.addFunc("isGamepad", &Pad::isGamepad);
39 pad.addFunc("getGamepadAxis", &Pad::getGamepadAxis);
40 pad.addFunc("isGamepadDown",
41 static_cast<bool (Pad::*)(const std::string&) const>(&Pad::isGamepadDown));
42 pad.addFunc("getGamepadMappingString", &Pad::getGamepadMappingString);
43 pad.addFunc("getGUID", &Pad::getGUID);
44 pad.addFunc("getInstanceID", &Pad::getInstanceID);
45 pad.addFunc("getID", &Pad::getID);
46 pad.addFunc("getVendorID", [](Pad* self) -> int {
47 int vendor = 0, product = 0, version = 0;
48 if (self) self->getDeviceInfo(vendor, product, version);
49 return vendor;
50 });
51 pad.addFunc("getProductID", [](Pad* self) -> int {
52 int vendor = 0, product = 0, version = 0;
53 if (self) self->getDeviceInfo(vendor, product, version);
54 return product;
55 });
56 pad.addFunc("getProductVersion", [](Pad* self) -> int {
57 int vendor = 0, product = 0, version = 0;
58 if (self) self->getDeviceInfo(vendor, product, version);
59 return version;
60 });
61 pad.addFunc("isVibrationSupported", &Pad::isVibrationSupported);
62 pad.addFunc("setVibration", [](Pad* self, float left, float right) -> bool {
63 return self && self->setVibration(left, right);
64 });
65 pad.addFunc("setVibrationTimed",
66 [](Pad* self, float left, float right, float duration) -> bool {
67 return self && self->setVibration(left, right, duration);
68 });
69 pad.addFunc("stopVibration", [](Pad* self) -> bool { return self && self->setVibration(); });
70 pad.addFunc("getVibrationLeft", [](Pad* self) -> float {
71 float left = 0.f, right = 0.f;
72 if (self) self->getVibration(left, right);
73 return left;
74 });
75 pad.addFunc("getVibrationRight", [](Pad* self) -> float {
76 float left = 0.f, right = 0.f;
77 if (self) self->getVibration(left, right);
78 return right;
79 });
80}
81
82void Joystick::expose(ssq::Class& cls) {
83 cls.addFunc("getName", &Joystick::getName);
84 cls.addFunc("getJoystickCount", &Joystick::getJoystickCount);
85 cls.addFunc("getJoystick", &Joystick::getJoystick);
86 cls.addFunc("getJoystickFromID", &Joystick::getJoystickFromID);
87 cls.addFunc("getIndex", &Joystick::getIndex);
88 cls.addFunc("addJoystick", &Joystick::addJoystick);
89 cls.addFunc("removeJoystick", &Joystick::removeJoystick);
90 cls.addFunc("loadGamepadMappings", &Joystick::loadGamepadMappings);
91 cls.addFunc("saveGamepadMappings", &Joystick::saveGamepadMappings);
92 cls.addFunc("getGamepadMappingString", &Joystick::getGamepadMappingString);
93}
94
95} // namespace eve::joystick
float x
Definition AnimClip.cpp:738
float duration
HSQOBJECT cls
Definition ECS.cpp:21
HexVec3 left
HexVec3 right
std::string name
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:26
virtual std::string getName() const =0
Returns the name.
Joystick / gamepad manager. Tracks connected pads and gamecontroller mappings.
Definition Joystick.h:14
virtual int getJoystickCount() const =0
Number of currently tracked pads.
virtual std::string saveGamepadMappings()=0
Serializes the in-memory GameController mapping database.
virtual void removeJoystick(Pad *pad)=0
Removes and destroys a previously opened pad.
virtual Pad * getJoystickFromID(int instanceid)=0
Looks up a pad by SDL instance id; null if unknown.
virtual void loadGamepadMappings(const std::string &mappings)=0
Load SDL GameController mapping database (newline-separated).
virtual std::string getGamepadMappingString(const std::string &guid) const =0
Mapping string for a device GUID, or empty if none.
virtual int getIndex(const Pad *pad)=0
Dense index of pad, or -1 if not tracked.
virtual ~Joystick()
Closes tracked pads and releases backend resources.
Definition Joystick.cpp:12
virtual Pad * getJoystick(int joyindex)=0
Pad at a dense joyindex in [0, getJoystickCount()).
virtual Pad * addJoystick(int deviceindex)=0
Open a device by SDL device index; returns null on failure.
virtual bool isDown(int button) const =0
True if the raw button index is currently pressed.
virtual std::string getGamepadMappingString() const =0
Current SDL GameController mapping string for this device.
virtual bool isGamepadDown(const std::string &button) const =0
True if the named gamepad button is pressed.
virtual bool isVibrationSupported()=0
True when haptic/rumble output is available.
virtual float getAxis(int axisindex) const =0
Normalized axis value in [-1, 1] for a raw axis index.
virtual int getButtonCount() const =0
Number of raw joystick buttons.
virtual int getAxisCount() const =0
Number of raw joystick axes.
virtual float getGamepadAxis(const std::string &axis) const =0
Named gamepad axis value (SDL GameController axis string).
virtual std::string getHat(int hatindex) const =0
Hat direction string for a hat index (see class brief).
virtual std::string getGUID() const =0
Stable GUID string for mapping lookup.
virtual int getID() const =0
Engine-side stable pad id assigned at construction.
virtual int getHatCount() const =0
Number of hat (D-pad) controls.
virtual std::vector< float > getAxes() const =0
Snapshot of all raw axis values.
virtual std::string getName() const =0
Human-readable device name from the OS/driver.
virtual bool isGamepad() const =0
True when a GameController mapping is active for this pad.
virtual int getInstanceID() const =0
SDL instance id that stays unique while the device is open.
SDL joystick/gamepad module backend (device enumeration and mappings).
Definition Joystick.h:14
float clampAxis(float x)
Clamps a raw axis sample into [-1, 1].
Definition Joystick.cpp:14
uint32_t pad[2]