2013-03-15 09:17:30 +00:00
|
|
|
#include "fallback.hpp"
|
2015-04-14 13:55:56 +00:00
|
|
|
|
2023-03-18 09:30:48 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2017-06-11 07:23:19 +00:00
|
|
|
|
2019-09-30 15:51:11 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2023-03-18 09:30:48 +00:00
|
|
|
#include <components/misc/strings/algorithm.hpp>
|
|
|
|
#include <components/misc/strings/conversion.hpp>
|
2018-12-29 12:18:26 +00:00
|
|
|
|
2023-04-27 09:01:21 +00:00
|
|
|
#include "validate.hpp"
|
|
|
|
|
2016-01-06 11:46:06 +00:00
|
|
|
namespace Fallback
|
2013-03-15 09:17:30 +00:00
|
|
|
{
|
2023-04-27 09:01:21 +00:00
|
|
|
std::map<std::string, int, std::less<>> Map::mIntFallbackMap;
|
|
|
|
std::map<std::string, float, std::less<>> Map::mFloatFallbackMap;
|
|
|
|
std::map<std::string, std::string, std::less<>> Map::mNonNumericFallbackMap;
|
2013-03-15 09:26:04 +00:00
|
|
|
|
2019-01-22 06:08:48 +00:00
|
|
|
void Map::init(const std::map<std::string, std::string>& fallback)
|
|
|
|
{
|
2023-04-27 09:01:21 +00:00
|
|
|
for (const auto& [key, value] : fallback)
|
|
|
|
{
|
|
|
|
if (isAllowedIntFallbackKey(key))
|
|
|
|
mIntFallbackMap.emplace(key, Misc::StringUtils::toNumeric<int>(value, 0));
|
|
|
|
else if (isAllowedFloatFallbackKey(key))
|
|
|
|
mFloatFallbackMap.emplace(key, Misc::StringUtils::toNumeric<float>(value, 0.0f));
|
|
|
|
else if (isAllowedNonNumericFallbackKey(key))
|
|
|
|
mNonNumericFallbackMap.emplace(key, value);
|
|
|
|
else if (!isAllowedUnusedFallbackKey(key))
|
|
|
|
Log(Debug::Error) << "Ignoring unknown fallback: " << key;
|
|
|
|
}
|
2019-01-22 06:08:48 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 15:20:49 +00:00
|
|
|
std::string_view Map::getString(std::string_view fall)
|
2013-03-15 09:17:30 +00:00
|
|
|
{
|
2023-04-27 09:01:21 +00:00
|
|
|
const auto it = mNonNumericFallbackMap.find(fall);
|
|
|
|
if (it == mNonNumericFallbackMap.end())
|
2013-03-15 09:17:30 +00:00
|
|
|
{
|
2023-04-27 09:01:21 +00:00
|
|
|
if (!isAllowedNonNumericFallbackKey(fall))
|
|
|
|
throw std::logic_error("Requested invalid string fallback: " + std::string(fall));
|
2022-08-28 15:20:49 +00:00
|
|
|
return {};
|
2013-03-15 09:17:30 +00:00
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
2017-06-11 07:32:30 +00:00
|
|
|
|
2022-08-28 15:20:49 +00:00
|
|
|
float Map::getFloat(std::string_view fall)
|
2013-03-15 09:17:30 +00:00
|
|
|
{
|
2023-04-27 09:01:21 +00:00
|
|
|
const auto it = mFloatFallbackMap.find(fall);
|
|
|
|
if (it == mFloatFallbackMap.end())
|
|
|
|
{
|
|
|
|
if (!isAllowedFloatFallbackKey(fall))
|
|
|
|
throw std::logic_error("Requested invalid float fallback: " + std::string(fall));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return it->second;
|
2013-03-15 09:17:30 +00:00
|
|
|
}
|
2017-06-11 07:32:30 +00:00
|
|
|
|
2022-08-28 15:20:49 +00:00
|
|
|
int Map::getInt(std::string_view fall)
|
2015-02-09 18:28:29 +00:00
|
|
|
{
|
2023-04-27 09:01:21 +00:00
|
|
|
const auto it = mIntFallbackMap.find(fall);
|
|
|
|
if (it == mIntFallbackMap.end())
|
|
|
|
{
|
|
|
|
if (!isAllowedIntFallbackKey(fall))
|
|
|
|
throw std::logic_error("Requested invalid int fallback: " + std::string(fall));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return it->second;
|
2015-02-09 18:28:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 15:20:49 +00:00
|
|
|
bool Map::getBool(std::string_view fall)
|
2013-03-15 09:17:30 +00:00
|
|
|
{
|
2023-04-27 09:01:21 +00:00
|
|
|
return getInt(fall) != 0;
|
2013-03-15 09:17:30 +00:00
|
|
|
}
|
2017-06-11 07:32:30 +00:00
|
|
|
|
2022-08-28 15:20:49 +00:00
|
|
|
osg::Vec4f Map::getColour(std::string_view fall)
|
2013-03-15 09:17:30 +00:00
|
|
|
{
|
2023-03-18 09:30:48 +00:00
|
|
|
const std::string_view sum = getString(fall);
|
|
|
|
|
2018-12-28 20:49:06 +00:00
|
|
|
if (!sum.empty())
|
2013-03-15 09:17:30 +00:00
|
|
|
{
|
2023-03-18 09:30:48 +00:00
|
|
|
std::vector<std::string> ret;
|
|
|
|
Misc::StringUtils::split(sum, ret, ",");
|
|
|
|
|
|
|
|
if (ret.size() == 3)
|
2018-12-28 20:49:06 +00:00
|
|
|
{
|
2023-03-18 09:30:48 +00:00
|
|
|
const auto r = Misc::StringUtils::toNumeric<float>(ret[0]);
|
|
|
|
const auto g = Misc::StringUtils::toNumeric<float>(ret[1]);
|
|
|
|
const auto b = Misc::StringUtils::toNumeric<float>(ret[2]);
|
|
|
|
|
|
|
|
if (r.has_value() && g.has_value() && b.has_value())
|
2018-12-28 20:49:06 +00:00
|
|
|
{
|
2023-03-18 09:30:48 +00:00
|
|
|
return osg::Vec4f(*r / 255.0f, *g / 255.0f, *b / 255.0f, 1.0f);
|
2018-12-28 20:49:06 +00:00
|
|
|
}
|
2013-03-15 09:17:30 +00:00
|
|
|
}
|
2023-03-18 09:30:48 +00:00
|
|
|
|
|
|
|
Log(Debug::Error) << "Error: '" << fall << "' setting value (" << sum
|
|
|
|
<< ") is not a valid color, using middle gray as a fallback";
|
2013-03-15 09:17:30 +00:00
|
|
|
}
|
2018-12-28 20:49:06 +00:00
|
|
|
|
|
|
|
return osg::Vec4f(0.5f, 0.5f, 0.5f, 1.f);
|
2013-03-15 09:17:30 +00:00
|
|
|
}
|
|
|
|
}
|