1 /*
2     This file is part of the KDE games kwin4 program
3     SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "scoresprite.h"
9 
10 // own
11 #include "kfourinline_debug.h"
12 // KF
13 #include <KConfig>
14 #include <KConfigGroup>
15 #include <KLocalizedString>
16 // Qt
17 #include <QFont>
18 #include <QGraphicsScene>
19 // Std
20 #include <cmath>
21 
22 // Constructor for the score sprite
ScoreSprite(const QString & id,ThemeManager * theme,int no,QGraphicsScene * scene)23 ScoreSprite::ScoreSprite(const QString &id, ThemeManager* theme, int no, QGraphicsScene* scene)
24            :  Themeable(id, theme), PixmapSprite(no, scene)
25 {
26 	// Create all sub sprites
27   for (int i=0; i<2; i++)
28   {
29     mWon[i]   = new QGraphicsTextItem(this);
30     scene->addItem(mWon[i]);
31     mDraw[i]  = new QGraphicsTextItem(this);
32     scene->addItem(mDraw[i]);
33     mLoss[i]  = new QGraphicsTextItem(this);
34     scene->addItem(mLoss[i]);
35     mBreak[i] = new QGraphicsTextItem(this);
36     scene->addItem(mBreak[i]);
37     mName[i]  = new QGraphicsTextItem(this);
38     scene->addItem(mName[i]);
39     mInput[i] = new PixmapSprite(QStringLiteral("scoreinput%1").arg(i), theme, i, scene);
40     if (!mInput[i]) qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" << "scoreinput"<<i;
41     mInput[i]->setParentItem(this);
42     mInput[i]->setOffsetStatus(false);
43     mInput[i]->show();
44     mInputFrame[i] = 0;
45   }
46 
47   // Default turn is nobody
48   mTurn  = -1;
49 
50   // Redraw us
51   if (theme) theme->updateTheme(this);
52 
53 }
54 
55 
56 // Destructor
~ScoreSprite()57 ScoreSprite::~ScoreSprite()
58 {
59 	// Clean up
60   for (int i=0; i<2; i++)
61   {
62     delete mWon[i];
63     delete mDraw[i];
64     delete mLoss[i];
65     delete mBreak[i];
66     delete mName[i];
67     delete mInput[i];
68   }
69 }
70 
71 
72 // Redraw the theme.
changeTheme()73 void ScoreSprite::changeTheme()
74 {
75   // The main display is handled by the parent
76   PixmapSprite::changeTheme();
77 
78   // Retrieve our size
79   double width  = this->boundingRect().width();
80   double height = this->boundingRect().height();
81 
82   // Retrieve theme data
83   KConfigGroup config = thememanager()->config(id());
84   QPointF posWon0     = config.readEntry("posWon0", QPointF(1.0,1.0));
85   QPointF posWon1     = config.readEntry("posWon1", QPointF(1.0,1.0));
86   QPointF posDraw0    = config.readEntry("posDraw0", QPointF(1.0,1.0));
87   QPointF posDraw1    = config.readEntry("posDraw1", QPointF(1.0,1.0));
88   QPointF posLoss0    = config.readEntry("posLoss0", QPointF(1.0,1.0));
89   QPointF posLoss1    = config.readEntry("posLoss1", QPointF(1.0,1.0));
90   QPointF posBreak0   = config.readEntry("posBreak0", QPointF(1.0,1.0));
91   QPointF posBreak1   = config.readEntry("posBreak1", QPointF(1.0,1.0));
92   QPointF posName0    = config.readEntry("posName0", QPointF(1.0,1.0));
93   QPointF posName1    = config.readEntry("posName1", QPointF(1.0,1.0));
94   //QPointF posAI       = config.readEntry("posAI", QPointF(1.0,1.0));
95 
96   // Calculate proper font size
97   double fontHeight = config.readEntry("fontHeight", 1.0);
98   fontHeight *= height;
99   double fontWidth = config.readEntry("fontWidth", 1.0);
100   fontWidth *= width;
101 
102   // Retrieve font color
103   QColor fontColor[2];
104   fontColor[0] = config.readEntry("fontColorPlayer0", QColor(Qt::white));
105   fontColor[1] = config.readEntry("fontColorPlayer1", QColor(Qt::white));
106 
107   // Set position of sub sprites
108   mWon[0]->setPos(posWon0.x()*width, posWon0.y()*height);
109   mWon[1]->setPos(posWon1.x()*width, posWon1.y()*height);
110   mDraw[0]->setPos(posDraw0.x()*width, posDraw0.y()*height);
111   mDraw[1]->setPos(posDraw1.x()*width, posDraw1.y()*height);
112   mLoss[0]->setPos(posLoss0.x()*width, posLoss0.y()*height);
113   mLoss[1]->setPos(posLoss1.x()*width, posLoss1.y()*height);
114   mBreak[0]->setPos(posBreak0.x()*width, posBreak0.y()*height);
115   mBreak[1]->setPos(posBreak1.x()*width, posBreak1.y()*height);
116   mName[0]->setPos(posName0.x()*width, posName0.y()*height);
117   mName[1]->setPos(posName1.x()*width, posName1.y()*height);
118 
119 
120   // Create and set current font
121   QFont font;
122   font.setPixelSize(int(fontHeight));
123 
124   // Set font and color for all text items
125   for (int i=0; i<2; i++)
126   {
127     mWon[i]->setFont(font);
128     mDraw[i]->setFont(font);
129     mLoss[i]->setFont(font);
130     mBreak[i]->setFont(font);
131     mName[i]->setFont(font);
132 
133     mWon[i]->setDefaultTextColor(fontColor[i]);
134     mDraw[i]->setDefaultTextColor(fontColor[i]);
135     mLoss[i]->setDefaultTextColor(fontColor[i]);
136     mBreak[i]->setDefaultTextColor(fontColor[i]);
137     mName[i]->setDefaultTextColor(fontColor[i]);
138 
139     mWon[i]->setTextWidth(fontWidth);
140     mDraw[i]->setTextWidth(fontWidth);
141     mLoss[i]->setTextWidth(fontWidth);
142     mBreak[i]->setTextWidth(fontWidth);
143     mName[i]->setTextWidth(fontWidth);
144 
145     // Restore the frame of the input device sprite
146     if (mInputFrame[i]>=0) mInput[i]->setFrame(mInputFrame[i]);
147   }
148 
149   // Update next player
150   if (mTurn>=0) setTurn(mTurn);
151 }
152 
153 
154 // QGI advance method
advance(int phase)155 void ScoreSprite::advance(int phase)
156 {
157   // Advance time and animation etc
158   PixmapSprite::advance(phase);
159 }
160 
161 
162 // Store and display the level of the AI
setLevel(int level,int no)163 void ScoreSprite::setLevel(int level, int no)
164 {
165   if (level >= 0)
166   {
167     mName[no]->setPlainText(i18nc("computer level","Level %1", level));
168     update();
169   }
170 }
171 
172 
173 // Store and display the name of a player
setPlayerName(const QString & s,int no)174 void ScoreSprite::setPlayerName(const QString &s,int no)
175 {
176   mName[no]->setPlainText(s);
177   update();
178 }
179 
180 
181 // Store and display amount of wins
setWon(const QString & s,int no)182 void ScoreSprite::setWon(const QString &s,int no)
183 {
184   mWon[no]->setPlainText(s);
185   update();
186 }
187 
188 
189 // Store and display amount of draws
setDraw(const QString & s,int no)190 void ScoreSprite::setDraw(const QString &s,int no)
191 {
192   mDraw[no]->setPlainText(s);
193   update();
194 }
195 
196 
197 // Store and display amount of losses
setLoss(const QString & s,int no)198 void ScoreSprite::setLoss(const QString &s,int no)
199 {
200   mLoss[no]->setPlainText(s);
201   update();
202 }
203 
204 
205 // Store and display amount of breaks
setBreak(const QString & s,int no)206 void ScoreSprite::setBreak(const QString &s,int no)
207 {
208   mBreak[no]->setPlainText(s);
209   update();
210 }
211 
212 
213 
214 // Store and display input device
setInput(int device,int no)215 void ScoreSprite::setInput(int device, int no)
216 {
217   // Map KGameIO device numbers to sprite frames
218   int frame;
219   if (device == 8) frame = 2; // AI
220   else if (device == 4) frame = 0; // Mouse
221   else if (device == 2) frame = 1; // Key
222   else frame = 3; //Network
223 
224   mInputFrame[no] = frame;
225   mInput[no]->setFrame(frame);
226   update();
227 }
228 
229 
230 
231 // Store and display current player. This is done by coloring the
232 // name text sprite.
setTurn(int no)233 void ScoreSprite::setTurn(int no)
234 {
235 	// Retrieve theme data
236   KConfigGroup config     = thememanager()->config(id());
237   QColor fontColorActive  = config.readEntry("fontColorActive", QColor(Qt::white));
238   QColor fontColor0       = config.readEntry("fontColorPlayer0", QColor(Qt::white));
239   QColor fontColor1       = config.readEntry("fontColorPlayer1", QColor(Qt::white));
240 
241   // Store data
242   mTurn = no;
243 
244   // Switch color
245   if (no==0)
246   {
247     mName[0]->setDefaultTextColor(fontColorActive);
248     mName[1]->setDefaultTextColor(fontColor1);
249   }
250   else
251   {
252     mName[0]->setDefaultTextColor(fontColor0);
253     mName[1]->setDefaultTextColor(fontColorActive);
254   }
255 
256   update();
257 }
258 
259 
260