Added ScriptableImage plus the beginnings of dependency stuff

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@58 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2006-10-29 16:23:31 +00:00
parent 368082ade2
commit f46b0b6b7b
22 changed files with 533 additions and 18 deletions
+141
View File
@@ -0,0 +1,141 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <script/image.hpp>
#include <script/context.hpp>
#include <util/dynamic_arg.hpp>
#include <util/io/package.hpp>
// image generating functions have two modes
// if last_update_age > 0 they return whether the image is still up to date
// if last_update_age == 0 they generate the image
DECLARE_DYNAMIC_ARG (long, last_update_age);
IMPLEMENT_DYNAMIC_ARG(long, last_update_age, 0);
IMPLEMENT_DYNAMIC_ARG(Package*, load_images_from, nullptr);
// ----------------------------------------------------------------------------- : ScriptImage
ScriptType ScriptImage::type() const { return SCRIPT_IMAGE; }
String ScriptImage::typeName() const { return _("image"); }
// ----------------------------------------------------------------------------- : Utility
/// Convert a script value to an image
ScriptImageP to_script_image(const ScriptValueP& value) {
if (ScriptImageP img = dynamic_pointer_cast<ScriptImage>(value)) {
return img; // already an image
} else if (value->type() == SCRIPT_STRING) {
// open a file
String filename = *value;
Package* pkg = load_images_from();
if (!pkg) throw ScriptError(_("Can only load images in a context where an image is expected"));
InputStreamP file = pkg->openIn(filename);
ScriptImageP img = new_intrusive<ScriptImage>();
if (img->image.LoadFile(*file)) {
if (img->image.HasMask()) img->image.InitAlpha(); // we can't handle masks
return img;
} else {
throw ScriptError(_("Unable to load image '") + filename + _("' from '" + pkg->name() + _("'")));
}
} else {
throw ScriptError(_("Can not convert from '") + value->typeName() + _("' to image"));
}
}
/// Is the given image up to date?
bool script_image_up_to_date(const ScriptValueP& value) {
if (value->type() == SCRIPT_INT) {
return (int)*value; // boolean up-to-dateness from parameter
} else {
return true;
}
}
// ----------------------------------------------------------------------------- : ScriptableImage
ScriptImageP ScriptableImage::update(Context& ctx, UInt width, UInt height, PreserveAspect preserve_aspect, bool saturate) {
// up to date?
if (!cache || (UInt)cache->image.GetWidth() != width || (UInt)cache->image.GetHeight() == height) {
// cache must be updated
cache = generate(ctx, width, height, preserve_aspect, saturate);
last_update.update();
}
return cache;
}
// ----------------------------------------------------------------------------- : Reflection
// we need some custom io, because the behaviour is different for each of Reader/Writer/GetMember
template <> void Reader::handle(ScriptableImage& s) {
handle(s.script.unparsed);
if (starts_with(s.script.unparsed, _("script:"))) {
s.script.unparsed = s.script.unparsed.substr(7);
s.script.parse(*this);
} else {
// script is a constant function
s.script.script = new_intrusive<Script>();
s.script.script->addInstruction(I_PUSH_CONST, s.script.unparsed);
}
}
template <> void Writer::handle(const ScriptableImage& s) {
handle(s.script.unparsed);
}
template <> void GetDefaultMember::handle(const ScriptableImage& s) {
handle(s.script.unparsed);
}
// ----------------------------------------------------------------------------- : Functions
SCRIPT_FUNCTION(linear_blend) {
if (last_update_age() == 0) {
ScriptImageP image1 = to_script_image(ctx.getVariable(_("image1")));
ScriptImageP image2 = to_script_image(ctx.getVariable(_("image2")));
SCRIPT_PARAM(double, x1); SCRIPT_PARAM(double, y1);
SCRIPT_PARAM(double, x2); SCRIPT_PARAM(double, y2);
linear_blend(image1->image, image2->image, x1, y1, x2, y2);
return image1;
} else {
SCRIPT_RETURN(
script_image_up_to_date(ctx.getVariable(_("image1"))) &&
script_image_up_to_date(ctx.getVariable(_("image2")))
);
}
}
SCRIPT_FUNCTION(masked_blend) {
if (last_update_age() == 0) {
ScriptImageP light = to_script_image(ctx.getVariable(_("light")));
ScriptImageP dark = to_script_image(ctx.getVariable(_("dark")));
ScriptImageP mask = to_script_image(ctx.getVariable(_("mask")));
mask_blend(light->image, dark->image, mask->image);
return light;
} else {
SCRIPT_RETURN(
script_image_up_to_date(ctx.getVariable(_("light"))) &&
script_image_up_to_date(ctx.getVariable(_("dark" ))) &&
script_image_up_to_date(ctx.getVariable(_("mask" )))
);
}
}
SCRIPT_FUNCTION(set_mask) {
if (last_update_age() == 0) {
ScriptImageP image = to_script_image(ctx.getVariable(_("image")));
ScriptImageP mask = to_script_image(ctx.getVariable(_("mask")));
set_alpha(image->image, mask->image);
return image;
} else {
SCRIPT_RETURN(
script_image_up_to_date(ctx.getVariable(_("image"))) &&
script_image_up_to_date(ctx.getVariable(_("mask")))
);
}
}
+78
View File
@@ -0,0 +1,78 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_SCRIPT_IMAGE
#define HEADER_SCRIPT_IMAGE
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/age.hpp>
#include <script/scriptable.hpp>
#include <gfx/gfx.hpp>
#include <util/dynamic_arg.hpp>
class Package;
DECLARE_INTRUSIVE_POINTER_TYPE(ScriptImage);
// ----------------------------------------------------------------------------- : ScriptableImage
DECLARE_DYNAMIC_ARG(Package*, load_images_from);
/// An image, returned by a script function
class ScriptImage : public ScriptValue {
public:
inline ScriptImage() : combine(COMBINE_NORMAL) {}
inline ScriptImage(const Image& image, ImageCombine combine = COMBINE_NORMAL)
: image(image), combine(combine)
{}
Image image; ///< The image
ImageCombine combine; ///< How to combine the image with the background
virtual ScriptType type() const;
virtual String typeName() const;
};
/// An image that can also be scripted
/** Differs from Scriptable<Image> in that:
* - A script is always used
* - Age is checked, chached images are used if possible
* - The image can be scaled
*/
class ScriptableImage {
public:
/// Is there an image set?
inline operator bool() const { return script; }
/// Generate an image, doesn't cache, and doesn't scale
ScriptImageP generate(Context& ctx) const;
/// Generate an image, scaling it and optionally saturating it
ScriptImageP generate(Context& ctx, UInt width, UInt height, PreserveAspect preserve_aspect = ASPECT_STRETCH, bool saturate = false) const;
/// Update and return the cached image
/** Only recomputes the image if it is out of date, or the size doesn't match.
* If width==height==0 then doesn't resample.
*/
ScriptImageP update(Context& ctx, UInt width = 0, UInt height = 0, PreserveAspect preserve_aspect = ASPECT_STRETCH, bool saturate = false);
/// Is the cached image up to date?
bool upToDate(Context& ctx, Age age) const;
private:
OptionalScript script; ///< The script, not really optional
ScriptImageP cache; ///< The cached image
Age last_update; ///< Age of last image update of the cached image
DECLARE_REFLECTION();
};
/// Missing for now
inline ScriptValueP toScript(const ScriptableImage&) { return script_nil; }
// ----------------------------------------------------------------------------- : EOF
#endif
+11
View File
@@ -0,0 +1,11 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <script/script_manager.hpp>
// ----------------------------------------------------------------------------- :
+81
View File
@@ -0,0 +1,81 @@
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_SCRIPT_SCRIPT_MANAGER
#define HEADER_SCRIPT_SCRIPT_MANAGER
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <script/context.hpp>
#include <data/set.hpp>
// ----------------------------------------------------------------------------- : Dependency
/// Types of dependencies
enum DependencyType
{ DEP_CARD_FIELD ///< dependency of a script in a "card" field
, DEP_CARDS_FIELD ///< dependency of a script in a "card" field for all cards
, DEP_SET_INFO_FIELD ///< dependency of a script in a "set" field
, DEP_STYLESHEET_FIELD ///< dependency of a script in a "style" property
, DEP_CARD_COPY_DEP ///< copy the dependencies from a card field
, DEP_SET_COPY_DEP ///< copy the dependencies from a set field
, DEP_CHOICE_IMAGE ///< dependency of a generated choice image, index2 gives the index of the choice image
};
/// A 'pointer' to some script that depends on another script
class Dependency {
public:
DependencyType type : 4; ///< Type of the dependent script
UInt index2 : 10; ///< A second index, used for some types
size_t index; ///< index into an IndexMap
};
// ----------------------------------------------------------------------------- : Dependencies of data type members
// ----------------------------------------------------------------------------- : ScriptManager
/// Manager of the script context for a set, keeps scripts up to date
/** Whenever there is an action all necessary scripts are executed.
* Executes both Value scripts and Style scriptables.
*
* The context contains a normal pointer to the set, not a shared_ptr, because the set
* itself owns this object.
*/
class ScriptManager : public ActionListener {
public:
ScriptManager(Set& set);
private:
Context context; ///< Context for evaluating scripts
void initScriptStuff();
void initDependencies();
void initDependencies(const StyleSheetP&);
void initContext(const StyleSheetP&);
// Update all styles for a particular card
void updateStyles(const CardP& card);
/// Updates scripts, starting at some value
/** if the value changes any dependend values are updated as well */
void updateValue(Value* value, const CardP& card);
/// Update all fields of all cards
/** Update all set info fields
* Doesn't update styles
*/
void updateAll();
// Update all values with a specific dependency
void updateAllDependend(const vector<Dependency>& dependendScripts);
protected:
/// Respond to actions by updating scripts
void onAction(const Action&, bool undone);
};
// ----------------------------------------------------------------------------- : EOF
#endif
+1
View File
@@ -35,6 +35,7 @@ enum ScriptType
, SCRIPT_DOUBLE
, SCRIPT_STRING
, SCRIPT_COLOR
, SCRIPT_IMAGE
, SCRIPT_BUILDIN_FUN
, SCRIPT_SCRIPT_FUN
, SCRIPT_OBJECT