1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** \file
3 * SVG <fepointlight> implementation.
4 */
5 /*
6 * Authors:
7 * Hugo Rodrigues <haa.rodrigues@gmail.com>
8 * Niko Kiirala <niko@kiirala.com>
9 * Jean-Rene Reinhard <jr@komite.net>
10 * Abhishek Sharma
11 *
12 * Copyright (C) 2006,2007 Authors
13 *
14 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15 */
16
17 // Same directory
18 #include "pointlight.h"
19 #include "diffuselighting.h"
20 #include "specularlighting.h"
21
22 #include <glib.h>
23
24 #include "attributes.h"
25 #include "document.h"
26
27
28 #include "xml/node.h"
29 #include "xml/repr.h"
30
SPFePointLight()31 SPFePointLight::SPFePointLight()
32 : SPObject(), x(0), x_set(FALSE), y(0), y_set(FALSE), z(0), z_set(FALSE) {
33 }
34
35 SPFePointLight::~SPFePointLight() = default;
36
37
38 /**
39 * Reads the Inkscape::XML::Node, and initializes SPPointLight variables. For this to get called,
40 * our name must be associated with a repr via "sp_object_type_register". Best done through
41 * sp-object-repr.cpp's repr_name_entries array.
42 */
build(SPDocument * document,Inkscape::XML::Node * repr)43 void SPFePointLight::build(SPDocument *document, Inkscape::XML::Node *repr) {
44 SPObject::build(document, repr);
45
46 //Read values of key attributes from XML nodes into object.
47 this->readAttr(SPAttr::X);
48 this->readAttr(SPAttr::Y);
49 this->readAttr(SPAttr::Z);
50
51 //is this necessary?
52 document->addResource("fepointlight", this);
53 }
54
55 /**
56 * Drops any allocated memory.
57 */
release()58 void SPFePointLight::release() {
59 if ( this->document ) {
60 // Unregister ourselves
61 this->document->removeResource("fepointlight", this);
62 }
63
64 //TODO: release resources here
65 }
66
67 /**
68 * Sets a specific value in the SPFePointLight.
69 */
set(SPAttr key,gchar const * value)70 void SPFePointLight::set(SPAttr key, gchar const *value) {
71 gchar *end_ptr;
72
73 switch (key) {
74 case SPAttr::X:
75 end_ptr = nullptr;
76
77 if (value) {
78 this->x = g_ascii_strtod(value, &end_ptr);
79
80 if (end_ptr) {
81 this->x_set = TRUE;
82 }
83 }
84
85 if (!value || !end_ptr) {
86 this->x = 0;
87 this->x_set = FALSE;
88 }
89
90 if (this->parent &&
91 (SP_IS_FEDIFFUSELIGHTING(this->parent) ||
92 SP_IS_FESPECULARLIGHTING(this->parent))) {
93 this->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
94 }
95 break;
96 case SPAttr::Y:
97 end_ptr = nullptr;
98
99 if (value) {
100 this->y = g_ascii_strtod(value, &end_ptr);
101
102 if (end_ptr) {
103 this->y_set = TRUE;
104 }
105 }
106
107 if (!value || !end_ptr) {
108 this->y = 0;
109 this->y_set = FALSE;
110 }
111
112 if (this->parent &&
113 (SP_IS_FEDIFFUSELIGHTING(this->parent) ||
114 SP_IS_FESPECULARLIGHTING(this->parent))) {
115 this->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
116 }
117 break;
118 case SPAttr::Z:
119 end_ptr = nullptr;
120
121 if (value) {
122 this->z = g_ascii_strtod(value, &end_ptr);
123
124 if (end_ptr) {
125 this->z_set = TRUE;
126 }
127 }
128
129 if (!value || !end_ptr) {
130 this->z = 0;
131 this->z_set = FALSE;
132 }
133
134 if (this->parent &&
135 (SP_IS_FEDIFFUSELIGHTING(this->parent) ||
136 SP_IS_FESPECULARLIGHTING(this->parent))) {
137 this->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
138 }
139 break;
140 default:
141 // See if any parents need this value.
142 SPObject::set(key, value);
143 break;
144 }
145 }
146
147 /**
148 * * Receives update notifications.
149 * */
update(SPCtx * ctx,guint flags)150 void SPFePointLight::update(SPCtx *ctx, guint flags) {
151 if (flags & SP_OBJECT_MODIFIED_FLAG) {
152 /* do something to trigger redisplay, updates? */
153 this->readAttr(SPAttr::X);
154 this->readAttr(SPAttr::Y);
155 this->readAttr(SPAttr::Z);
156 }
157
158 SPObject::update(ctx, flags);
159 }
160
161 /**
162 * Writes its settings to an incoming repr object, if any.
163 */
write(Inkscape::XML::Document * doc,Inkscape::XML::Node * repr,guint flags)164 Inkscape::XML::Node* SPFePointLight::write(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags) {
165 if (!repr) {
166 repr = this->getRepr()->duplicate(doc);
167 }
168
169 if (this->x_set)
170 sp_repr_set_css_double(repr, "x", this->x);
171 if (this->y_set)
172 sp_repr_set_css_double(repr, "y", this->y);
173 if (this->z_set)
174 sp_repr_set_css_double(repr, "z", this->z);
175
176 SPObject::write(doc, repr, flags);
177
178 return repr;
179 }
180
181 /*
182 Local Variables:
183 mode:c++
184 c-file-style:"stroustrup"
185 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
186 indent-tabs-mode:nil
187 fill-column:99
188 End:
189 */
190 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
191