~cairo-dock-team/cairo-dock-plug-ins/plug-ins

« back to all changes in this revision

Viewing changes to Animated-icons/src/applet-wave.c

  • Committer: Matthieu Baerts
  • Date: 2014-10-19 00:26:10 UTC
  • Revision ID: matttbe@gmail.com-20141019002610-ulf26s9b4c4rw10r
We just switched from BZR to Git.
Follow us on Github: https://github.com/Cairo-Dock

Note: we will only use Github to manage our source code and all pull requests.
Please continue to report your bugs/ideas/messages on our forum or Launchpad! 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
* This file is a part of the Cairo-Dock project
3
 
*
4
 
* Copyright : (C) see the 'copyright' file.
5
 
* E-mail    : see the 'copyright' file.
6
 
*
7
 
* This program is free software; you can redistribute it and/or
8
 
* modify it under the terms of the GNU General Public License
9
 
* as published by the Free Software Foundation; either version 3
10
 
* of the License, or (at your option) any later version.
11
 
*
12
 
* This program 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
15
 
* GNU General Public License for more details.
16
 
* You should have received a copy of the GNU General Public License
17
 
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
*/
19
 
 
20
 
#include <stdlib.h>
21
 
#include <string.h>
22
 
#include <math.h>
23
 
 
24
 
#include "applet-struct.h"
25
 
#include "applet-notifications.h"
26
 
#include "applet-wave.h"
27
 
 
28
 
static inline void _init_wave (GLfloat *pVertices, GLfloat *pCoords)
29
 
{
30
 
        // point bas gauche.
31
 
        pVertices[0] = -.5;
32
 
        pVertices[1] = -.5;
33
 
        pCoords[0] = 0.;
34
 
        pCoords[1] = 1.;
35
 
        
36
 
        // point bas droit.
37
 
        pVertices[2] = .5;
38
 
        pVertices[3] = -.5;
39
 
        pCoords[2] = 1.;
40
 
        pCoords[3] = 1.;
41
 
        
42
 
        // point haut gauche.
43
 
        pVertices[4] = -.5;
44
 
        pVertices[5] = .5;
45
 
        pCoords[4] = 0.;
46
 
        pCoords[5] = 0.;
47
 
        
48
 
        // point haut droit.
49
 
        pVertices[6] = .5;
50
 
        pVertices[7] = .5;
51
 
        pCoords[6] = 1;
52
 
        pCoords[7] = 0.;
53
 
}
54
 
 
55
 
static void init (Icon *pIcon, CairoDock *pDock, CDAnimationData *pData, double dt, gboolean bUseOpenGL)
56
 
{
57
 
        if (!bUseOpenGL)  // no cairo rendering (yet).
58
 
                return;
59
 
 
60
 
        _init_wave (pData->pVertices, pData->pCoords);
61
 
        
62
 
        pData->iNumActiveNodes = 4;
63
 
        pData->fWavePosition = - myConfig.fWaveWidth / 2 + .01;  // on rajoute epsilon pour commencer avec 2 points.
64
 
}
65
 
 
66
 
 
67
 
static gboolean update (Icon *pIcon, CairoDock *pDock, CDAnimationData *pData, double dt, gboolean bUseOpenGL, gboolean bRepeat)
68
 
