TGUI  1.4.0
Loading...
Searching...
No Matches
ThemeLoader.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_THEME_LOADER_HPP
26#define TGUI_THEME_LOADER_HPP
27
28#include <TGUI/String.hpp>
29#include <TGUI/Loading/DataIO.hpp>
30
31#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <memory>
33 #include <string>
34 #include <vector>
35 #include <map>
36 #include <set>
37#endif
38
40
41TGUI_MODULE_EXPORT namespace tgui
42{
46 class TGUI_API BaseThemeLoader
47 {
48 public:
49
53 virtual ~BaseThemeLoader() = default;
54
60 virtual void preload(const String& primary);
61
71 TGUI_NODISCARD virtual std::map<String, String> getGlobalProperties(const String& primary);
72
83 TGUI_NODISCARD virtual const std::map<String, String>& load(const String& primary, const String& secondary) = 0;
84
95 TGUI_NODISCARD virtual bool canLoad(const String& primary, const String& secondary) = 0;
96
98 protected:
99
101 // Turns texture and font filenames into paths relative to the theme file
103 void injectThemePath(const std::unique_ptr<DataIO::Node>& node, const String& path) const;
104
106 // Resolves references to sections
108 void resolveReferences(std::map<String, std::reference_wrapper<const std::unique_ptr<DataIO::Node>>>& sections,
109 const std::map<String, String>& globalProperties,
110 const std::unique_ptr<DataIO::Node>& node) const;
111
113 };
114
123 class TGUI_API DefaultThemeLoader : public BaseThemeLoader
124 {
125 public:
126
134 void preload(const String& filename) override;
135
143 TGUI_NODISCARD std::map<String, String> getGlobalProperties(const String& filename) override;
144
156 TGUI_NODISCARD const std::map<String, String>& load(const String& filename, const String& section) override;
157
166 TGUI_NODISCARD bool canLoad(const String& filename, const String& section) override;
167
177 static void flushCache(const String& filename = "");
178
180 protected:
181
189 TGUI_NODISCARD virtual std::unique_ptr<DataIO::Node> readFile(const String& filename) const;
190
192 protected:
193 static std::map<String, std::map<String, std::map<String, String>>> m_propertiesCache;
194 static std::map<String, std::map<String, String>> m_globalPropertiesCache;
195 };
196
198}
199
201
202#endif // TGUI_THEME_LOADER_HPP
Base class for theme loader implementations.
Definition ThemeLoader.hpp:47
virtual TGUI_NODISCARD const std::map< String, String > & load(const String &primary, const String &secondary)=0
Loads the property-value pairs from the theme.
virtual void preload(const String &primary)
Optionally already do some work when only the primary parameter is known yet.
virtual TGUI_NODISCARD bool canLoad(const String &primary, const String &secondary)=0
Check if the requested property-value pairs are available.
virtual ~BaseThemeLoader()=default
Virtual destructor.
virtual TGUI_NODISCARD std::map< String, String > getGlobalProperties(const String &primary)
Loads the global property-value pairs from the theme.
Default implementation for theme loading.
Definition ThemeLoader.hpp:124
static void flushCache(const String &filename="")
Empties the caches and force files to be reloaded.
TGUI_NODISCARD const std::map< String, String > & load(const String &filename, const String &section) override
Loads the property-value pairs from the theme file.
TGUI_NODISCARD std::map< String, String > getGlobalProperties(const String &filename) override
Loads the global property-value pairs from the theme.
virtual TGUI_NODISCARD std::unique_ptr< DataIO::Node > readFile(const String &filename) const
Reads and return the contents of the entire file.
TGUI_NODISCARD bool canLoad(const String &filename, const String &section) override
Check if the requested property-value pairs are available.
void preload(const String &filename) override
Loads the theme file in cache.
Wrapper class to store strings.
Definition String.hpp:96
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38