mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Add love.sensor.getName(sensorType)
This commit is contained in:
@@ -71,6 +71,8 @@ public:
|
||||
**/
|
||||
virtual std::vector<void *> getHandles() = 0;
|
||||
|
||||
virtual const char *getSensorName(SensorType type) = 0;
|
||||
|
||||
STRINGMAP_CLASS_DECLARE(SensorType);
|
||||
|
||||
}; // Sensor
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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 }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user