X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/036922ba5e90ae7f6f6abe3f99b1783dec5132fc..refs/heads/master:/qt/multypo.cpp?ds=sidebyside diff --git a/qt/multypo.cpp b/qt/multypo.cpp index 0c5451e..07d60b3 100644 --- a/qt/multypo.cpp +++ b/qt/multypo.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include @@ -11,26 +13,71 @@ MultypoWindow::MultypoWindow(QWidget *parent) : QWidget(parent), + state(Naming), xiInited(false) { /* Prepare colors */ - setStyleSheet("background-color: black; color: green; font-size: 30pt"); + setStyleSheet("background-color: black; font-size: 45pt"); /* Prepare conents */ setLayout(new QVBoxLayout(this)); - - mainLabel = new QLabel("Hit to quit", this); + mainLabel = new QLabel(this); + mainLabel->setTextFormat(Qt::RichText); + setLabel("Namen eingeben, dann Return", BASE_COLOR); layout()->addWidget(mainLabel); /* Fullscreen, no cursor */ setWindowState(Qt::WindowFullScreen); setCursor(QCursor(Qt::BlankCursor)); + + words.open(stdin,QIODevice::ReadOnly); } MultypoWindow::~MultypoWindow() { } +void MultypoWindow::setLabel(QString text, QString color) +{ + mainLabel->setText(QString("%2").arg(color).arg(Qt::escape(text))); +} + +void MultypoWindow::nextWord() { + QByteArray tmp = words.readLine().trimmed(); + currentWord = QString::fromUtf8(tmp); + qDebug() << "New word:" << currentWord; + if (currentWord.isEmpty()) { // game over + setLabel("GAME OVER", READY_COLOR); + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + it.value()->showScore(); + } + state = Scoring; + } else { + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + it.value()->nextWord(); + } + setLabel(currentWord, BASE_COLOR); + state = Playing; + } +} + +bool MultypoWindow::allPlayersWaiting() +{ + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + if (it.value()->getState() != Player::Waiting) return false; + } + return true; +} + +int MultypoWindow::typingPlayers() +{ + int n = 0; + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + if (it.value()->getState() == Player::Typing) ++n; + } + return n; +} + void MultypoWindow::handleKeyPress(int device, QString string) { qDebug() << "Device" << device << "String" << string; @@ -41,9 +88,30 @@ void MultypoWindow::handleKeyPress(int device, QString string) } if (!players.contains(device)) { + if (state > Naming) + return; players[device] = new Player(this); } - players[device]->handleKey(string); + Player *player = players[device]; + player->handleKey(string); + + if (state == Naming) { + // someone's still naming (or nobody's there yet) + if (!players.empty() && allPlayersWaiting()) { + qDebug() << "Game starting"; + nextWord(); + } + } + else if (state == Playing) { // all players are waiting or typing + if (player->getState() == Player::Typing && player->getCurrentWord() == currentWord) { + int points = typingPlayers(); + player->wordComplete(points); + QString readyString = QString("READY: %1 points").arg(points); + if (allPlayersWaiting()) { + nextWord(); + } + } + } } bool MultypoWindow::handleX11Event(XEvent *event) @@ -82,7 +150,7 @@ bool MultypoWindow::handleX11Event(XEvent *event) for (int i = 0; i < ndevices; ++i) { if (devices[i].use != XIMasterKeyboard) continue; qDebug() << "Found master keyboard with ID" << devices[i].deviceid; - //XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime); + XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime); } XIFreeDeviceInfo(devices);