summaryrefslogtreecommitdiffstats
path: root/katomic/gamewidget.cpp
blob: e688ac745d7123eaf01de063f6ac1b6b91123e3f (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
244
245
246
247
248
249
250
251
252
253
/* toplevel.cpp

  Copyright (C) 1998   Andreas W�st (AndreasWuest@gmx.de)

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

  This program 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  */

#include "settings.h"
#include "gamewidget.h"
#include "feld.h"
#include "molek.h"

#include <tqscrollbar.h>
#include <tqgroupbox.h>
#include <tqlayout.h>
#include <tqvbox.h>
#include <tqlabel.h>

#include <tdeapplication.h>
#include <highscore/kscoredialog.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <tdeglobal.h>
#include <ksimpleconfig.h>
#include <tdeglobalsettings.h>

Options settings;

#define MPOSX 480
#define MPOSY 90

// ##########################
// #	class GameWidget    #
// ##########################

int level;

void GameWidget::moveUp()
{
  feld->startAnimation (Feld::MoveUp);
}

void GameWidget::moveDown()
{
  feld->startAnimation (Feld::MoveDown);
}

void GameWidget::moveLeft()
{
  feld->startAnimation (Feld::MoveLeft);
}

void GameWidget::moveRight()
{
  feld->startAnimation (Feld::MoveRight);
}

void GameWidget::nextAtom()
{
  feld->nextAtom();
}

void GameWidget::previousAtom()
{
  feld->previousAtom();
}

void GameWidget::getButton (int button)
{
    feld->startAnimation ((Feld::Direction)button);
}

void GameWidget::doUndo ()
{
  feld->doUndo ();
}

void GameWidget::doRedo ()
{
  feld->doRedo ();
}

void GameWidget::gameOver(int moves) {
    KMessageBox::information(this, i18n("You solved level %1 with %2 moves!").arg(level).arg(moves), i18n("Congratulations"));

    KScoreDialog high(KScoreDialog::Name | KScoreDialog::Score, this);
    high.setCaption(i18n("Level %1 Highscores").arg(level));
    high.setConfigGroup(TQString("Highscores Level %1").arg(level));

    KScoreDialog::FieldInfo scoreInfo;

    if (high.addScore(moves, scoreInfo, true, true))
    {
       high.exec();
    }
    updateLevel(level+1);
}

void GameWidget::getMoves(int moves)
{
    current.setNum(moves);
    ys->setText(current);
}

void GameWidget::mergeHighScores(int l)
{
    TDEConfigGroup oldConfig(kapp->config(), TQString("High Scores Level %1").arg(l).utf8());
    TDEConfigGroup newConfig(kapp->config(), TQString("Highscores Level %1").arg(l).utf8());

    newConfig.writeEntry("LastPlayer", oldConfig.readEntry("LastPlayer"));

    TQString num;
    for (int i = 1; i <= 10; ++i)
    {
	num.setNum(i);
	TQString key = "Pos" + num + "Name";
        newConfig.writeEntry(key, oldConfig.readEntry(key, "-"));
	key = "Pos" + num + "Score";
	newConfig.writeEntry(key, oldConfig.readEntry(key, "-"));
    }
    kapp->config()->sync();
}

void GameWidget::updateLevel (int l)
{
    level=l;
    TQString levelFile = locate("appdata", TQString("levels/level_%1").arg(l));
    if (levelFile.isNull()) {
	return updateLevel(1);
    }

    KSimpleConfig cfg(levelFile, true);
    cfg.setGroup("Level");
    feld->load(cfg);

    if (!kapp->config()->hasGroup(TQString("Highscores Level %1").arg(level)) &&
            kapp->config()->hasGroup(TQString("High Scores Level %1").arg(level)))
	mergeHighScores(level);

    highScore->setConfigGroup(TQString("Highscores Level %1").arg(level));
    highest.setNum(highScore->highScore());

    if (highest != "0" ) hs->setText(highest);
    else hs->setText("-");
    ys->setText("0");
    scrl->setValue(level);

    feld->repaint();
}

void GameWidget::restartLevel()
{
    updateLevel(level);
}

GameWidget::GameWidget ( TQWidget *parent, const char* name )
    : TQWidget( parent, name )
{
    level = 1;
    nlevels = TDEGlobal::dirs()->findAllResources("appdata", "levels/level_*",
				false, true).count();

    TQHBoxLayout *top = new TQHBoxLayout(this, 10);

    // spielfeld
    feld = new Feld (this, "feld");
    feld->setFocus();

    top->addWidget(feld);

    TQVBox *vb = new TQVBox(this);
    vb->setSpacing(20);
    top->addWidget(vb);

    // scrollbar
    scrl = new TQScrollBar(1, nlevels, 1,
			5, 1, TQt::Horizontal, vb, "scrl" );
    connect (scrl, TQ_SIGNAL (valueChanged (int)), TQ_SLOT (updateLevel (int)));

    // molek�l
    molek = new Molek (vb, "molek");
    feld->setMolek(molek);

    connect (feld, TQ_SIGNAL (gameOver(int)), TQ_SLOT(gameOver(int)));
    connect (feld, TQ_SIGNAL (sendMoves(int)), TQ_SLOT(getMoves(int)));
    connect (feld, TQ_SIGNAL (enableRedo(bool)), TQ_SIGNAL(enableRedo(bool)));
    connect (feld, TQ_SIGNAL (enableUndo(bool)), TQ_SIGNAL(enableUndo(bool)));

    highScore = new KScoreDialog(KScoreDialog::Name | KScoreDialog::Score, this);

    // the score group
    TQGroupBox *bg = new TQGroupBox (i18n("Score"), vb, "bg");
    TQBoxLayout *slay = new TQVBoxLayout (bg, 10);

    slay->addSpacing(10);

    slay->addWidget(new TQLabel(i18n("Highscore:"), bg));

    TQFont headerFont = TDEGlobalSettings::generalFont();
    headerFont.setBold(true);

    hs = new TQLabel (highest, bg);
    hs->setAlignment(TQt::AlignRight);
    hs->setFont(headerFont);
    slay->addWidget(hs);

    slay->addSpacing(10);

    slay->addWidget(new TQLabel(i18n("Your score so far:"), bg));

    ys = new TQLabel (current, bg);
    ys->setAlignment(TQt::AlignRight);
    ys->setFont(headerFont);
    slay->addWidget(ys);

    updateLevel(1);

    TDEConfig *config = TDEGlobal::config();
    config->setGroup("Options");
    settings.anim_speed = config->readNumEntry("Animation Speed", 1);
    if (settings.anim_speed < 1 || settings.anim_speed > MAX_SPEED)
	settings.anim_speed = 1;

    settings.changed = false;
}

GameWidget::~GameWidget()
{
}

void GameWidget::showHighscores ()
{
    KScoreDialog high(KScoreDialog::Name | KScoreDialog::Score, this);
    high.setCaption(i18n("Level %1 Highscores").arg(level));
    high.setConfigGroup(TQString("Highscores Level %1").arg(level));
    high.exec();
}

#include "gamewidget.moc"