1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** \file
3  * SVG <fedistantlight> 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 #include <glib.h>
18 
19 // In same directory
20 #include "distantlight.h"
21 #include "diffuselighting.h"
22 #include "specularlighting.h"
23 
24 #include "attributes.h"
25 #include "document.h"
26 
27 #include "xml/repr.h"
28 
SPFeDistantLight()29 SPFeDistantLight::SPFeDistantLight()
30     : SPObject(), azimuth(0), azimuth_set(FALSE), elevation(0), elevation_set(FALSE) {
31 }
32 
33 SPFeDistantLight::~SPFeDistantLight() = default;
34 
35 /**
36  * Reads the Inkscape::XML::Node, and initializes SPDistantLight variables.  For this to get called,
37  * our name must be associated with a repr via "sp_object_type_register".  Best done through
38  * sp-object-repr.cpp's repr_name_entries array.
39  */
build(SPDocument * document,Inkscape::XML::Node * repr)40 void SPFeDistantLight::build(SPDocument *document, Inkscape::XML::Node *repr) {
41 	SPObject::build(document, repr);
42 
43     //Read values of key attributes from XML nodes into object.
44     this->readAttr(SPAttr::AZIMUTH);
45     this->readAttr(SPAttr::ELEVATION);
46 
47 //is this necessary?
48     document->addResource("fedistantlight", this);
49 }
50 
51 /**
52  * Drops any allocated memory.
53  */
release()54 void SPFeDistantLight::release() {
55     if ( this->document ) {
56         // Unregister ourselves
57         this->document->removeResource("fedistantlight", this);
58     }
59 
60 //TODO: release resources here
61 }
62 
63 /**
64  * Sets a specific value in the SPFeDistantLight.
65  */
set(SPAttr key,gchar const * value)66 void SPFeDistantLight::set(SPAttr key, gchar const *value) {
67     gchar *end_ptr;
68 
69     switch (key) {
70     case SPAttr::AZIMUTH:
71         end_ptr =nullptr;
72 
73         if (value) {
74             this->azimuth = g_ascii_strtod(value, &end_ptr);
75 
76             if (end_ptr) {
77                 this->azimuth_set = TRUE;
78             }
79         }
80 
81         if (!value || !end_ptr) {
82                 this->azimuth_set = FALSE;
83                 this->azimuth = 0;
84         }
85 
86         if (this->parent &&
87                 (SP_IS_FEDIFFUSELIGHTING(this->parent) ||
88                  SP_IS_FESPECULARLIGHTING(this->parent))) {
89             this->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
90         }
91         break;
92     case SPAttr::ELEVATION:
93         end_ptr =nullptr;
94 
95         if (value) {
96             this->elevation = g_ascii_strtod(value, &end_ptr);
97 
98             if (end_ptr) {
99                 this->elevation_set = TRUE;
100             }
101         }
102 
103         if (!value || !end_ptr) {
104                 this->elevation_set = FALSE;
105                 this->elevation = 0;
106         }
107 
108         if (this->parent &&
109                 (SP_IS_FEDIFFUSELIGHTING(this->parent) ||
110                  SP_IS_FESPECULARLIGHTING(this->parent))) {
111             this->parent->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
112         }
113         break;
114     default:
115         // See if any parents need this value.
116     	SPObject::set(key, value);
117         break;
118     }
119 }
120 
121 /**
122  *  * Receives update notifications.
123  *   */
update(SPCtx * ctx,guint flags)124 void SPFeDistantLight::update(SPCtx *ctx, guint flags) {
125     if (flags & SP_OBJECT_MODIFIED_FLAG) {
126         /* do something to trigger redisplay, updates? */
127         this->readAttr(SPAttr::AZIMUTH);
128         this->readAttr(SPAttr::ELEVATION);
129     }
130 
131     SPObject::update(ctx, flags);
132 }
133 
134 /**
135  * Writes its settings to an incoming repr object, if any.
136  */
write(Inkscape::XML::Document * doc,Inkscape::XML::Node * repr,guint flags)137 Inkscape::XML::Node* SPFeDistantLight::write(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags) {
138     if (!repr) {
139         repr = this->getRepr()->duplicate(doc);
140     }
141 
142     if (this->azimuth_set) {
143         sp_repr_set_css_double(repr, "azimuth", this->azimuth);
144     }
145 
146     if (this->elevation_set) {
147         sp_repr_set_css_double(repr, "elevation", this->elevation);
148     }
149 
150     SPObject::write(doc, repr, flags);
151 
152     return repr;
153 }
154 
155 /*
156   Local Variables:
157   mode:c++
158   c-file-style:"stroustrup"
159   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
160   indent-tabs-mode:nil
161   fill-column:99
162   End:
163 */
164 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
165