File indexing completed on 2023-12-03 04:14:49
0001 /* 0002 This file is part of the KDE games kwin4 program 0003 SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "scoresprite.h" 0009 0010 // own 0011 #include "kfourinline_debug.h" 0012 // KF 0013 #include <KConfig> 0014 #include <KConfigGroup> 0015 #include <KLocalizedString> 0016 // Qt 0017 #include <QFont> 0018 #include <QGraphicsScene> 0019 // Std 0020 #include <cmath> 0021 0022 // Constructor for the score sprite 0023 ScoreSprite::ScoreSprite(const QString &id, ThemeManager *theme, int no, QGraphicsScene *scene) 0024 : Themeable(id, theme) 0025 , PixmapSprite(no, scene) 0026 { 0027 // Create all sub sprites 0028 for (int i = 0; i < 2; i++) { 0029 mWon[i] = new QGraphicsTextItem(this); 0030 scene->addItem(mWon[i]); 0031 mDraw[i] = new QGraphicsTextItem(this); 0032 scene->addItem(mDraw[i]); 0033 mLoss[i] = new QGraphicsTextItem(this); 0034 scene->addItem(mLoss[i]); 0035 mBreak[i] = new QGraphicsTextItem(this); 0036 scene->addItem(mBreak[i]); 0037 mName[i] = new QGraphicsTextItem(this); 0038 scene->addItem(mName[i]); 0039 mInput[i] = new PixmapSprite(QStringLiteral("scoreinput%1").arg(i), theme, i, scene); 0040 if (!mInput[i]) 0041 qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" 0042 << "scoreinput" << i; 0043 mInput[i]->setParentItem(this); 0044 mInput[i]->setOffsetStatus(false); 0045 mInput[i]->show(); 0046 mInputFrame[i] = 0; 0047 } 0048 0049 // Default turn is nobody 0050 mTurn = -1; 0051 0052 // Redraw us 0053 if (theme) 0054 theme->updateTheme(this); 0055 } 0056 0057 // Destructor 0058 ScoreSprite::~ScoreSprite() 0059 { 0060 // Clean up 0061 for (int i = 0; i < 2; i++) { 0062 delete mWon[i]; 0063 delete mDraw[i]; 0064 delete mLoss[i]; 0065 delete mBreak[i]; 0066 delete mName[i]; 0067 delete mInput[i]; 0068 } 0069 } 0070 0071 // Redraw the theme. 0072 void ScoreSprite::changeTheme() 0073 { 0074 // The main display is handled by the parent 0075 PixmapSprite::changeTheme(); 0076 0077 // Retrieve our size 0078 double width = this->boundingRect().width(); 0079 double height = this->boundingRect().height(); 0080 0081 // Retrieve theme data 0082 KConfigGroup config = thememanager()->config(id()); 0083 QPointF posWon0 = config.readEntry("posWon0", QPointF(1.0, 1.0)); 0084 QPointF posWon1 = config.readEntry("posWon1", QPointF(1.0, 1.0)); 0085 QPointF posDraw0 = config.readEntry("posDraw0", QPointF(1.0, 1.0)); 0086 QPointF posDraw1 = config.readEntry("posDraw1", QPointF(1.0, 1.0)); 0087 QPointF posLoss0 = config.readEntry("posLoss0", QPointF(1.0, 1.0)); 0088 QPointF posLoss1 = config.readEntry("posLoss1", QPointF(1.0, 1.0)); 0089 QPointF posBreak0 = config.readEntry("posBreak0", QPointF(1.0, 1.0)); 0090 QPointF posBreak1 = config.readEntry("posBreak1", QPointF(1.0, 1.0)); 0091 QPointF posName0 = config.readEntry("posName0", QPointF(1.0, 1.0)); 0092 QPointF posName1 = config.readEntry("posName1", QPointF(1.0, 1.0)); 0093 // QPointF posAI = config.readEntry("posAI", QPointF(1.0,1.0)); 0094 0095 // Calculate proper font size 0096 double fontHeight = config.readEntry("fontHeight", 1.0); 0097 fontHeight *= height; 0098 double fontWidth = config.readEntry("fontWidth", 1.0); 0099 fontWidth *= width; 0100 0101 // Retrieve font color 0102 QColor fontColor[2]; 0103 fontColor[0] = config.readEntry("fontColorPlayer0", QColor(Qt::white)); 0104 fontColor[1] = config.readEntry("fontColorPlayer1", QColor(Qt::white)); 0105 0106 // Set position of sub sprites 0107 mWon[0]->setPos(posWon0.x() * width, posWon0.y() * height); 0108 mWon[1]->setPos(posWon1.x() * width, posWon1.y() * height); 0109 mDraw[0]->setPos(posDraw0.x() * width, posDraw0.y() * height); 0110 mDraw[1]->setPos(posDraw1.x() * width, posDraw1.y() * height); 0111 mLoss[0]->setPos(posLoss0.x() * width, posLoss0.y() * height); 0112 mLoss[1]->setPos(posLoss1.x() * width, posLoss1.y() * height); 0113 mBreak[0]->setPos(posBreak0.x() * width, posBreak0.y() * height); 0114 mBreak[1]->setPos(posBreak1.x() * width, posBreak1.y() * height); 0115 mName[0]->setPos(posName0.x() * width, posName0.y() * height); 0116 mName[1]->setPos(posName1.x() * width, posName1.y() * height); 0117 0118 // Create and set current font 0119 QFont font; 0120 font.setPixelSize(int(fontHeight)); 0121 0122 // Set font and color for all text items 0123 for (int i = 0; i < 2; i++) { 0124 mWon[i]->setFont(font); 0125 mDraw[i]->setFont(font); 0126 mLoss[i]->setFont(font); 0127 mBreak[i]->setFont(font); 0128 mName[i]->setFont(font); 0129 0130 mWon[i]->setDefaultTextColor(fontColor[i]); 0131 mDraw[i]->setDefaultTextColor(fontColor[i]); 0132 mLoss[i]->setDefaultTextColor(fontColor[i]); 0133 mBreak[i]->setDefaultTextColor(fontColor[i]); 0134 mName[i]->setDefaultTextColor(fontColor[i]); 0135 0136 mWon[i]->setTextWidth(fontWidth); 0137 mDraw[i]->setTextWidth(fontWidth); 0138 mLoss[i]->setTextWidth(fontWidth); 0139 mBreak[i]->setTextWidth(fontWidth); 0140 mName[i]->setTextWidth(fontWidth); 0141 0142 // Restore the frame of the input device sprite 0143 if (mInputFrame[i] >= 0) 0144 mInput[i]->setFrame(mInputFrame[i]); 0145 } 0146 0147 // Update next player 0148 if (mTurn >= 0) 0149 setTurn(mTurn); 0150 } 0151 0152 // QGI advance method 0153 void ScoreSprite::advance(int phase) 0154 { 0155 // Advance time and animation etc 0156 PixmapSprite::advance(phase); 0157 } 0158 0159 // Store and display the level of the AI 0160 void ScoreSprite::setLevel(int level, int no) 0161 { 0162 if (level >= 0) { 0163 mName[no]->setPlainText(i18nc("computer level", "Level %1", level)); 0164 update(); 0165 } 0166 } 0167 0168 // Store and display the name of a player 0169 void ScoreSprite::setPlayerName(const QString &s, int no) 0170 { 0171 mName[no]->setPlainText(s); 0172 update(); 0173 } 0174 0175 // Store and display amount of wins 0176 void ScoreSprite::setWon(const QString &s, int no) 0177 { 0178 mWon[no]->setPlainText(s); 0179 update(); 0180 } 0181 0182 // Store and display amount of draws 0183 void ScoreSprite::setDraw(const QString &s, int no) 0184 { 0185 mDraw[no]->setPlainText(s); 0186 update(); 0187 } 0188 0189 // Store and display amount of losses 0190 void ScoreSprite::setLoss(const QString &s, int no) 0191 { 0192 mLoss[no]->setPlainText(s); 0193 update(); 0194 } 0195 0196 // Store and display amount of breaks 0197 void ScoreSprite::setBreak(const QString &s, int no) 0198 { 0199 mBreak[no]->setPlainText(s); 0200 update(); 0201 } 0202 0203 // Store and display input device 0204 void ScoreSprite::setInput(int device, int no) 0205 { 0206 // Map KGameIO device numbers to sprite frames 0207 int frame; 0208 if (device == 8) 0209 frame = 2; // AI 0210 else if (device == 4) 0211 frame = 0; // Mouse 0212 else if (device == 2) 0213 frame = 1; // Key 0214 else 0215 frame = 3; // Network 0216 0217 mInputFrame[no] = frame; 0218 mInput[no]->setFrame(frame); 0219 update(); 0220 } 0221 0222 // Store and display current player. This is done by coloring the 0223 // name text sprite. 0224 void ScoreSprite::setTurn(int no) 0225 { 0226 // Retrieve theme data 0227 KConfigGroup config = thememanager()->config(id()); 0228 QColor fontColorActive = config.readEntry("fontColorActive", QColor(Qt::white)); 0229 QColor fontColor0 = config.readEntry("fontColorPlayer0", QColor(Qt::white)); 0230 QColor fontColor1 = config.readEntry("fontColorPlayer1", QColor(Qt::white)); 0231 0232 // Store data 0233 mTurn = no; 0234 0235 // Switch color 0236 if (no == 0) { 0237 mName[0]->setDefaultTextColor(fontColorActive); 0238 mName[1]->setDefaultTextColor(fontColor1); 0239 } else { 0240 mName[0]->setDefaultTextColor(fontColor0); 0241 mName[1]->setDefaultTextColor(fontColorActive); 0242 } 0243 0244 update(); 0245 }