{
69
 
        if (!bUseOpenGL)  // no cairo rendering (yet).
70
 
                return FALSE;
71
 
 
72
 
        GLfloat *pVertices = pData->pVertices;
73
 
        GLfloat *pCoords = pData->pCoords;
74
 
        
75
 
        double p = pData->fWavePosition, w = myConfig.fWaveWidth;
76
 
        if (p + w/2 < 0 || p - w/2 > 1)  // la vague est entierement en-dehors du cote.
77
 
        {
78
 
                _init_wave (pVertices, pCoords);  // on s'assure d'avoir au moins un carre.
79
 
                pData->iNumActiveNodes = 4;
80
 
        }
81
 
        else
82
 
        {
83
 
                int j = 0;  // nombre de points calcules.
84
 
                if (p - w/2 > 0)  // la vague n'englobe pas le point du bas.
85
 
                {
86
 
                        pVertices[0] = -.5;
87
 
                        pVertices[1] = -.5;
88
 
                        pCoords[0] = 0.;
89
 
                        pCoords[1] = 1.;
90
 
                        j ++;
91
 
                }
92
 
                
93
 
                double x, x_, y;  // position du point et du point precedent.
94
 
                double a, a_;  // position sur la vague dans [-1, 1].
95
 
                int n = CD_WAVE_NB_POINTS / 2;  // nbre de points pour une demi-vague (N est impair).
96
 
                int i;
97
 
                for (i = 0; i < CD_WAVE_NB_POINTS; i ++)  // on discretise la vague.
98
 
                {
99
 
                        a = 1. * (i-n) / (n);  // position courante sur la vague, dans [-1, 1].
100
 
                        x = p + a * w/2;  // abscisse correspondante.
101
 
                        
102
 
                        a = 1. * (i-n) / n;  // position courante sur la vague, dans [-1, 1].
103
 
                        x = p + a * w/2;  // abscisse correspondante.
104
 
                        if (x < 0)  // le point sort par le bas.
105
 
                        {
106
 
                                a_ = 1. * (i+1-n) / n;
107
 
                                x_ = p + a_ * w/2;
108
 
                                if (x_ > 0)  // le point suivant est dedans.
109
 
                                {
110
 
                                        y = myConfig.fWaveAmplitude * cos (G_PI/2 * a - x / (w/2) * G_PI/2);  // on depasse de x, donc on dephase d'autant.
111
 
                                        x = 0.;  // coin de la texture.
112
 
                                }
113
 
                                else  // tout le segment est dehors, on zappe.
114
 
                                        continue ;
115
 
                        }
116
 
                        else if (x > 1)  // le point sort par le haut.
117
 
                        {
118
 
                                a_ = 1. * (i-1-n) / n;
119
 
                                x_ = p + a_ * w/2;
120
 
                                if (x_ < 1)  // le point precedent est dedans.
121
 
                                {
122
 
                                        y = myConfig.fWaveAmplitude * cos (G_PI/2 * a - (x-1) / (w/2) * G_PI/2);  // on depasse de x-1, donc on dephase d'autant.
123
 
                                        x = 1.;  // coin de la texture.
124
 
                                }
125
 
                                else  // tout le segment est dehors, on zappe.
126
 
                                        continue;
127
 
                        }
128
 
                        else  // le point est dans l'icone.
129
 
                                y = myConfig.fWaveAmplitude * cos (G_PI/2 * a);
130
 
                        
131
 
                        pVertices[4*j] = -.5 - y;
132
 
                        pVertices[4*j+1] = x - .5;
133
 
                        pCoords[4*j] = 0.;
134
 
                        pCoords[4*j+1] = 1. - x;
135
 
                        j ++;
136
 
                }
137
 
                
138
 
                if (p + w/2 < 1)  // la vague n'englobe pas le point du haut.
139
 
                {
140
 
                        pVertices[4*j] = -.5;
141
 
                        pVertices[4*j+1] = .5;
142
 
                        pCoords[4*j] = 0.;
143
 
                        pCoords[4*j+1] = 0.;
144
 
                        j ++;
145
 
                }
146
 
                
147
 
                for (i = 0; i < j; i ++)  // on complete l'autre cote symetriquement.
148
 
                {
149
 
                        pVertices[4*i+2] = - pVertices[4*i];
150
 
                        pVertices[4*i+3] = pVertices[4*i+1];
151
 
                        pCoords[4*i+2] = 1.;
152
 
                        pCoords[4*i+3] = pCoords[4*i+1];
153
 
                }
154
 
                pData->iNumActiveNodes = 2*j;
155
 
        }
156
 
        
157
 
        pData->fWavePosition += dt / myConfig.iWaveDuration;
158
 
        
159
 
        cairo_dock_redraw_container (CAIRO_CONTAINER (pDock));
160
 
        
161
 
        gboolean bContinue = (pData->fWavePosition - w/2 < 1);
162
 
        if (! bContinue && bRepeat)
163
 
                pData->fWavePosition = - myConfig.fWaveWidth / 2;
164
 
        return bContinue;
165
 
}
166
 
 
167
 
 
168
 
