blob: ce06d9679531b7bf9578f507add735cc6293828a [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>
Ethan Yonker74db1572015-10-28 12:44:49 -05009#include "rapidxml.hpp"
thatb2e8f672015-03-05 20:25:39 +010010
11struct ZipArchive;
Dees Troyb7ae0982013-09-10 20:47:35 +000012
thatf37aec22015-02-01 13:38:35 +010013extern "C" {
14#include "../minuitwrp/minui.h"
15}
16
Dees_Troy51a0e822012-09-05 15:24:24 -040017// Base Objects
18class Resource
19{
20public:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020021 Resource(xml_node<>* node, ZipArchive* pZip);
22 virtual ~Resource() {}
Dees_Troy51a0e822012-09-05 15:24:24 -040023
24public:
thatf6ed8fc2015-02-14 20:23:16 +010025 std::string GetName() { return mName; }
Dees_Troy51a0e822012-09-05 15:24:24 -040026
27private:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020028 std::string mName;
Dees_Troy51a0e822012-09-05 15:24:24 -040029
30protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020031 static int ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile);
Ethan Yonker63e414f2015-02-06 15:44:39 -060032 static void LoadImage(ZipArchive* pZip, std::string file, gr_surface* source);
33 static void CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect);
Dees_Troy51a0e822012-09-05 15:24:24 -040034};
35
Dees_Troy51a0e822012-09-05 15:24:24 -040036class FontResource : public Resource
37{
38public:
Vojtech Bocek76ee9032014-09-07 15:01:56 +020039 enum Type
40 {
41 TYPE_TWRP,
42#ifndef TW_DISABLE_TTF
43 TYPE_TTF,
44#endif
45 };
46
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020047 FontResource(xml_node<>* node, ZipArchive* pZip);
48 virtual ~FontResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040049
50public:
thatf6ed8fc2015-02-14 20:23:16 +010051 void* GetResource() { return this ? mFont : NULL; }
52 int GetHeight() { return gr_getMaxFontHeight(this ? mFont : NULL); }
Ethan Yonker74db1572015-10-28 12:44:49 -050053 void Override(xml_node<>* node, ZipArchive* pZip);
thatf6ed8fc2015-02-14 20:23:16 +010054
Dees_Troy51a0e822012-09-05 15:24:24 -040055protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020056 void* mFont;
Vojtech Bocek76ee9032014-09-07 15:01:56 +020057 Type m_type;
Ethan Yonker74db1572015-10-28 12:44:49 -050058
59private:
60 void LoadFont(xml_node<>* node, ZipArchive* pZip);
61 void DeleteFont();
62
63private:
64 int origFontSize;
65 void* origFont;
Dees_Troy51a0e822012-09-05 15:24:24 -040066};
67
68class ImageResource : public Resource
69{
70public:
that5267a212015-05-06 23:45:57 +020071 ImageResource(xml_node<>* node, ZipArchive* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 virtual ~ImageResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040073
74public:
thatf6ed8fc2015-02-14 20:23:16 +010075 gr_surface GetResource() { return this ? mSurface : NULL; }
76 int GetWidth() { return gr_get_width(this ? mSurface : NULL); }
77 int GetHeight() { return gr_get_height(this ? mSurface : NULL); }
78
Dees_Troy51a0e822012-09-05 15:24:24 -040079protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 gr_surface mSurface;
Dees_Troy51a0e822012-09-05 15:24:24 -040081};
82
83class AnimationResource : public Resource
84{
85public:
that5267a212015-05-06 23:45:57 +020086 AnimationResource(xml_node<>* node, ZipArchive* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 virtual ~AnimationResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040088
89public:
thatf6ed8fc2015-02-14 20:23:16 +010090 gr_surface GetResource() { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(0); }
91 gr_surface GetResource(int entry) { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(entry); }
92 int GetWidth() { return gr_get_width(this ? GetResource() : NULL); }
93 int GetHeight() { return gr_get_height(this ? GetResource() : NULL); }
94 int GetResourceCount() { return mSurfaces.size(); }
Dees_Troy51a0e822012-09-05 15:24:24 -040095
96protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097 std::vector<gr_surface> mSurfaces;
Dees_Troy51a0e822012-09-05 15:24:24 -040098};
99
100class ResourceManager
101{
102public:
that74ac6062015-03-04 22:39:34 +0100103 ResourceManager();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200104 virtual ~ResourceManager();
Ethan Yonker74db1572015-10-28 12:44:49 -0500105 void AddStringResource(std::string resource_source, std::string resource_name, std::string value);
106 void LoadResources(xml_node<>* resList, ZipArchive* pZip, std::string resource_source);
Dees_Troy51a0e822012-09-05 15:24:24 -0400107
108public:
that74ac6062015-03-04 22:39:34 +0100109 FontResource* FindFont(const std::string& name) const;
110 ImageResource* FindImage(const std::string& name) const;
111 AnimationResource* FindAnimation(const std::string& name) const;
thatb2e8f672015-03-05 20:25:39 +0100112 std::string FindString(const std::string& name) const;
Ethan Yonker74db1572015-10-28 12:44:49 -0500113 std::string FindString(const std::string& name, const std::string& default_string) const;
114 void DumpStrings() const;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115
116private:
Ethan Yonker74db1572015-10-28 12:44:49 -0500117 struct string_resource_struct {
118 std::string value;
119 std::string source;
120 };
that74ac6062015-03-04 22:39:34 +0100121 std::vector<FontResource*> mFonts;
122 std::vector<ImageResource*> mImages;
123 std::vector<AnimationResource*> mAnimations;
Ethan Yonker74db1572015-10-28 12:44:49 -0500124 std::map<std::string, string_resource_struct> mStrings;
Dees_Troy51a0e822012-09-05 15:24:24 -0400125};
126
127#endif // _RESOURCE_HEADER