blob: de673184c547118b04fff27657589ce2eabc124c [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"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050028#include "../zipwrap.hpp"
Dees Troyb7ae0982013-09-10 20:47:35 +000029
thatf37aec22015-02-01 13:38:35 +010030extern "C" {
31#include "../minuitwrp/minui.h"
32}
33
Dees_Troy51a0e822012-09-05 15:24:24 -040034// Base Objects
35class Resource
36{
37public:
Ethan Yonker8373cfe2017-09-08 06:50:54 -050038 Resource(xml_node<>* node, ZipWrap* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020039 virtual ~Resource() {}
Dees_Troy51a0e822012-09-05 15:24:24 -040040
41public:
thatf6ed8fc2015-02-14 20:23:16 +010042 std::string GetName() { return mName; }
Dees_Troy51a0e822012-09-05 15:24:24 -040043
44private:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020045 std::string mName;
Dees_Troy51a0e822012-09-05 15:24:24 -040046
47protected:
Ethan Yonker8373cfe2017-09-08 06:50:54 -050048 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 Yonker63e414f2015-02-06 15:44:39 -060050 static void CheckAndScaleImage(gr_surface source, gr_surface* destination, int retain_aspect);
Dees_Troy51a0e822012-09-05 15:24:24 -040051};
52
Dees_Troy51a0e822012-09-05 15:24:24 -040053class FontResource : public Resource
54{
55public:
Ethan Yonker8373cfe2017-09-08 06:50:54 -050056 FontResource(xml_node<>* node, ZipWrap* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 virtual ~FontResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040058
59public:
thatf6ed8fc2015-02-14 20:23:16 +010060 void* GetResource() { return this ? mFont : NULL; }
Ethan Yonkerfbb43532015-12-28 21:54:50 +010061 int GetHeight() { return gr_ttf_getMaxFontHeight(this ? mFont : NULL); }
Ethan Yonker8373cfe2017-09-08 06:50:54 -050062 void Override(xml_node<>* node, ZipWrap* pZip);
thatf6ed8fc2015-02-14 20:23:16 +010063
Dees_Troy51a0e822012-09-05 15:24:24 -040064protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020065 void* mFont;
Ethan Yonker74db1572015-10-28 12:44:49 -050066
67private:
Ethan Yonker8373cfe2017-09-08 06:50:54 -050068 void LoadFont(xml_node<>* node, ZipWrap* pZip);
Ethan Yonker74db1572015-10-28 12:44:49 -050069 void DeleteFont();
70
71private:
72 int origFontSize;
73 void* origFont;
Dees_Troy51a0e822012-09-05 15:24:24 -040074};
75
76class ImageResource : public Resource
77{
78public:
Ethan Yonker8373cfe2017-09-08 06:50:54 -050079 ImageResource(xml_node<>* node, ZipWrap* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 virtual ~ImageResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040081
82public:
thatf6ed8fc2015-02-14 20:23:16 +010083 gr_surface GetResource() { return this ? mSurface : NULL; }
84 int GetWidth() { return gr_get_width(this ? mSurface : NULL); }
85 int GetHeight() { return gr_get_height(this ? mSurface : NULL); }
86
Dees_Troy51a0e822012-09-05 15:24:24 -040087protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 gr_surface mSurface;
Dees_Troy51a0e822012-09-05 15:24:24 -040089};
90
91class AnimationResource : public Resource
92{
93public:
Ethan Yonker8373cfe2017-09-08 06:50:54 -050094 AnimationResource(xml_node<>* node, ZipWrap* pZip);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 virtual ~AnimationResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040096
97public:
thatf6ed8fc2015-02-14 20:23:16 +010098 gr_surface GetResource() { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(0); }
99 gr_surface GetResource(int entry) { return (!this || mSurfaces.empty()) ? NULL : mSurfaces.at(entry); }
100 int GetWidth() { return gr_get_width(this ? GetResource() : NULL); }
101 int GetHeight() { return gr_get_height(this ? GetResource() : NULL); }
102 int GetResourceCount() { return mSurfaces.size(); }
Dees_Troy51a0e822012-09-05 15:24:24 -0400103
104protected:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 std::vector<gr_surface> mSurfaces;
Dees_Troy51a0e822012-09-05 15:24:24 -0400106};
107
108class ResourceManager
109{
110public:
that74ac6062015-03-04 22:39:34 +0100111 ResourceManager();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 virtual ~ResourceManager();
Ethan Yonker74db1572015-10-28 12:44:49 -0500113 void AddStringResource(std::string resource_source, std::string resource_name, std::string value);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500114 void LoadResources(xml_node<>* resList, ZipWrap* pZip, std::string resource_source);
Dees_Troy51a0e822012-09-05 15:24:24 -0400115
116public:
that74ac6062015-03-04 22:39:34 +0100117 FontResource* FindFont(const std::string& name) const;
118 ImageResource* FindImage(const std::string& name) const;
119 AnimationResource* FindAnimation(const std::string& name) const;
thatb2e8f672015-03-05 20:25:39 +0100120 std::string FindString(const std::string& name) const;
Ethan Yonker74db1572015-10-28 12:44:49 -0500121 std::string FindString(const std::string& name, const std::string& default_string) const;
122 void DumpStrings() const;
Dees_Troy51a0e822012-09-05 15:24:24 -0400123
124private:
Ethan Yonker74db1572015-10-28 12:44:49 -0500125 struct string_resource_struct {
126 std::string value;
127 std::string source;
128 };
that74ac6062015-03-04 22:39:34 +0100129 std::vector<FontResource*> mFonts;
130 std::vector<ImageResource*> mImages;
131 std::vector<AnimationResource*> mAnimations;
Ethan Yonker74db1572015-10-28 12:44:49 -0500132 std::map<std::string, string_resource_struct> mStrings;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133};
134
135#endif // _RESOURCE_HEADER