mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Merge pull request #36 from TomTkacz/master
implemented import_image function
This commit is contained in:
@@ -803,6 +803,17 @@ error:
|
||||
To resolve this, add:
|
||||
depends on: %s %s
|
||||
|
||||
# Image import
|
||||
import not found: File not found: '%s'
|
||||
can't import image without set: Must first save or load a set file before importing file: '%s'
|
||||
can't create file stream: Failed to create file stream: '%s'
|
||||
can't write image to set: Failed to write image to set: '%s'
|
||||
can't import image: Failed to import image: '%s'
|
||||
|
||||
# Card creation
|
||||
no field with name: Card doesn't have a field named '%s'
|
||||
can't set value: Can not set card value '%s', it is not of the right type
|
||||
|
||||
# Script stuff
|
||||
has no member: %s has no member '%s'
|
||||
can't convert: Can't convert from %s to %s
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
Function: import_image
|
||||
|
||||
--Usage--
|
||||
> import_image(image_path)
|
||||
|
||||
Load an image from outside the data folder. Intended for use from the CLI.
|
||||
|
||||
--Parameters--
|
||||
! Parameter Type Description
|
||||
| @input@ [[type:string]] Full path of the image to load
|
||||
|
||||
--Examples--
|
||||
> new_card([image: import_image("D:/Art/Ajani.png"), card_color: "green"])
|
||||
@@ -94,6 +94,7 @@ These functions are built into the program, other [[type:function]]s can be defi
|
||||
| [[fun:rotate_image]] Rotate an image.
|
||||
| [[fun:drop_shadow]] Add a drop shadow to an image.
|
||||
| [[fun:symbol_variation]] Render a variation of a [[type:symbol]].
|
||||
| [[fun:import_image]] Load an image from outside the data folder.
|
||||
| [[fun:built_in_image]] Return an image built into the program.
|
||||
|
||||
! Cards <<<
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -528,3 +529,48 @@ 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);
|
||||
String filePathString = fname.GetAbsolutePath();
|
||||
|
||||
// has a pre-existing .mse-set file been loaded?
|
||||
if (opt.local_package->needSaveAs()) throw ScriptError(_ERROR_1_("can't import image without set", filePathString));
|
||||
|
||||
// does the file pointed to by filepath exist?
|
||||
if (!fname.FileExists()) throw ScriptError(_ERROR_1_("import not found", filePathString));
|
||||
|
||||
String fileExt = fname.GetExt();
|
||||
wxBitmapType bitmapType;
|
||||
if (fileExt == _("png")) bitmapType = wxBITMAP_TYPE_PNG;
|
||||
else if (fileExt == _("jpg") || fileExt == _("jpeg")) 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(_ERROR_1_("can't create file stream", filePathString));
|
||||
outStream->Write(inStream);
|
||||
if (!outStream->IsOk()) throw ScriptError(_ERROR_1_("can't write image to set", filePathString));
|
||||
outStream->Close();
|
||||
}
|
||||
|
||||
// save the package with the new image
|
||||
opt.local_package->save(false);
|
||||
|
||||
auto imageInputStream = opt.local_package->openIn(fileNameNoExtension);
|
||||
Image img(*imageInputStream.get(), bitmapType);
|
||||
|
||||
if (!img.IsOk()) throw ScriptError(_ERROR_1_("can't import image", filePathString));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
bool ExternalImage::operator == (const GeneratedImage& that) const {
|
||||
const ExternalImage* that2 = dynamic_cast<const ExternalImage*>(&that);
|
||||
return that2 && that2->filepath == filepath;
|
||||
}
|
||||
|
||||
@@ -414,3 +414,16 @@ private:
|
||||
Age age; ///< Age the image was last updated
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : ExternalImage
|
||||
|
||||
/// Load an image from the filesystem
|
||||
class ExternalImage : public GeneratedImage {
|
||||
public:
|
||||
ExternalImage(const String& filepath) : filepath(filepath) {};
|
||||
Image generate(const Options&) const override;
|
||||
bool operator == (const GeneratedImage& that) const override;
|
||||
inline String toString() { return filepath; }
|
||||
inline String toCode() const override { return _("<image>"); }
|
||||
private:
|
||||
String filepath;
|
||||
};
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <data/field/choice.hpp>
|
||||
#include <data/field/package_choice.hpp>
|
||||
#include <data/field/color.hpp>
|
||||
#include <data/field/image.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/card.hpp>
|
||||
#include <util/error.hpp>
|
||||
@@ -33,7 +34,7 @@ SCRIPT_FUNCTION(new_card) {
|
||||
// find value to update
|
||||
IndexMap<FieldP,ValueP>::const_iterator value_it = new_card->data.find(name);
|
||||
if (value_it == new_card->data.end()) {
|
||||
throw ScriptError(format_string(_("Card doesn't have a field named '%s'"),name));
|
||||
throw ScriptError(_ERROR_1_("no field with name", name));
|
||||
}
|
||||
Value* value = value_it->get();
|
||||
// set the value
|
||||
@@ -45,8 +46,11 @@ SCRIPT_FUNCTION(new_card) {
|
||||
pvalue->package_name = v->toString();
|
||||
} else if (ColorValue* cvalue = dynamic_cast<ColorValue*>(value)) {
|
||||
cvalue->value = v->toColor();
|
||||
} else if (ImageValue* ivalue = dynamic_cast<ImageValue*>(value)) {
|
||||
wxFileName fname( static_cast<ExternalImage*>(v.get())->toString() );
|
||||
ivalue->filename = LocalFileName::fromReadString( fname.GetName(), "");
|
||||
} else {
|
||||
throw ScriptError(format_string(_("Can not set value '%s', it is not of the right type"),name));
|
||||
throw ScriptError(_ERROR_1_("can't set value", name));
|
||||
}
|
||||
}
|
||||
SCRIPT_RETURN(new_card);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <data/format/formats.hpp>
|
||||
#include <gfx/generated_image.hpp>
|
||||
#include <render/symbol/filter.hpp>
|
||||
#include <cli/text_io_handler.hpp> // for MSE_CLI
|
||||
|
||||
void parse_enum(const String&, ImageCombine& out);
|
||||
|
||||
@@ -44,6 +45,15 @@ SCRIPT_FUNCTION(to_card_image) {
|
||||
}
|
||||
}
|
||||
|
||||
SCRIPT_FUNCTION(import_image) {
|
||||
SCRIPT_PARAM(Set*, set);
|
||||
SCRIPT_PARAM(String, input);
|
||||
auto extImg = make_intrusive<ExternalImage>(input);
|
||||
if (cli.haveConsole()) // makes sure generate() is called, but only once, when using the CLI
|
||||
extImg->generate(GeneratedImage::Options(0, 0, set->stylesheet.get(), set));
|
||||
return extImg;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Image functions
|
||||
|
||||
SCRIPT_FUNCTION(width_of) {
|
||||
@@ -269,4 +279,5 @@ void init_script_image_functions(Context& ctx) {
|
||||
ctx.setVariable(_("drop_shadow"), script_drop_shadow);
|
||||
ctx.setVariable(_("symbol_variation"), script_symbol_variation);
|
||||
ctx.setVariable(_("built_in_image"), script_built_in_image);
|
||||
ctx.setVariable(_("import_image"), script_import_image);
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@ $built_in_functions = array(
|
||||
'rotate' =>'',
|
||||
'drop_shadow' =>'',
|
||||
'symbol_variation' =>'',
|
||||
'import_image' =>'',
|
||||
'built_in_image' =>'',
|
||||
// cards
|
||||
'new_card' =>'',
|
||||
|
||||
Reference in New Issue
Block a user