2013-03-15 09:17:30 +00:00
|
|
|
#include "fallback.hpp"
|
2015-04-14 13:55:56 +00:00
|
|
|
|
2019-09-30 15:51:11 +00:00
|
|
|
#include <sstream>
|
2017-06-11 07:23:19 +00:00
|
|
|
|
2019-09-30 15:51:11 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2018-12-29 12:18:26 +00:00
|
|
|
|
2016-01-06 11:46:06 +00:00
|
|
|
namespace Fallback
|
2013-03-15 09:17:30 +00:00
|
|
|
{
|
2022-08-28 15:20:49 +00:00
|
|
|
std::map<std::string, std::string, std::less<>> Map::mFallbackMap;
|
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)
|
|
|
|
{
|
2022-08-28 15:20:49 +00:00
|
|
|
for (const auto& entry : fallback)
|
|
|
|
mFallbackMap.insert(entry);
|
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
|
|
|
{
|
2022-08-28 15:20:49 +00:00
|
|
|
auto it = mFallbackMap.find(fall);
|
|
|
|
if (it == mFallbackMap.end())
|
2013-03-15 09:17:30 +00:00
|
|
|
{
|
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
|
|
|
{
|
2022-08-28 15:20:49 +00:00
|
|
|
std::string_view fallback = getString(fall);
|
2018-12-28 20:49:06 +00:00
|
|
|
if (!fallback.empty())
|
|
|
|
{
|
2022-08-28 15:20:49 +00:00
|
|
|
std::stringstream stream;
|
|
|
|
stream << fallback;
|
2019-09-30 15:51:11 +00:00
|
|
|
float number = 0.f;
|
|
|
|
stream >> number;
|
|
|
|
return number;
|
2018-12-28 20:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 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
|
|
|
int Map::getInt(std::string_view fall)
|
2015-02-09 18:28:29 +00:00
|
|
|
{
|
2022-08-28 15:20:49 +00:00
|
|
|
std::string_view fallback = getString(fall);
|
2018-12-28 20:49:06 +00:00
|
|
|
if (!fallback.empty())
|
|
|
|
{
|
2022-08-28 15:20:49 +00:00
|
|
|
std::stringstream stream;
|
|
|
|
stream << fallback;
|
2019-09-30 15:51:11 +00:00
|
|
|
int number = 0;
|
|
|
|
stream >> number;
|
|
|
|
return number;
|
2018-12-28 20:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
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
|
|
|
{
|
2022-08-28 15:20:49 +00:00
|
|
|
std::string_view fallback = getString(fall);
|
2018-12-31 11:47:00 +00:00
|
|
|
return !fallback.empty() && fallback != "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
|
|
|
{
|
2022-08-28 15:20:49 +00:00
|
|
|
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
|
|
|
{
|
2018-12-28 20:49:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string ret[3];
|
|
|
|
unsigned int j = 0;
|
|
|
|
for (unsigned int i = 0; i < sum.length(); ++i)
|
|
|
|
{
|
|
|
|
if (sum[i] == ',')
|
|
|
|
j++;
|
|
|
|
else if (sum[i] != ' ')
|
|
|
|
ret[j] += sum[i];
|
|
|
|
}
|
|
|
|
return osg::Vec4f(std::stoi(ret[0]) / 255.f, std::stoi(ret[1]) / 255.f, std::stoi(ret[2]) / 255.f, 1.f);
|
|
|
|
}
|
|
|
|
catch (const std::invalid_argument&)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2015-06-16 18:36:48 +00:00
|
|
|
|
2013-03-15 09:17:30 +00:00
|
|
|
}
|