blob: aeacd197da75c24c1689e798722f735ecd7267e7 [file] [log] [blame]
Matt Mowere04eee72016-12-31 00:38:57 -06001/*
bigbiffd58ba182020-03-23 10:02:29 -04002 Copyright 2012 to 2020 TeamWin
Matt Mowere04eee72016-12-31 00:38:57 -06003 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_Troy51a0e822012-09-05 15:24:24 -040019// resources.hpp - Base classes for resource management of GUI
20
21#ifndef _RESOURCE_HEADER
22#define _RESOURCE_HEADER
23
thatb2e8f672015-03-05 20:25:39 +010024#include <string>
25#include <vector>
26#include <map>
Ethan Yonker74db1572015-10-28 12:44:49 -050027#include "rapidxml.hpp"
bigbiff673c7ae2020-12-02 19:44:56 -050028#include "ziparchive/zip_archive.h"
bigbiffd58ba182020-03-23 10:02:29 -040029#include "../minuitwrp/truetype.hpp"
Dees Troyb7ae0982013-09-10 20:47:35 +000030
thatf37aec22015-02-01 13:38:35 +010031extern "C" {
32#include "../minuitwrp/minui.h"
33}
34
Dees_Troy51a0e822012-09-05 15:24:24 -040035// Base Objects
36class Resource
37{
38public:
bigbiff673c7ae2020-12-02 19:44:56 -050039 Resource(xml_node<>* node, ZipArchiveHandle pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020040 virtual ~Resource() {}
Dees_Troy51a0e822012-09-05 15:24:24 -040041
42public:
thatf6ed8fc2015-02-14 20:23:16 +010043 std::string GetName() { return mName; }
Dees_Troy51a0e822012-09-05 15:24:24 -040044
45private:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020046 std::string mName;
Dees_Troy51a0e822012-09-05 15:24:24 -040047
48protected:
bigbiff673c7ae2020-12-02 19:44:56 -050049 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 Yonker63e414f2015-02-06 15:44:39 -060051 static void CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect);
Dees_Troy51a0e822012-09-05 15:24:24 -040052};
53
Dees_Troy51a0e822012-09-05 15:24:24 -040054class FontResource : public Resource
55{
56public:
bigbiff673c7ae2020-12-02 19:44:56 -050057 FontResource(xml_node<>* node, ZipArchiveHandle pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020058 virtual ~FontResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040059
60public:
Ethan Yonker58f21322018-08-24 11:17:36 -050061 void* GetResource() { return mFont; }
bigbiffd58ba182020-03-23 10:02:29 -040062 int GetHeight() { return twrpTruetype::gr_ttf_getMaxFontHeight(mFont); }
bigbiff673c7ae2020-12-02 19:44:56 -050063 void Override(xml_node<>* node, ZipArchiveHandle pZip);
thatf6ed8fc2015-02-14 20:23:16 +010064
Dees_Troy51a0e822012-09-05 15:24:24 -040065protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 void* mFont;
Ethan Yonker74db1572015-10-28 12:44:49 -050067
68private:
bigbiff673c7ae2020-12-02 19:44:56 -050069 void LoadFont(xml_node<>* node, ZipArchiveHandle pZip);
Ethan Yonker74db1572015-10-28 12:44:49 -050070 void DeleteFont();
71
72private:
73 int origFontSize;
74 void* origFont;
Dees_Troy51a0e822012-09-05 15:24:24 -040075};
76
77class ImageResource : public Resource
78{
79public:
bigbiff673c7ae2020-12-02 19:44:56 -050080 ImageResource(xml_node<>* node, ZipArchiveHandle pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 virtual ~ImageResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040082
83public:
Ethan Yonker58f21322018-08-24 11:17:36 -050084 gr_surface GetResource() { return mSurface; }
85 int GetWidth() { return gr_get_width(mSurface); }
86 int GetHeight() { return gr_get_height(mSurface); }
thatf6ed8fc2015-02-14 20:23:16 +010087
Dees_Troy51a0e822012-09-05 15:24:24 -040088protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020089 gr_surface mSurface;
Dees_Troy51a0e822012-09-05 15:24:24 -040090};
91
92class AnimationResource : public Resource
93{
94public:
bigbiff673c7ae2020-12-02 19:44:56 -050095 AnimationResource(xml_node<>* node, ZipArchiveHandle pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 virtual ~AnimationResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040097
98public:
Ethan Yonker58f21322018-08-24 11:17:36 -050099 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()); }
thatf6ed8fc2015-02-14 20:23:16 +0100103 int GetResourceCount() { return mSurfaces.size(); }
Dees_Troy51a0e822012-09-05 15:24:24 -0400104
105protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106 std::vector<gr_surface> mSurfaces;
Dees_Troy51a0e822012-09-05 15:24:24 -0400107};
108
109class ResourceManager
110{
111public:
that74ac6062015-03-04 22:39:34 +0100112 ResourceManager();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200113 virtual ~ResourceManager();
Ethan Yonker74db1572015-10-28 12:44:49 -0500114 void AddStringResource(std::string resource_source, std::string resource_name, std::string value);
bigbiff673c7ae2020-12-02 19:44:56 -0500115 void LoadResources(xml_node<>* resList, ZipArchiveHandle pZip, std::string resource_source);
Dees_Troy51a0e822012-09-05 15:24:24 -0400116
117public:
that74ac6062015-03-04 22:39:34 +0100118 FontResource* FindFont(const std::string& name) const;
119 ImageResource* FindImage(const std::string& name) const;
120 AnimationResource* FindAnimation(const std::string& name) const;
thatb2e8f672015-03-05 20:25:39 +0100121 std::string FindString(const std::string& name) const;
Ethan Yonker74db1572015-10-28 12:44:49 -0500122 std::string FindString(const std::string& name, const std::string& default_string) const;
123 void DumpStrings() const;
Dees_Troy51a0e822012-09-05 15:24:24 -0400124
125private:
Ethan Yonker74db1572015-10-28 12:44:49 -0500126 struct string_resource_struct {
127 std::string value;
128 std::string source;
129 };
that74ac6062015-03-04 22:39:34 +0100130 std::vector<FontResource*> mFonts;
131 std::vector<ImageResource*> mImages;
132 std::vector<AnimationResource*> mAnimations;
Ethan Yonker74db1572015-10-28 12:44:49 -0500133 std::map<std::string, string_resource_struct> mStrings;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134};
135
136#endif // _RESOURCE_HEADER