b66912468ce8d5efa5606f7f6adc5cffa39d4e6a
1 #define __SP_FESPOTLIGHT_CPP__
3 /** \file
4 * SVG <fespotlight> implementation.
5 */
6 /*
7 * Authors:
8 * Hugo Rodrigues <haa.rodrigues@gmail.com>
9 * Niko Kiirala <niko@kiirala.com>
10 * Jean-Rene Reinhard <jr@komite.net>
11 *
12 * Copyright (C) 2006,2007 Authors
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <glib.h>
23 #include "attributes.h"
24 #include "document.h"
25 #include "sp-fespotlight.h"
26 #include "sp-fediffuselighting-fns.h"
27 #include "sp-fespecularlighting-fns.h"
28 #include "xml/repr.h"
30 #define SP_MACROS_SILENT
31 #include "macros.h"
33 /* FeSpotLight class */
35 static void sp_fespotlight_class_init(SPFeSpotLightClass *klass);
36 static void sp_fespotlight_init(SPFeSpotLight *fespotlight);
38 static void sp_fespotlight_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
39 static void sp_fespotlight_release(SPObject *object);
40 static void sp_fespotlight_set(SPObject *object, unsigned int key, gchar const *value);
41 static void sp_fespotlight_update(SPObject *object, SPCtx *ctx, guint flags);
42 static Inkscape::XML::Node *sp_fespotlight_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
44 static SPObjectClass *feSpotLight_parent_class;
46 GType
47 sp_fespotlight_get_type()
48 {
49 static GType fespotlight_type = 0;
51 if (!fespotlight_type) {
52 GTypeInfo fespotlight_info = {
53 sizeof(SPFeSpotLightClass),
54 NULL, NULL,
55 (GClassInitFunc) sp_fespotlight_class_init,
56 NULL, NULL,
57 sizeof(SPFeSpotLight),
58 16,
59 (GInstanceInitFunc) sp_fespotlight_init,
60 NULL, /* value_table */
61 };
62 fespotlight_type = g_type_register_static(SP_TYPE_OBJECT, "SPFeSpotLight", &fespotlight_info, (GTypeFlags)0);
63 }
64 return fespotlight_type;
65 }
67 static void
68 sp_fespotlight_class_init(SPFeSpotLightClass *klass)
69 {
71 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
73 feSpotLight_parent_class = (SPObjectClass*)g_type_class_peek_parent(klass);
75 sp_object_class->build = sp_fespotlight_build;
76 sp_object_class->release = sp_fespotlight_release;
77 sp_object_class->write = sp_fespotlight_write;
78 sp_object_class->set = sp_fespotlight_set;
79 sp_object_class->update = sp_fespotlight_update;
80 }
82 static void
83 sp_fespotlight_init(SPFeSpotLight *fespotlight)
84 {
85 fespotlight->x = 0;
86 fespotlight->y = 0;
87 fespotlight->z = 0;
88 fespotlight->pointsAtX = 0;
89 fespotlight->pointsAtY = 0;
90 fespotlight->pointsAtZ = 0;
91 fespotlight->specularExponent = 1;
92 fespotlight->limitingConeAngle = 90;
94 fespotlight->x_set = FALSE;
95 fespotlight->y_set = FALSE;
96 fespotlight->z_set = FALSE;
97 fespotlight->pointsAtX_set = FALSE;
98 fespotlight->pointsAtY_set = FALSE;
99 fespotlight->pointsAtZ_set = FALSE;
100 fespotlight->specularExponent_set = FALSE;
101 fespotlight->limitingConeAngle_set = FALSE;
102 }
104 /**
105 * Reads the Inkscape::XML::Node, and initializes SPPointLight 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_fespotlight_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
111 {
112 if (((SPObjectClass *) feSpotLight_parent_class)->build) {
113 ((SPObjectClass *) feSpotLight_parent_class)->build(object, document, repr);
114 }
116 //Read values of key attributes from XML nodes into object.
117 sp_object_read_attr(object, "x");
118 sp_object_read_attr(object, "y");
119 sp_object_read_attr(object, "z");
120 sp_object_read_attr(object, "pointsAtX");
121 sp_object_read_attr(object, "pointsAtY");
122 sp_object_read_attr(object, "pointsAtZ");
123 sp_object_read_attr(object, "specularExponent");
124 sp_object_read_attr(object, "limitingConeAngle");
126 //is this necessary?
127 sp_document_add_resource(document, "fespotlight", object);
128 }
130 /**
131 * Drops any allocated memory.
132 */
133 static void
134 sp_fespotlight_release(SPObject *object)
135 {
136 //SPFeSpotLight *fespotlight = SP_FESPOTLIGHT(object);
138 if (SP_OBJECT_DOCUMENT(object)) {
139 /* Unregister ourselves */
140 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "fespotlight", SP_OBJECT(object));
141 }
143 //TODO: release resources here
144 }
146 /**
147 * Sets a specific value in the SPFeSpotLight.
148 */
149 static void
150 sp_fespotlight_set(SPObject *object, unsigned int key, gchar const *value)
151 {
152 SPFeSpotLight *fespotlight = SP_FESPOTLIGHT(object);
153 gchar *end_ptr;
155 switch (key) {
156 case SP_ATTR_X:
157 end_ptr = NULL;
158 if (value) {
159 fespotlight->x = g_ascii_strtod(value, &end_ptr);
160 if (end_ptr)
161 fespotlight->x_set = TRUE;
162 }
163 if(!value || !end_ptr) {
164 fespotlight->x = 0;
165 fespotlight->x_set = FALSE;
166 }
167 if (object->parent &&
168 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
169 SP_IS_FESPECULARLIGHTING(object->parent))) {
170 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
171 }
172 break;
173 case SP_ATTR_Y:
174 end_ptr = NULL;
175 if (value) {
176 fespotlight->y = g_ascii_strtod(value, &end_ptr);
177 if (end_ptr)
178 fespotlight->y_set = TRUE;
179 }
180 if(!value || !end_ptr) {
181 fespotlight->y = 0;
182 fespotlight->y_set = FALSE;
183 }
184 if (object->parent &&
185 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
186 SP_IS_FESPECULARLIGHTING(object->parent))) {
187 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
188 }
189 break;
190 case SP_ATTR_Z:
191 end_ptr = NULL;
192 if (value) {
193 fespotlight->z = g_ascii_strtod(value, &end_ptr);
194 if (end_ptr)
195 fespotlight->z_set = TRUE;
196 }
197 if(!value || !end_ptr) {
198 fespotlight->z = 0;
199 fespotlight->z_set = FALSE;
200 }
201 if (object->parent &&
202 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
203 SP_IS_FESPECULARLIGHTING(object->parent))) {
204 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
205 }
206 break;
207 case SP_ATTR_POINTSATX:
208 end_ptr = NULL;
209 if (value) {
210 fespotlight->pointsAtX = g_ascii_strtod(value, &end_ptr);
211 if (end_ptr)
212 fespotlight->pointsAtX_set = TRUE;
213 }
214 if(!value || !end_ptr) {
215 fespotlight->pointsAtX = 0;
216 fespotlight->pointsAtX_set = FALSE;
217 }
218 if (object->parent &&
219 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
220 SP_IS_FESPECULARLIGHTING(object->parent))) {
221 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
222 }
223 break;
224 case SP_ATTR_POINTSATY:
225 end_ptr = NULL;
226 if (value) {
227 fespotlight->pointsAtY = g_ascii_strtod(value, &end_ptr);
228 if (end_ptr)
229 fespotlight->pointsAtY_set = TRUE;
230 }
231 if(!value || !end_ptr) {
232 fespotlight->pointsAtY = 0;
233 fespotlight->pointsAtY_set = FALSE;
234 }
235 if (object->parent &&
236 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
237 SP_IS_FESPECULARLIGHTING(object->parent))) {
238 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
239 }
240 break;
241 case SP_ATTR_POINTSATZ:
242 end_ptr = NULL;
243 if (value) {
244 fespotlight->pointsAtZ = g_ascii_strtod(value, &end_ptr);
245 if (end_ptr)
246 fespotlight->pointsAtZ_set = TRUE;
247 }
248 if(!value || !end_ptr) {
249 fespotlight->pointsAtZ = 0;
250 fespotlight->pointsAtZ_set = FALSE;
251 }
252 if (object->parent &&
253 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
254 SP_IS_FESPECULARLIGHTING(object->parent))) {
255 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
256 }
257 break;
258 case SP_ATTR_SPECULAREXPONENT:
259 end_ptr = NULL;
260 if (value) {
261 fespotlight->specularExponent = g_ascii_strtod(value, &end_ptr);
262 if (end_ptr)
263 fespotlight->specularExponent_set = TRUE;
264 }
265 if(!value || !end_ptr) {
266 fespotlight->specularExponent = 1;
267 fespotlight->specularExponent_set = FALSE;
268 }
269 if (object->parent &&
270 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
271 SP_IS_FESPECULARLIGHTING(object->parent))) {
272 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
273 }
274 break;
275 case SP_ATTR_LIMITINGCONEANGLE:
276 end_ptr = NULL;
277 if (value) {
278 fespotlight->limitingConeAngle = g_ascii_strtod(value, &end_ptr);
279 if (end_ptr)
280 fespotlight->limitingConeAngle_set = TRUE;
281 }
282 if(!value || !end_ptr) {
283 fespotlight->limitingConeAngle = 90;
284 fespotlight->limitingConeAngle_set = FALSE;
285 }
286 if (object->parent &&
287 (SP_IS_FEDIFFUSELIGHTING(object->parent) ||
288 SP_IS_FESPECULARLIGHTING(object->parent))) {
289 object->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
290 }
291 break;
292 default:
293 // See if any parents need this value.
294 if (((SPObjectClass *) feSpotLight_parent_class)->set) {
295 ((SPObjectClass *) feSpotLight_parent_class)->set(object, key, value);
296 }
297 break;
298 }
299 }
301 /**
302 * * Receives update notifications.
303 * */
304 static void
305 sp_fespotlight_update(SPObject *object, SPCtx *ctx, guint flags)
306 {
307 SPFeSpotLight *feSpotLight = SP_FESPOTLIGHT(object);
308 (void)feSpotLight;
310 if (flags & SP_OBJECT_MODIFIED_FLAG) {
311 /* do something to trigger redisplay, updates? */
312 sp_object_read_attr(object, "x");
313 sp_object_read_attr(object, "y");
314 sp_object_read_attr(object, "z");
315 sp_object_read_attr(object, "pointsAtX");
316 sp_object_read_attr(object, "pointsAtY");
317 sp_object_read_attr(object, "pointsAtZ");
318 sp_object_read_attr(object, "specularExponent");
319 sp_object_read_attr(object, "limitingConeAngle");
320 }
322 if (((SPObjectClass *) feSpotLight_parent_class)->update) {
323 ((SPObjectClass *) feSpotLight_parent_class)->update(object, ctx, flags);
324 }
325 }
327 /**
328 * Writes its settings to an incoming repr object, if any.
329 */
330 static Inkscape::XML::Node *
331 sp_fespotlight_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
332 {
333 SPFeSpotLight *fespotlight = SP_FESPOTLIGHT(object);
335 if (!repr) {
336 repr = SP_OBJECT_REPR(object)->duplicate(doc);
337 }
339 if (fespotlight->x_set)
340 sp_repr_set_css_double(repr, "x", fespotlight->x);
341 if (fespotlight->y_set)
342 sp_repr_set_css_double(repr, "y", fespotlight->y);
343 if (fespotlight->z_set)
344 sp_repr_set_css_double(repr, "z", fespotlight->z);
345 if (fespotlight->pointsAtX_set)
346 sp_repr_set_css_double(repr, "pointsAtX", fespotlight->pointsAtX);
347 if (fespotlight->pointsAtY_set)
348 sp_repr_set_css_double(repr, "pointsAtY", fespotlight->pointsAtY);
349 if (fespotlight->pointsAtZ_set)
350 sp_repr_set_css_double(repr, "pointsAtZ", fespotlight->pointsAtZ);
351 if (fespotlight->specularExponent_set)
352 sp_repr_set_css_double(repr, "specularExponent", fespotlight->specularExponent);
353 if (fespotlight->limitingConeAngle_set)
354 sp_repr_set_css_double(repr, "limitingConeAngle", fespotlight->limitingConeAngle);
356 if (((SPObjectClass *) feSpotLight_parent_class)->write) {
357 ((SPObjectClass *) feSpotLight_parent_class)->write(object, doc, repr, flags);
358 }
360 return repr;
361 }
363 /*
364 Local Variables:
365 mode:c++
366 c-file-style:"stroustrup"
367 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
368 indent-tabs-mode:nil
369 fill-column:99
370 End:
371 */
372 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :