Made some tweaks in the internal code related to resource paths

0.8
Bruno Van de Velde 2017-09-08 14:53:07 +02:00
parent 51d30906af
commit ef1072e8bc
3 changed files with 53 additions and 31 deletions

View File

@ -90,14 +90,16 @@ namespace tgui
if (value == "null" || value == "nullptr")
return Font{};
auto font = std::make_shared<sf::Font>();
sf::String filename = Deserializer::deserialize(ObjectConverter::Type::String, value).getString();
if (filename.isEmpty())
return Font{};
// Load the font but insert the resource path into the filename unless the filename is an absolute path
sf::String filename = Deserializer::deserialize(ObjectConverter::Type::String, value).getString();
auto font = std::make_shared<sf::Font>();
#ifdef SFML_SYSTEM_WINDOWS
if ((filename.getSize() > 1) && (filename[0] != '/') && (filename[0] != '\\') && (filename[1] != ':'))
if ((filename[0] != '/') && (filename[0] != '\\') && ((filename.getSize() <= 1) || (filename[1] != ':')))
#else
if ((filename.getSize() > 0) && (filename[0] != '/'))
if (filename[0] != '/')
#endif
font->loadFromFile(getResourcePath() + filename);
else
@ -265,31 +267,26 @@ namespace tgui
ObjectConverter deserializeTexture(const std::string& value)
{
std::string::const_iterator c = value.begin();
// Remove all whitespaces and return an empty texture when the string does not contain any text
if (!removeWhitespace(value, c))
if (value.empty() || (toLower(value) == "none"))
return Texture{};
if (toLower(value) == "none")
return Texture{};
// There has to be a quote if the value contains more than just the filename
if (*c == '"')
++c;
else
// If there are no quotes then the value just contains a filename
if (value[0] != '"')
{
// Load the texture but insert the resource path into the filename unless the filename is an absolute path
#ifdef SFML_SYSTEM_WINDOWS
if ((value.size() > 1) && (value[0] != '/') && (value[0] != '\\') && (value[1] != ':'))
if ((value[0] != '/') && (value[0] != '\\') && ((value.size() <= 1) || (value[1] != ':')))
#else
if ((value.size() > 0) && (value[0] != '/'))
if (value[0] != '/')
#endif
return Texture{getResourcePath() + value};
else
return Texture{value};
}
std::string::const_iterator c = value.begin();
c++; // Skip the opening quote
std::string filename;
char prev = '\0';
@ -367,12 +364,14 @@ namespace tgui
std::advance(c, closeBracketPos - (c - value.begin()) + 1);
}
if (filename.empty())
return Texture{};
// Load the texture but insert the resource path into the filename unless the filename is an absolute path
#ifdef SFML_SYSTEM_WINDOWS
if ((filename.size() > 1) && (filename[0] != '/') && (filename[0] != '\\') && (filename[1] != ':'))
if ((filename[0] != '/') && (filename[0] != '\\') && ((filename.size() <= 1) || (filename[1] != ':')))
#else
if ((filename.size() > 0) && (filename[0] != '/'))
if (filename[0] != '/')
#endif
return Texture{getResourcePath() + filename, partRect, middleRect, smooth};
else

View File

@ -65,16 +65,30 @@ namespace tgui
{
if ((pair.first.size() >= 7) && (toLower(pair.first.substr(0, 7)) == "texture"))
{
auto quotePos = pair.second->value.find('"');
if (quotePos != std::string::npos)
if (pair.second->value.isEmpty())
continue;
// Load the texture but insert the resource path into the filename unless the filename is an absolute path
if (pair.second->value[0] != '"')
{
// Only inject the path of the theme file when the texture filename is not an absolute path
#ifdef SFML_SYSTEM_WINDOWS
if ((pair.second->value.getSize() > quotePos + 2) && (pair.second->value[quotePos+1] != '/') && (pair.second->value[quotePos+1] != '\\') && (pair.second->value[quotePos+2] != ':'))
if ((pair.second->value[0] != '/') && (pair.second->value[0] != '\\') && ((pair.second->value.getSize() <= 1) || (pair.second->value[1] != ':')))
#else
if ((pair.second->value.getSize() > quotePos + 1) && (pair.second->value[quotePos+1] != '/'))
if (pair.second->value[0] != '/')
#endif
pair.second->value = pair.second->value.substring(0, quotePos+1) + path + pair.second->value.substring(quotePos+1);
pair.second->value = path + pair.second->value;
}
else // The filename is between quotes
{
if (pair.second->value.getSize() <= 1)
continue;
#ifdef SFML_SYSTEM_WINDOWS
if ((pair.second->value[1] != '/') && (pair.second->value[1] != '\\') && ((pair.second->value.getSize() <= 2) || (pair.second->value[2] != ':')))
#else
if (pair.second->value[1] != '/')
#endif
pair.second->value = '"' + path + pair.second->value.substring(1);
}
}
}
@ -171,12 +185,12 @@ namespace tgui
{
char* buffer;
#ifdef SFML_SYSTEM_WINDOWS
if ((resourcePath.size() > 1) && (resourcePath[0] != '/') && (resourcePath[0] != '\\') && (resourcePath[1] != ':'))
if ((resourcePath[0] != '/') && (resourcePath[0] != '\\') && ((resourcePath.size() <= 1) || (resourcePath[1] != ':')))
resourcePath = getResourcePath() + resourcePath;
buffer = _fullpath(nullptr, resourcePath.c_str(), 512);
#else
if ((resourcePath.size() > 0) && (resourcePath[0] != '/'))
if (resourcePath[0] != '/')
resourcePath = getResourcePath() + resourcePath;
buffer = realpath(resourcePath.c_str(), nullptr);
@ -247,11 +261,14 @@ namespace tgui
void DefaultThemeLoader::readFile(const std::string& filename, std::stringstream& contents) const
{
if (filename.empty())
return;
std::string fullFilename;
#ifdef SFML_SYSTEM_WINDOWS
if ((filename.size() > 1) && (filename[0] != '/') && (filename[0] != '\\') && (filename[1] != ':'))
if ((filename[0] != '/') && (filename[0] != '\\') && ((filename.size() <= 1) || (filename[1] != ':')))
#else
if ((filename.size() > 0) && (filename[0] != '/'))
if (filename[0] != '/')
#endif
fullFilename = getResourcePath() + filename;
else

View File

@ -135,6 +135,12 @@ namespace tgui
void Texture::load(const sf::String& id, const sf::IntRect& partRect, const sf::IntRect& middleRect, bool smooth)
{
if (id.isEmpty())
{
*this = Texture{};
return;
}
if (getData() && (m_destructCallback != nullptr))
m_destructCallback(getData());
@ -142,9 +148,9 @@ namespace tgui
std::shared_ptr<TextureData> data;
#ifdef SFML_SYSTEM_WINDOWS
if ((id.getSize() > 1) && (id[0] != '/') && (id[0] != '\\') && (id[1] != ':'))
if ((id[0] != '/') && (id[0] != '\\') && ((id.getSize() <= 1) || (id[1] != ':')))
#else
if ((id.getSize() > 0) && (id[0] != '/'))
if (id[0] != '/')
#endif
data = m_textureLoader(*this, getResourcePath() + id, partRect);
else