summaryrefslogtreecommitdiffstats
path: root/ksmiletris/gamewindow.cpp
blob: fc53b5ffe69dfbc66a1645c163cf05dbdf81f86f (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
/****************************************************************
Copyright (c) 1998 Sandro Sigala <ssigala@globalnet.it>.
All rights reserved.

Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness.  In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
****************************************************************/

#include "config.h"

#include <tdeapplication.h>
#include <klocale.h>
#include <tdeconfig.h>
#include <kmenubar.h>
#include <kstatusbar.h>

#include "ksmiletris.h"
#include "gamewindow.h"
#include "gamewidget.h"
#include "kscoredialog.h"

#include <tdeaction.h>
#include <kstdgameaction.h>



const int default_width = 362;
const int default_height = 460;

GameWindow::GameWindow(TQWidget *, const char *name)
        : TDEMainWindow(0, name)
{
        //New Games
        (void)KStdGameAction::gameNew(TQT_TQOBJECT(this),
                                  TQT_SLOT(menu_newGame()),
                                  actionCollection());

        //Pause Game
        (void)KStdGameAction::pause(TQT_TQOBJECT(this),
                                      TQT_SLOT(menu_pause()),
                                      actionCollection());

        //End Game
        (void)KStdGameAction::end(TQT_TQOBJECT(this),
                                  TQT_SLOT(menu_endGame()),
                                  actionCollection());

        //Highscores
        (void)KStdGameAction::highscores(TQT_TQOBJECT(this),
                                    TQT_SLOT(menu_highScores()),
                                    actionCollection());

        //Quit
        (void)KStdGameAction::quit(TQT_TQOBJECT(this),
                               TQT_SLOT(close()),
                               actionCollection());

        TQStringList list;
        TDESelectAction* piecesAct =
          new TDESelectAction(i18n("&Pieces"), 0, TQT_TQOBJECT(this), TQT_SLOT(menu_pieces()),
                             actionCollection(), "settings_pieces");
        list.append(i18n("&Smiles"));
        list.append(i18n("S&ymbols"));
        list.append(i18n("&Icons"));
        piecesAct->setItems(list);

        (void)new TDEToggleAction(i18n("&Sounds"), 0, TQT_TQOBJECT(this),
		TQT_SLOT(menu_sounds()), actionCollection(), "settings_sounds");





	//connect(menu, TQT_SIGNAL(moved(menuPosition)),
	//	this, TQT_SLOT(movedMenu(menuPosition))); ?

	status = new KStatusBar(this);
	status->insertItem(i18n("Level: 99"), 1);
	status->insertItem(i18n("Score: 999999"), 2);
	status->changeItem("", 1);
	status->changeItem("", 2);

	game = new GameWidget(this);
	setCentralWidget(game);
	connect(game, TQT_SIGNAL(changedStats(int, int)),
		this, TQT_SLOT(updateStats(int, int)));
	connect(game, TQT_SIGNAL(gameOver()), TQT_TQOBJECT(this), TQT_SLOT(gameOver()));

        //keys
        (void)new TDEAction(i18n("Move Left"), Key_Left, TQT_TQOBJECT(game), TQT_SLOT(keyLeft()), actionCollection(), "left");
        (void)new TDEAction(i18n("Move Right"), Key_Right, TQT_TQOBJECT(game), TQT_SLOT(keyRight()), actionCollection(), "right");
        (void)new TDEAction(i18n("Rotate Left"), Key_Up, TQT_TQOBJECT(game), TQT_SLOT(keyUp()), actionCollection(), "up");
        (void)new TDEAction(i18n("Rotate Right"), Key_Down, TQT_TQOBJECT(game), TQT_SLOT(keyDown()), actionCollection(), "down");
        (void)new TDEAction(i18n("Drop Down"), Key_Space, TQT_TQOBJECT(game), TQT_SLOT(keySpace()), actionCollection(), "space");

	game->setFixedSize(default_width, default_height);
	adjustSize();
 	setFixedSize(size());

	// Read configuration
	TDEConfig *config = kapp->config();
	config->setGroup("Options");
	PiecesType pieces_type = (PiecesType)config->readNumEntry("Pieces", static_cast<int>(Pieces_Smiles));
	game->setPieces(pieces_type);
        ((TDESelectAction*)actionCollection()->action("settings_pieces"))->setCurrentItem((int)pieces_type);


	game->do_sounds = config->readBoolEntry("Sounds", true);
        ((TDEToggleAction*)actionCollection()->action("settings_sounds"))->setChecked(game->do_sounds);

	setupGUI(TDEMainWindow::Save | Keys | StatusBar | Create);
}

void GameWindow::menu_newGame()
{
        ((TDEToggleAction*)actionCollection()->action(KStdGameAction::stdName(KStdGameAction::Pause)))->setChecked(false);
	game->newGame();
}

void GameWindow::menu_pause()
{
	if (game->in_game) {
		game->in_pause = !game->in_pause;
                ((TDEToggleAction*)actionCollection()->action(KStdGameAction::stdName(KStdGameAction::Pause)))->setChecked(game->in_pause);
		game->repaintChilds();
	}
}

void GameWindow::menu_endGame()
{
	if (game->in_game) {
		game->in_game = false;
		game->repaintChilds();
                ((TDEToggleAction*)actionCollection()->action(KStdGameAction::stdName(KStdGameAction::Pause)))->setChecked(false);
		gameOver();
	}
}

void GameWindow::menu_highScores()
{
    KScoreDialog d(KScoreDialog::Name | KScoreDialog::Level | KScoreDialog::Score, this);
    d.exec();
}

void GameWindow::menu_pieces()
{
  int index = ((TDESelectAction*)actionCollection()->action("settings_pieces"))->currentItem();
  game->setPieces((PiecesType)index);

  TDEConfig *config = kapp->config();
  config->setGroup("Options");
  config->writeEntry("Pieces", index);
}

void GameWindow::menu_sounds()
{
	game->do_sounds = !game->do_sounds;
        ((TDEToggleAction*)actionCollection()->action("settings_sounds"))->setChecked(game->do_sounds);

        TDEConfig *config = kapp->config();
	config->setGroup("Options");
	config->writeEntry("Sounds", game->do_sounds);
}

void GameWindow::updateStats(int level, int points)
{
	TQString l, p;
	l.setNum(level);
	p.setNum(points);
	status->changeItem(i18n("Level: %1").arg(l), 1);
	status->changeItem(i18n("Score: %1").arg(p), 2);
}

void GameWindow::gameOver()
{
    KScoreDialog d(KScoreDialog::Name | KScoreDialog::Level | KScoreDialog::Score, this);

    KScoreDialog::FieldInfo scoreInfo;

    scoreInfo[KScoreDialog::Level].setNum(game->num_level);

    if (!d.addScore(game->num_points, scoreInfo))
       return;

    // Show highscore & ask for name.
    d.exec();
}

#include "gamewindow.moc"