Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 1 | /* |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 2 | Copyright 2012 to 2020 TeamWin |
Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 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" |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 28 | #include "ziparchive/zip_archive.h" |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 29 | #include "../minuitwrp/truetype.hpp" |
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: |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 39 | Resource(xml_node<>* node, ZipArchiveHandle pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 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: |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 49 | static int ExtractResource(ZipArchiveHandle pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile); |
| 50 | static void LoadImage(ZipArchiveHandle 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: |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 57 | FontResource(xml_node<>* node, ZipArchiveHandle pZip); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 58 | virtual ~FontResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 59 | |
| 60 | public: |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 61 | void* GetResource() { return mFont; } |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 62 | int GetHeight() { return twrpTruetype::gr_ttf_getMaxFontHeight(mFont); } |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 63 | void Override(xml_node<>* node, ZipArchiveHandle 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: |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 69 | void LoadFont(xml_node<>* node, ZipArchiveHandle pZip); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 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: |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 80 | ImageResource(xml_node<>* node, ZipArchiveHandle 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: |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 84 | gr_surface GetResource() { return mSurface; } |
| 85 | int GetWidth() { return gr_get_width(mSurface); } |
| 86 | int GetHeight() { return gr_get_height(mSurface); } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 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: |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 95 | AnimationResource(xml_node<>* node, ZipArchiveHandle 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: |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 99 | gr_surface GetResource() { return mSurfaces.empty() ? NULL : mSurfaces.at(0); } |
| 100 | gr_surface GetResource(int entry) { return mSurfaces.empty() ? NULL : mSurfaces.at(entry); } |
| 101 | int GetWidth() { return gr_get_width(GetResource()); } |
| 102 | int GetHeight() { return gr_get_height(GetResource()); } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 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); |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 115 | void LoadResources(xml_node<>* resList, ZipArchiveHandle 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 |