File: display_intro.cpp

package info (click to toggle)
lskat 4%3A18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,404 kB
  • sloc: cpp: 4,934; xml: 96; makefile: 5; sh: 2
file content (243 lines) | stat: -rw-r--r-- 7,488 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
   This file is part of the KDE games lskat program
   Copyright (c) 2006 Martin Heni <kde@heni-online.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "display_intro.h"

// General includes
#include <cmath>

// Qt includes
#include <QPoint>

// KDE includes
#include <KLocalizedString>
#include <KConfigGroup>

// Local includes
#include "cardsprite.h"
#include "lskat_debug.h"
#include "textsprite.h"

#define WAIT_CNT       100  /* Wait this [ms] before clearing board */

// Constructor for the display
DisplayIntro::DisplayIntro(Deck *deck, QGraphicsScene *theScene, ThemeManager *theme,
                           int advancePeriod, QGraphicsView *parent)
            : Themable(QLatin1String("display_intro"), theme), AbstractDisplay(deck, theScene, theme, advancePeriod, parent)
{
    mTextShown = false;

    // Choose a background color
    scene()->setBackgroundBrush(QColor(0, 0, 128));

    mTimer = new QTimer(this);
    connect(mTimer, &QTimer::timeout, this, &DisplayIntro::loop);
    mTimer->stop();

    // Redraw
    if (theme) theme->updateTheme(this);
}

// Called by thememanager when theme or theme geometry changes. Redraw and resize
// this display.
void DisplayIntro::changeTheme()
{
    // Retrieve theme data
    KConfigGroup config = thememanager()->config(id());

    // Retrieve background pixmap
    QString bgsvgid = config.readEntry("background-svgid");
    QPixmap pixmap  = thememanager()->getPixmap(bgsvgid, scene()->sceneRect().size().toSize());
    scene()->setBackgroundBrush(pixmap);
    mView->update();
}

// Start the intro.
void DisplayIntro::start()
{
    mAnimCnt = 0;
    mState   = Putting;
    mTimer->start(50);

    // Stop all card sprites
    for (int i = 0; i < mCards.size(); i++)
    {
        mCards[i]->stop();
    }
}

// Animation loop
void DisplayIntro::loop()
{
    int no = mCards.size();
    // Catch no card error
    if (no < 1) return;

    // Retrieve theme data
    KConfigGroup cardconfig = thememanager()->config(QLatin1String("card"));
    double card_width       = cardconfig.readEntry("width", 1.0);
    KConfigGroup config     = thememanager()->config(id());
    QPointF start_shift     = config.readEntry("start-shift", QPointF(1.0, 1.0));
    QPointF start_pos       = config.readEntry("start-pos", QPointF(1.0, 1.0));
    double time_clear_in    = config.readEntry("time-clear-in", 1.0);
    double time_clear_out   = config.readEntry("time-clear-out", 1.0);
    double aspectRatio      = thememanager()->aspectRatio();

    // Display the intro text delayed
    if (mAnimCnt == 2  && mState == Putting && !mTextShown)
    {
        mTextShown = true;
        QString s1 = i18nc("Title of the game - line 1", "Lieutenant Skat");
        QString s2 = i18nc("Title of the game - line 2", "for");
        QString s3 = i18nc("Title of the game - line 3", "K D E");

        // Text sprite title foreground
        TextSprite *text1a = new TextSprite(s1, QLatin1String("name-front"), mTheme, scene());
        mSprites.append(text1a);
        text1a->show();

        // Text sprite title background
        TextSprite *text1b = new TextSprite(s1, QLatin1String("name-back"), mTheme, scene());
        mSprites.append(text1b);
        text1b->show();

        // Text sprite title foreground
        TextSprite *text2a = new TextSprite(s2, QLatin1String("for-front"), mTheme, scene());
        mSprites.append(text2a);
        text2a->show();

        // Text sprite title background
        TextSprite *text2b = new TextSprite(s2, QLatin1String("for-back"), mTheme, scene());
        mSprites.append(text2b);
        text2b->show();

        // Text sprite title foreground
        TextSprite *text3a = new TextSprite(s3, QLatin1String("kde-front"), mTheme, scene());
        mSprites.append(text3a);
        text3a->show();

        // Text sprite title background
        TextSprite *text3b = new TextSprite(s3, QLatin1String("kde-back"), mTheme, scene());
        mSprites.append(text3b);
        text3b->show();
    }

    // Display a card
    if (mAnimCnt < no && mState == Putting)
    {
        double factor = double(mAnimCnt) / double(no - 1);
        double fsin = sin(factor * M_PI);

        CardSprite *sprite = mCards[mAnimCnt];

        QPointF pos;
        if (mAnimCnt % 2 == 0)
        {
            pos  = QPointF(start_pos.x(), start_pos.y());
            pos += QPointF(start_shift.x() * fsin, start_shift.y() * factor);
        }
        else
        {
            pos  = QPointF(1.0 - start_pos.x() - card_width, start_pos.y());
            pos += QPointF(-start_shift.x() * fsin, start_shift.y() * factor);
        }
        sprite->setBackside();
        sprite->setPosition(pos);
        sprite->setZValue(50 + mAnimCnt);
        sprite->show();
        mAnimCnt++;
    }
    // Change state to turning
    else if (mState == Putting)
    {
        mState   = Turning;
        mAnimCnt = 0;
    }
    // Turn cards
    else if (mAnimCnt < no && mState == Turning)
    {
        CardSprite *sprite = mCards[mAnimCnt];
        sprite->setTurning(true);
        mAnimCnt++;
    }
    // Change state to waiting
    else if (mState == Turning)
    {
        mState   = Waiting;
        mAnimCnt = 0;
    }
    // Wait
    else if (mAnimCnt < WAIT_CNT && mState == Waiting)
    {
        mAnimCnt++;
    }
    // Change state to clearing the board
    else if (mState == Waiting)
    {
        mState   = Clearing;
        mAnimCnt = 0;
    }
    // Clear the board, step 1
    else if (mAnimCnt == 0 && mState == Clearing)
    {
        for (int i = 0; i < no; i++)
        {
            CardSprite *sprite = mCards[i];
            sprite->setMove(QPointF((1.0 - card_width) / 2.0, (1.0 / aspectRatio - card_width) / 2.0), time_clear_in);
        }
        mAnimCnt++;
    }
    // Clear the board, step 2
    else if (mAnimCnt < 30 && mState == Clearing)
    {
        mAnimCnt++;
    }
    // Clear the board, step 3 and change state to waiting
    else if (mState == Clearing)
    {
        for (int i = 0; i < no; i++)
        {
            double r = 1.0;
            double x = r * cos(double(i) / double(no - 1) * M_PI * 2.0) + 0.5;
            double y = r * sin(double(i) / double(no - 1) * M_PI * 2.0) + 0.5;
            CardSprite *sprite = mCards[i];
            sprite->setMove(QPointF(x, y / aspectRatio), time_clear_out);
        }
        mState = Waiting2;
        mAnimCnt = 0;
    }
    // Wait
    else if (mAnimCnt < WAIT_CNT && mState == Waiting2)
    {
        mAnimCnt++;
    }
    // Restart cycle
    else if (mState == Waiting2)
    {
        for (int i = 0; i < no; i++)
        {
            CardSprite *sprite = mCards[i];
            sprite->stop();
        }

        mState   = Putting;
        mAnimCnt = 0;
    }
}