mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
implemented import_image function
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <data/field/symbol.hpp>
|
||||
#include <render/symbol/filter.hpp>
|
||||
#include <gui/util.hpp> // load_resource_image
|
||||
#include <wx/wfstream.h>
|
||||
|
||||
// ----------------------------------------------------------------------------- : GeneratedImage
|
||||
|
||||
@@ -515,3 +516,50 @@ bool ImageValueToImage::operator == (const GeneratedImage& that) const {
|
||||
return that2 && filename == that2->filename
|
||||
&& age == that2->age;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : ExternalImage
|
||||
|
||||
Image ExternalImage::generate(const Options& opt) const {
|
||||
wxFileName fname(filepath, wxPATH_UNIX);
|
||||
|
||||
// does the file pointed to by filepath exist?
|
||||
if (!fname.FileExists()) {
|
||||
String filePathString = fname.GetAbsolutePath().ToStdString();
|
||||
throw ScriptError(format_string(_("The file '%s' was not found."),filePathString));
|
||||
}
|
||||
|
||||
String fileExt = fname.GetExt();
|
||||
wxBitmapType bitmapType;
|
||||
if (fileExt == _("png"))
|
||||
bitmapType = wxBITMAP_TYPE_PNG;
|
||||
else if (fileExt == _("jpg"))
|
||||
bitmapType = wxBITMAP_TYPE_JPEG;
|
||||
else
|
||||
bitmapType = wxBITMAP_TYPE_BMP;
|
||||
|
||||
// does the file exist in the package?
|
||||
String fileNameNoExtension = fname.GetName();
|
||||
if (!opt.local_package->existsIn(fileNameNoExtension)) {
|
||||
auto outStream = opt.local_package->openOut(fileNameNoExtension);
|
||||
wxFileInputStream inStream = wxFileInputStream(filepath.ToStdString());
|
||||
if (!inStream.IsOk()) throw ScriptError("Failed to create file stream.");
|
||||
outStream->Write(inStream);
|
||||
if (!outStream->IsOk()) throw ScriptError("Failed to write image to set.");
|
||||
outStream->Close();
|
||||
}
|
||||
|
||||
// save the package with the new image
|
||||
opt.local_package->saveAs(opt.local_package->relativeFilename(), false, false);
|
||||
|
||||
auto imageInputStream = opt.local_package->openIn(fileNameNoExtension);
|
||||
Image img(*imageInputStream.get(), bitmapType);
|
||||
|
||||
if (!img.IsOk()) throw ScriptError("The image could not be created.");
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
bool ExternalImage::operator == (const GeneratedImage& that) const {
|
||||
const ExternalImage* that2 = dynamic_cast<const ExternalImage*>(&that);
|
||||
return that2 && that2->filepath == filepath;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user