载入中...
搜索中...
未找到
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
HSQOBJECT cls
Definition ECS.cpp:21
int x
Definition Grass.cpp:135
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
const char * name
Definition RockMesh.cpp:21
virtual std::string getName() const =0
Joystick / gamepad manager. Tracks connected pads and gamecontroller mappings.
Definition Joystick.h:13
virtual int getJoystickCount() const =0
virtual std::string saveGamepadMappings()=0
virtual void removeJoystick(Pad *pad)=0
virtual Pad * getJoystickFromID(int instanceid)=0
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
virtual int getIndex(const Pad *pad)=0
virtual Pad * getJoystick(int joyindex)=0
virtual Pad * addJoystick(int deviceindex)=0
Open a device by SDL device index; returns null on failure.
virtual bool isDown(int button) const =0
virtual std::string getGamepadMappingString() const =0
virtual bool isGamepadDown(const std::string &button) const =0
virtual bool isVibrationSupported()=0
virtual float getAxis(int axisindex) const =0
virtual int getButtonCount() const =0
virtual int getAxisCount() const =0
virtual float getGamepadAxis(const std::string &axis) const =0
virtual std::string getHat(int hatindex) const =0
virtual std::string getGUID() const =0
virtual int getID() const =0
virtual int getHatCount() const =0
virtual std::vector< float > getAxes() const =0
virtual std::string getName() const =0
virtual bool isGamepad() const =0
virtual int getInstanceID() const =0
SDL 摇杆/手柄模块后端实现(设备枚举与映射)。
Definition Joystick.h:14
float clampAxis(float x)
Definition Joystick.cpp:14