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