File indexing completed on 2023-09-24 08:17:46

0001 /*
0002     This file is part of the KDE games lskat 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 "display_intro.h"
0009 
0010 // General includes
0011 #include <cmath>
0012 
0013 // Qt includes
0014 #include <QPoint>
0015 
0016 // KF includes
0017 #include <KLocalizedString>
0018 #include <KConfigGroup>
0019 
0020 // Local includes
0021 #include "cardsprite.h"
0022 #include "textsprite.h"
0023 
0024 #define WAIT_CNT       100  /* Wait this [ms] before clearing board */
0025 
0026 // Constructor for the display
0027 DisplayIntro::DisplayIntro(Deck *deck, QGraphicsScene *theScene, ThemeManager *theme,
0028                            int advancePeriod, QGraphicsView *parent)
0029             : Themable(QStringLiteral("display_intro"), theme), AbstractDisplay(deck, theScene, theme, advancePeriod, parent)
0030 {
0031     mTextShown = false;
0032 
0033     // Choose a background color
0034     scene()->setBackgroundBrush(QColor(0, 0, 128));
0035 
0036     mTimer = new QTimer(this);
0037     connect(mTimer, &QTimer::timeout, this, &DisplayIntro::loop);
0038     mTimer->stop();
0039 
0040     // Redraw
0041     if (theme) theme->updateTheme(this);
0042 }
0043 
0044 // Called by thememanager when theme or theme geometry changes. Redraw and resize
0045 // this display.
0046 void DisplayIntro::changeTheme()
0047 {
0048     // Retrieve theme data
0049     KConfigGroup config = thememanager()->config(id());
0050 
0051     // Retrieve background pixmap
0052     QString bgsvgid = config.readEntry("background-svgid");
0053     QPixmap pixmap  = thememanager()->getPixmap(bgsvgid, scene()->sceneRect().size().toSize());
0054     scene()->setBackgroundBrush(pixmap);
0055     mView->update();
0056 }
0057 
0058 // Start the intro.
0059 void DisplayIntro::start()
0060 {
0061     mAnimCnt = 0;
0062     mState   = Putting;
0063     mTimer->start(50);
0064 
0065     // Stop all card sprites
0066     for (int i = 0; i < mCards.size(); i++)
0067     {
0068         mCards[i]->stop();
0069     }
0070 }
0071 
0072 // Animation loop
0073 void DisplayIntro::loop()
0074 {
0075     int no = mCards.size();
0076     // Catch no card error
0077     if (no < 1) return;
0078 
0079     // Retrieve theme data
0080     KConfigGroup cardconfig = thememanager()->config(QStringLiteral("card"));
0081     double card_width       = cardconfig.readEntry("width", 1.0);
0082     KConfigGroup config     = thememanager()->config(id());
0083     QPointF start_shift     = config.readEntry("start-shift", QPointF(1.0, 1.0));
0084     QPointF start_pos       = config.readEntry("start-pos", QPointF(1.0, 1.0));
0085     double time_clear_in    = config.readEntry("time-clear-in", 1.0);
0086     double time_clear_out   = config.readEntry("time-clear-out", 1.0);
0087     double aspectRatio      = thememanager()->aspectRatio();
0088 
0089     // Display the intro text delayed
0090     if (mAnimCnt == 2  && mState == Putting && !mTextShown)
0091     {
0092         mTextShown = true;
0093         QString s1 = i18nc("Title of the game - line 1", "Lieutenant Skat");
0094         QString s2 = i18nc("Title of the game - line 2", "for");
0095         QString s3 = i18nc("Title of the game - line 3", "K D E");
0096 
0097         // Text sprite title foreground
0098         TextSprite *text1a = new TextSprite(s1, QStringLiteral("name-front"), mTheme, scene());
0099         mSprites.append(text1a);
0100         text1a->show();
0101 
0102         // Text sprite title background
0103         TextSprite *text1b = new TextSprite(s1, QStringLiteral("name-back"), mTheme, scene());
0104         mSprites.append(text1b);
0105         text1b->show();
0106 
0107         // Text sprite title foreground
0108         TextSprite *text2a = new TextSprite(s2, QStringLiteral("for-front"), mTheme, scene());
0109         mSprites.append(text2a);
0110         text2a->show();
0111 
0112         // Text sprite title background
0113         TextSprite *text2b = new TextSprite(s2, QStringLiteral("for-back"), mTheme, scene());
0114         mSprites.append(text2b);
0115         text2b->show();
0116 
0117         // Text sprite title foreground
0118         TextSprite *text3a = new TextSprite(s3, QStringLiteral("kde-front"), mTheme, scene());
0119         mSprites.append(text3a);
0120         text3a->show();
0121 
0122         // Text sprite title background
0123         TextSprite *text3b = new TextSprite(s3, QStringLiteral("kde-back"), mTheme, scene());
0124         mSprites.append(text3b);
0125         text3b->show();
0126     }
0127 
0128     // Display a card
0129     if (mAnimCnt < no && mState == Putting)
0130     {
0131         double factor = double(mAnimCnt) / double(no - 1);
0132         double fsin = sin(factor * M_PI);
0133 
0134         CardSprite *sprite = mCards[mAnimCnt];
0135 
0136         QPointF pos;
0137         if (mAnimCnt % 2 == 0)
0138         {
0139             pos  = QPointF(start_pos.x(), start_pos.y());
0140             pos += QPointF(start_shift.x() * fsin, start_shift.y() * factor);
0141         }
0142         else
0143         {
0144             pos  = QPointF(1.0 - start_pos.x() - card_width, start_pos.y());
0145             pos += QPointF(-start_shift.x() * fsin, start_shift.y() * factor);
0146         }
0147         sprite->setBackside();
0148         sprite->setPosition(pos);
0149         sprite->setZValue(50 + mAnimCnt);
0150         sprite->show();
0151         mAnimCnt++;
0152     }
0153     // Change state to turning
0154     else if (mState == Putting)
0155     {
0156         mState   = Turning;
0157         mAnimCnt = 0;
0158     }
0159     // Turn cards
0160     else if (mAnimCnt < no && mState == Turning)
0161     {
0162         CardSprite *sprite = mCards[mAnimCnt];
0163         sprite->setTurning(true);
0164         mAnimCnt++;
0165     }
0166     // Change state to waiting
0167     else if (mState == Turning)
0168     {
0169         mState   = Waiting;
0170         mAnimCnt = 0;
0171     }
0172     // Wait
0173     else if (mAnimCnt < WAIT_CNT && mState == Waiting)
0174     {
0175         mAnimCnt++;
0176     }
0177     // Change state to clearing the board
0178     else if (mState == Waiting)
0179     {
0180         mState   = Clearing;
0181         mAnimCnt = 0;
0182     }
0183     // Clear the board, step 1
0184     else if (mAnimCnt == 0 && mState == Clearing)
0185     {
0186         for (int i = 0; i < no; i++)
0187         {
0188             CardSprite *sprite = mCards[i];
0189             sprite->setMove(QPointF((1.0 - card_width) / 2.0, (1.0 / aspectRatio - card_width) / 2.0), time_clear_in);
0190         }
0191         mAnimCnt++;
0192     }
0193     // Clear the board, step 2
0194     else if (mAnimCnt < 30 && mState == Clearing)
0195     {
0196         mAnimCnt++;
0197     }
0198     // Clear the board, step 3 and change state to waiting
0199     else if (mState == Clearing)
0200     {
0201         for (int i = 0; i < no; i++)
0202         {
0203             double r = 1.0;
0204             double x = r * cos(double(i) / double(no - 1) * M_PI * 2.0) + 0.5;
0205             double y = r * sin(double(i) / double(no - 1) * M_PI * 2.0) + 0.5;
0206             CardSprite *sprite = mCards[i];
0207             sprite->setMove(QPointF(x, y / aspectRatio), time_clear_out);
0208         }
0209         mState = Waiting2;
0210         mAnimCnt = 0;
0211     }
0212     // Wait
0213     else if (mAnimCnt < WAIT_CNT && mState == Waiting2)
0214     {
0215         mAnimCnt++;
0216     }
0217     // Restart cycle
0218     else if (mState == Waiting2)
0219     {
0220         for (int i = 0; i < no; i++)
0221         {
0222             CardSprite *sprite = mCards[i];
0223             sprite->stop();
0224         }
0225 
0226         mState   = Putting;
0227         mAnimCnt = 0;
0228     }
0229 }
0230 
0231 #include "moc_display_intro.cpp"