not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kwin / effects / trackmouse.cpp
blob1d3573d05b0e424259e282e22178fd5da09e94e6
1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2006 Lubos Lunak <l.lunak@kde.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
21 #include "trackmouse.h"
23 #include <QTime>
25 #include <kwinconfig.h>
27 #include <kglobal.h>
28 #include <kstandarddirs.h>
30 #include <math.h>
32 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
33 #include <GL/gl.h>
34 #endif
36 #include <kdebug.h>
38 namespace KWin
41 KWIN_EFFECT( trackmouse, TrackMouseEffect )
43 const int STARS = 5;
44 const int DIST = 50;
46 TrackMouseEffect::TrackMouseEffect()
47 : active( false )
48 , angle( 0 )
49 , texture( NULL )
53 TrackMouseEffect::~TrackMouseEffect()
55 delete texture;
58 void TrackMouseEffect::prePaintScreen( ScreenPrePaintData& data, int time )
60 if( active ) {
61 QTime t = QTime::currentTime();
62 angle = ((t.second() % 4) * 90.0) + (t.msec() / 1000.0 * 90.0);
64 effects->prePaintScreen( data, time );
67 void TrackMouseEffect::paintScreen( int mask, QRegion region, ScreenPaintData& data )
69 effects->paintScreen( mask, region, data ); // paint normal screen
70 if( !active )
71 return;
72 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
73 if( texture )
75 glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
76 texture->bind();
77 glEnable( GL_BLEND );
78 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
79 for( int i = 0;
80 i < STARS;
81 ++i )
83 QRect r = starRect( i );
84 texture->render( region, r );
86 texture->unbind();
87 glPopAttrib();
89 #endif
92 void TrackMouseEffect::postPaintScreen()
94 if( active )
96 for( int i = 0;
97 i < STARS;
98 ++i )
99 effects->addRepaint( starRect( i ));
101 effects->postPaintScreen();
104 void TrackMouseEffect::mouseChanged( const QPoint&, const QPoint&, Qt::MouseButtons,
105 Qt::MouseButtons, Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers )
107 if( modifiers == ( Qt::CTRL | Qt::META ))
109 if( !active )
111 if( texture == NULL )
112 loadTexture();
113 if( texture == NULL )
114 return;
115 active = true;
116 angle = 0;
118 for( int i = 0;
119 i < STARS;
120 ++i )
121 effects->addRepaint( starRect( i ));
123 else
125 if( active )
127 for( int i = 0;
128 i < STARS;
129 ++i )
130 effects->addRepaint( starRect( i ));
131 active = false;
136 QRect TrackMouseEffect::starRect( int num ) const
138 int a = angle + 360 / STARS * num;
139 int x = cursorPos().x() + int( DIST * cos( a * ( 2 * M_PI / 360 )));
140 int y = cursorPos().y() + int( DIST * sin( a * ( 2 * M_PI / 360 )));
141 return QRect( QPoint( x - textureSize.width() / 2,
142 y - textureSize.height() / 2 ), textureSize );
145 void TrackMouseEffect::loadTexture()
147 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
148 QString file = KGlobal::dirs()->findResource( "appdata", "trackmouse.png" );
149 if( file.isEmpty())
150 return;
151 QImage im( file );
152 texture = new GLTexture( im );
153 textureSize = im.size();
154 #endif
157 } // namespace