From f025630b8b12cb19ece036092ea2ebb8ee71173e Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 29 Mar 2007 18:27:05 +0000 Subject: [PATCH] Fixed mask for image fields, added mask support to slice window git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@243 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/data/field/choice.cpp | 5 +++-- src/data/field/choice.hpp | 24 ++++++++++++------------ src/data/field/image.cpp | 4 ++++ src/data/field/image.hpp | 2 ++ src/gui/image_slice_window.cpp | 25 +++++++++++++++++++++---- src/gui/image_slice_window.hpp | 6 ++++-- src/gui/value/image.cpp | 15 ++++++++++++++- 7 files changed, 60 insertions(+), 21 deletions(-) diff --git a/src/data/field/choice.cpp b/src/data/field/choice.cpp index 8d2f5e38..e2a942cd 100644 --- a/src/data/field/choice.cpp +++ b/src/data/field/choice.cpp @@ -177,7 +177,8 @@ ChoiceStyle::~ChoiceStyle() { bool ChoiceStyle::update(Context& ctx) { // Don't update the choice images, leave that to invalidate() - return Style::update(ctx); + return Style ::update(ctx) + | mask_filename.update(ctx); } void ChoiceStyle::initDependencies(Context& ctx, const Dependency& dep) const { Style::initDependencies(ctx, dep); @@ -199,7 +200,7 @@ void ChoiceStyle::invalidate() { } void ChoiceStyle::loadMask(Package& pkg) { - if (mask.Ok() || mask_filename.empty()) return; + if (mask.Ok() || mask_filename().empty()) return; // load file InputStreamP image_file = pkg.openIn(mask_filename); mask.LoadFile(*image_file); diff --git a/src/data/field/choice.hpp b/src/data/field/choice.hpp index 779dcbfd..91f5f9b0 100644 --- a/src/data/field/choice.hpp +++ b/src/data/field/choice.hpp @@ -121,18 +121,18 @@ class ChoiceStyle : public Style { DECLARE_STYLE_TYPE(Choice); ~ChoiceStyle(); - ChoicePopupStyle popup_style; ///< Style of popups/menus - ChoiceRenderStyle render_style; ///< Style of rendering - Font font; ///< Font for drawing text (when RENDER_TEXT) - map choice_images; ///< Images for the various choices (when RENDER_IMAGE) - map choice_colors; ///< Colors for the various choices (when color_cardlist) - bool colors_card_list;///< Does this field determine colors of the rows in the card list? - String mask_filename; ///< Filename of an additional mask over the images - ImageCombine combine; ///< Combining mode for drawing the images - Alignment alignment; ///< Alignment of images - Image mask; ///< The actual mask image - wxImageList* thumbnails; ///< Thumbnails for the choices - Age thumbnail_age; ///< Age the thumbnails were generated + ChoicePopupStyle popup_style; ///< Style of popups/menus + ChoiceRenderStyle render_style; ///< Style of rendering + Font font; ///< Font for drawing text (when RENDER_TEXT) + map choice_images; ///< Images for the various choices (when RENDER_IMAGE) + map choice_colors; ///< Colors for the various choices (when color_cardlist) + bool colors_card_list; ///< Does this field determine colors of the rows in the card list? + Scriptable mask_filename; ///< Filename of an additional mask over the images + ImageCombine combine; ///< Combining mode for drawing the images + Alignment alignment; ///< Alignment of images + Image mask; ///< The actual mask image + wxImageList* thumbnails; ///< Thumbnails for the choices + Age thumbnail_age; ///< Age the thumbnails were generated bool invalidated_images; ///< Have the images been invalidated? /// Load the mask image, if it's not already done diff --git a/src/data/field/image.cpp b/src/data/field/image.cpp index 3de71dd8..ee13b56c 100644 --- a/src/data/field/image.cpp +++ b/src/data/field/image.cpp @@ -28,6 +28,10 @@ IMPLEMENT_REFLECTION(ImageStyle) { REFLECT_N("mask", mask_filename); } +bool ImageStyle::update(Context& ctx) { + return Style ::update(ctx) + | mask_filename.update(ctx); +} // ----------------------------------------------------------------------------- : ImageValue diff --git a/src/data/field/image.hpp b/src/data/field/image.hpp index 2f96c1c2..8f960f38 100644 --- a/src/data/field/image.hpp +++ b/src/data/field/image.hpp @@ -39,6 +39,8 @@ class ImageStyle : public Style { Scriptable mask_filename; ///< Filename for a mask image + virtual bool update(Context&); + private: DECLARE_REFLECTION(); }; diff --git a/src/gui/image_slice_window.cpp b/src/gui/image_slice_window.cpp index c4541196..35ad3ba1 100644 --- a/src/gui/image_slice_window.cpp +++ b/src/gui/image_slice_window.cpp @@ -7,7 +7,9 @@ // ----------------------------------------------------------------------------- : Includes #include +#include #include +#include #include #include #include @@ -72,7 +74,7 @@ DEFINE_EVENT_TYPE(EVENT_SLICE_CHANGED); // ----------------------------------------------------------------------------- : ImageSliceWindow -ImageSliceWindow::ImageSliceWindow(Window* parent, const Image& source, const wxSize& target_size) +ImageSliceWindow::ImageSliceWindow(Window* parent, const Image& source, const wxSize& target_size, const AlphaMaskP& mask) : wxDialog(parent,wxID_ANY,_TITLE_("slice image"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE) , slice(source, target_size) { @@ -80,7 +82,7 @@ ImageSliceWindow::ImageSliceWindow(Window* parent, const Image& source, const wx const wxPoint defPos = wxDefaultPosition; const wxSize spinSize(80,-1); selector = new ImageSliceSelector(this, ID_SELECTOR, slice); - preview = new ImageSlicePreview (this, ID_PREVIEW, slice); + preview = new ImageSlicePreview (this, ID_PREVIEW, slice, mask); String sizes[] = { _("&Original Size") , _("Size to &Fit") @@ -331,9 +333,10 @@ END_EVENT_TABLE () // ----------------------------------------------------------------------------- : ImageSlicePreview -ImageSlicePreview::ImageSlicePreview(Window* parent, int id, ImageSlice& slice) +ImageSlicePreview::ImageSlicePreview(Window* parent, int id, ImageSlice& slice, const AlphaMaskP& mask) : wxControl(parent, id) , slice(slice) + , mask(mask) , mouse_down(false) {} @@ -356,7 +359,21 @@ void ImageSlicePreview::onPaint(wxPaintEvent&) { } void ImageSlicePreview::draw(DC& dc) { if (!bitmap.Ok()) { - bitmap = Bitmap(slice.getSlice()); + Image image = slice.getSlice(); + if (mask && mask->size == slice.target_size) { + mask->setAlpha(image); + // create bitmap + bitmap = Bitmap(image.GetWidth(), image.GetHeight()); + wxMemoryDC mdc; mdc.SelectObject(bitmap); + // draw checker pattern behind image + RealRect rect = GetClientSize(); + RotatedDC rdc(mdc, 0, rect, 1, false); + draw_checker(rdc, rect); + rdc.DrawImage(image, RealPoint(0,0)); + mdc.SelectObject(wxNullBitmap); + } else { + bitmap = Bitmap(image); + } } if (bitmap.Ok()) { dc.DrawBitmap(bitmap, 0, 0); diff --git a/src/gui/image_slice_window.hpp b/src/gui/image_slice_window.hpp index cab6b6fc..13fc72fd 100644 --- a/src/gui/image_slice_window.hpp +++ b/src/gui/image_slice_window.hpp @@ -14,6 +14,7 @@ class ImageSlicePreview; class ImageSliceSelector; class wxSpinEvent; +DECLARE_POINTER_TYPE(AlphaMask); // ----------------------------------------------------------------------------- : ImageSlice @@ -50,7 +51,7 @@ class ImageSlice { /// Dialog for selecting a slice of an image class ImageSliceWindow : public wxDialog { public: - ImageSliceWindow(Window* parent, const Image& source, const wxSize& target_size); + ImageSliceWindow(Window* parent, const Image& source, const wxSize& target_size, const AlphaMaskP& target_mask); /// Return the sliced image Image getImage() const; @@ -106,7 +107,7 @@ class ImageSliceWindow : public wxDialog { /// A preview of the sliced image class ImageSlicePreview : public wxControl { public: - ImageSlicePreview(Window* parent, int id, ImageSlice& slice); + ImageSlicePreview(Window* parent, int id, ImageSlice& slice, const AlphaMaskP& mask); /// Notify that the slice was updated void update(); @@ -115,6 +116,7 @@ class ImageSlicePreview : public wxControl { private: Bitmap bitmap; ImageSlice& slice; + AlphaMaskP mask; bool mouse_down; int mouseX, mouseY; ///< starting mouse position diff --git a/src/gui/value/image.cpp b/src/gui/value/image.cpp index 683222c6..35415ba9 100644 --- a/src/gui/value/image.cpp +++ b/src/gui/value/image.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include // ----------------------------------------------------------------------------- : ImageValueEditor @@ -28,8 +29,20 @@ bool ImageValueEditor::onLeftDClick(const RealPoint&, wxMouseEvent&) { void ImageValueEditor::sliceImage(const Image& image) { if (!image.Ok()) return; + // mask? + AlphaMaskP mask; + if (!style().mask_filename().empty()) { + Image mask_image; + InputStreamP image_file = viewer.stylesheet->openIn(style().mask_filename); + if (mask_image.LoadFile(*image_file)) { + Image resampled(style().width, style().height); + resample(mask_image, resampled); + mask = new_shared1(resampled); + } + } // slice - ImageSliceWindow s(wxGetTopLevelParent(&editor()), image, style().getSize()); + ImageSliceWindow s(wxGetTopLevelParent(&editor()), image, style().getSize(), mask); + // clicked ok? if (s.ShowModal() == wxID_OK) { // store the image into the set FileName new_image_file = getSet().newFileName(field().name,_("")); // a new unique name in the package