static void render (Icon *pIcon, CairoDock *pDock, CDAnimationData *pData, cairo_t *pCairoContext)
169
 
{
170
 
        if (pCairoContext != NULL)  // no cairo rendering (yet).
171
 
                return;
172
 
 
173
 
        glPushMatrix ();
174
 
        cairo_dock_set_icon_scale (pIcon, CAIRO_CONTAINER (pDock), 1.);
175
 
        
176
 
        glColor4f (1., 1., 1., pIcon->fAlpha);
177
 
        glEnable(GL_BLEND);
178
 
        if (pIcon->fAlpha == 1)
179
 
                _cairo_dock_set_blend_pbuffer ();
180
 
        else
181
 
                _cairo_dock_set_blend_over ();
182
 
        
183
 
        glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
184
 
        
185
 
        glEnable(GL_TEXTURE_2D); // Je veux de la texture
186
 
        glBindTexture(GL_TEXTURE_2D, pIcon->image.iTexture);
187
 
        glPolygonMode(GL_FRONT, GL_FILL);
188
 
        
189
 
        glEnableClientState (GL_TEXTURE_COORD_ARRAY);
190
 
        glEnableClientState (GL_VERTEX_ARRAY);
191
 
        
192
 
        glTexCoordPointer (2, GL_FLOAT, 0, pData->pCoords);
193
 
        glVertexPointer (2, GL_FLOAT, 0, pData->pVertices);
194
 
 
195
 
        glDrawArrays (GL_QUAD_STRIP, 0, pData->iNumActiveNodes);
196
 
        
197
 
        glPopMatrix ();
198
 
        
199
 
        if (pDock->container.bUseReflect)
200
 
        {
201
 
                glPushMatrix ();
202
 
                // double fReflectRatio = pDock->iIconSize * myIconsParam.fReflectHeightRatio * pDock->container.fRatio / pIcon->fHeight / pIcon->fScale;
203
 
                double fOffsetY = pIcon->fHeight * pIcon->fScale/2 + (pDock->iIconSize * myIconsParam.fReflectHeightRatio/2 + pIcon->fDeltaYReflection) * pDock->container.fRatio;
204
 
                if (pDock->container.bIsHorizontal)
205
 
                {
206
 
                        if (pDock->container.bDirectionUp)
207
 
                        {
208
 
                                fOffsetY = pIcon->fHeight * pIcon->fScale + pIcon->fDeltaYReflection;
209
 
                                glTranslatef (0., - fOffsetY, 0.);
210
 
                                glScalef (pIcon->fWidth * pIcon->fWidthFactor * pIcon->fScale, - pIcon->fHeight * pIcon->fScale, 1.);  // taille du reflet et on se retourne.
211
 
                        }
212
 
                        else
213
 
                        {
214
 
                                glTranslatef (0., fOffsetY, 0.);
215
 
                                glScalef (pIcon->fWidth * pIcon->fWidthFactor * pIcon->fScale, pDock->iIconSize * myIconsParam.fReflectHeightRatio * pDock->container.fRatio, 1.);
216
 
                        }
217
 
                }
218
 
                else
219
 
                {
220
 
                        if (pDock->container.bDirectionUp)
221
 
                        {
222
 
                                glTranslatef (fOffsetY, 0., 0.);
223
 
                                glScalef (- pDock->iIconSize * myIconsParam.fReflectHeightRatio * pDock->container.fRatio, pIcon->fWidth * pIcon->fWidthFactor * pIcon->fScale, 1.);
224
 
                        }
225
 
                        else
226
 
                        {
227
 
                                glTranslatef (- fOffsetY, 0., 0.);
228
 
                                glScalef (pDock->iIconSize * myIconsParam.fReflectHeightRatio * pDock->container.fRatio, pIcon->fWidth * pIcon->fWidthFactor * pIcon->fScale, 1.);
229
 
                        }
230
 
                }
231
 
                
232
 
                glActiveTexture(GL_TEXTURE0_ARB); // Go pour le multitexturing 1ere passe
233
 
                glEnable(GL_TEXTURE_2D); // On active le texturing sur cette passe
234
 
                glBindTexture(GL_TEXTURE_2D, pIcon->image.iTexture);
235
 
                
236
 
                glColor4f(1., 1., 1., myIconsParam.fAlbedo * pIcon->fAlpha);  // transparence du reflet.
237
 
                glEnable(GL_BLEND);
238
 
                glBlendFunc (1, 0);
239
 
                glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
240
 
                
241
 
                glActiveTexture(GL_TEXTURE1_ARB); // Go pour le texturing 2eme passe
242
 
                glEnable(GL_TEXTURE_2D);
243
 
                glBindTexture(GL_TEXTURE_2D, g_pGradationTexture[pDock->container.bIsHorizontal]);
244
 
                glColor4f(1., 1., 1., 1.);  // transparence du reflet.
245
 
                glEnable(GL_BLEND);
246
 
                glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
247
 
                glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // Le mode de combinaison des textures
248
 
                glTexEnvi (GL_TEXTURE_ENV, GL_COMBINE_ALPHA_EXT, GL_MODULATE);  // multiplier les alpha.
249
 
                /*glEnable(GL_TEXTURE_GEN_S);                                // generation texture en S
250
 
                glEnable(GL_TEXTURE_GEN_T);        // et en T
251
 
                glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); // la je veux un mapping tout ce qu'il y a de plus classique
252
 
                glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);*/
253
 
                glEnableClientState (GL_TEXTURE_COORD_ARRAY);
254
 
                glEnableClientState (GL_VERTEX_ARRAY);
255
 
                
256
 
                glTexCoordPointer (2, GL_FLOAT, 0, pData->pCoords);
257
 
                glVertexPointer (2, GL_FLOAT, 0, pData->pVertices);
258
 
                
259
 
                glDrawArrays (GL_QUAD_STRIP, 0, pData->iNumActiveNodes);
260
 
                
261
 
                glActiveTexture(GL_TEXTURE1_ARB);
262
 
                glDisable(GL_TEXTURE_2D);
263
 
                glDisableClientState (GL_TEXTURE_COORD_ARRAY);
264
 
                glDisableClientState (GL_VERTEX_ARRAY);
265
 
                glDisable(GL_TEXTURE_GEN_S);
266
 
                glDisable(GL_TEXTURE_GEN_T);
267
 
                glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
268
 
                glActiveTexture(GL_TEXTURE0_ARB);
269
 
                glDisable(GL_TEXTURE_2D);
270
 
                glDisable(GL_TEXTURE_GEN_S);
271
 
                glDisable(GL_TEXTURE_GEN_T);
272
 
                
273
 
                glPopMatrix ();
274
 
        }
275
 
        
276
 
        glDisableClientState (GL_TEXTURE_COORD_ARRAY);
277
 
        glDisableClientState (GL_VERTEX_ARRAY);
278
 
        glDisable (GL_TEXTURE_2D);
279
 
        glDisable (GL_BLEND);
280
 
}
281
 
 
282
 
 
283
 
void cd_animations_register_wave (void)
284
 
{
285
 
        CDAnimation *pAnimation = &myData.pAnimations[CD_ANIMATIONS_WAVE];
286
 
        pAnimation->cName = "wave";
287
 
        pAnimation->cDisplayedName = D_("Wave");
288
 
        pAnimation->id = CD_ANIMATIONS_WAVE;
289
 
        pAnimation->bDrawIcon = TRUE;
290
 
        pAnimation->bDrawReflect = FALSE;
291
 
        pAnimation->init = init;
292
 
        pAnimation->update = update;
293
 
        pAnimation->render = render;
294
 
        pAnimation->post_render = NULL;
295
 
        cd_animations_register_animation (pAnimation);
296
 
}