Merge branch 'v0.7-dev' of https://github.com/Creepsky/TGUI into v0.7-dev

0.8
Bruno Van de Velde 2015-04-02 23:09:53 +02:00
commit d4ff94fd04
4 changed files with 165 additions and 49 deletions

View File

@ -33,17 +33,30 @@
#include <list>
#include <map>
#include <memory>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui
{
class Texture;
class ImageLoader;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API TextureManager
{
public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Sets the imageloader.
///
/// @param imageLoader An instance of the new imageloader.
///
/// If the new imageloader is a nullptr, nothing is done.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void setImageLoader(std::shared_ptr<ImageLoader> imageLoader);
public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -113,9 +126,39 @@ namespace tgui
};
std::map<std::string, ImageMapData> m_imageMap;
static std::shared_ptr<ImageLoader> m_imageLoader;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Imageloader
///
/// A virtual class for loading images on different ways.
/// It is mainly used by the texturemanager.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API ImageLoader
{
public:
ImageLoader() = default;
virtual ~ImageLoader() = default;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Loads an image.
///
/// @param filename The name of the filename to load.
/// @param image The image instance, which will be loaded.
///
/// A virtual function to load an desired image.
/// How and where the image is loaded fully depends on implementation.
/// The standard-implementations simply loads the file from harddrive.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool load(const std::string& filename, sf::Image& image);
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -35,6 +35,8 @@
namespace tgui
{
class ThemeFileLoader;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// Reads the theme files that are used to load widgets.
@ -42,7 +44,6 @@ namespace tgui
class TGUI_API ThemeFileParser
{
public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Cpen and read the properties inside a section of the given file.
// An exception will be thrown when the file could not be opened or when the section could not be read.
@ -72,6 +73,15 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void flushCache();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Sets the fileloader.
///
/// @param imageLoader An instance of the new fileloader.
///
/// If the new fileloader is a nullptr, nothing is done.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void setFileLoader(std::shared_ptr<ThemeFileLoader> fileloader);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:
@ -95,11 +105,40 @@ namespace tgui
std::string m_section;
std::vector<std::pair<std::string, std::string>> m_properties;
static std::shared_ptr<ThemeFileLoader> m_themeFileLoader;
static std::map<std::string, std::map<std::string, std::vector<std::pair<std::string, std::string>>>> m_cache;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Theme-File-Loader
///
/// A virtual class for loading theme-files on different ways.
/// It is mainly used by the ThemeFileParser.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API ThemeFileLoader
{
public:
ThemeFileLoader() = default;
virtual ~ThemeFileLoader() = default;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Loads a theme-file.
///
/// @param filename The name of the filename to load.
/// @param content The std::stringstream instance, in which the content of the file will be loaded.
///
/// A virtual function to load an desired theme-file.
/// How and where the theme-file is loaded fully depends on implementation.
/// The standard-implementations simply loads the file from harddrive.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void load(const std::string& filename, std::stringstream& content);
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -31,6 +31,10 @@
namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<ImageLoader> TextureManager::m_imageLoader = std::make_shared<ImageLoader>();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextureManager::getTexture(Texture& texture, const std::string& filename, const sf::IntRect& partRect, const sf::IntRect& middleRect, bool repeated)
@ -70,7 +74,7 @@ namespace tgui
data.image = &imageIt->second.image;
// Load the image
if (data.image->loadFromFile(filename))
if (m_imageLoader->load(filename, *data.image))
{
// Create a texture from the image
bool success;
@ -160,6 +164,20 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextureManager::setImageLoader(std::shared_ptr<ImageLoader> imageLoader)
{
if (imageLoader != nullptr)
m_imageLoader = imageLoader;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ImageLoader::load(const std::string& filename, sf::Image& image)
{
return image.loadFromFile(filename);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -50,6 +50,7 @@
namespace tgui
{
std::map<std::string, std::map<std::string, std::vector<std::pair<std::string, std::string>>>> ThemeFileParser::m_cache;
std::shared_ptr<ThemeFileLoader> ThemeFileParser::m_themeFileLoader = std::make_shared<ThemeFileLoader>();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -67,7 +68,7 @@ namespace tgui
return false;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -81,53 +82,9 @@ namespace tgui
// The file may be cached
if (m_cache.find(m_filename) == m_cache.end())
{
#ifdef SFML_SYSTEM_ANDROID
// If the file does not start with a slash then load it from the assets
if (!filename.empty() && (filename[0] != '/'))
{
/// TODO: Workaround until SFML makes native activity publically accessible
/// When this happens, extra SFML folder in include can be removed as well.
ANativeActivity* activity = sf::priv::getActivity(NULL)->activity;
m_themeFileLoader->load(filename, fileContents);
JNIEnv* env = 0;
activity->vm->AttachCurrentThread(&env, NULL);
jclass clazz = env->GetObjectClass(activity->clazz);
jmethodID methodID = env->GetMethodID(clazz, "getAssets", "()Landroid/content/res/AssetManager;");
jobject assetManagerObject = env->CallObjectMethod(activity->clazz, methodID);
jobject globalAssetManagerRef = env->NewGlobalRef(assetManagerObject);
AAssetManager* assetManager = AAssetManager_fromJava(env, globalAssetManagerRef);
assert(assetManager);
AAsset* asset = AAssetManager_open(assetManager, filename.c_str(), AASSET_MODE_UNKNOWN);
if (!asset)
throw Exception{"Failed to open theme file '" + filename + "' from assets."};
off_t assetLength = AAsset_getLength(asset);
char* buffer = new char[assetLength + 1];
AAsset_read(asset, buffer, assetLength);
buffer[assetLength] = 0;
fileContents << buffer;
AAsset_close(asset);
delete[] buffer;
activity->vm->DetachCurrentThread();
}
else
#endif
{
std::ifstream file{filename};
if (!file.is_open())
throw Exception{"Failed to open theme file '" + filename + "'."};
fileContents << file.rdbuf();
file.close();
}
std::string sectionName;
std::string sectionName;
unsigned int lineNumber = 0;
// Stop reading when we reach the end of the file
@ -236,7 +193,66 @@ namespace tgui
m_cache.clear();
}
void ThemeFileParser::setFileLoader(std::shared_ptr<ThemeFileLoader> fileloader)
{
if (fileloader != nullptr)
m_themeFileLoader = fileloader;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ThemeFileLoader::load(const std::string& filename, std::stringstream& content)
{
content.clear();
#ifdef SFML_SYSTEM_ANDROID
// If the file does not start with a slash then load it from the assets
if (!filename.empty() && (filename[0] != '/'))
{
/// TODO: Workaround until SFML makes native activity publically accessible
/// When this happens, extra SFML folder in include can be removed as well.
ANativeActivity* activity = sf::priv::getActivity(NULL)->activity;
JNIEnv* env = 0;
activity->vm->AttachCurrentThread(&env, NULL);
jclass clazz = env->GetObjectClass(activity->clazz);
jmethodID methodID = env->GetMethodID(clazz, "getAssets", "()Landroid/content/res/AssetManager;");
jobject assetManagerObject = env->CallObjectMethod(activity->clazz, methodID);
jobject globalAssetManagerRef = env->NewGlobalRef(assetManagerObject);
AAssetManager* assetManager = AAssetManager_fromJava(env, globalAssetManagerRef);
assert(assetManager);
AAsset* asset = AAssetManager_open(assetManager, filename.c_str(), AASSET_MODE_UNKNOWN);
if (!asset)
throw Exception{ "Failed to open theme file '" + filename + "' from assets." };
off_t assetLength = AAsset_getLength(asset);
char* buffer = new char[assetLength + 1];
AAsset_read(asset, buffer, assetLength);
buffer[assetLength] = 0;
content << buffer;
AAsset_close(asset);
delete[] buffer;
activity->vm->DetachCurrentThread();
}
else
#endif
{
std::ifstream file{ filename };
if (!file.is_open())
throw Exception{ "Failed to open theme file '" + filename + "'." };
content << file.rdbuf();
file.close();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////