Reviewed by Mitz.
[WebKit-https.git] / WebCore / ksvg2 / svg / SVGAnimateElement.cpp
1 /*
2     Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox@kde.org>
3                   2004, 2005, 2006 Rob Buis <buis@kde.org>
4
5     This file is part of the KDE project
6
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License as published by the Free Software Foundation; either
10     version 2 of the License, or (at your option) any later version.
11
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Library General Public License for more details.
16
17     You should have received a copy of the GNU Library General Public License
18     along with this library; see the file COPYING.LIB.  If not, write to
19     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20     Boston, MA 02111-1307, USA.
21 */
22
23 #include "config.h"
24 #ifdef SVG_SUPPORT
25 #include "SVGAnimateElement.h"
26
27 #include "KSVGTimeScheduler.h"
28 #include "SVGDocumentExtensions.h"
29 #include "SVGSVGElement.h"
30
31 namespace WebCore {
32
33 SVGAnimateElement::SVGAnimateElement(const QualifiedName& tagName, Document* doc)
34     : SVGAnimationElement(tagName, doc)
35     , m_currentItem(-1)
36 {
37 }
38
39 SVGAnimateElement::~SVGAnimateElement()
40 {
41 }
42
43 void SVGAnimateElement::handleTimerEvent(double timePercentage)
44 {
45     // Start condition.
46     if (!m_connected) {
47         // Save initial attribute value...
48         String attr(targetAttribute());
49         m_savedTo = attr.deprecatedString();
50
51         // Animation mode handling
52         switch (detectAnimationMode()) {
53 /*            case TO_ANIMATION:
54             case FROM_TO_ANIMATION:
55             {
56                 String toColorString(m_to);
57                 m_toColor->setRGBColor(toColorString.impl());
58     
59                 String fromColorString;
60                 if (!m_from.isEmpty()) // from-to animation
61                     fromColorString = m_from;
62                 else // to animation
63                     fromColorString = m_initialColor.name();
64     
65                 m_fromColor->setRGBColor(fromColorString.impl());    
66
67                 // Calculate color differences, once.
68                 Color qTo = m_toColor->color();
69                 Color qFrom = m_fromColor->color();
70     
71                 m_redDiff = qTo.red() - qFrom.red();
72                 m_greenDiff = qTo.green() - qFrom.green();
73                 m_blueDiff = qTo.blue() - qFrom.blue();
74                 
75                 break;
76             }
77             case BY_ANIMATION:
78             case FROM_BY_ANIMATION:
79             {
80                 String byColorString(m_by);
81                 m_toColor->setRGBColor(byColorString.impl());
82
83                 String fromColorString;
84             
85                 if (!m_from.isEmpty()) // from-by animation
86                     fromColorString = m_from;
87                 else // by animation
88                     fromColorString = m_initialColor.name();
89
90                 m_fromColor->setRGBColor(fromColorString.impl());
91
92                 Color qBy = m_toColor->color();
93                 Color qFrom = m_fromColor->color();
94
95                 // Calculate 'm_toColor' using relative values
96                 int r = qFrom.red() + qBy.red();
97                 int g = qFrom.green() + qBy.green();
98                 int b = qFrom.blue() + qBy.blue();
99
100                 Color qTo = clampColor(r, g, b);
101             
102                 String toColorString(qTo.name());
103                 m_toColor->setRGBColor(toColorString.impl());
104             
105                 m_redDiff = qTo.red() - qFrom.red();
106                 m_greenDiff = qTo.green() - qFrom.green();
107                 m_blueDiff = qTo.blue() - qFrom.blue();
108
109                 break;
110             }
111             case VALUES_ANIMATION:
112                 break;*/
113             default:
114             {
115                 //kdError() << k_funcinfo << " Unable to detect animation mode! Aborting creation!" << endl;
116                 return;
117             }
118         }
119
120         ownerSVGElement()->timeScheduler()->connectIntervalTimer(this);
121         m_connected = true;
122
123         return;
124     }
125
126     // Calculations...
127     if (timePercentage >= 1.0)
128         timePercentage = 1.0;
129
130 //    int r = 0, g = 0, b = 0;
131 //    if ((m_redDiff != 0 || m_greenDiff != 0 || m_blueDiff != 0) && !m_values)
132 //        calculateColor(timePercentage, r, g, b);
133 /*    else */if (m_values)
134     {
135 /*        int itemByPercentage = calculateCurrentValueItem(timePercentage);
136
137         if (itemByPercentage == -1)
138             return;
139
140         if (m_currentItem != itemByPercentage) // Item changed...
141         {
142             // Extract current 'from' / 'to' values
143             String value1 = String(m_values->getItem(itemByPercentage));
144             String value2 = String(m_values->getItem(itemByPercentage + 1));
145
146             // Calculate r/g/b shifting values...
147             if (!value1.isEmpty() && !value2.isEmpty())
148             {
149                 bool apply = false;
150                 if (m_redDiff != 0 || m_greenDiff != 0 || m_blueDiff != 0)
151                 {
152                     r = m_toColor->color().red();
153                     g = m_toColor->color().green();
154                     b = m_toColor->color().blue();
155
156                     apply = true;
157                 }
158
159                 String toColorString(value2);
160                 m_toColor->setRGBColor(toColorString.impl());
161     
162                 String fromColorString(value1);
163                 m_fromColor->setRGBColor(fromColorString.impl());    
164
165                 Color qTo = m_toColor->color();
166                 Color qFrom = m_fromColor->color();
167
168                 m_redDiff = qTo.red() - qFrom.red();
169                 m_greenDiff = qTo.green() - qFrom.green();
170                 m_blueDiff = qTo.blue() - qFrom.blue();
171
172                 m_currentItem = itemByPercentage;
173
174                 if (!apply)
175                     return;
176             }
177         }
178         else if (m_redDiff != 0 || m_greenDiff != 0 || m_blueDiff != 0)
179         {
180             double relativeTime = calculateRelativeTimePercentage(timePercentage, m_currentItem);
181             calculateColor(relativeTime, r, g, b);
182         }*/
183     }
184     
185     if (!isFrozen() && timePercentage == 1.0) {
186     }
187
188     if (isAccumulated() && repeations() != 0.0) {
189     }
190
191     // Commit changes!
192     
193     // End condition.
194     if (timePercentage == 1.0) {
195         if ((m_repeatCount > 0 && m_repeations < m_repeatCount - 1) || isIndefinite(m_repeatCount)) {
196             m_repeations++;
197             return;
198         }
199
200         ownerSVGElement()->timeScheduler()->disconnectIntervalTimer(this);
201         m_connected = false;
202
203         // Reset...
204         m_currentItem = -1;
205     }
206 }
207
208 }
209
210 // vim:ts=4:noet
211 #endif // SVG_SUPPORT
212