Code

543c16e8b03834536dabee69118a449baaefad58
[inkscape.git] / src / sp-fediffuselighting.cpp
1 #define __SP_FEDIFFUSELIGHTING_CPP__
3 /** \file
4  * SVG <feDiffuseLighting> implementation.
5  *
6  */
7 /*
8  * Authors:
9  *   hugo Rodrigues <haa.rodrigues@gmail.com>
10  *   Jean-Rene Reinhard <jr@komite.net>
11  *
12  * Copyright (C) 2006 Hugo Rodrigues
13  *               2007 authors
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include "attributes.h"
23 #include "svg/svg.h"
24 #include "sp-object.h"
25 #include "svg/svg-color.h"
26 #include "sp-fediffuselighting.h"
27 #include "xml/repr.h"
28 #include "display/nr-filter-diffuselighting.h"
30 /* FeDiffuseLighting base class */
32 static void sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass);
33 static void sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting);
35 static void sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
36 static void sp_feDiffuseLighting_release(SPObject *object);
37 static void sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value);
38 static void sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags);
39 //we assume that svg:feDiffuseLighting can have any number of children
40 //only the first one is considered as the light source of the filter
41 //TODO is that right?
42 //if not modify child_added and remove_child to raise errors
43 static void sp_feDiffuseLighting_child_added(SPObject *object,
44                                     Inkscape::XML::Node *child,
45                                     Inkscape::XML::Node *ref);
46 static void sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child);
47 static void sp_feDiffuseLighting_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
48 static Inkscape::XML::Node *sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
49 static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
50 static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting);
52 static SPFilterPrimitiveClass *feDiffuseLighting_parent_class;
54 GType
55 sp_feDiffuseLighting_get_type()
56 {
57     static GType feDiffuseLighting_type = 0;
59     if (!feDiffuseLighting_type) {
60         GTypeInfo feDiffuseLighting_info = {
61             sizeof(SPFeDiffuseLightingClass),
62             NULL, NULL,
63             (GClassInitFunc) sp_feDiffuseLighting_class_init,
64             NULL, NULL,
65             sizeof(SPFeDiffuseLighting),
66             16,
67             (GInstanceInitFunc) sp_feDiffuseLighting_init,
68             NULL,    /* value_table */
69         };
70         feDiffuseLighting_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeDiffuseLighting", &feDiffuseLighting_info, (GTypeFlags)0);
71     }
72     return feDiffuseLighting_type;
73 }
75 static void
76 sp_feDiffuseLighting_class_init(SPFeDiffuseLightingClass *klass)
77 {
78     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
79     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
80     feDiffuseLighting_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
82     sp_object_class->build = sp_feDiffuseLighting_build;
83     sp_object_class->release = sp_feDiffuseLighting_release;
84     sp_object_class->write = sp_feDiffuseLighting_write;
85     sp_object_class->set = sp_feDiffuseLighting_set;
86     sp_object_class->update = sp_feDiffuseLighting_update;
87     sp_object_class->child_added = sp_feDiffuseLighting_child_added;
88     sp_object_class->remove_child = sp_feDiffuseLighting_remove_child;
89     sp_object_class->order_changed = sp_feDiffuseLighting_order_changed;
91     sp_primitive_class->build_renderer = sp_feDiffuseLighting_build_renderer;
92 }
94 static void
95 sp_feDiffuseLighting_init(SPFeDiffuseLighting *feDiffuseLighting)
96 {
97     feDiffuseLighting->surfaceScale = 1;
98     feDiffuseLighting->diffuseConstant = 1;
99     feDiffuseLighting->lighting_color = 0xffffffff;
100     //TODO kernelUnit
101     feDiffuseLighting->renderer = NULL;
104 /**
105  * Reads the Inkscape::XML::Node, and initializes SPFeDiffuseLighting variables.  For this to get called,
106  * our name must be associated with a repr via "sp_object_type_register".  Best done through
107  * sp-object-repr.cpp's repr_name_entries array.
108  */
109 static void
110 sp_feDiffuseLighting_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
112     if (((SPObjectClass *) feDiffuseLighting_parent_class)->build) {
113         ((SPObjectClass *) feDiffuseLighting_parent_class)->build(object, document, repr);
114     }
116     /*LOAD ATTRIBUTES FROM REPR HERE*/
117     sp_object_read_attr(object, "surfaceScale");
118     sp_object_read_attr(object, "diffuseConstant");
119     sp_object_read_attr(object, "kernelUnitLength");
120     sp_object_read_attr(object, "lighting-color");
121     
124 /**
125  * Drops any allocated memory.
126  */
127 static void
128 sp_feDiffuseLighting_release(SPObject *object)
130     if (((SPObjectClass *) feDiffuseLighting_parent_class)->release)
131         ((SPObjectClass *) feDiffuseLighting_parent_class)->release(object);
134 /**
135  * Sets a specific value in the SPFeDiffuseLighting.
136  */
137 static void
138 sp_feDiffuseLighting_set(SPObject *object, unsigned int key, gchar const *value)
140     SPFeDiffuseLighting *feDiffuseLighting = SP_FEDIFFUSELIGHTING(object);
141     
142     switch(key) {
143         /*DEAL WITH SETTING ATTRIBUTES HERE*/
144 //TODO test forbidden values
145         case SP_ATTR_SURFACESCALE:
146             feDiffuseLighting->surfaceScale = g_ascii_strtod(value, NULL);
147             if (feDiffuseLighting->renderer) {
148                 feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->surfaceScale;
149             }
150             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
151             break;
152         case SP_ATTR_DIFFUSECONSTANT:
153             feDiffuseLighting->diffuseConstant = g_ascii_strtod(value, NULL);
154             if (feDiffuseLighting->renderer) {
155                 feDiffuseLighting->renderer->diffuseConstant = feDiffuseLighting->diffuseConstant;
156             }
157             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
158             break;
159         case SP_ATTR_KERNELUNITLENGTH:
160             //TODO kernelUnit
161             //feDiffuseLighting->kernelUnitLength.set(value);
162             /*TODOif (feDiffuseLighting->renderer) {
163                 feDiffuseLighting->renderer->surfaceScale = feDiffuseLighting->renderer;
164             }
165             */
166             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
167             break;
168         case SP_PROP_LIGHTING_COLOR:
169             feDiffuseLighting->lighting_color = sp_svg_read_color(value, 0xffffffff);
170             if (feDiffuseLighting->renderer) {
171                 feDiffuseLighting->renderer->lighting_color = feDiffuseLighting->lighting_color;
172             }
173             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
174             break;
175         default:
176             if (((SPObjectClass *) feDiffuseLighting_parent_class)->set)
177                 ((SPObjectClass *) feDiffuseLighting_parent_class)->set(object, key, value);
178             break;
179     }
183 /**
184  * Receives update notifications.
185  */
186 static void
187 sp_feDiffuseLighting_update(SPObject *object, SPCtx *ctx, guint flags)
189     if (flags & (SP_OBJECT_MODIFIED_FLAG)) {
190         sp_object_read_attr(object, "surfaceScale");
191         sp_object_read_attr(object, "diffuseConstant");
192         sp_object_read_attr(object, "kernelUnit");
193         sp_object_read_attr(object, "lighting-color");
194     }
196     if (((SPObjectClass *) feDiffuseLighting_parent_class)->update) {
197         ((SPObjectClass *) feDiffuseLighting_parent_class)->update(object, ctx, flags);
198     }
201 /**
202  * Writes its settings to an incoming repr object, if any.
203  */
204 static Inkscape::XML::Node *
205 sp_feDiffuseLighting_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
207     SPFeDiffuseLighting *fediffuselighting = SP_FEDIFFUSELIGHTING(object);
208     
209     // Inkscape-only object, not copied during an "plain SVG" dump:
210     if (flags & SP_OBJECT_WRITE_EXT) {
211         if (repr) {
212             // is this sane?
213             //repr->mergeFrom(SP_OBJECT_REPR(object), "id");
214         } else {
215             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
216         }
217     }
219     sp_repr_set_css_double(repr, "surfaceScale", fediffuselighting->surfaceScale);
220     sp_repr_set_css_double(repr, "diffuseConstant", fediffuselighting->diffuseConstant);
221    /*TODO kernelUnits */ 
222     gchar c[64];
223     sp_svg_write_color(c, 64, fediffuselighting->lighting_color);
224     repr->setAttribute("lighting-color", c);
225     if (((SPObjectClass *) feDiffuseLighting_parent_class)->write) {
226         ((SPObjectClass *) feDiffuseLighting_parent_class)->write(object, repr, flags);
227     }
229     return repr;
232 /**
233  * Callback for child_added event.
234  */
235 static void
236 sp_feDiffuseLighting_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
238     SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
240     if (((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)
241         (* ((SPObjectClass *) feDiffuseLighting_parent_class)->child_added)(object, child, ref);
243     sp_feDiffuseLighting_children_modified(f);
244     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
246             
248 /**
249  * Callback for remove_child event.
250  */
251 static void
252 sp_feDiffuseLighting_remove_child(SPObject *object, Inkscape::XML::Node *child)
253 {   
254     SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
256     if (((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)
257         (* ((SPObjectClass *) feDiffuseLighting_parent_class)->remove_child)(object, child);   
259     sp_feDiffuseLighting_children_modified(f);
260     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
263 static void
264 sp_feDiffuseLighting_order_changed (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref)
266     SPFeDiffuseLighting *f = SP_FEDIFFUSELIGHTING(object);
267     if (((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed)
268         (* ((SPObjectClass *) (feDiffuseLighting_parent_class))->order_changed) (object, child, old_ref, new_ref);
270     sp_feDiffuseLighting_children_modified(f);
271     object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
274 static void sp_feDiffuseLighting_children_modified(SPFeDiffuseLighting *sp_diffuselighting)
276    if (sp_diffuselighting->renderer) {
277         sp_diffuselighting->renderer->light_type = NR::NO_LIGHT;
278         if (SP_IS_FEDISTANTLIGHT(sp_diffuselighting->children)) {
279             sp_diffuselighting->renderer->light_type = NR::DISTANT_LIGHT;
280             sp_diffuselighting->renderer->light.distant = SP_FEDISTANTLIGHT(sp_diffuselighting->children);
281         }
282         if (SP_IS_FEPOINTLIGHT(sp_diffuselighting->children)) {
283             sp_diffuselighting->renderer->light_type = NR::POINT_LIGHT;
284             sp_diffuselighting->renderer->light.point = SP_FEPOINTLIGHT(sp_diffuselighting->children);
285         }
286         if (SP_IS_FESPOTLIGHT(sp_diffuselighting->children)) {
287             sp_diffuselighting->renderer->light_type = NR::SPOT_LIGHT;
288             sp_diffuselighting->renderer->light.spot = SP_FESPOTLIGHT(sp_diffuselighting->children);
289         }
290    }
293 static void sp_feDiffuseLighting_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
294     g_assert(primitive != NULL);
295     g_assert(filter != NULL);
297     SPFeDiffuseLighting *sp_diffuselighting = SP_FEDIFFUSELIGHTING(primitive);
299     int primitive_n = filter->add_primitive(NR::NR_FILTER_DIFFUSELIGHTING);
300     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
301     NR::FilterDiffuseLighting *nr_diffuselighting = dynamic_cast<NR::FilterDiffuseLighting*>(nr_primitive);
302     g_assert(nr_diffuselighting != NULL);
304     sp_diffuselighting->renderer = nr_diffuselighting;
305     sp_filter_primitive_renderer_common(primitive, nr_primitive);
307     nr_diffuselighting->diffuseConstant = sp_diffuselighting->diffuseConstant;
308     nr_diffuselighting->surfaceScale = sp_diffuselighting->surfaceScale;
309     nr_diffuselighting->lighting_color = sp_diffuselighting->lighting_color;
310     //We assume there is at most one child
311     nr_diffuselighting->light_type = NR::NO_LIGHT;
312     if (SP_IS_FEDISTANTLIGHT(primitive->children)) {
313         nr_diffuselighting->light_type = NR::DISTANT_LIGHT;
314         nr_diffuselighting->light.distant = SP_FEDISTANTLIGHT(primitive->children);
315     }
316     if (SP_IS_FEPOINTLIGHT(primitive->children)) {
317         nr_diffuselighting->light_type = NR::POINT_LIGHT;
318         nr_diffuselighting->light.point = SP_FEPOINTLIGHT(primitive->children);
319     }
320     if (SP_IS_FESPOTLIGHT(primitive->children)) {
321         nr_diffuselighting->light_type = NR::SPOT_LIGHT;
322         nr_diffuselighting->light.spot = SP_FESPOTLIGHT(primitive->children);
323     }
324         
325     //nr_offset->set_dx(sp_offset->dx);
326     //nr_offset->set_dy(sp_offset->dy);
330 /*
331   Local Variables:
332   mode:c++
333   c-file-style:"stroustrup"
334   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
335   indent-tabs-mode:nil
336   fill-column:99
337   End:
338 */
339 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :