Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | // resources.hpp - Base classes for resource management of GUI |
| 20 | |
| 21 | #ifndef _RESOURCE_HEADER |
| 22 | #define _RESOURCE_HEADER |
| 23 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | #include <map> |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 27 | #include "rapidxml.hpp" |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 28 | |
| 29 | struct ZipArchive; |
Dees Troy | b7ae098 | 2013-09-10 20:47:35 +0000 | [diff] [blame] | 30 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 31 | extern "C" { |
| 32 | #include "../minuitwrp/minui.h" |
| 33 | } |
| 34 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | // Base Objects |
| 36 | class Resource |
| 37 | { |
| 38 | public: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 39 | Resource(xml_node<>* node, ZipArchive* pZip); |
| 40 | virtual ~Resource() {} |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
| 42 | public: |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 43 | std::string GetName() { return mName; } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 44 | |
| 45 | private: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 46 | std::string mName; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | |
| 48 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 49 | static int ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile); |
that | b240f4a | 2016-03-14 01:21:38 +0100 | [diff] [blame] | 50 | static void LoadImage(ZipArchive* pZip, std::string file, gr_surface* surface); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 51 | static void CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 52 | }; |
| 53 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | class FontResource : public Resource |
| 55 | { |
| 56 | public: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 57 | FontResource(xml_node<>* node, ZipArchive* pZip); |
| 58 | virtual ~FontResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 59 | |
| 60 | public: |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 61 | void* GetResource() { return this ? mFont : NULL; } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 62 | int GetHeight() { return gr_ttf_getMaxFontHeight(this ? mFont : NULL); } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 63 | void Override(xml_node<>* node, ZipArchive* pZip); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 64 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 65 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 66 | void* mFont; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | void LoadFont(xml_node<>* node, ZipArchive* pZip); |
| 70 | void DeleteFont(); |
| 71 | |
| 72 | private: |
| 73 | int origFontSize; |
| 74 | void* origFont; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | class ImageResource : public Resource |
| 78 | { |
| 79 | public: |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 80 | ImageResource(xml_node<>* node, ZipArchive* pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 81 | virtual ~ImageResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 82 | |
| 83 | public: |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 84 | gr_surface GetResource() { return this ? mSurface : NULL; } |
| 85 | int GetWidth() { return gr_get_width(this ? mSurface : NULL); } |
| 86 | int GetHeight() { return gr_get_height(this ? mSurface : NULL); } |
| 87 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 88 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 89 | gr_surface mSurface; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | class AnimationResource : public Resource |
| 93 | { |
| 94 | public: |
that | 5267a21 | 2015-05-06 23:45:57 +0200 | [diff] [blame] | 95 | AnimationResource(xml_node<>* node, ZipArchive* pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 96 | virtual ~AnimationResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 97 | |
| 98 | public: |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 99 | gr_surface GetResource() { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(0); } |
| 100 | gr_surface GetResource(int entry) { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(entry); } |
| 101 | int GetWidth() { return gr_get_width(this ? GetResource() : NULL); } |
| 102 | int GetHeight() { return gr_get_height(this ? GetResource() : NULL); } |
| 103 | int GetResourceCount() { return mSurfaces.size(); } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 104 | |
| 105 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 106 | std::vector<gr_surface> mSurfaces; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | class ResourceManager |
| 110 | { |
| 111 | public: |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 112 | ResourceManager(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 113 | virtual ~ResourceManager(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 114 | void AddStringResource(std::string resource_source, std::string resource_name, std::string value); |
| 115 | void LoadResources(xml_node<>* resList, ZipArchive* pZip, std::string resource_source); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 116 | |
| 117 | public: |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 118 | FontResource* FindFont(const std::string& name) const; |
| 119 | ImageResource* FindImage(const std::string& name) const; |
| 120 | AnimationResource* FindAnimation(const std::string& name) const; |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 121 | std::string FindString(const std::string& name) const; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 122 | std::string FindString(const std::string& name, const std::string& default_string) const; |
| 123 | void DumpStrings() const; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 124 | |
| 125 | private: |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 126 | struct string_resource_struct { |
| 127 | std::string value; |
| 128 | std::string source; |
| 129 | }; |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 130 | std::vector<FontResource*> mFonts; |
| 131 | std::vector<ImageResource*> mImages; |
| 132 | std::vector<AnimationResource*> mAnimations; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 133 | std::map<std::string, string_resource_struct> mStrings; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | #endif // _RESOURCE_HEADER |