diff options
Diffstat (limited to 'src/sp-fediffuselighting.cpp')
| -rw-r--r-- | src/sp-fediffuselighting.cpp | 185 |
1 files changed, 175 insertions, 10 deletions
diff --git a/src/sp-fediffuselighting.cpp b/src/sp-fediffuselighting.cpp index d48c02c96..543c16e8b 100644 --- a/src/sp-fediffuselighting.cpp +++ b/src/sp-fediffuselighting.cpp @@ -7,8 +7,10 @@ /* * Authors: * hugo Rodrigues <haa.rodrigues@gmail.com> + * Jean-Rene Reinhard <jr@komite.net> * * Copyright (C) 2006 Hugo Rodrigues + * 2007 authors * * Released under GNU GPL, read the file 'COPYING' for more information */ @@ -19,9 +21,11 @@ #include "attributes.h" #include "svg/svg.h" +#include "sp-object.h" +#include "svg/svg-color.h" #include "sp-fediffuselighting.h" #include "xml/repr.h" - +#include "display/nr-filter-diffuselighting.h" /* FeDiffuseLighting base class */ @@ -32,7 +36,18 @@ static void sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, I static void sp_feDiffuseLighting_release(SPObject *object); static void sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value); static void sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags); +//we assume that svg:feDiffuseLighting can have any number of children +//only the first one is considered as the light source of the filter +//TODO is that right? +//if not modify child_added and remove_child to raise errors +static void sp_feDiffuseLighting_child_added(SPObject *object, + Inkscape::XML::Node *child, + Inkscape::XML::Node *ref); +static void sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child); +static void sp_feDiffuseLighting_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref); static Inkscape::XML::Node *sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags); +static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter); +static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting); static SPFilterPrimitiveClass *feDiffuseLighting_parent_class; @@ -61,7 +76,7 @@ static void sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass) { SPObjectClass *sp_object_class = (SPObjectClass *)klass; - + SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass; feDiffuseLighting_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass); sp_object_class->build = sp_feDiffuseLighting_build; @@ -69,11 +84,21 @@ sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass) sp_object_class->write = sp_feDiffuseLighting_write; sp_object_class->set = sp_feDiffuseLighting_set; sp_object_class->update = sp_feDiffuseLighting_update; + sp_object_class->child_added = sp_feDiffuseLighting_child_added; + sp_object_class->remove_child = sp_feDiffuseLighting_remove_child; + sp_object_class->order_changed = sp_feDiffuseLighting_order_changed; + + sp_primitive_class->build_renderer = sp_feDiffuseLighting_build_renderer; } static void sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting) { + feDiffuseLighting->surfaceScale = 1; + feDiffuseLighting->diffuseConstant = 1; + feDiffuseLighting->lighting_color = 0xffffffff; + //TODO kernelUnit + feDiffuseLighting->renderer = NULL; } /** @@ -89,6 +114,11 @@ sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML } /*LOAD ATTRIBUTES FROM REPR HERE*/ + sp_object_read_attr(object, "surfaceScale"); + sp_object_read_attr(object, "diffuseConstant"); + sp_object_read_attr(object, "kernelUnitLength"); + sp_object_read_attr(object, "lighting-color"); + } /** @@ -108,10 +138,40 @@ static void sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value) { SPFeDiffuseLighting *feDiffuseLighting = SP_FEDIFFUSELIGHTING(object); - (void)feDiffuseLighting; - + switch(key) { /*DEAL WITH SETTING ATTRIBUTES HERE*/ +//TODO test forbidden values + case SP_ATTR_SURFACESCALE: + feDiffuseLighting->surfaceScale = g_ascii_strtod(value, NULL); + if (feDiffuseLighting->renderer) { + feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->surfaceScale; + } + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); + break; + case SP_ATTR_DIFFUSECONSTANT: + feDiffuseLighting->diffuseConstant = g_ascii_strtod(value, NULL); + if (feDiffuseLighting->renderer) { + feDiffuseLighting->renderer->diffuseConstant = feDiffuseLighting->diffuseConstant; + } + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); + break; + case SP_ATTR_KERNELUNITLENGTH: + //TODO kernelUnit + //feDiffuseLighting->kernelUnitLength.set(value); + /*TODOif (feDiffuseLighting->renderer) { + feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->renderer; + } + */ + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); + break; + case SP_PROP_LIGHTING_COLOR: + feDiffuseLighting->lighting_color = sp_svg_read_color(value, 0xffffffff); + if (feDiffuseLighting->renderer) { + feDiffuseLighting->renderer->lighting_color = feDiffuseLighting->lighting_color; + } + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); + break; default: if (((SPObjectClass *) feDiffuseLighting_parent_class)->set) ((SPObjectClass *) feDiffuseLighting_parent_class)->set(object, key, value); @@ -126,11 +186,11 @@ sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value) static void sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags) { - if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | - SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) { - - /* do something to trigger redisplay, updates? */ - + if (flags & (SP_OBJECT_MODIFIED_FLAG)) { + sp_object_read_attr(object, "surfaceScale"); + sp_object_read_attr(object, "diffuseConstant"); + sp_object_read_attr(object, "kernelUnit"); + sp_object_read_attr(object, "lighting-color"); } if (((SPObjectClass *) feDiffuseLighting_parent_class)->update) { @@ -144,16 +204,24 @@ sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags) static Inkscape::XML::Node * sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags) { + SPFeDiffuseLighting *fediffuselighting = SP_FEDIFFUSELIGHTING(object); + // Inkscape-only object, not copied during an "plain SVG" dump: if (flags & SP_OBJECT_WRITE_EXT) { if (repr) { // is this sane? - repr->mergeFrom(SP_OBJECT_REPR(object), "id"); + //repr->mergeFrom(SP_OBJECT_REPR(object), "id"); } else { repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME } } + sp_repr_set_css_double(repr, "surfaceScale", fediffuselighting->surfaceScale); + sp_repr_set_css_double(repr, "diffuseConstant", fediffuselighting->diffuseConstant); + /*TODO kernelUnits */ + gchar c[64]; + sp_svg_write_color(c, 64, fediffuselighting->lighting_color); + repr->setAttribute("lighting-color", c); if (((SPObjectClass *) feDiffuseLighting_parent_class)->write) { ((SPObjectClass *) feDiffuseLighting_parent_class)->write(object, repr, flags); } @@ -161,6 +229,103 @@ sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint fl return repr; } +/** + * Callback for child_added event. + */ +static void +sp_feDiffuseLighting_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref) +{ + SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object); + + if (((SPObjectClass *) feDiffuseLighting_parent_class)->child_added) + (* ((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)(object, child, ref); + + sp_feDiffuseLighting_children_modified(f); + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); +} + + +/** + * Callback for remove_child event. + */ +static void +sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child) +{ + SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object); + + if (((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child) + (* ((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)(object, child); + + sp_feDiffuseLighting_children_modified(f); + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); +} + +static void +sp_feDiffuseLighting_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref) +{ + SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object); + if (((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed) + (* ((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed) (object, child, old_ref, new_ref); + + sp_feDiffuseLighting_children_modified(f); + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); +} + +static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting) +{ + if (sp_diffuselighting->renderer) { + sp_diffuselighting->renderer->light_type = NR::NO_LIGHT; + if (SP_IS_FEDISTANTLIGHT(sp_diffuselighting->children)) { + sp_diffuselighting->renderer->light_type = NR::DISTANT_LIGHT; + sp_diffuselighting->renderer->light.distant = SP_FEDISTANTLIGHT(sp_diffuselighting->children); + } + if (SP_IS_FEPOINTLIGHT(sp_diffuselighting->children)) { + sp_diffuselighting->renderer->light_type = NR::POINT_LIGHT; + sp_diffuselighting->renderer->light.point = SP_FEPOINTLIGHT(sp_diffuselighting->children); + } + if (SP_IS_FESPOTLIGHT(sp_diffuselighting->children)) { + sp_diffuselighting->renderer->light_type = NR::SPOT_LIGHT; + sp_diffuselighting->renderer->light.spot = SP_FESPOTLIGHT(sp_diffuselighting->children); + } + } +} + +static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) { + g_assert(primitive != NULL); + g_assert(filter != NULL); + + SPFeDiffuseLighting *sp_diffuselighting = SP_FEDIFFUSELIGHTING(primitive); + + int primitive_n = filter->add_primitive(NR::NR_FILTER_DIFFUSELIGHTING); + NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n); + NR::FilterDiffuseLighting *nr_diffuselighting = dynamic_cast<NR::FilterDiffuseLighting*>(nr_primitive); + g_assert(nr_diffuselighting != NULL); + + sp_diffuselighting->renderer = nr_diffuselighting; + sp_filter_primitive_renderer_common(primitive, nr_primitive); + + nr_diffuselighting->diffuseConstant = sp_diffuselighting->diffuseConstant; + nr_diffuselighting->surfaceScale = sp_diffuselighting->surfaceScale; + nr_diffuselighting->lighting_color = sp_diffuselighting->lighting_color; + //We assume there is at most one child + nr_diffuselighting->light_type = NR::NO_LIGHT; + if (SP_IS_FEDISTANTLIGHT(primitive->children)) { + nr_diffuselighting->light_type = NR::DISTANT_LIGHT; + nr_diffuselighting->light.distant = SP_FEDISTANTLIGHT(primitive->children); + } + if (SP_IS_FEPOINTLIGHT(primitive->children)) { + nr_diffuselighting->light_type = NR::POINT_LIGHT; + nr_diffuselighting->light.point = SP_FEPOINTLIGHT(primitive->children); + } + if (SP_IS_FESPOTLIGHT(primitive->children)) { + nr_diffuselighting->light_type = NR::SPOT_LIGHT; + nr_diffuselighting->light.spot = SP_FESPOTLIGHT(primitive->children); + } + + //nr_offset->set_dx(sp_offset->dx); + //nr_offset->set_dy(sp_offset->dy); +} + /* Local Variables: |
