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" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 28 | #include "../zipwrap.hpp" |
Dees Troy | b7ae098 | 2013-09-10 20:47:35 +0000 | [diff] [blame] | 29 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 30 | extern "C" { |
| 31 | #include "../minuitwrp/minui.h" |
| 32 | } |
| 33 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | // Base Objects |
| 35 | class Resource |
| 36 | { |
| 37 | public: |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 38 | Resource(xml_node<>* node, ZipWrap* pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 39 | virtual ~Resource() {} |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | |
| 41 | public: |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 42 | std::string GetName() { return mName; } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
| 44 | private: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 45 | std::string mName; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 46 | |
| 47 | protected: |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 48 | static int ExtractResource(ZipWrap* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile); |
| 49 | static void LoadImage(ZipWrap* pZip, std::string file, gr_surface* surface); |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 50 | static void CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | }; |
| 52 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | class FontResource : public Resource |
| 54 | { |
| 55 | public: |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 56 | FontResource(xml_node<>* node, ZipWrap* pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 57 | virtual ~FontResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | |
| 59 | public: |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 60 | void* GetResource() { return mFont; } |
| 61 | int GetHeight() { return gr_ttf_getMaxFontHeight(mFont); } |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 62 | void Override(xml_node<>* node, ZipWrap* pZip); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 63 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 64 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 65 | void* mFont; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 66 | |
| 67 | private: |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 68 | void LoadFont(xml_node<>* node, ZipWrap* pZip); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 69 | void DeleteFont(); |
| 70 | |
| 71 | private: |
| 72 | int origFontSize; |
| 73 | void* origFont; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | class ImageResource : public Resource |
| 77 | { |
| 78 | public: |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 79 | ImageResource(xml_node<>* node, ZipWrap* pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 80 | virtual ~ImageResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 81 | |
| 82 | public: |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 83 | gr_surface GetResource() { return mSurface; } |
| 84 | int GetWidth() { return gr_get_width(mSurface); } |
| 85 | int GetHeight() { return gr_get_height(mSurface); } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 86 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 87 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 88 | gr_surface mSurface; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | class AnimationResource : public Resource |
| 92 | { |
| 93 | public: |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 94 | AnimationResource(xml_node<>* node, ZipWrap* pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 95 | virtual ~AnimationResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 96 | |
| 97 | public: |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 98 | gr_surface GetResource() { return mSurfaces.empty() ? NULL : mSurfaces.at(0); } |
| 99 | gr_surface GetResource(int entry) { return mSurfaces.empty() ? NULL : mSurfaces.at(entry); } |
| 100 | int GetWidth() { return gr_get_width(GetResource()); } |
| 101 | int GetHeight() { return gr_get_height(GetResource()); } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 102 | int GetResourceCount() { return mSurfaces.size(); } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 103 | |
| 104 | protected: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 105 | std::vector<gr_surface> mSurfaces; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | class ResourceManager |
| 109 | { |
| 110 | public: |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 111 | ResourceManager(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 112 | virtual ~ResourceManager(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 113 | void AddStringResource(std::string resource_source, std::string resource_name, std::string value); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 114 | void LoadResources(xml_node<>* resList, ZipWrap* pZip, std::string resource_source); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 115 | |
| 116 | public: |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 117 | FontResource* FindFont(const std::string& name) const; |
| 118 | ImageResource* FindImage(const std::string& name) const; |
| 119 | AnimationResource* FindAnimation(const std::string& name) const; |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 120 | std::string FindString(const std::string& name) const; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 121 | std::string FindString(const std::string& name, const std::string& default_string) const; |
| 122 | void DumpStrings() const; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 123 | |
| 124 | private: |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 125 | struct string_resource_struct { |
| 126 | std::string value; |
| 127 | std::string source; |
| 128 | }; |
that | 74ac606 | 2015-03-04 22:39:34 +0100 | [diff] [blame] | 129 | std::vector<FontResource*> mFonts; |
| 130 | std::vector<ImageResource*> mImages; |
| 131 | std::vector<AnimationResource*> mAnimations; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 132 | std::map<std::string, string_resource_struct> mStrings; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | #endif // _RESOURCE_HEADER |