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