Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // resources.hpp - Base classes for resource management of GUI |
| 2 | |
| 3 | #ifndef _RESOURCE_HEADER |
| 4 | #define _RESOURCE_HEADER |
| 5 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
| 8 | #include <map> |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 9 | #include "rapidxml.hpp" |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 10 | |
| 11 | struct ZipArchive; |
Dees Troy | b7ae098 | 2013-09-10 20:47:35 +0000 | [diff] [blame] | 12 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 13 | extern "C" { |
| 14 | #include "../minuitwrp/minui.h" |
| 15 | } |
| 16 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 17 | // Base Objects |
| 18 | class Resource |
| 19 | { |
| 20 | public: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 21 | Resource(xml_node<>* node, ZipArchive* pZip); |
| 22 | virtual ~Resource() {} |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | |
| 24 | public: |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 25 | std::string GetName() { return mName; } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 26 | |
| 27 | private: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 28 | std::string mName; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | |
| 30 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 31 | static int ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 32 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | }; |
| 35 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | class FontResource : public Resource |
| 37 | { |
| 38 | public: |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 39 | enum Type |
| 40 | { |
| 41 | TYPE_TWRP, |
| 42 | #ifndef TW_DISABLE_TTF |
| 43 | TYPE_TTF, |
| 44 | #endif |
| 45 | }; |
| 46 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 47 | FontResource(xml_node<>* node, ZipArchive* pZip); |
| 48 | virtual ~FontResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | |
| 50 | public: |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 51 | void* GetResource() { return this ? mFont : NULL; } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 52 | int GetHeight() { return gr_ttf_getMaxFontHeight(this ? mFont : NULL); } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 53 | void Override(xml_node<>* node, ZipArchive* pZip); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 54 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 55 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 56 | void* mFont; |
Vojtech Bocek | 76ee903 | 2014-09-07 15:01:56 +0200 | [diff] [blame] | 57 | Type m_type; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | void LoadFont(xml_node<>* node, ZipArchive* pZip); |
| 61 | void DeleteFont(); |
| 62 | |
| 63 | private: |
| 64 | int origFontSize; |
| 65 | void* origFont; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | class ImageResource : public Resource |
| 69 | { |
| 70 | public: |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 71 | ImageResource(xml_node<>* node, ZipArchive* pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 72 | virtual ~ImageResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 73 | |
| 74 | public: |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 75 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 79 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 80 | gr_surface mSurface; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | class AnimationResource : public Resource |
| 84 | { |
| 85 | public: |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 86 | AnimationResource(xml_node<>* node, ZipArchive* pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 87 | virtual ~AnimationResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 88 | |
| 89 | public: |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 90 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 95 | |
| 96 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 97 | std::vector<gr_surface> mSurfaces; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | class ResourceManager |
| 101 | { |
| 102 | public: |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 103 | ResourceManager(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 104 | virtual ~ResourceManager(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 105 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | |
| 108 | public: |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 109 | FontResource* FindFont(const std::string& name) const; |
| 110 | ImageResource* FindImage(const std::string& name) const; |
| 111 | AnimationResource* FindAnimation(const std::string& name) const; |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 112 | std::string FindString(const std::string& name) const; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 113 | std::string FindString(const std::string& name, const std::string& default_string) const; |
| 114 | void DumpStrings() const; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 115 | |
| 116 | private: |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 117 | struct string_resource_struct { |
| 118 | std::string value; |
| 119 | std::string source; |
| 120 | }; |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 121 | std::vector<FontResource*> mFonts; |
| 122 | std::vector<ImageResource*> mImages; |
| 123 | std::vector<AnimationResource*> mAnimations; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 124 | std::map<std::string, string_resource_struct> mStrings; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | #endif // _RESOURCE_HEADER |