You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
326 lines
8.3 KiB
326 lines
8.3 KiB
14 years ago
|
#include <tqheader.h>
|
||
13 years ago
|
#include <tqlayout.h>
|
||
14 years ago
|
#include <tqcolor.h>
|
||
|
#include <tqlabel.h>
|
||
|
#include <tqslider.h>
|
||
|
#include <tqevent.h>
|
||
|
#include <tqkeycode.h>
|
||
|
#include <tqlistview.h>
|
||
|
#include <tqpushbutton.h>
|
||
|
#include <tqlineedit.h>
|
||
15 years ago
|
|
||
12 years ago
|
#include <tdeapplication.h>
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
|
#include <tdemessagebox.h>
|
||
15 years ago
|
#include <kpushbutton.h>
|
||
|
#include <kstdguiitem.h>
|
||
|
|
||
|
#include "newgamedlg.h"
|
||
|
#include "newgamedlg.moc"
|
||
|
|
||
|
#include "newGameDlg_ui.h"
|
||
|
|
||
|
/*************************************************************************
|
||
|
New Game Dialog Members
|
||
|
************************************************************************/
|
||
|
|
||
13 years ago
|
NewGameDlg::NewGameDlg( TQWidget *parent, Map *pmap, PlayerList *players,
|
||
15 years ago
|
Player *neutralPlayer, PlanetList *planets )
|
||
13 years ago
|
: KDialogBase( parent, "new_game_dialog", true, i18n("Start New Game"),
|
||
15 years ago
|
KDialogBase::Ok|KDialogBase::Default|KDialogBase::Cancel, KDialogBase::NoDefault, true ),
|
||
|
plrList(players), plnetList(planets), neutral(neutralPlayer),
|
||
|
map(pmap)
|
||
|
{
|
||
|
w = new NewGameDlgUI(this);
|
||
|
w->map->setMap(map);
|
||
|
w->listPlayers->header()->hide();
|
||
|
// w->listPlayers->setMinimumSize( 100, 150 );
|
||
|
w->listPlayers->setSortColumn(-1);
|
||
14 years ago
|
w->listPlayers->setHScrollBarMode(TQScrollView::AlwaysOff);
|
||
15 years ago
|
w->sliderPlayers->setMinimumWidth(270);
|
||
|
w->sliderPlanets->setMinimumWidth(270);
|
||
|
|
||
|
w->newPlayer->setMaxLength( 8 );
|
||
|
|
||
14 years ago
|
connect(w->sliderPlayers, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(slotPlayerCount(int)));
|
||
|
connect(w->sliderPlanets, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(slotNewMap()));
|
||
|
connect(w->sliderTurns, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(slotTurns()));
|
||
|
connect(w->rejectMap, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotNewMap()));
|
||
|
connect(w->newPlayer, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(slotNewPlayer()));
|
||
|
connect(w->newPlayer, TQT_SIGNAL(returnPressed()), this, TQT_SLOT(slotAddPlayer()));
|
||
|
connect(w->addPlayer, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAddPlayer()));
|
||
15 years ago
|
|
||
|
init();
|
||
|
|
||
|
setMainWidget(w);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::slotDefault()
|
||
|
{
|
||
|
w->sliderPlayers->setValue(2);
|
||
|
w->sliderPlanets->setValue(3);
|
||
|
w->sliderTurns->setValue(15);
|
||
|
|
||
|
w->listPlayers->clear();
|
||
|
|
||
|
setPlayerCount(2);
|
||
|
|
||
|
updateMiniMap();
|
||
|
updateLabels();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::init()
|
||
|
{
|
||
12 years ago
|
TDEConfig *config = kapp->config();
|
||
15 years ago
|
config->setGroup("Game");
|
||
|
int nrOfPlayers = config->readNumEntry("NrOfPlayers");
|
||
|
if (nrOfPlayers < 2)
|
||
|
nrOfPlayers = 2;
|
||
|
if (nrOfPlayers > MAX_PLAYERS)
|
||
|
nrOfPlayers = MAX_PLAYERS;
|
||
|
|
||
|
int nrOfPlanets = config->readNumEntry("NrOfPlanets", 3);
|
||
|
int nrOfTurns = config->readNumEntry("NrOfTurns", 15);
|
||
|
|
||
|
w->sliderPlayers->setValue(nrOfPlayers);
|
||
|
w->sliderPlanets->setValue(nrOfPlanets);
|
||
|
w->sliderTurns->setValue(nrOfTurns);
|
||
|
setPlayerCount(nrOfPlayers);
|
||
|
slotNewPlayer();
|
||
|
|
||
|
// Restore player names
|
||
|
int plrNum = 0;
|
||
14 years ago
|
for( TQListViewItem *item = w->listPlayers->firstChild();
|
||
15 years ago
|
item; item = item->nextSibling(), plrNum++ )
|
||
|
{
|
||
13 years ago
|
TQString key = TQString("Player_%1").arg(plrNum);
|
||
15 years ago
|
|
||
14 years ago
|
TQString playerName = config->readEntry(key);
|
||
15 years ago
|
if (playerName.isEmpty())
|
||
|
continue;
|
||
|
|
||
|
item->setText(2, "H"); // Human
|
||
|
item->setText(1, i18n("Human Player"));
|
||
|
item->setText(0, playerName);
|
||
|
}
|
||
|
|
||
|
updateMiniMap();
|
||
|
updateLabels();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::slotNewPlayer()
|
||
|
{
|
||
|
w->addPlayer->setEnabled(!w->newPlayer->text().isEmpty());
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::slotAddPlayer()
|
||
|
{
|
||
14 years ago
|
TQString playerName = w->newPlayer->text();
|
||
15 years ago
|
if (playerName.isEmpty())
|
||
|
return;
|
||
|
|
||
14 years ago
|
TQListViewItem *item;
|
||
15 years ago
|
do
|
||
|
{
|
||
|
item = w->listPlayers->firstChild();
|
||
|
while( item )
|
||
|
{
|
||
|
if (item->text(2) == "A")
|
||
|
break;
|
||
|
|
||
|
item = item->nextSibling();
|
||
|
}
|
||
|
if (!item)
|
||
|
{
|
||
|
int nrPlayers = w->listPlayers->childCount();
|
||
|
if (nrPlayers >= MAX_PLAYERS)
|
||
|
return; // Too bad
|
||
|
nrPlayers++;
|
||
|
w->sliderPlayers->setValue(nrPlayers);
|
||
|
setPlayerCount(nrPlayers);
|
||
|
}
|
||
|
}
|
||
|
while(!item);
|
||
|
|
||
|
item->setText(2, "H"); // Human
|
||
|
item->setText(1, i18n("Human Player"));
|
||
|
item->setText(0, playerName);
|
||
|
|
||
14 years ago
|
w->newPlayer->setText(TQString());
|
||
15 years ago
|
|
||
|
updateMiniMap();
|
||
|
updateLabels();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::setPlayerCount(int playerCount)
|
||
|
{
|
||
14 years ago
|
TQColor PlayerColors[MAX_PLAYERS] = { TQColor( 130, 130, 255 ), yellow, red, green,
|
||
|
white, cyan, magenta, TQColor( 235, 153, 46 ),
|
||
|
TQColor( 106, 157, 104 ), TQColor( 131, 153, 128) };
|
||
15 years ago
|
|
||
|
int i = 0;
|
||
14 years ago
|
TQListViewItem *lastItem = 0;
|
||
|
TQListViewItem *item = 0;
|
||
|
TQListViewItem *nextItem = w->listPlayers->firstChild();
|
||
15 years ago
|
while( (item = nextItem) )
|
||
|
{
|
||
|
nextItem = item->nextSibling();
|
||
|
if (i >= playerCount)
|
||
|
{
|
||
|
delete item;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
lastItem = item;
|
||
|
}
|
||
|
i++;
|
||
|
}
|
||
|
|
||
|
while(w->listPlayers->childCount() < playerCount)
|
||
|
{
|
||
13 years ago
|
TQString playerName = i18n("Generated AI player name", "Comp%1").arg(i+1);
|
||
14 years ago
|
TQPixmap pm(16,16);
|
||
|
TQColor color(PlayerColors[i]);
|
||
15 years ago
|
pm.fill(color);
|
||
14 years ago
|
TQListViewItem *item = new TQListViewItem(w->listPlayers, lastItem, playerName, i18n("Computer Player"), "A", color.name());
|
||
15 years ago
|
item->setPixmap(0, pm);
|
||
|
lastItem = item;
|
||
|
i++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::slotPlayerCount(int playerCount)
|
||
|
{
|
||
|
if (w->listPlayers->childCount() == playerCount)
|
||
|
return;
|
||
|
|
||
|
setPlayerCount(playerCount);
|
||
|
|
||
|
updateMiniMap();
|
||
|
updateLabels();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::slotTurns()
|
||
|
{
|
||
|
updateLabels();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::slotNewMap()
|
||
|
{
|
||
|
updateMiniMap();
|
||
|
updateLabels();
|
||
|
}
|
||
|
|
||
|
int
|
||
|
NewGameDlg::turns()
|
||
|
{
|
||
|
return w->sliderTurns->value();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::updateLabels()
|
||
|
{
|
||
13 years ago
|
w->labelPlayers->setText(i18n("Number of &players: %1").arg(w->sliderPlayers->value()));
|
||
|
w->labelPlanets->setText(i18n("Number of neutral p&lanets: %1").arg(w->sliderPlanets->value()));
|
||
|
w->labelTurns->setText(i18n("Number of &turns: %1").arg(w->sliderTurns->value()));
|
||
15 years ago
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::slotOk()
|
||
|
{
|
||
|
bool hasHumans = false;
|
||
14 years ago
|
for( TQListViewItem *item = w->listPlayers->firstChild();
|
||
15 years ago
|
item; item = item->nextSibling() )
|
||
|
{
|
||
|
bool ai = (item->text(2) == "A");
|
||
|
if (!ai)
|
||
|
hasHumans = true;
|
||
|
}
|
||
|
|
||
|
if (!hasHumans)
|
||
|
{
|
||
|
KMessageBox::information(this, i18n("The game is much more fun when you add a human player!"));
|
||
|
w->newPlayer->setFocus();
|
||
|
return;
|
||
|
}
|
||
|
KDialogBase::slotOk();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::save()
|
||
|
{
|
||
12 years ago
|
TDEConfig *config = kapp->config();
|
||
15 years ago
|
config->setGroup("Game");
|
||
|
|
||
|
config->writeEntry("NrOfPlayers", w->sliderPlayers->value());
|
||
|
config->writeEntry("NrOfPlanets", w->sliderPlanets->value());
|
||
|
config->writeEntry("NrOfTurns", w->sliderTurns->value());
|
||
|
|
||
|
int plrNum = 0;
|
||
14 years ago
|
for( TQListViewItem *item = w->listPlayers->firstChild();
|
||
15 years ago
|
item; item = item->nextSibling() )
|
||
|
{
|
||
13 years ago
|
TQString key = TQString("Player_%1").arg(plrNum);
|
||
14 years ago
|
TQString playerName = item->text(0);
|
||
15 years ago
|
bool ai = (item->text(2) == "A");
|
||
|
if (ai)
|
||
|
{
|
||
|
if (config->hasKey(key))
|
||
|
config->deleteEntry(key);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
config->writeEntry(key, playerName);
|
||
|
}
|
||
|
plrNum++;
|
||
|
}
|
||
|
config->sync();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
NewGameDlg::updateMiniMap()
|
||
|
{
|
||
|
// Clear map,, player and planet lists
|
||
|
map->clearMap();
|
||
|
|
||
|
Planet *planet;
|
||
|
planet = plnetList->first();
|
||
|
for( planet = plnetList->take(); planet != 0; planet = plnetList->take() ) {
|
||
|
delete planet;
|
||
|
}
|
||
|
|
||
|
Player *player;
|
||
|
player = plrList->first();
|
||
|
for( player = plrList->take(); player != 0; player = plrList->take() ) {
|
||
|
delete player;
|
||
|
}
|
||
|
|
||
|
// Make player list
|
||
|
// Does the name already exist in the list
|
||
|
int plrNum = 0;
|
||
14 years ago
|
for( TQListViewItem *item = w->listPlayers->firstChild();
|
||
15 years ago
|
item; item = item->nextSibling() )
|
||
|
{
|
||
14 years ago
|
TQString playerName = item->text(0);
|
||
15 years ago
|
bool ai = (item->text(2) == "A");
|
||
14 years ago
|
TQColor color(item->text(3));
|
||
15 years ago
|
plrList->append( Player::createPlayer( playerName, color, plrNum, ai ));
|
||
|
plrNum++;
|
||
|
}
|
||
|
|
||
|
// make the planets
|
||
|
map->populateMap( *plrList, neutral,
|
||
|
w->sliderPlanets->value(),
|
||
|
*plnetList );
|
||
|
}
|
||
|
|