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