From a75366234fe6d1ffcb34b75766a099b2eb0b307f Mon Sep 17 00:00:00 2001 From: coppro Date: Sat, 14 Jul 2007 03:16:45 +0000 Subject: [PATCH] Added crop image function. Fixed 'difference' symbol type. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@574 0fc631ac-6414-0410-93d0-97cfa31319b6 --- doc/type/double.txt | 2 +- src/gfx/generated_image.cpp | 23 +++++++++++++++++++---- src/gfx/generated_image.hpp | 17 +++++++++++++++++ src/gui/symbol/window.cpp | 4 ++-- src/render/symbol/viewer.cpp | 2 +- src/script/functions/image.cpp | 10 ++++++++++ 6 files changed, 50 insertions(+), 8 deletions(-) diff --git a/doc/type/double.txt b/doc/type/double.txt index f107c838..ad1fdc5c 100644 --- a/doc/type/double.txt +++ b/doc/type/double.txt @@ -2,7 +2,7 @@ Primitive type: real number Real or floating point numbers are numbers with a decimal point. -Conversio from integer to real numbers happens automatically in scripting. +Conversion from integer to real numbers happens automatically in scripting. --File syntax-- > something: 123 diff --git a/src/gfx/generated_image.cpp b/src/gfx/generated_image.cpp index d6a8706b..f1046eee 100644 --- a/src/gfx/generated_image.cpp +++ b/src/gfx/generated_image.cpp @@ -185,15 +185,15 @@ bool SetCombineImage::operator == (const GeneratedImage& that) const { Image EnlargeImage::generate(const Options& opt) const { // generate 'sub' image Options sub_opt - ( opt.width * (border_size < 0.5 ? 1 - 2 * border_size : 0) - , opt.height * (border_size < 0.5 ? 1 - 2 * border_size : 0) + ( int(opt.width * (border_size < 0.5 ? 1 - 2 * border_size : 0)) + , int(opt.height * (border_size < 0.5 ? 1 - 2 * border_size : 0)) , opt.package , opt.local_package , opt.preserve_aspect); Image img = image->generate(sub_opt); // size of generated image int w = img.GetWidth(), h = img.GetHeight(); // original image size - int dw = w * border_size, dh = h * border_size; // delta + int dw = int(w * border_size), dh = int(h * border_size); // delta int w2 = w + dw + dw, h2 = h + dh + dh; // new image size Image larger(w2,h2); larger.InitAlpha(); @@ -221,6 +221,21 @@ bool EnlargeImage::operator == (const GeneratedImage& that) const { && border_size == that2->border_size; } +// ----------------------------------------------------------------------------- : CropImage + +Image CropImage::generate(const Options& opt) const { + return image->generate(opt).Size(wxSize((int)width, (int)height), wxPoint(-(int)offset_x, -(int)offset_y)); +} +ImageCombine CropImage::combine() const { + return image->combine(); +} +bool CropImage::operator == (const GeneratedImage& that) const { + const CropImage* that2 = dynamic_cast(&that); + return that2 && *image == *that2->image + && width == that2->width && height == that2->height + && offset_x == that2->offset_x && offset_y == that2->offset_y; +} + // ----------------------------------------------------------------------------- : DropShadowImage /// Preform a gaussian blur, from the image in of w*h bytes to out @@ -287,7 +302,7 @@ Image DropShadowImage::generate(const Options& opt) const { UInt total = 255 * gaussian_blur(alpha, shadow, w, h, shadow_blur_radius); // combine Byte* data = img.GetData(); - int dw = w * offset_x, dh = h * offset_y; + int dw = int(w * offset_x), dh = int(h * offset_y); int x_start = max(0, dw), y_start = max(0, dh); int x_end = min(w, w+dw), y_end = min(h, h+dh); int delta = dw + w * dh; diff --git a/src/gfx/generated_image.hpp b/src/gfx/generated_image.hpp index 9a05d2b1..85dcfa4e 100644 --- a/src/gfx/generated_image.hpp +++ b/src/gfx/generated_image.hpp @@ -180,6 +180,23 @@ class EnlargeImage : public GeneratedImage { double border_size; }; +// ----------------------------------------------------------------------------- : CropImage + +/// Crop an image at a certain point, to a certain size +class CropImage : public GeneratedImage { + public: + inline CropImage(const GeneratedImageP& image, double width, double height, double offset_x, double offset_y) + : image(image), width(width), height(height), offset_x(offset_x), offset_y(offset_y) + {} + virtual Image generate(const Options& opt) const; + virtual ImageCombine combine() const; + virtual bool operator == (const GeneratedImage& that) const; + private: + GeneratedImageP image; + double width, height; + double offset_x, offset_y; +}; + // ----------------------------------------------------------------------------- : DropShadowImage /// Add a drop shadow to an image diff --git a/src/gui/symbol/window.cpp b/src/gui/symbol/window.cpp index 6a3eab65..3064330d 100644 --- a/src/gui/symbol/window.cpp +++ b/src/gui/symbol/window.cpp @@ -246,14 +246,14 @@ void SymbolWindow::onFileExit(wxCommandEvent& ev) { void SymbolWindow::onEditUndo(wxCommandEvent& ev) { - if (!control->isEditing()) { + if (!control->isEditing() && control->getSymbol()->actions.canUndo()) { control->getSymbol()->actions.undo(); control->Refresh(false); } } void SymbolWindow::onEditRedo(wxCommandEvent& ev) { - if (!control->isEditing()) { + if (!control->isEditing() && control->getSymbol()->actions.canRedo()) { control->getSymbol()->actions.redo(); control->Refresh(false); } diff --git a/src/render/symbol/viewer.cpp b/src/render/symbol/viewer.cpp index 7357fc8f..30fd1900 100644 --- a/src/render/symbol/viewer.cpp +++ b/src/render/symbol/viewer.cpp @@ -242,7 +242,7 @@ void SymbolViewer::combineSymbolShape(const SymbolShape& shape, DC& border, DC& break; } case SYMBOL_COMBINE_DIFFERENCE: { interior.SetLogicalFunction(wxXOR); - drawSymbolShape(shape, &border, &interior, 0, ~interiorCol, directB, true); + drawSymbolShape(shape, &border, &interior, 0, interiorCol, directB, true); interior.SetLogicalFunction(wxCOPY); break; } case SYMBOL_COMBINE_BORDER: { diff --git a/src/script/functions/image.cpp b/src/script/functions/image.cpp index ae02b1ba..799f29f7 100644 --- a/src/script/functions/image.cpp +++ b/src/script/functions/image.cpp @@ -84,6 +84,15 @@ SCRIPT_FUNCTION(enlarge) { return new_intrusive2(input, border_size); } +SCRIPT_FUNCTION(crop) { + SCRIPT_PARAM(GeneratedImageP, input); + SCRIPT_PARAM_N(int, _("width"), width); + SCRIPT_PARAM_N(int, _("height"), height); + SCRIPT_PARAM_N(double, _("offset x"), offset_x); + SCRIPT_PARAM_N(double, _("offset y"), offset_y); + return new_intrusive5(input, width, height, offset_x, offset_y); +} + SCRIPT_FUNCTION(drop_shadow) { SCRIPT_PARAM(GeneratedImageP, input); SCRIPT_OPTIONAL_PARAM_N_(double, _("offset x"), offset_x); @@ -161,6 +170,7 @@ void init_script_image_functions(Context& ctx) { ctx.setVariable(_("set alpha"), script_set_alpha); ctx.setVariable(_("set combine"), script_set_combine); ctx.setVariable(_("enlarge"), script_enlarge); + ctx.setVariable(_("crop"), script_crop); ctx.setVariable(_("drop shadow"), script_drop_shadow); ctx.setVariable(_("symbol variation"), script_symbol_variation); ctx.setVariable(_("built in image"), script_built_in_image);