blob: 0eb32674d2b25ca31ff525eab1f84a88a4f1eb88 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// resources.hpp - Base classes for resource management of GUI
2
3#ifndef _RESOURCE_HEADER
4#define _RESOURCE_HEADER
5
thatb2e8f672015-03-05 20:25:39 +01006#include <string>
7#include <vector>
8#include <map>
9
10struct ZipArchive;
Dees Troyb7ae0982013-09-10 20:47:35 +000011
thatf37aec22015-02-01 13:38:35 +010012extern "C" {
13#include "../minuitwrp/minui.h"
14}
15
Dees_Troy51a0e822012-09-05 15:24:24 -040016// Base Objects
17class Resource
18{
19public:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020020 Resource(xml_node<>* node, ZipArchive* pZip);
21 virtual ~Resource() {}
Dees_Troy51a0e822012-09-05 15:24:24 -040022
23public:
thatf6ed8fc2015-02-14 20:23:16 +010024 std::string GetName() { return mName; }
Dees_Troy51a0e822012-09-05 15:24:24 -040025
26private:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020027 std::string mName;
Dees_Troy51a0e822012-09-05 15:24:24 -040028
29protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020030 static int ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile);
Ethan Yonker63e414f2015-02-06 15:44:39 -060031 static void LoadImage(ZipArchive* pZip, std::string file, gr_surface* source);
32 static void CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect);
Dees_Troy51a0e822012-09-05 15:24:24 -040033};
34
Dees_Troy51a0e822012-09-05 15:24:24 -040035class FontResource : public Resource
36{
37public:
Vojtech Bocek76ee9032014-09-07 15:01:56 +020038 enum Type
39 {
40 TYPE_TWRP,
41#ifndef TW_DISABLE_TTF
42 TYPE_TTF,
43#endif
44 };
45
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020046 FontResource(xml_node<>* node, ZipArchive* pZip);
47 virtual ~FontResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040048
49public:
thatf6ed8fc2015-02-14 20:23:16 +010050 void* GetResource() { return this ? mFont : NULL; }
51 int GetHeight() { return gr_getMaxFontHeight(this ? mFont : NULL); }
52
Dees_Troy51a0e822012-09-05 15:24:24 -040053protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020054 void* mFont;
Vojtech Bocek76ee9032014-09-07 15:01:56 +020055 Type m_type;
Dees_Troy51a0e822012-09-05 15:24:24 -040056};
57
58class ImageResource : public Resource
59{
60public:
that5267a212015-05-06 23:45:57 +020061 ImageResource(xml_node<>* node, ZipArchive* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020062 virtual ~ImageResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040063
64public:
thatf6ed8fc2015-02-14 20:23:16 +010065 gr_surface GetResource() { return this ? mSurface : NULL; }
66 int GetWidth() { return gr_get_width(this ? mSurface : NULL); }
67 int GetHeight() { return gr_get_height(this ? mSurface : NULL); }
68
Dees_Troy51a0e822012-09-05 15:24:24 -040069protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 gr_surface mSurface;
Dees_Troy51a0e822012-09-05 15:24:24 -040071};
72
73class AnimationResource : public Resource
74{
75public:
that5267a212015-05-06 23:45:57 +020076 AnimationResource(xml_node<>* node, ZipArchive* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 virtual ~AnimationResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040078
79public:
thatf6ed8fc2015-02-14 20:23:16 +010080 gr_surface GetResource() { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(0); }
81 gr_surface GetResource(int entry) { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(entry); }
82 int GetWidth() { return gr_get_width(this ? GetResource() : NULL); }
83 int GetHeight() { return gr_get_height(this ? GetResource() : NULL); }
84 int GetResourceCount() { return mSurfaces.size(); }
Dees_Troy51a0e822012-09-05 15:24:24 -040085
86protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 std::vector<gr_surface> mSurfaces;
Dees_Troy51a0e822012-09-05 15:24:24 -040088};
89
90class ResourceManager
91{
92public:
that74ac6062015-03-04 22:39:34 +010093 ResourceManager();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 virtual ~ResourceManager();
Ethan Yonker780cd392014-07-21 15:24:39 -050095 void LoadResources(xml_node<>* resList, ZipArchive* pZip);
Dees_Troy51a0e822012-09-05 15:24:24 -040096
97public:
that74ac6062015-03-04 22:39:34 +010098 FontResource* FindFont(const std::string& name) const;
99 ImageResource* FindImage(const std::string& name) const;
100 AnimationResource* FindAnimation(const std::string& name) const;
thatb2e8f672015-03-05 20:25:39 +0100101 std::string FindString(const std::string& name) const;
Dees_Troy51a0e822012-09-05 15:24:24 -0400102
103private:
that74ac6062015-03-04 22:39:34 +0100104 std::vector<FontResource*> mFonts;
105 std::vector<ImageResource*> mImages;
106 std::vector<AnimationResource*> mAnimations;
thatb2e8f672015-03-05 20:25:39 +0100107 std::map<std::string, std::string> mStrings;
Dees_Troy51a0e822012-09-05 15:24:24 -0400108};
109
110#endif // _RESOURCE_HEADER