scoresprite.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE games kwin4 program
00003    Copyright (c) 2006 Martin Heni <kde@heni-online.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // General includes
00022 #include <math.h>
00023 
00024 // Qt includes
00025 #include <QSizeF>
00026 #include <QFont>
00027 
00028 // KDE includes
00029 #include <kdebug.h>
00030 #include <kconfig.h>
00031 #include <klocale.h>
00032 
00033 // Local includes
00034 #include "scoresprite.h"
00035 
00036 // Constructor for the score sprite
00037 ScoreSprite::ScoreSprite(QString id, ThemeManager* theme, int advancePeriod, int no, QGraphicsScene* scene)
00038            :  Themable(id, theme), PixmapSprite(advancePeriod, no, scene)
00039 {
00040         // Create all sub sprites
00041   for (int i=0; i<2; i++)
00042   {
00043     mWon[i]   = new QGraphicsTextItem(this, scene);
00044     mDraw[i]  = new QGraphicsTextItem(this, scene);
00045     mLoss[i]  = new QGraphicsTextItem(this, scene);
00046     mBreak[i] = new QGraphicsTextItem(this, scene);
00047     mName[i]  = new QGraphicsTextItem(this, scene);
00048     mInput[i] = new PixmapSprite(QString("scoreinput%1").arg(i), theme, advancePeriod, i, scene);
00049     if (!mInput[i]) kFatal() << "Cannot load sprite " << "scoreinput"<<i << endl;
00050     mInput[i]->setParentItem(this);
00051     mInputFrame[i] = 0;
00052   }
00053 
00054   // Default turn is nobody
00055   mTurn  = -1;
00056 
00057   // Redraw us
00058   if (theme) theme->updateTheme(this);
00059 
00060 }
00061 
00062 
00063 // Destructor
00064 ScoreSprite::~ScoreSprite()
00065 {
00066         // Clean up
00067   for (int i=0; i<2; i++)
00068   {
00069     delete mWon[i];
00070     delete mDraw[i];
00071     delete mLoss[i];
00072     delete mBreak[i];
00073     delete mName[i];
00074     delete mInput[i];
00075   }
00076 }
00077 
00078 
00079 // Redraw the theme.
00080 void ScoreSprite::changeTheme()
00081 {
00082         // Teh main display is handled by theparent
00083   PixmapSprite::changeTheme();
00084   
00085   // Retrieve our size
00086   double width  = this->boundingRect().width();
00087   double height = this->boundingRect().height();
00088 
00089   // Retrieve theme data
00090   KConfigGroup config = thememanager()->config(id());
00091   QPointF posWon0     = config.readEntry("posWon0", QPointF(1.0,1.0));
00092   QPointF posWon1     = config.readEntry("posWon1", QPointF(1.0,1.0));
00093   QPointF posDraw0    = config.readEntry("posDraw0", QPointF(1.0,1.0));
00094   QPointF posDraw1    = config.readEntry("posDraw1", QPointF(1.0,1.0));
00095   QPointF posLoss0    = config.readEntry("posLoss0", QPointF(1.0,1.0));
00096   QPointF posLoss1    = config.readEntry("posLoss1", QPointF(1.0,1.0));
00097   QPointF posBreak0   = config.readEntry("posBreak0", QPointF(1.0,1.0));
00098   QPointF posBreak1   = config.readEntry("posBreak1", QPointF(1.0,1.0));
00099   QPointF posName0    = config.readEntry("posName0", QPointF(1.0,1.0));
00100   QPointF posName1    = config.readEntry("posName1", QPointF(1.0,1.0));
00101   QPointF posAI       = config.readEntry("posAI", QPointF(1.0,1.0));
00102 
00103   // Calculate proper font size
00104   double fontHeight = config.readEntry("fontHeight", 1.0);
00105   fontHeight *= height;
00106   double fontWidth = config.readEntry("fontWidth", 1.0);
00107   fontWidth *= width;
00108   
00109   // Retrieve font color
00110   QColor fontColor[2];
00111   fontColor[0] = config.readEntry("fontColorPlayer0", Qt::white);
00112   fontColor[1] = config.readEntry("fontColorPlayer1", Qt::white);
00113 
00114   // Set position of sub sprites
00115   mWon[0]->setPos(posWon0.x()*width, posWon0.y()*height);
00116   mWon[1]->setPos(posWon1.x()*width, posWon1.y()*height);
00117   mDraw[0]->setPos(posDraw0.x()*width, posDraw0.y()*height);
00118   mDraw[1]->setPos(posDraw1.x()*width, posDraw1.y()*height);
00119   mLoss[0]->setPos(posLoss0.x()*width, posLoss0.y()*height);
00120   mLoss[1]->setPos(posLoss1.x()*width, posLoss1.y()*height);
00121   mBreak[0]->setPos(posBreak0.x()*width, posBreak0.y()*height);
00122   mBreak[1]->setPos(posBreak1.x()*width, posBreak1.y()*height);
00123   mName[0]->setPos(posName0.x()*width, posName0.y()*height);
00124   mName[1]->setPos(posName1.x()*width, posName1.y()*height);
00125 
00126 
00127   // Create and set current font
00128   QFont font;
00129   font.setPixelSize(int(fontHeight));
00130 
00131   // Set font and color for all text items
00132   for (int i=0; i<2; i++)
00133   {
00134     mWon[i]->setFont(font);   
00135     mDraw[i]->setFont(font); 
00136     mLoss[i]->setFont(font);
00137     mBreak[i]->setFont(font);
00138     mName[i]->setFont(font);
00139     
00140     mWon[i]->setDefaultTextColor(fontColor[i]);   
00141     mDraw[i]->setDefaultTextColor(fontColor[i]);
00142     mLoss[i]->setDefaultTextColor(fontColor[i]);
00143     mBreak[i]->setDefaultTextColor(fontColor[i]);
00144     mName[i]->setDefaultTextColor(fontColor[i]);
00145 
00146     mWon[i]->setTextWidth(fontWidth);   
00147     mDraw[i]->setTextWidth(fontWidth);
00148     mLoss[i]->setTextWidth(fontWidth);
00149     mBreak[i]->setTextWidth(fontWidth);
00150     mName[i]->setTextWidth(fontWidth);
00151 
00152     // Restore the frame of the input device sprite
00153     if (mInputFrame[i]>=0) mInput[i]->setFrame(mInputFrame[i]);
00154   }
00155 
00156   // Update next player
00157   if (mTurn>=0) setTurn(mTurn);
00158 }
00159 
00160 
00161 // QGI advance method 
00162 void ScoreSprite::advance(int phase)
00163 {
00164   // Advance time and animation etc
00165   PixmapSprite::advance(phase);
00166 }
00167 
00168 
00169 // Store and display the level of the AI
00170 void ScoreSprite::setLevel(int level, int no)
00171 {
00172   if (level >= 0)
00173   {
00174     mName[no]->setPlainText(i18nc("computer level","Level %1", level));
00175     update();
00176   }
00177 }
00178 
00179 
00180 // Store and display the name of a player
00181 void ScoreSprite::setPlayerName(QString s,int no)
00182 {
00183   mName[no]->setPlainText(s);
00184   update();
00185 }
00186 
00187 
00188 // Store and display amount of wins
00189 void ScoreSprite::setWon(QString s,int no)
00190 {
00191   mWon[no]->setPlainText(s);
00192   update();
00193 }
00194 
00195 
00196 // Store and display amount of draws
00197 void ScoreSprite::setDraw(QString s,int no)
00198 {
00199   mDraw[no]->setPlainText(s);
00200   update();
00201 }
00202 
00203 
00204 // Store and display amount of losses
00205 void ScoreSprite::setLoss(QString s,int no)
00206 {
00207   mLoss[no]->setPlainText(s);
00208   update();
00209 }
00210 
00211 
00212 // Store and display amount of breaks
00213 void ScoreSprite::setBreak(QString s,int no)
00214 {
00215   mBreak[no]->setPlainText(s);
00216   update();
00217 }
00218 
00219 
00220 
00221 // Store and display input device
00222 void ScoreSprite::setInput(int device, int no)
00223 {
00224   // Map KGameIO device numbers to sprite frames
00225   int frame;
00226   if (device == 8) frame = 2; // AI
00227   else if (device == 4) frame = 0; // Mouse
00228   else if (device == 2) frame = 1; // Key
00229   else frame = 3; //Network
00230 
00231   mInputFrame[no] = frame;
00232   mInput[no]->setFrame(frame);
00233   update();
00234 }
00235 
00236 
00237 
00238 // Store and display current player. This is done by coloring the
00239 // name text sprite.
00240 void ScoreSprite::setTurn(int no)
00241 {
00242         // Retrieve theme data
00243   KConfigGroup config     = thememanager()->config(id());
00244   QColor fontColorActive  = config.readEntry("fontColorActive", Qt::white);
00245   QColor fontColor0       = config.readEntry("fontColorPlayer0", Qt::white);
00246   QColor fontColor1       = config.readEntry("fontColorPlayer1", Qt::white);
00247 
00248   // Store data
00249   mTurn = no;
00250 
00251   // Switch color
00252   if (no==0)
00253   {
00254     mName[0]->setDefaultTextColor(fontColorActive);
00255     mName[1]->setDefaultTextColor(fontColor1);
00256   }
00257   else
00258   {
00259     mName[0]->setDefaultTextColor(fontColor0);
00260     mName[1]->setDefaultTextColor(fontColorActive);
00261   }
00262 
00263   update();
00264 }
00265 
00266 

Generated on Sun Mar 4 10:56:43 2007 for KWin4 by  doxygen 1.4.6