X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fsp-fediffuselighting.cpp;h=96dee569b80c3dc716100109ad21b4f67e1c2a1c;hb=8d358698ecbf192ba7c6dc05d4f7de7592753d9f;hp=d48c02c96d0978014c42a7a8cd5a7101f24a9649;hpb=1ed1764f573921b7f9d210a3fcca322234c36480;p=inkscape.git diff --git a/src/sp-fediffuselighting.cpp b/src/sp-fediffuselighting.cpp index d48c02c96..96dee569b 100644 --- a/src/sp-fediffuselighting.cpp +++ b/src/sp-fediffuselighting.cpp @@ -7,8 +7,10 @@ /* * Authors: * hugo Rodrigues + * Jean-Rene Reinhard * * 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); -static Inkscape::XML::Node *sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, 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::Document *doc, 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,25 @@ 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; + + feDiffuseLighting->surfaceScale_set = FALSE; + feDiffuseLighting->diffuseConstant_set = FALSE; + feDiffuseLighting->lighting_color_set = FALSE; } /** @@ -89,6 +118,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 +142,73 @@ static void sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value) { SPFeDiffuseLighting *feDiffuseLighting = SP_FEDIFFUSELIGHTING(object); - (void)feDiffuseLighting; - + gchar const *cend_ptr = NULL; + gchar *end_ptr = NULL; + switch(key) { /*DEAL WITH SETTING ATTRIBUTES HERE*/ +//TODO test forbidden values + case SP_ATTR_SURFACESCALE: + end_ptr = NULL; + if (value) { + feDiffuseLighting->surfaceScale = g_ascii_strtod(value, &end_ptr); + if (end_ptr) { + feDiffuseLighting->surfaceScale_set = TRUE; + } + } + if (!value || !end_ptr) { + feDiffuseLighting->surfaceScale = 1; + feDiffuseLighting->surfaceScale_set = FALSE; + } + if (feDiffuseLighting->renderer) { + feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->surfaceScale; + } + object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); + break; + case SP_ATTR_DIFFUSECONSTANT: + end_ptr = NULL; + if (value) { + feDiffuseLighting->diffuseConstant = g_ascii_strtod(value, &end_ptr); + if (end_ptr && feDiffuseLighting->diffuseConstant >= 0) { + feDiffuseLighting->diffuseConstant_set = TRUE; + } else { + end_ptr = NULL; + g_warning("feDiffuseLighting: diffuseConstant should be a positive number ... defaulting to 1"); + } + } + if (!value || !end_ptr) { + feDiffuseLighting->diffuseConstant = 1; + feDiffuseLighting->diffuseConstant_set = FALSE; + } + 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: + cend_ptr = NULL; + feDiffuseLighting->lighting_color = sp_svg_read_color(value, &cend_ptr, 0xffffffff); + //if a value was read + if (cend_ptr) { + feDiffuseLighting->lighting_color_set = TRUE; + } else { + //lighting_color already contains the default value + feDiffuseLighting->lighting_color_set = FALSE; + } + 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 +223,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) { @@ -142,25 +239,140 @@ sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags) * Writes its settings to an incoming repr object, if any. */ static Inkscape::XML::Node * -sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags) +sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Document *doc, 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 + repr = SP_OBJECT_REPR(object)->duplicate(doc); } } - + + if (fediffuselighting->surfaceScale_set) + sp_repr_set_css_double(repr, "surfaceScale", fediffuselighting->surfaceScale); + else + repr->setAttribute("surfaceScale", NULL); + if (fediffuselighting->diffuseConstant_set) + sp_repr_set_css_double(repr, "diffuseConstant", fediffuselighting->diffuseConstant); + else + repr->setAttribute("diffuseConstant", NULL); + /*TODO kernelUnits */ + if (fediffuselighting->lighting_color_set) { + gchar c[64]; + sp_svg_write_color(c, sizeof(c), fediffuselighting->lighting_color); + repr->setAttribute("lighting-color", c); + } else + repr->setAttribute("lighting-color", NULL); + if (((SPObjectClass *) feDiffuseLighting_parent_class)->write) { - ((SPObjectClass *) feDiffuseLighting_parent_class)->write(object, repr, flags); + ((SPObjectClass *) feDiffuseLighting_parent_class)->write(object, doc, repr, flags); } 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_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: