mirror of
https://github.com/minetest/minetest.git
synced 2025-01-07 22:07:30 +01:00
Improve quicktune feature a bit
This commit is contained in:
parent
33b8307119
commit
612d4f9656
@ -13,6 +13,8 @@ std::string QuicktuneValue::getString()
|
||||
return "(none)";
|
||||
case QVT_FLOAT:
|
||||
return ftos(value_QVT_FLOAT.current);
|
||||
case QVT_INT:
|
||||
return itos(value_QVT_INT.current);
|
||||
}
|
||||
return "<invalid type>";
|
||||
}
|
||||
@ -22,14 +24,21 @@ void QuicktuneValue::relativeAdd(float amount)
|
||||
switch(type){
|
||||
case QVT_NONE:
|
||||
break;
|
||||
case QVT_FLOAT:
|
||||
value_QVT_FLOAT.current += amount * (value_QVT_FLOAT.max - value_QVT_FLOAT.min);
|
||||
if(value_QVT_FLOAT.current > value_QVT_FLOAT.max)
|
||||
value_QVT_FLOAT.current = value_QVT_FLOAT.max;
|
||||
if(value_QVT_FLOAT.current < value_QVT_FLOAT.min)
|
||||
value_QVT_FLOAT.current = value_QVT_FLOAT.min;
|
||||
case QVT_FLOAT: {
|
||||
float &v = value_QVT_FLOAT.current;
|
||||
v += amount * (value_QVT_FLOAT.max - value_QVT_FLOAT.min);
|
||||
v = core::clamp(v, value_QVT_FLOAT.min, value_QVT_FLOAT.max);
|
||||
break;
|
||||
}
|
||||
case QVT_INT: {
|
||||
int &v = value_QVT_INT.current;
|
||||
int diff = std::floor(amount * (value_QVT_INT.max - value_QVT_INT.min));
|
||||
if (!diff)
|
||||
diff = amount < 0 ? -1 : 1;
|
||||
v = core::clamp(v + diff, value_QVT_INT.min, value_QVT_INT.max);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static std::map<std::string, QuicktuneValue> g_values;
|
||||
@ -44,12 +53,9 @@ const std::vector<std::string> &getQuicktuneNames()
|
||||
QuicktuneValue getQuicktuneValue(const std::string &name)
|
||||
{
|
||||
MutexAutoLock lock(g_mutex);
|
||||
std::map<std::string, QuicktuneValue>::iterator i = g_values.find(name);
|
||||
if(i == g_values.end()){
|
||||
QuicktuneValue val;
|
||||
val.type = QVT_NONE;
|
||||
return val;
|
||||
}
|
||||
auto i = g_values.find(name);
|
||||
if (i == g_values.end())
|
||||
return QuicktuneValue();
|
||||
return i->second;
|
||||
}
|
||||
|
||||
@ -64,15 +70,15 @@ void updateQuicktuneValue(const std::string &name, QuicktuneValue &val)
|
||||
{
|
||||
MutexAutoLock lock(g_mutex);
|
||||
auto i = g_values.find(name);
|
||||
if(i == g_values.end()){
|
||||
if (i == g_values.end()) {
|
||||
g_values[name] = val;
|
||||
g_names.push_back(name);
|
||||
return;
|
||||
}
|
||||
QuicktuneValue &ref = i->second;
|
||||
if(ref.modified)
|
||||
if (ref.modified) {
|
||||
val = ref;
|
||||
else{
|
||||
} else {
|
||||
ref = val;
|
||||
ref.modified = false;
|
||||
}
|
||||
|
@ -37,19 +37,21 @@
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
enum QuicktuneValueType{
|
||||
enum QuicktuneValueType {
|
||||
QVT_NONE,
|
||||
QVT_FLOAT
|
||||
QVT_FLOAT,
|
||||
QVT_INT
|
||||
};
|
||||
struct QuicktuneValue
|
||||
{
|
||||
QuicktuneValueType type = QVT_NONE;
|
||||
union{
|
||||
struct{
|
||||
float current;
|
||||
float min;
|
||||
float max;
|
||||
union {
|
||||
struct {
|
||||
float current, min, max;
|
||||
} value_QVT_FLOAT;
|
||||
struct {
|
||||
int current, min, max;
|
||||
} value_QVT_INT;
|
||||
};
|
||||
bool modified = false;
|
||||
|
||||
@ -65,19 +67,15 @@ void setQuicktuneValue(const std::string &name, const QuicktuneValue &val);
|
||||
|
||||
void updateQuicktuneValue(const std::string &name, QuicktuneValue &val);
|
||||
|
||||
#ifndef NDEBUG
|
||||
#define QUICKTUNE(type_, var, min_, max_, name){\
|
||||
QuicktuneValue qv;\
|
||||
qv.type = type_;\
|
||||
qv.value_##type_.current = var;\
|
||||
qv.value_##type_.min = min_;\
|
||||
qv.value_##type_.max = max_;\
|
||||
updateQuicktuneValue(name, qv);\
|
||||
var = qv.value_##type_.current;\
|
||||
}
|
||||
#else // NDEBUG
|
||||
#define QUICKTUNE(type, var, min_, max_, name){}
|
||||
#endif
|
||||
#define QUICKTUNE(type_, var, min_, max_, name) do { \
|
||||
QuicktuneValue qv; \
|
||||
qv.type = type_; \
|
||||
qv.value_##type_.current = var; \
|
||||
qv.value_##type_.min = min_; \
|
||||
qv.value_##type_.max = max_; \
|
||||
updateQuicktuneValue(name, qv); \
|
||||
var = qv.value_##type_.current; \
|
||||
} while (0)
|
||||
|
||||
#define QUICKTUNE_AUTONAME(type_, var, min_, max_)\
|
||||
QUICKTUNE(type_, var, min_, max_, #var)
|
||||
|
Loading…
Reference in New Issue
Block a user