From 46fcc4b43f6cd701830ffe85f29e5ed273b6859a Mon Sep 17 00:00:00 2001 From: Mikulas Florek Date: Sun, 16 Jun 2024 09:34:16 +0200 Subject: [PATCH] texture's path in sprite can be relative to sprite's path --- src/gui/sprite.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/gui/sprite.cpp b/src/gui/sprite.cpp index 5d90230f4b..cb277d0518 100644 --- a/src/gui/sprite.cpp +++ b/src/gui/sprite.cpp @@ -39,7 +39,13 @@ void Sprite::setTexture(const Path& path) if (path.isEmpty()) { m_texture = nullptr; } else { - m_texture = (Texture*)getResourceManager().getOwner().load(path); + if (path.c_str()[0] == '\\' || path.c_str()[0] == '/') { + m_texture = (Texture*)getResourceManager().getOwner().load(path); + } else { + StringView dir = Path::getDir(getPath()); + StaticString tmp(dir, "/", path); + m_texture = (Texture*)getResourceManager().getOwner().load(Path(tmp)); + } } } @@ -52,7 +58,18 @@ void Sprite::serialize(OutputMemoryStream& out) out << "bottom(" << bottom << ")\n"; out << "left(" << left << ")\n"; out << "right(" << right << ")\n"; - out << "texture \"" << (m_texture ? m_texture->getPath() : Path()) << "\""; + if (m_texture) { + StringView dir = Path::getDir(getPath()); + if (startsWith(m_texture->getPath(), dir)) { + StringView path = m_texture->getPath(); + path.removePrefix(dir.size()); + out << "texture \"" << path << "\""; + } else { + out << "texture \"/" << m_texture->getPath() << "\""; + } + } else { + out << "texture \"\""; + } } namespace LuaSpriteAPI {