d81fdab44737287c481df4bd22a07229158aa906
[WebKit-https.git] / WebCore / ksvg2 / svg / SVGAnimateElement.cpp
1 /*
2     Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox@kde.org>
3                   2004, 2005 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 "Document.h"
29 #include "SVGDocumentExtensions.h"
30 #include "SVGSVGElement.h"
31
32 using namespace WebCore;
33
34 SVGAnimateElement::SVGAnimateElement(const QualifiedName& tagName, Document *doc)
35     : SVGAnimationElement(tagName, doc)
36     , m_currentItem(-1)
37 {
38 }
39
40 SVGAnimateElement::~SVGAnimateElement()
41 {
42 }
43
44 void SVGAnimateElement::handleTimerEvent(double timePercentage)
45 {
46     // Start condition.
47     if (!m_connected) {
48         // Save initial attribute value...
49         String attr(targetAttribute());
50         m_savedTo = attr.deprecatedString();
51
52         // Animation mode handling
53         switch(detectAnimationMode())
54         {
55 /*            case TO_ANIMATION:
56             case FROM_TO_ANIMATION:
57             {
58                 String toColorString(m_to);
59                 m_toColor->setRGBColor(toColorString.impl());
60     
61                 String fromColorString;
62                 if (!m_from.isEmpty()) // from-to animation
63                     fromColorString = m_from;
64                 else // to animation
65                     fromColorString = m_initialColor.name();
66     
67                 m_fromColor->setRGBColor(fromColorString.impl());    
68
69                 // Calculate color differences, once.
70                 Color qTo = m_toColor->color();
71                 Color qFrom = m_fromColor->color();
72     
73                 m_redDiff = qTo.red() - qFrom.red();
74                 m_greenDiff = qTo.green() - qFrom.green();
75                 m_blueDiff = qTo.blue() - qFrom.blue();
76                 
77                 break;
78             }
79             case BY_ANIMATION:
80             case FROM_BY_ANIMATION:
81             {
82                 String byColorString(m_by);
83                 m_toColor->setRGBColor(byColorString.impl());
84
85                 String fromColorString;
86             
87                 if (!m_from.isEmpty()) // from-by animation
88                     fromColorString = m_from;
89                 else // by animation
90                     fromColorString = m_initialColor.name();
91
92                 m_fromColor->setRGBColor(fromColorString.impl());
93
94                 Color qBy = m_toColor->color();
95                 Color qFrom = m_fromColor->color();
96
97                 // Calculate 'm_toColor' using relative values
98                 int r = qFrom.red() + qBy.red();
99                 int g = qFrom.green() + qBy.green();
100                 int b = qFrom.blue() + qBy.blue();
101
102                 Color qTo = clampColor(r, g, b);
103             
104                 String toColorString(qTo.name());
105                 m_toColor->setRGBColor(toColorString.impl());
106             
107                 m_redDiff = qTo.red() - qFrom.red();
108                 m_greenDiff = qTo.green() - qFrom.green();
109                 m_blueDiff = qTo.blue() - qFrom.blue();
110
111                 break;
112             }
113             case VALUES_ANIMATION:
114                 break;*/
115             default:
116             {
117                 //kdError() << k_funcinfo << " Unable to detect animation mode! Aborting creation!" << endl;
118                 return;
119             }
120         }
121
122         ownerSVGElement()->timeScheduler()->connectIntervalTimer(this);
123         m_connected = true;
124
125         return;
126     }
127
128     // Calculations...
129     if (timePercentage >= 1.0)
130         timePercentage = 1.0;
131
132 //    int r = 0, g = 0, b = 0;
133 //    if ((m_redDiff != 0 || m_greenDiff != 0 || m_blueDiff != 0) && !m_values)
134 //        calculateColor(timePercentage, r, g, b);
135 /*    else */if (m_values)
136     {
137 /*        int itemByPercentage = calculateCurrentValueItem(timePercentage);
138
139         if (itemByPercentage == -1)
140             return;
141
142         if (m_currentItem != itemByPercentage) // Item changed...
143         {
144             // Extract current 'from' / 'to' values
145             String value1 = String(m_values->getItem(itemByPercentage));
146             String value2 = String(m_values->getItem(itemByPercentage + 1));
147
148             // Calculate r/g/b shifting values...
149             if (!value1.isEmpty() && !value2.isEmpty())
150             {
151                 bool apply = false;
152                 if (m_redDiff != 0 || m_greenDiff != 0 || m_blueDiff != 0)
153                 {
154                     r = m_toColor->color().red();
155                     g = m_toColor->color().green();
156                     b = m_toColor->color().blue();
157
158                     apply = true;
159                 }
160
161                 String toColorString(value2);
162                 m_toColor->setRGBColor(toColorString.impl());
163     
164                 String fromColorString(value1);
165                 m_fromColor->setRGBColor(fromColorString.impl());    
166
167                 Color qTo = m_toColor->color();
168                 Color qFrom = m_fromColor->color();
169
170                 m_redDiff = qTo.red() - qFrom.red();
171                 m_greenDiff = qTo.green() - qFrom.green();
172                 m_blueDiff = qTo.blue() - qFrom.blue();
173
174                 m_currentItem = itemByPercentage;
175
176                 if (!apply)
177                     return;
178             }
179         }
180         else if (m_redDiff != 0 || m_greenDiff != 0 || m_blueDiff != 0)
181         {
182             double relativeTime = calculateRelativeTimePercentage(timePercentage, m_currentItem);
183             calculateColor(relativeTime, r, g, b);
184         }*/
185     }
186     
187     if (!isFrozen() && timePercentage == 1.0) {
188     }
189
190     if (isAccumulated() && repeations() != 0.0) {
191     }
192
193     // Commit changes!
194     
195     // End condition.
196     if (timePercentage == 1.0) {
197         if ((m_repeatCount > 0 && m_repeations < m_repeatCount - 1) || isIndefinite(m_repeatCount)) {
198             m_repeations++;
199             return;
200         }
201
202         ownerSVGElement()->timeScheduler()->disconnectIntervalTimer(this);
203         m_connected = false;
204
205         // Reset...
206         m_currentItem = -1;
207     }
208 }
209
210 // vim:ts=4:noet
211 #endif // SVG_SUPPORT
212