summaryrefslogtreecommitdiffstats
path: root/libksirtet/common/field.cpp
blob: 8b1d3f7b455761365846889e2cb3f4ac772517bf (plain)
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
#include "field.h"
#include "field.moc"

#include <tqwhatsthis.h>
#include <tqlabel.h>
#include <tqlayout.h>

#include <klocale.h>
#include <kprogress.h>
#include <kgameprogress.h>
#include <kcanvasrootpixmap.h>
#include <kgamelcd.h>

#include "base/baseprefs.h"
#include "factory.h"
#include "highscores.h"
#include "misc_ui.h"
#include "board.h"
#include "commonprefs.h"


Field::Field(TQWidget *parent)
: MPSimpleBoard(parent), BaseField(this)
{
// column 1
    // score LCD
    scoreList = new KGameLCDList(this);
    showScore = new KGameLCD(6, scoreList);
    scoreList->append(showScore);
    showTime = new KGameLCDClock(scoreList);
    scoreList->append(showTime);
    lcds->addWidget(scoreList, 1, 0);
    lcds->setRowStretch(2, 1);

    // removed LCD
    removedList = new KGameLCDList(i18n(bfactory->mainData.removedLabel), this);
    lcds->addWidget(removedList, 3, 0);
    lcds->setRowStretch(4, 1);

    // level progress
    levelLabel = new TQLabel(this);
    levelLabel->tqsetAlignment(AlignCenter);
    lcds->addWidget(levelLabel, 5, 0);
    toLevel = new KProgress(this);
    toLevel->setTextEnabled(true);
    toLevel->setFormat("1");
    TQWhatsThis::add(toLevel, i18n("Display the progress to complete the current level or stage."));
    lcds->addWidget(toLevel, 6, 0);
    lcds->setRowStretch(7, 1);

// column 2
    // previous player height
    prevHeight = new PlayerProgress(board, this, "prev_progress");
    TQWhatsThis::add(prevHeight, i18n("Previous player's height"));
    top->addWidget(prevHeight, 1, 1, AlignHCenter);

// column 3
    // pending gift shower
    Board *b = static_cast<Board *>(board);
    top->addWidget(b->giftPool(), 0, 2, AlignCenter);

    // shadow piece
    shadow = new Shadow(board, this);
    TQWhatsThis::add(shadow, i18n("Shadow of the current piece"));
    top->addWidget(shadow, 2, 2);

// column 4
    // next player height
    nextHeight = new PlayerProgress(board, this, "next_progress");
    TQWhatsThis::add(nextHeight, i18n("Next player's height"));
    top->addWidget(nextHeight, 1, 3, AlignHCenter);

// column 5
    // next piece shower
    TQVBoxLayout *vbl = new TQVBoxLayout(10);
    top->addLayout(vbl, 1, 4);
    vbl->addStretch(1);

    labShowNext = new TQLabel(i18n("Next Tile"), this);
    labShowNext->tqsetAlignment(AlignCenter);
    vbl->addWidget(labShowNext, 0);
    showNext = new ShowNextPiece(board, this);
    _snRootPixmap = new KCanvasRootPixmap(showNext);
    _snRootPixmap->start();
    vbl->addWidget(showNext, 0);
    vbl->addStretch(4);

    connect(board, TQT_SIGNAL(scoreUpdated()), TQT_SLOT(scoreUpdatedSlot()));
    connect(board, TQT_SIGNAL(levelUpdated()), TQT_SLOT(levelUpdated()));
    connect(board, TQT_SIGNAL(removedUpdated()), TQT_SLOT(removedUpdated()));

    initVariableGUI();
}

void Field::levelUpdated()
{
    toLevel->setFormat(TQString::number(board->level()));
    // necessary to update string ...
    int p = toLevel->progress();
    toLevel->setProgress(p+1);
    toLevel->setProgress(p);
}

void Field::removedUpdated()
{
    uint nb = cfactory->cbi.nbRemovedToLevel;
    toLevel->setProgress(isArcade() ? board->arcadeDone()
                         : board->nbRemoved() % nb);
}

void Field::showOpponents(bool show)
{
    Board *b = static_cast<Board *>(board);
    if (show) {
        prevHeight->show();
        nextHeight->show();
        b->giftPool()->show();
    } else {
        prevHeight->hide();
        nextHeight->hide();
        b->giftPool()->hide();
    }
}

