Add love.sensor.getName(sensorType)

This commit is contained in:
Miku AuahDark
2022-12-05 15:28:44 +08:00
parent 8cfb2d54d1
commit a4b94cd848
4 changed files with 27 additions and 0 deletions
+2
View File
@@ -71,6 +71,8 @@ public:
**/
virtual std::vector<void *> getHandles() = 0;
virtual const char *getSensorName(SensorType type) = 0;
STRINGMAP_CLASS_DECLARE(SensorType);
}; // Sensor
+13
View File
@@ -124,6 +124,19 @@ std::vector<void*> Sensor::getHandles()
return nativeSensor;
}
const char *Sensor::getSensorName(SensorType type)
{
if (sensors[type] == nullptr)
{
const char *name = nullptr;
getConstant(type, name);
throw love::Exception("\"%s\" sensor is not enabled", name);
}
return SDL_SensorGetName(sensors[type]);
}
Sensor::SensorType Sensor::convert(SDL_SensorType type)
{
switch (type)
+1
View File
@@ -51,6 +51,7 @@ public:
void setEnabled(SensorType type, bool enable) override;
std::vector<float> getData(SensorType type) override;
std::vector<void*> getHandles() override;
const char *getSensorName(SensorType type) override;
static SensorType convert(SDL_SensorType type);
+11
View File
@@ -78,6 +78,16 @@ static int w_getData(lua_State *L)
return data.size();
}
static int w_getName(lua_State *L)
{
Sensor::SensorType type = luax_checksensortype(L, 1);
const char *name = nullptr;
luax_catchexcept(L, [&](){ name = instance()->getSensorName(type); });
lua_pushstring(L, name);
return 1;
}
static const luaL_Reg functions[] =
{
{ "isAvailable", w_isAvailable },
@@ -85,6 +95,7 @@ static const luaL_Reg functions[] =
{ "isEnabled", w_isEnabled },
{ "setEnabled", w_setEnabled },
{ "getData", w_getData },
{ "getName", w_getName },
{ nullptr, nullptr }
};