summaryrefslogtreecommitdiffstats
path: root/kue/newgame.cpp
blob: 9ddf8f1b81d314c5be8910310728e9d353fcbfc2 (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
#include <assert.h>

#include <tqhbox.h>
#include <tqvbox.h>
#include <tqlayout.h>
#include <tqlabel.h>

#include <tdelocale.h>
#include <kcombobox.h>
#include <klineedit.h>
#include <kseparator.h>
#include <tqpushbutton.h>
#include <kdebug.h>
#include <kcolorbutton.h>

#include "team.h"
#include "localplayer.h"
#include "newgame.h"

newGameDialog::newGameDialog(TQWidget *parent, const char *name) :
                             KDialogBase(parent, name, true, i18n("New Game"), Ok|Cancel)
{
	_page = new TQWidget( this );
	// Just makes it look nicer
	setMinimumSize(400, 300);

	setMainWidget(_page);
	_top_layout = new TQVBoxLayout(_page, 0, KDialogBase::spacingHint());

	_game_type = new KComboBox(_page);

	// Get a list of available rules plugins
	_plugins_list = KuePluginLoader::available();
	TQValueList<KuePluginInfo>::const_iterator plugin = _plugins_list.begin();

	// Go over the plugins that KuePluginLoader::available claims we have
	for(;plugin != _plugins_list.end();plugin++)
	{
		// We only want plugins of the type "rulesengine"
		if ((*plugin).type == "rulesengine")
			_game_type->insertItem((*plugin).name);
	}

	connect(_game_type, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotNewPluginSelection()));

	// Set up an hbox to put the combo and its label in
	TQHBoxLayout *hbox = new TQHBoxLayout(_top_layout, KDialogBase::spacingHint());
	hbox->addWidget(new TQLabel(_game_type, i18n("Game &rules:"), _page));
	hbox->addWidget(_game_type);
	hbox->setStretchFactor(_game_type, 2);

	// Add a separator
	_top_layout->addWidget(new KSeparator(KSeparator::HLine, _page));

	// Add the players widget
	_player_widget = new TQWidget(_page);
	_player_layout = new TQGridLayout(_player_widget);
	_player_layout->setSpacing(KDialogBase::spacingHint());
	_top_layout->addWidget(_player_widget);

	// Buttons to add/remove players
	_add_player_button = new TQPushButton(i18n("&Add Player"), _page);
	connect(_add_player_button, TQ_SIGNAL(clicked()), this, TQ_SLOT(addPlayerRow())); 
	_remove_player_button = new TQPushButton(i18n("Remove &Player"), _page);
	connect(_remove_player_button, TQ_SIGNAL(clicked()), this, TQ_SLOT(removePlayerRow())); 

	// Set up a second hbox for the add/remove player buttons
	hbox = new TQHBoxLayout(_top_layout, KDialogBase::spacingHint());
	hbox->addStretch(2);
	hbox->addWidget(_add_player_button);
	hbox->addWidget(_remove_player_button);

	// This dialog is slightly less confusing with separator
	enableButtonSeparator(true);

	// Populates the player widget initially, and sets up the player
	// buttons' enabled state
	slotNewPluginSelection();
}

newGameDialog::~newGameDialog()
{
}

void newGameDialog::slotOk()
{
	KDialogBase::slotOk();
}

KuePluginInfo newGameDialog::selectedPlugin()
{
	// Find the plugin the user requested
	TQValueList<KuePluginInfo>::const_iterator plugin = _plugins_list.begin();

	for(;plugin != _plugins_list.end();plugin++)
	{
		if (((*plugin).name == _game_type->currentText()) && ((*plugin).type == "rulesengine"))
			return *plugin;
	}

	kdWarning() << "Unable to find KuePluginInfo matching user selection" << endl;

	// Oops
	return KuePluginInfo();
}

TQValueList<KueTeam*> newGameDialog::selectedTeams()
{
	TQValueList<KueTeam *> ret;

	while (_players.count())
	{
		KueTeam *temp = new KueTeam;		
		playerRow *row = _players.first();

		// Add the player
		temp->addPlayer(new KueLocalPlayer(row->nameEdit->text(), row->colorButton->color()));
		ret.append(temp);

		// Remove the row
		removePlayerRow(row);
	}

	return ret;
}

void newGameDialog::addPlayerRow()
{
	playerRow *row = new playerRow;
	int new_row = _players.count();

	row->label = new TQLabel(i18n("Player %1:").arg(new_row + 1), _player_widget);
	row->nameEdit = new KLineEdit(i18n("Player %1").arg(new_row + 1), _player_widget);
	row->colorButton = new KColorButton(defaultPlayerColor(new_row), _player_widget);

	_player_layout->addWidget(row->label, new_row, 0);
	_player_layout->addWidget(row->nameEdit, new_row, 1);
	_player_layout->addWidget(row->colorButton, new_row, 2);
	_player_layout->setRowStretch(new_row, 0);
	_player_layout->setRowStretch(new_row + 1, 1);

	row->label->show();
	row->nameEdit->show();
	row->colorButton->show();


	_players.append(row);
	updateButtons();
}

void newGameDialog::removePlayerRow(playerRow *row)
{
	delete row->label;
	delete row->nameEdit;
	delete row->colorButton;
	delete row;

	_players.remove(row);
	updateButtons();
}

void newGameDialog::removePlayerRow()
{
	playerRow *row = _players.last();
	removePlayerRow(row);
}

void newGameDialog::slotNewPluginSelection()
{
	KuePluginInfo info = selectedPlugin();

	// Cache the result so we don't have to call the (relatively) expensive
	// selectedPlugin() every time we need these values
	_min_players = info.minTeams;
	_max_players = info.maxTeams;

	assert(_min_players <= _max_players);

	while (_players.count() < _min_players)
	{
		addPlayerRow();
	}

	while (_players.count() > _max_players)
	{
		removePlayerRow();
	}

	updateButtons();
}


void newGameDialog::updateButtons()
{
	_remove_player_button->setEnabled(_players.count() > _min_players);
	_add_player_button->setEnabled(_players.count() < _max_players);
}

TQColor newGameDialog::defaultPlayerColor(unsigned int player)
{
	// We only have 16 colors :(
	player = player % 16;

	switch (player)
	{
	case 0:
		return TQt::white;
	case 1:
		return TQt::black;
	case 2:
		return TQt::red;
	case 3:
		return TQt::green;
	case 4:
		return TQt::blue;
	case 5:
		return TQt::cyan;
	case 6:
		return TQt::magenta;
	case 7:
		return TQt::yellow;
	case 8:
		return TQt::gray;
	case 9:
		return TQt::darkRed;
	case 10:
		return TQt::darkGreen;
	case 11:
		return TQt::darkBlue;
	case 12:
		return TQt::darkCyan;
	case 13:
		return TQt::darkMagenta;
	case 14:
		return TQt::darkYellow;
	default:
		return TQt::darkGray;
	}
}

#include "newgame.moc"