File indexing completed on 2024-03-24 04:08:02

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 (CardSprite *sprite : std::as_const(mCards)) {
0067         sprite->stop();
0068     }
0069 }
0070 
0071 // Animation loop
0072 void DisplayIntro::loop()
0073 {
0074     int no = mCards.size();
0075     // Catch no card error
0076     if (no < 1) return;
0077 
0078     // Retrieve theme data
0079     KConfigGroup cardconfig = thememanager()->config(QStringLiteral("card"));
0080     double card_width       = cardconfig.readEntry("width", 1.0);
0081     KConfigGroup config     = thememanager()->config(id());
0082     QPointF start_shift     = config.readEntry("start-shift", QPointF(1.0, 1.0));
0083     QPointF start_pos       = config.readEntry("start-pos", QPointF(1.0, 1.0));
0084     double time_clear_in    = config.readEntry("time-clear-in", 1.0);
0085     double time_clear_out   = config.readEntry("time-clear-out", 1.0);
0086     double aspectRatio      = thememanager()->aspectRatio();
0087 
0088     // Display the intro text delayed
0089     if (mAnimCnt == 2  && mState == Putting && !mTextShown)
0090     {
0091         mTextShown = true;
0092         QString s1 = i18nc("Title of the game - line 1", "Lieutenant Skat");
0093         QString s2 = i18nc("Title of the game - line 2", "for");
0094         QString s3 = i18nc("Title of the game - line 3", "K D E");
0095 
0096         mSprites.reserve(6);
0097 
0098         // Text sprite title foreground
0099         TextSprite *text1a = new TextSprite(s1, QStringLiteral("name-front"), mTheme, scene());
0100         mSprites.append(text1a);
0101         text1a->show();
0102 
0103         // Text sprite title background
0104         TextSprite *text1b = new TextSprite(s1, QStringLiteral("name-back"), mTheme, scene());
0105         mSprites.append(text1b);
0106         text1b->show();
0107 
0108         // Text sprite title foreground
0109         TextSprite *text2a = new TextSprite(s2, QStringLiteral("for-front"), mTheme, scene());
0110         mSprites.append(text2a);
0111         text2a->show();
0112 
0113         // Text sprite title background
0114         TextSprite *text2b = new TextSprite(s2, QStringLiteral("for-back"), mTheme, scene());
0115         mSprites.append(text2b);
0116         text2b->show();
0117 
0118         // Text sprite title foreground
0119         TextSprite *text3a = new TextSprite(s3, QStringLiteral("kde-front"), mTheme, scene());
0120         mSprites.append(text3a);
0121         text3a->show();
0122 
0123         // Text sprite title background
0124         TextSprite *text3b = new TextSprite(s3, QStringLiteral("kde-back"), mTheme, scene());
0125         mSprites.append(text3b);
0126         text3b->show();
0127     }
0128 
0129     // Display a card
0130     if (mAnimCnt < no && mState == Putting)
0131     {
0132         double factor = double(mAnimCnt) / double(no - 1);
0133         double fsin = sin(factor * M_PI);
0134 
0135         CardSprite *sprite = mCards[mAnimCnt];
0136 
0137         QPointF pos;
0138         if (mAnimCnt % 2 == 0)
0139         {
0140             pos  = QPointF(start_pos.x(), start_pos.y());
0141             pos += QPointF(start_shift.x() * fsin, start_shift.y() * factor);
0142         }
0143         else
0144         {
0145             pos  = QPointF(1.0 - start_pos.x() - card_width, start_pos.y());
0146             pos += QPointF(-start_shift.x() * fsin, start_shift.y() * factor);
0147         }
0148         sprite->setBackside();
0149         sprite->setPosition(pos);
0150         sprite->setZValue(50 + mAnimCnt);
0151         sprite->show();
0152         mAnimCnt++;
0153     }
0154     // Change state to turning
0155     else if (mState == Putting)
0156     {
0157         mState   = Turning;
0158         mAnimCnt = 0;
0159     }
0160     // Turn cards
0161     else if (mAnimCnt < no && mState == Turning)
0162     {
0163         CardSprite *sprite = mCards[mAnimCnt];
0164         sprite->setTurning(true);
0165         mAnimCnt++;
0166     }
0167     // Change state to waiting
0168     else if (mState == Turning)
0169     {
0170         mState   = Waiting;
0171         mAnimCnt = 0;
0172     }
0173     // Wait
0174     else if (mAnimCnt < WAIT_CNT && mState == Waiting)
0175     {
0176         mAnimCnt++;
0177     }
0178     // Change state to clearing the board
0179     else if (mState == Waiting)
0180     {
0181         mState   = Clearing;
0182         mAnimCnt = 0;
0183     }
0184     // Clear the board, step 1
0185     else if (mAnimCnt == 0 && mState == Clearing)
0186     {
0187         for (int i = 0; i < no; i++)
0188         {
0189             CardSprite *sprite = mCards[i];
0190             sprite->setMove(QPointF((1.0 - card_width) / 2.0, (1.0 / aspectRatio - card_width) / 2.0), time_clear_in);
0191         }
0192         mAnimCnt++;
0193     }
0194     // Clear the board, step 2
0195     else if (mAnimCnt < 30 && mState == Clearing)
0196     {
0197         mAnimCnt++;
0198     }
0199     // Clear the board, step 3 and change state to waiting
0200     else if (mState == Clearing)
0201     {
0202         for (int i = 0; i < no; i++)
0203         {
0204             double r = 1.0;
0205             double x = r * cos(double(i) / double(no - 1) * M_PI * 2.0) + 0.5;
0206             double y = r * sin(double(i) / double(no - 1) * M_PI * 2.0) + 0.5;
0207             CardSprite *sprite = mCards[i];
0208             sprite->setMove(QPointF(x, y / aspectRatio), time_clear_out);
0209         }
0210         mState = Waiting2;
0211         mAnimCnt = 0;
0212     }
0213     // Wait
0214     else if (mAnimCnt < WAIT_CNT && mState == Waiting2)
0215     {
0216         mAnimCnt++;
0217     }
0218     // Restart cycle
0219     else if (mState == Waiting2)
0220     {
0221         for (int i = 0; i < no; i++)
0222         {
0223             CardSprite *sprite = mCards[i];
0224             sprite->stop();
0225         }
0226 
0227         mState   = Putting;
0228         mAnimCnt = 0;
0229     }
0230 }
0231 
0232 #include "moc_display_intro.cpp"