blob: 2d306766770e2330e67d0715712c54068b5f57f6 [file] [log] [blame]
Matt Mowere04eee72016-12-31 00:38:57 -06001/*
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_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"
thatb2e8f672015-03-05 20:25:39 +010028
29struct ZipArchive;
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:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020039 Resource(xml_node<>* node, ZipArchive* pZip);
40 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:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020049 static int ExtractResource(ZipArchive* pZip, std::string folderName, std::string fileName, std::string fileExtn, std::string destFile);
thatb240f4a2016-03-14 01:21:38 +010050 static void LoadImage(ZipArchive* 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:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 FontResource(xml_node<>* node, ZipArchive* pZip);
58 virtual ~FontResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040059
60public:
thatf6ed8fc2015-02-14 20:23:16 +010061 void* GetResource() { return this ? mFont : NULL; }
Ethan Yonkerfbb43532015-12-28 21:54:50 +010062 int GetHeight() { return gr_ttf_getMaxFontHeight(this ? mFont : NULL); }
Ethan Yonker74db1572015-10-28 12:44:49 -050063 void Override(xml_node<>* node, ZipArchive* 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:
69 void LoadFont(xml_node<>* node, ZipArchive* pZip);
70 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:
that5267a212015-05-06 23:45:57 +020080 ImageResource(xml_node<>* node, ZipArchive* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 virtual ~ImageResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040082
83public:
thatf6ed8fc2015-02-14 20:23:16 +010084 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_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:
that5267a212015-05-06 23:45:57 +020095 AnimationResource(xml_node<>* node, ZipArchive* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 virtual ~AnimationResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040097
98public:
thatf6ed8fc2015-02-14 20:23:16 +010099 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_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);
115 void LoadResources(xml_node<>* resList, ZipArchive* 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