#include "mp_interface.h" #include "mp_interface.moc" #include #include #include #include #include #include #include "defines.h" #include "types.h" #include "meeting.h" #include "internal.h" #include "keys.h" #include "wizard.h" /*****************************************************************************/ /* Constructor & Destructor */ /*****************************************************************************/ MPInterface::MPInterface(const MPGameInfo &_gameInfo, uint nbActions, const ActionData *data, TQWidget *tqparent, const char *name) : TQWidget(tqparent, name), internal(0), gameInfo(_gameInfo), nbLocalHumans(0) { Q_ASSERT( gameInfo.maxNbLocalPlayers>=1 ); hbl = new TQHBoxLayout(this, 0, 5); hbl->setResizeMode( TQLayout::Fixed ); _keyData = new KeyData(gameInfo.maxNbLocalPlayers, nbActions, data, TQT_TQOBJECT(this)); } MPInterface::~MPInterface() { delete internal; } /*****************************************************************************/ /* Game creation */ /*****************************************************************************/ void MPInterface::clear() { if (internal) { stop(); delete internal; internal = 0; _keyData->clear(); } } void MPInterface::dialog() { clear(); // configuration wizard ConnectionData cd; MPWizard wiz(gameInfo, cd, this); if ( wiz.exec()==TQDialog::Rejected ) { singleHuman(); // create a single game return; } // net meeting TQPtrList rhd; rhd.setAutoDelete(TRUE); if (cd.network) { cId id(kapp->name(), gameInfo.gameId); MPOptionWidget *ow = newOptionWidget(); NetMeeting *nm; if (cd.server) nm = new ServerNetMeeting(id, cd.rhd, ow, rhd, this); else nm = new ClientNetMeeting(id, cd.rhd, ow, this); int res = nm->exec(); if (ow) { if (res) ow->saveData(); delete ow; } delete nm; if (!res) { singleHuman(); return; } } createLocalGame(cd); if (cd.server) createServerGame(rhd); else createClientGame(cd.rhd); } void MPInterface::specialLocalGame(uint nbHumans, uint nbAIs) { clear(); ConnectionData cd; BoardData bd; PlayerComboBox::Type t; KConfigGroupSaver cg(kapp->config(), MP_GROUP); for (uint i=0; i<(nbHumans+nbAIs); i++) { bd.type = (ireadNumEntry(TQString(MP_PLAYER_TYPE).tqarg(i), PlayerComboBox::None); if ( bd.type==t ) bd.name = cg.config()->readEntry(TQString(MP_PLAYER_NAME).tqarg(i), TQString()); if ( bd.name.isNull() ) bd.name = (i rhd; createServerGame(rhd); } void MPInterface::createServerGame(const TQPtrList &rhd) { internal = (rhd.count() ? (Local *)new NetworkServer(this, boards, rhd, gameInfo.interval) : (Local *)new LocalServer(this, boards, gameInfo.interval)); init(); } void MPInterface::createClientGame(const RemoteHostData &rhd) { TQPtrList r; r.append((RemoteHostData *)&rhd); internal = new Client(this, boards, r); init(); } void MPInterface::createLocalGame(const ConnectionData &cd) { _server = cd.server; nbLocalHumans = 0; for (uint i=0; iaddWidget(d.ptr); d.ptr->show(); connect(d.ptr, TQT_SIGNAL(enableKeys(bool)), TQT_SLOT(enableKeys(bool))); boards += d; } // init local boards _keyData->setCurrentNb(nbLocalHumans); int k = 0; for (uint i=0; icreateActionCollection(k, boards[i].ptr); k++; } boards[i].name = cd.rhd.bds[i].name; boards[i].ptr->init(!h, cd.network || boards.count()>1, _server, i==0, cd.rhd.bds[i].name); } } /*****************************************************************************/ /* Key management */ /*****************************************************************************/ void MPInterface::setDefaultKeycodes(uint nb, uint i, const int *def) { _keyData->setKeycodes(nb, i, def); } void MPInterface::addKeys(KKeyDialog &d) { _keyData->addKeys(d); } void MPInterface::saveKeys() { _keyData->save(); } void MPInterface::enableKeys(bool enable) { if ( nbLocalHumans==0 ) return; // find the sending board uint i; for (i=0; isetEnabled(hi, enable); } void MPInterface::keyPressEvent(TQKeyEvent *e) { _keyData->keyEvent(e, true); } void MPInterface::keyReleaseEvent(TQKeyEvent *e) { _keyData->keyEvent(e, false); } /*****************************************************************************/ /* Misc. functions */ /*****************************************************************************/ uint MPInterface::nbPlayers() const { return internal->nbPlayers(); } TQString MPInterface::playerName(uint i) const { Q_ASSERT(_server); return internal->playerName(i); } TQDataStream &MPInterface::readingStream(uint i) const { Q_ASSERT(_server); return internal->ioBuffer(i)->reading; } TQDataStream &MPInterface::writingStream(uint i) const { return internal->ioBuffer(i)->writing; } TQDataStream &MPInterface::dataToClientsStream() const { Q_ASSERT(_server); return *internal->globalStream(); } void MPInterface::immediateWrite() { internal->writeData(_server); } void MPInterface::hostDisconnected(uint, const TQString &msg) { errorBox(msg, TQString(), this); if ( !disconnected ) { // to avoid multiple calls disconnected = TRUE; // the zero timer is used to be outside the "internal" class TQTimer::singleShot(0, this, TQT_SLOT(singleHumanSlot())); } } void MPInterface::singleHumanSlot() { disconnected = FALSE; singleHuman(); } void MPInterface::paintEvent(TQPaintEvent *e) { TQPainter p(this); p.fillRect(e->rect(), darkGray); }