mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
New class CachedScriptableMask: like CachedScriptableImage, only containing an AlphaMask instead of an Image/Bitmap.
Use CachedScriptableMask for all masks. TODO: This introduces some duplicate code in ValueViewers that could be fixed by moving mask to the Style base class. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1182 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
+57
-4
@@ -94,7 +94,7 @@ template <> void GetDefaultMember::handle(const ScriptableImage& s) {
|
||||
// ----------------------------------------------------------------------------- : CachedScriptableImage
|
||||
|
||||
void CachedScriptableImage::generateCached(const GeneratedImage::Options& options,
|
||||
Image* mask,
|
||||
CachedScriptableMask* mask,
|
||||
ImageCombine* combine, wxBitmap* bitmap, wxImage* image, RealSize* size) {
|
||||
// ready?
|
||||
if (!isReady()) {
|
||||
@@ -137,13 +137,24 @@ void CachedScriptableImage::generateCached(const GeneratedImage::Options& option
|
||||
}
|
||||
}
|
||||
}
|
||||
// hack: temporarily set angle to 0, do actual rotation after applying mask
|
||||
int a = options.angle;
|
||||
const_cast<GeneratedImage::Options&>(options).angle = 0;
|
||||
// generate
|
||||
cached_i = generate(options);
|
||||
cached_angle = options.angle;
|
||||
const_cast<GeneratedImage::Options&>(options).angle = cached_angle = a;
|
||||
*size = cached_size = RealSize(options.width, options.height);
|
||||
if (mask && mask->Ok()) {
|
||||
if (mask) {
|
||||
// apply mask
|
||||
set_alpha(cached_i, *mask);
|
||||
GeneratedImage::Options mask_opts(options);
|
||||
mask_opts.width = cached_i.GetWidth();
|
||||
mask_opts.height = cached_i.GetHeight();
|
||||
mask_opts.angle = 0;
|
||||
mask->get(mask_opts).setAlpha(cached_i);
|
||||
}
|
||||
if (options.angle != 0) {
|
||||
// hack(pt2) do the actual rotation now
|
||||
cached_i = rotate_image(cached_i, options.angle);
|
||||
}
|
||||
if (*combine <= COMBINE_NORMAL) {
|
||||
*bitmap = cached_b = Bitmap(cached_i);
|
||||
@@ -176,3 +187,45 @@ template <> void Writer::handle(const CachedScriptableImage& s) {
|
||||
template <> void GetDefaultMember::handle(const CachedScriptableImage& s) {
|
||||
handle((const ScriptableImage&)s);
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------- : CachedScriptableMask
|
||||
|
||||
|
||||
bool CachedScriptableMask::update(Context& ctx) {
|
||||
if (script.update(ctx)) {
|
||||
mask.clear();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const AlphaMask& CachedScriptableMask::get(const GeneratedImage::Options& img_options) {
|
||||
if (mask.isLoaded()) {
|
||||
// already loaded?
|
||||
if (img_options.width == 0 && img_options.height == 0) return mask;
|
||||
if (mask.hasSize(wxSize(img_options.width,img_options.height))) return mask;
|
||||
}
|
||||
// load?
|
||||
getNoCache(img_options,mask);
|
||||
return mask;
|
||||
}
|
||||
void CachedScriptableMask::getNoCache(const GeneratedImage::Options& img_options, AlphaMask& other_mask) {
|
||||
if (script.isBlank()) {
|
||||
other_mask.clear();
|
||||
} else {
|
||||
Image image = script.generate(img_options);
|
||||
other_mask.load(image);
|
||||
}
|
||||
}
|
||||
|
||||
template <> void Reader::handle(CachedScriptableMask& i) {
|
||||
handle(i.script);
|
||||
}
|
||||
template <> void Writer::handle(const CachedScriptableMask& i) {
|
||||
handle(i.script);
|
||||
}
|
||||
template <> void GetDefaultMember::handle(const CachedScriptableMask& i) {
|
||||
handle(i.script);
|
||||
}
|
||||
|
||||
+29
-2
@@ -15,6 +15,8 @@
|
||||
#include <script/scriptable.hpp>
|
||||
#include <gfx/generated_image.hpp>
|
||||
|
||||
class CachedScriptableMask;
|
||||
|
||||
// ----------------------------------------------------------------------------- : ScriptableImage
|
||||
|
||||
/// An image that can also be scripted
|
||||
@@ -49,9 +51,10 @@ class ScriptableImage {
|
||||
|
||||
/// Can this be safely generated from another thread?
|
||||
inline bool threadSafe() const { return !value || value->threadSafe(); }
|
||||
|
||||
/// Is this image specific to the set (the local_package)?
|
||||
inline bool local() const { return value && value->local(); }
|
||||
/// Is this image blank?
|
||||
inline bool isBlank() const { return !value || value->isBlank(); }
|
||||
|
||||
/// Get access to the script, be careful
|
||||
inline Script& getMutableScript() { return script.getMutableScript(); }
|
||||
@@ -89,7 +92,7 @@ class CachedScriptableImage : public ScriptableImage {
|
||||
* Optionally, an alpha mask is applied to the image.
|
||||
*/
|
||||
void generateCached(const GeneratedImage::Options& img_options,
|
||||
Image* mask,
|
||||
CachedScriptableMask* mask,
|
||||
ImageCombine* combine, wxBitmap* bitmap, wxImage* image, RealSize* size);
|
||||
|
||||
/// Update the script, returns true if the value has changed
|
||||
@@ -105,5 +108,29 @@ class CachedScriptableImage : public ScriptableImage {
|
||||
int cached_angle;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : CachedScriptableMask
|
||||
|
||||
/// A version of ScriptableImage that caches an AlphaMask
|
||||
class CachedScriptableMask {
|
||||
public:
|
||||
|
||||
/// Update the script, returns true if the value has changed
|
||||
bool update(Context& ctx);
|
||||
|
||||
/// Get the alpha mask; with the given options
|
||||
/** if img_options.width == 0 and the mask is already loaded, just returns it. */
|
||||
const AlphaMask& get(const GeneratedImage::Options& img_options);
|
||||
|
||||
/// Get a mask that is not cached
|
||||
void getNoCache(const GeneratedImage::Options& img_options, AlphaMask& mask);
|
||||
|
||||
private:
|
||||
ScriptableImage script;
|
||||
AlphaMask mask;
|
||||
friend class Reader;
|
||||
friend class Writer;
|
||||
friend class GetDefaultMember;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
|
||||
@@ -277,7 +277,11 @@ class ScriptString : public ScriptValue {
|
||||
return c;
|
||||
}
|
||||
virtual GeneratedImageP toImage(const ScriptValueP&) const {
|
||||
return new_intrusive1<PackagedImage>(value);
|
||||
if (value.empty()) {
|
||||
return new_intrusive<BlankImage>();
|
||||
} else {
|
||||
return new_intrusive1<PackagedImage>(value);
|
||||
}
|
||||
}
|
||||
virtual int itemCount() const { return (int)value.size(); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
|
||||
Reference in New Issue
Block a user