Browse Source

Add love.sensor.getName(sensorType)

Miku AuahDark 2 years ago
parent
commit
a4b94cd848

+ 2 - 0
src/modules/sensor/Sensor.h

@@ -71,6 +71,8 @@ public:
 	 **/
 	virtual std::vector<void *> getHandles() = 0;
 
+	virtual const char *getSensorName(SensorType type) = 0;
+
 	STRINGMAP_CLASS_DECLARE(SensorType);
 
 }; // Sensor

+ 13 - 0
src/modules/sensor/sdl/Sensor.cpp

@@ -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 - 0
src/modules/sensor/sdl/Sensor.h

@@ -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 - 0
src/modules/sensor/wrap_Sensor.cpp

@@ -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 }
 };