Support for masks for color fields

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@325 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2007-05-06 18:15:18 +00:00
parent 6625792b04
commit efa1992f4a
7 changed files with 95 additions and 37 deletions
+56 -18
View File
@@ -7,6 +7,8 @@
// ----------------------------------------------------------------------------- : Includes
#include <render/value/color.hpp>
#include <render/card/viewer.hpp>
#include <data/stylesheet.hpp>
DECLARE_TYPEOF_COLLECTION(ColorField::ChoiceP);
@@ -38,22 +40,31 @@ void ColorValueViewer::draw(RotatedDC& dc) {
dc.DrawRectangle(style().getRect().move(40, 0, -40, 0));
dc.DrawText(color_name, style().getPos() + RealSize(43, 3));
} else {
// do we need clipping?
bool clip = style().left_width < style().width && style().right_width < style().width &&
style().top_width < style().height && style().bottom_width < style().height;
if (clip) {
// clip away the inside of the rectangle
wxRegion r = dc.tr(style().getRect()).toRect();
r.Subtract(dc.tr(RealRect(
style().left + style().left_width,
style().top + style().top_width,
style().width - style().left_width - style().right_width,
style().height - style().top_width - style().bottom_width
)));
dc.getDC().SetClippingRegion(r);
// is there a mask?
loadMask(dc);
if (alpha_mask) {
Image img(alpha_mask->size.x, alpha_mask->size.y);
fill_image(img, value().value());
alpha_mask->setAlpha(img);
dc.DrawImage(img, style().getPos());
} else {
// do we need clipping?
bool clip = style().left_width < style().width && style().right_width < style().width &&
style().top_width < style().height && style().bottom_width < style().height;
if (clip) {
// clip away the inside of the rectangle
wxRegion r = dc.tr(style().getRect()).toRect();
r.Subtract(dc.tr(RealRect(
style().left + style().left_width,
style().top + style().top_width,
style().width - style().left_width - style().right_width,
style().height - style().top_width - style().bottom_width
)));
dc.getDC().SetClippingRegion(r);
}
dc.DrawRoundedRectangle(style().getRect(), style().radius);
if (clip) dc.getDC().DestroyClippingRegion();
}
dc.DrawRoundedRectangle(style().getRect(), style().radius);
if (clip) dc.getDC().DestroyClippingRegion();
}
}
@@ -61,7 +72,34 @@ bool ColorValueViewer::containsPoint(const RealPoint& p) const {
// distance to each side
double left = p.x - style().left, right = style().left + style().width - p.x - 1;
double top = p.y - style().top, bottom = style().top + style().height - p.y - 1;
return left >= 0 && right >= 0 && top >= 0 && bottom >= 0 && // inside bounding box
(left < style().left_width || right < style().right_width || // inside horizontal border
top < style().top_width || bottom < style().bottom_width); // inside vertical border
if (left < 0 || right < 0 || top < 0 || bottom < 0 || // outside bounding box
(left >= style().left_width && right >= style().right_width && // outside horizontal border
top >= style().top_width && bottom >= style().bottom_width)) { // outside vertical border
return false;
}
// check against mask
if (!style().mask_filename().empty()) {
loadMask(viewer.getRotation());
return !alpha_mask || !alpha_mask->isTransparent((int)left, (int)top);
} else {
return true;
}
}
void ColorValueViewer::onStyleChange() {
alpha_mask = AlphaMaskP();
}
void ColorValueViewer::loadMask(const Rotation& rot) const {
if (style().mask_filename().empty()) return; // no mask
int w = (int) rot.trS(style().width), h = (int) rot.trS(style().height);
if (alpha_mask && alpha_mask->size == wxSize(w,h)) return; // mask loaded and right size
// (re) load the mask
Image image;
InputStreamP image_file = viewer.stylesheet->openIn(style().mask_filename);
if (image.LoadFile(*image_file)) {
Image resampled(w,h);
resample(image, resampled);
alpha_mask = new_shared1<AlphaMask>(resampled);
}
}
+8
View File
@@ -13,6 +13,8 @@
#include <render/value/viewer.hpp>
#include <data/field/color.hpp>
DECLARE_POINTER_TYPE(AlphaMask);
// ----------------------------------------------------------------------------- : ColorValueViewer
/// Viewer that displays a color value
@@ -22,6 +24,12 @@ class ColorValueViewer : public ValueViewer {
virtual void draw(RotatedDC& dc);
virtual bool containsPoint(const RealPoint& p) const;
virtual void onStyleChange();
private:
mutable AlphaMaskP alpha_mask;
void loadMask(const Rotation& rot) const;
};
// ----------------------------------------------------------------------------- : EOF