Debian Codegraph @

wxcrafter/src/wxgui_bitmaploader.cpp

raw
1 #include "wxgui_bitmaploader.h"
2 #include "wxc_project_metadata.h"
3 #include "wxgui_globals.h"
4 #include <wx/ffile.h>
5 #include <wx/filename.h>
6 #include <wx/stdpaths.h>
7 #include <clZipReader.h>
8 #include "clFilesCollector.h"
9
10 std::map<wxString, wxBitmap> wxCrafter::ResourceLoader::m_bitmaps;
11 std::map<wxString, wxString> wxCrafter::ResourceLoader::m_files;
12
13 wxCrafter::ResourceLoader::ResourceLoader(const wxString& skin)
14 {
15 if(m_bitmaps.empty()) {
16 wxString zipFile;
17 zipFile << wxStandardPaths::Get().GetDataDir() << wxFileName::GetPathSeparator() << skin << wxT(".zip");
18 clZipReader zip(zipFile);
19
20 std::unordered_map<wxString, clZipReader::Entry> entries;
21 zip.ExtractAll(entries);
22 if(entries.empty()) { return; }
23
24 // Loop over the files
25 for(const auto& entry : entries) {
26 wxFileName fn = wxFileName(entry.first);
27 wxString name = fn.GetName();
28 clZipReader::Entry d = entry.second;
29 if(d.len && d.buffer) {
30 // Avoid wxAsserts by checking it's likely to be a png before creating the image
31 if(fn.GetExt() == "png") {
32 wxMemoryInputStream is(d.buffer, d.len);
33 wxImage img(is, wxBITMAP_TYPE_PNG);
34 wxBitmap bmp(img);
35 if(bmp.IsOk()) { m_bitmaps[name] = bmp; }
36 } else {
37 wxString fileContent((const char*)d.buffer, d.len);
38 m_files.insert({ fn.GetFullName(), fileContent });
39 }
40 // release the memory
41 free(d.buffer);
42 }
43 }
44 entries.clear();
45 }
46 }
47
48 wxCrafter::ResourceLoader::~ResourceLoader() {}
49
50 const wxBitmap& wxCrafter::ResourceLoader::Bitmap(const wxString& name) const
51 {
52 static wxBitmap s_nullBitmap;
53 std::map<wxString, wxBitmap>::const_iterator iter = m_bitmaps.find(name);
54 if(iter == m_bitmaps.end()) { return s_nullBitmap; }
55 return iter->second;
56 }
57
58 wxFileName wxCrafter::ResourceLoader::GetPlaceHolderImagePath() const
59 {
60 static const wxString PLACEHOLDER_IMG = wxT("placeholder");
61 const wxBitmap& bmp = Bitmap(PLACEHOLDER_IMG);
62 if(bmp.IsOk()) {
63 wxFileName path(wxFileName::GetTempDir(), wxT("placeholder.png"));
64 if(bmp.SaveFile(path.GetFullPath(), wxBITMAP_TYPE_PNG)) {
65 path.MakeRelativeTo(wxcProjectMetadata::Get().GetProjectPath());
66 return path;
67 }
68 }
69 return wxFileName();
70 }
71
72 wxFileName wxCrafter::ResourceLoader::GetPlaceHolder16ImagePath() const
73 {
74 static const wxString PLACEHOLDER_IMG16 = wxT("placeholder16");
75 const wxBitmap& bmp = Bitmap(PLACEHOLDER_IMG16);
76 if(bmp.IsOk()) {
77 wxFileName path(wxFileName::GetTempDir(), wxT("placeholder16.png"));
78 if(bmp.SaveFile(path.GetFullPath(), wxBITMAP_TYPE_PNG)) {
79 path.MakeRelativeTo(wxcProjectMetadata::Get().GetProjectPath());
80 return path;
81 }
82 }
83 return wxFileName();
84 }
85
86 wxString wxCrafter::ResourceLoader::File(const wxString& name) const
87 {
88 std::map<wxString, wxString>::const_iterator iter = m_files.find(name);
89 if(iter == m_files.end()) { return wxT(""); }
90 return iter->second;
91 }