void Field::settingsChanged()
{
    BaseField::settingsChanged();
    TQColor color = BasePrefs::fadeColor();
    double s = BasePrefs::fadeIntensity();
    _snRootPixmap->setFadeEffect(s, color);
    showNext->canvas()->setBackgroundColor(color);
    bool b = CommonPrefs::showNextPiece();
    if (b) {
       showNext->show();
       labShowNext->show();
   } else {
       showNext->hide();
       labShowNext->hide();
   }
    b = CommonPrefs::showPieceShadow();
    if (b) shadow->show();
    else shadow->hide();
}

void Field::_init(bool AI, bool multiplayer, bool server, bool first,
                  const TQString &name)
{
    BaseField::init(AI, multiplayer, server, first, name);
    showOpponents(multiplayer);
    static_cast<Board *>(board)->setType(AI);
}

void Field::_initFlag(TQDataStream &s)
{
    ServerInitData sid;
    s >> sid;
    GTInitData data;
    data.seed = sid.seed;
    data.initLevel = sid.initLevel;

    shadow->setDisplay(true);
    toLevel->setValue(0);
    showTime->reset();
    showTime->start();

    BaseField::start(data);
    initVariableGUI();
}

void Field::initVariableGUI()
{
    if ( board->isArcade() ) {
        scoreList->title()->setText(i18n("Elapsed time"));
        showScore->hide();
        showTime->show();
        TQWhatsThis::add(scoreList, i18n("Display the elapsed time."));
        levelLabel->setText(i18n("Stage"));
        toLevel->setTotalSteps( board->arcadeTodo() );
    } else {
        scoreList->title()->setText(i18n("Score"));
        showScore->show();
        showTime->hide();
        TQWhatsThis::add(scoreList, i18n("<qt>Display the current score.<br/>It turns <font color=\"blue\">blue</font> if it is a highscore and <font color=\"red\">red</font> if it is the best local score.</qt>"));
        levelLabel->setText(i18n("Level"));
        toLevel->setTotalSteps(cfactory->cbi.nbRemovedToLevel);
    }
}

void Field::_playFlag(TQDataStream &s)
{
    ServerPlayData spd;
    s >> spd;
    prevHeight->setValue(spd.prevHeight);
    nextHeight->setValue(spd.nextHeight);
    if (spd.gift)
        static_cast<Board *>(board)->giftPool()->put(spd.gift);
}

void Field::_pauseFlag(bool p)
{
    pause(p);
    shadow->setDisplay(!p);
    if (p) showTime->stop();
    else showTime->start();
}

void Field::_stopFlag(bool gameover)
{
    BaseField::stop(gameover);
    showTime->stop();
}

void Field::_dataOut(TQDataStream &s)
{
    _cpd.height = board->firstClearLine();
    _cpd.end    = static_cast<Board *>(board)->isGameOver();
    _cpd.gift   = static_cast<Board *>(board)->gift();
    s << _cpd;
}

KExtHighscore::Score Field::currentScore() const
{
    KExtHighscore::Score score(_cpd.end ? KExtHighscore::Lost : KExtHighscore::Won);
    score.setScore(board->score());
    score.setData("level", board->level());
    score.setData("removed", board->nbRemoved());
    return score;
}

void Field::_gameOverDataOut(TQDataStream &s)
{
    s << currentScore();
}

void Field::moveLeft() { static_cast<Board *>(board)->pMoveLeft(); }
void Field::moveRight() { static_cast<Board *>(board)->pMoveRight(); }
void Field::dropDownStart() { static_cast<Board *>(board)->pDropDownStart(); }
void Field::dropDownStop() { static_cast<Board *>(board)->pDropDownStop(); }
void Field::oneLineDown() { static_cast<Board *>(board)->pOneLineDown(); }
void Field::rotateLeft() { static_cast<Board *>(board)->pRotateLeft(); }
void Field::rotateRight() { static_cast<Board *>(board)->pRotateRight(); }
void Field::moveLeftTotal() { static_cast<Board *>(board)->pMoveLeftTotal(); }
void Field::moveRightTotal() { static_cast<Board *>(board)->pMoveRightTotal();}