blob: 528fecc1414b3ade9824ef98f9ae9816430ca7f3 [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);
thatb240f4a2016-03-14 01:21:38 +010032 static void LoadImage(ZipArchive* pZip, std::string file, gr_surface* surface);
Ethan Yonker63e414f2015-02-06 15:44:39 -060033 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 Bocekfafb0c52013-07-25 22:53:02 +020039 FontResource(xml_node<>* node, ZipArchive* pZip);
40 virtual ~FontResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040041
42public:
thatf6ed8fc2015-02-14 20:23:16 +010043 void* GetResource() { return this ? mFont : NULL; }
Ethan Yonkerfbb43532015-12-28 21:54:50 +010044 int GetHeight() { return gr_ttf_getMaxFontHeight(this ? mFont : NULL); }
Ethan Yonker74db1572015-10-28 12:44:49 -050045 void Override(xml_node<>* node, ZipArchive* pZip);
thatf6ed8fc2015-02-14 20:23:16 +010046
Dees_Troy51a0e822012-09-05 15:24:24 -040047protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 void* mFont;
Ethan Yonker74db1572015-10-28 12:44:49 -050049
50private:
51 void LoadFont(xml_node<>* node, ZipArchive* pZip);
52 void DeleteFont();
53
54private:
55 int origFontSize;
56 void* origFont;
Dees_Troy51a0e822012-09-05 15:24:24 -040057};
58
59class ImageResource : public Resource
60{
61public:
that5267a212015-05-06 23:45:57 +020062 ImageResource(xml_node<>* node, ZipArchive* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020063 virtual ~ImageResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040064
65public:
thatf6ed8fc2015-02-14 20:23:16 +010066 gr_surface GetResource() { return this ? mSurface : NULL; }
67 int GetWidth() { return gr_get_width(this ? mSurface : NULL); }
68 int GetHeight() { return gr_get_height(this ? mSurface : NULL); }
69
Dees_Troy51a0e822012-09-05 15:24:24 -040070protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020071 gr_surface mSurface;
Dees_Troy51a0e822012-09-05 15:24:24 -040072};
73
74class AnimationResource : public Resource
75{
76public:
that5267a212015-05-06 23:45:57 +020077 AnimationResource(xml_node<>* node, ZipArchive* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 virtual ~AnimationResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040079
80public:
thatf6ed8fc2015-02-14 20:23:16 +010081 gr_surface GetResource() { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(0); }
82 gr_surface GetResource(int entry) { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(entry); }
83 int GetWidth() { return gr_get_width(this ? GetResource() : NULL); }
84 int GetHeight() { return gr_get_height(this ? GetResource() : NULL); }
85 int GetResourceCount() { return mSurfaces.size(); }
Dees_Troy51a0e822012-09-05 15:24:24 -040086
87protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 std::vector<gr_surface> mSurfaces;
Dees_Troy51a0e822012-09-05 15:24:24 -040089};
90
91class ResourceManager
92{
93public:
that74ac6062015-03-04 22:39:34 +010094 ResourceManager();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 virtual ~ResourceManager();
Ethan Yonker74db1572015-10-28 12:44:49 -050096 void AddStringResource(std::string resource_source, std::string resource_name, std::string value);
97 void LoadResources(xml_node<>* resList, ZipArchive* pZip, std::string resource_source);
Dees_Troy51a0e822012-09-05 15:24:24 -040098
99public:
that74ac6062015-03-04 22:39:34 +0100100 FontResource* FindFont(const std::string& name) const;
101 ImageResource* FindImage(const std::string& name) const;
102 AnimationResource* FindAnimation(const std::string& name) const;
thatb2e8f672015-03-05 20:25:39 +0100103 std::string FindString(const std::string& name) const;
Ethan Yonker74db1572015-10-28 12:44:49 -0500104 std::string FindString(const std::string& name, const std::string& default_string) const;
105 void DumpStrings() const;
Dees_Troy51a0e822012-09-05 15:24:24 -0400106
107private:
Ethan Yonker74db1572015-10-28 12:44:49 -0500108 struct string_resource_struct {
109 std::string value;
110 std::string source;
111 };
that74ac6062015-03-04 22:39:34 +0100112 std::vector<FontResource*> mFonts;
113 std::vector<ImageResource*> mImages;
114 std::vector<AnimationResource*> mAnimations;
Ethan Yonker74db1572015-10-28 12:44:49 -0500115 std::map<std::string, string_resource_struct> mStrings;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116};
117
118#endif // _RESOURCE_HEADER