LuaState tovalue, pushvalue for dynamic::Value
This commit is contained in:
@@ -353,6 +353,10 @@ Value::~Value() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value Value::of(bool value) {
|
||||||
|
return Value(valtype::boolean, value);
|
||||||
|
}
|
||||||
|
|
||||||
Value Value::of(number_u value) {
|
Value Value::of(number_u value) {
|
||||||
if (std::holds_alternative<integer_t>(value)) {
|
if (std::holds_alternative<integer_t>(value)) {
|
||||||
return Value(valtype::integer, std::get<integer_t>(value));
|
return Value(valtype::integer, std::get<integer_t>(value));
|
||||||
@@ -360,3 +364,7 @@ Value Value::of(number_u value) {
|
|||||||
return Value(valtype::number, std::get<number_t>(value));
|
return Value(valtype::number, std::get<number_t>(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value Value::of(const std::string& value) {
|
||||||
|
return Value(valtype::string, value);
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,7 +33,9 @@ namespace dynamic {
|
|||||||
Value(valtype type, valvalue value);
|
Value(valtype type, valvalue value);
|
||||||
~Value();
|
~Value();
|
||||||
|
|
||||||
|
static Value of(bool value);
|
||||||
static Value of(number_u value);
|
static Value of(number_u value);
|
||||||
|
static Value of(const std::string& value);
|
||||||
};
|
};
|
||||||
|
|
||||||
class List {
|
class List {
|
||||||
|
|||||||
@@ -2,19 +2,13 @@
|
|||||||
|
|
||||||
#include "../util/stringutil.h"
|
#include "../util/stringutil.h"
|
||||||
|
|
||||||
template<class T>
|
std::string NumberSetting::toString() const {
|
||||||
std::string NumberSetting<T>::toString() const {
|
|
||||||
switch (getFormat()) {
|
switch (getFormat()) {
|
||||||
case setting_format::simple:
|
case setting_format::simple:
|
||||||
return util::to_string(value);
|
return util::to_string(value);
|
||||||
case setting_format::percent:
|
case setting_format::percent:
|
||||||
return std::to_string(static_cast<int64_t>(value * 100)) + "%";
|
return std::to_string(static_cast<integer_t>(value * 100)) + "%";
|
||||||
default:
|
default:
|
||||||
return "invalid format";
|
return "invalid format";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template class NumberSetting<float>;
|
|
||||||
template class NumberSetting<double>;
|
|
||||||
template class NumberSetting<int>;
|
|
||||||
template class NumberSetting<uint>;
|
|
||||||
|
|||||||
+16
-15
@@ -4,6 +4,8 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "../typedefs.h"
|
||||||
|
|
||||||
enum class setting_format {
|
enum class setting_format {
|
||||||
simple, percent
|
simple, percent
|
||||||
};
|
};
|
||||||
@@ -26,18 +28,17 @@ public:
|
|||||||
virtual std::string toString() const = 0;
|
virtual std::string toString() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
|
||||||
class NumberSetting : public Setting {
|
class NumberSetting : public Setting {
|
||||||
protected:
|
protected:
|
||||||
T initial;
|
number_t initial;
|
||||||
T value;
|
number_t value;
|
||||||
T min;
|
number_t min;
|
||||||
T max;
|
number_t max;
|
||||||
public:
|
public:
|
||||||
NumberSetting(
|
NumberSetting(
|
||||||
T value,
|
number_t value,
|
||||||
T min=std::numeric_limits<T>::min(),
|
number_t min=std::numeric_limits<number_t>::min(),
|
||||||
T max=std::numeric_limits<T>::max(),
|
number_t max=std::numeric_limits<number_t>::max(),
|
||||||
setting_format format=setting_format::simple
|
setting_format format=setting_format::simple
|
||||||
) : Setting(format),
|
) : Setting(format),
|
||||||
initial(value),
|
initial(value),
|
||||||
@@ -46,27 +47,27 @@ public:
|
|||||||
max(max)
|
max(max)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
T& operator*() {
|
number_t& operator*() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
T get() const {
|
number_t get() const {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set(T value) {
|
void set(number_t value) {
|
||||||
this->value = value;
|
this->value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
T getMin() const {
|
number_t getMin() const {
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
T getMax() const {
|
number_t getMax() const {
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
T getT() const {
|
number_t getT() const {
|
||||||
return (value - min) / (max - min);
|
return (value - min) / (max - min);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +77,7 @@ public:
|
|||||||
|
|
||||||
virtual std::string toString() const override;
|
virtual std::string toString() const override;
|
||||||
|
|
||||||
static inline NumberSetting createPercent(T def) {
|
static inline NumberSetting createPercent(number_t def) {
|
||||||
return NumberSetting(def, 0.0, 1.0, setting_format::percent);
|
return NumberSetting(def, 0.0, 1.0, setting_format::percent);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -19,6 +19,41 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
|
|||||||
map.emplace("camera.sensitivity", &settings.camera.sensitivity);
|
map.emplace("camera.sensitivity", &settings.camera.sensitivity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dynamic::Value SettingsHandler::getValue(const std::string& name) const {
|
||||||
|
auto found = map.find(name);
|
||||||
|
if (found == map.end()) {
|
||||||
|
throw std::runtime_error("setting '"+name+"' does not exist");
|
||||||
|
}
|
||||||
|
auto setting = found->second;
|
||||||
|
if (auto number = dynamic_cast<NumberSetting*>(setting)) {
|
||||||
|
return dynamic::Value::of((number_t)number->get());
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("type is not implemented for '"+name+"'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SettingsHandler::setValue(const std::string& name, dynamic::Value value) {
|
||||||
|
auto found = map.find(name);
|
||||||
|
if (found == map.end()) {
|
||||||
|
throw std::runtime_error("setting '"+name+"' does not exist");
|
||||||
|
}
|
||||||
|
auto setting = found->second;
|
||||||
|
if (auto number = dynamic_cast<NumberSetting*>(setting)) {
|
||||||
|
switch (value.type) {
|
||||||
|
case dynamic::valtype::integer:
|
||||||
|
number->set(std::get<integer_t>(value.value));
|
||||||
|
break;
|
||||||
|
case dynamic::valtype::number:
|
||||||
|
number->set(std::get<number_t>(value.value));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw std::runtime_error("type error, numeric value expected");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("type is not implement - setting '"+name+"'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
toml::Wrapper* create_wrapper(EngineSettings& settings) {
|
toml::Wrapper* create_wrapper(EngineSettings& settings) {
|
||||||
auto wrapper = std::make_unique<toml::Wrapper>();
|
auto wrapper = std::make_unique<toml::Wrapper>();
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ using namespace gui;
|
|||||||
static void create_volume_trackbar(
|
static void create_volume_trackbar(
|
||||||
std::shared_ptr<Panel> panel,
|
std::shared_ptr<Panel> panel,
|
||||||
const std::wstring& name,
|
const std::wstring& name,
|
||||||
NumberSetting<float>* field
|
NumberSetting* field
|
||||||
) {
|
) {
|
||||||
panel->add(menus::create_label([=]() {
|
panel->add(menus::create_label([=]() {
|
||||||
return langs::get(name, L"settings")+L": " +
|
return langs::get(name, L"settings")+L": " +
|
||||||
|
|||||||
@@ -205,6 +205,32 @@ int lua::LuaState::pushvalue(int idx) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lua::LuaState::pushvalue(const dynamic::Value& value) {
|
||||||
|
using dynamic::valtype;
|
||||||
|
switch (value.type) {
|
||||||
|
case valtype::boolean:
|
||||||
|
pushboolean(std::get<bool>(value.value));
|
||||||
|
break;
|
||||||
|
case valtype::integer:
|
||||||
|
pushinteger(std::get<integer_t>(value.value));
|
||||||
|
break;
|
||||||
|
case valtype::number:
|
||||||
|
pushnumber(std::get<number_t>(value.value));
|
||||||
|
break;
|
||||||
|
case valtype::string:
|
||||||
|
pushstring(std::get<std::string>(value.value).c_str());
|
||||||
|
break;
|
||||||
|
case valtype::none:
|
||||||
|
pushnil();
|
||||||
|
break;
|
||||||
|
case valtype::list:
|
||||||
|
throw std::runtime_error("type 'list' is not implemented");
|
||||||
|
case valtype::map:
|
||||||
|
throw std::runtime_error("type 'map' is not implemented");
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int lua::LuaState::pushglobals() {
|
int lua::LuaState::pushglobals() {
|
||||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -248,6 +274,31 @@ const char* lua::LuaState::tostring(int idx) {
|
|||||||
return lua_tostring(L, idx);
|
return lua_tostring(L, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dynamic::Value lua::LuaState::tovalue(int idx) {
|
||||||
|
using dynamic::valtype;
|
||||||
|
auto type = lua_type(L, idx);
|
||||||
|
switch (type) {
|
||||||
|
case LUA_TNIL:
|
||||||
|
case LUA_TNONE:
|
||||||
|
return dynamic::Value(valtype::none, 0);
|
||||||
|
case LUA_TBOOLEAN:
|
||||||
|
return dynamic::Value::of(lua_toboolean(L, idx) == 1);
|
||||||
|
case LUA_TNUMBER: {
|
||||||
|
auto number = lua_tonumber(L, idx);
|
||||||
|
auto integer = lua_tointeger(L, idx);
|
||||||
|
if (number == (lua_Number)integer) {
|
||||||
|
return dynamic::Value::of(integer);
|
||||||
|
} else {
|
||||||
|
return dynamic::Value::of(number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case LUA_TSTRING:
|
||||||
|
return dynamic::Value::of(lua_tostring(L, idx));
|
||||||
|
default:
|
||||||
|
throw std::runtime_error("lua type "+std::to_string(type)+" is not supported");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool lua::LuaState::isstring(int idx) {
|
bool lua::LuaState::isstring(int idx) {
|
||||||
return lua_isstring(L, idx);
|
return lua_isstring(L, idx);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "../../../data/dynamic.h"
|
||||||
|
|
||||||
#ifndef LUAJIT_VERSION
|
#ifndef LUAJIT_VERSION
|
||||||
#error LuaJIT required
|
#error LuaJIT required
|
||||||
#endif
|
#endif
|
||||||
@@ -36,6 +38,7 @@ namespace lua {
|
|||||||
int pushstring(const std::string& str);
|
int pushstring(const std::string& str);
|
||||||
int pushenv(int env);
|
int pushenv(int env);
|
||||||
int pushvalue(int idx);
|
int pushvalue(int idx);
|
||||||
|
int pushvalue(const dynamic::Value& value);
|
||||||
int pushnil();
|
int pushnil();
|
||||||
int pushglobals();
|
int pushglobals();
|
||||||
void pop(int n=1);
|
void pop(int n=1);
|
||||||
@@ -44,6 +47,7 @@ namespace lua {
|
|||||||
bool toboolean(int idx);
|
bool toboolean(int idx);
|
||||||
luaint tointeger(int idx);
|
luaint tointeger(int idx);
|
||||||
luanumber tonumber(int idx);
|
luanumber tonumber(int idx);
|
||||||
|
dynamic::Value tovalue(int idx);
|
||||||
const char* tostring(int idx);
|
const char* tostring(int idx);
|
||||||
bool isstring(int idx);
|
bool isstring(int idx);
|
||||||
bool isfunction(int idx);
|
bool isfunction(int idx);
|
||||||
|
|||||||
+6
-6
@@ -12,11 +12,11 @@ struct AudioSettings {
|
|||||||
/// @brief try to initialize AL backend
|
/// @brief try to initialize AL backend
|
||||||
bool enabled = true;
|
bool enabled = true;
|
||||||
|
|
||||||
NumberSetting<float> volumeMaster {1.0f, 0.0f, 1.0f, setting_format::percent};
|
NumberSetting volumeMaster {1.0f, 0.0f, 1.0f, setting_format::percent};
|
||||||
NumberSetting<float> volumeRegular {1.0f, 0.0f, 1.0f, setting_format::percent};
|
NumberSetting volumeRegular {1.0f, 0.0f, 1.0f, setting_format::percent};
|
||||||
NumberSetting<float> volumeUI {1.0f, 0.0f, 1.0f, setting_format::percent};
|
NumberSetting volumeUI {1.0f, 0.0f, 1.0f, setting_format::percent};
|
||||||
NumberSetting<float> volumeAmbient {1.0f, 0.0f, 1.0f, setting_format::percent};
|
NumberSetting volumeAmbient {1.0f, 0.0f, 1.0f, setting_format::percent};
|
||||||
NumberSetting<float> volumeMusic {1.0f, 0.0f, 1.0f, setting_format::percent};
|
NumberSetting volumeMusic {1.0f, 0.0f, 1.0f, setting_format::percent};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DisplaySettings {
|
struct DisplaySettings {
|
||||||
@@ -53,7 +53,7 @@ struct CameraSettings {
|
|||||||
/// @brief Camera field of view
|
/// @brief Camera field of view
|
||||||
float fov = 90.0f;
|
float fov = 90.0f;
|
||||||
/// @brief Camera sensitivity
|
/// @brief Camera sensitivity
|
||||||
NumberSetting<float> sensitivity {2.0f, 0.1f, 10.0f};
|
NumberSetting sensitivity {2.0f, 0.1f, 10.0f};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GraphicsSettings {
|
struct GraphicsSettings {
|
||||||
|
|||||||
Reference in New Issue
Block a user