X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/9892f674393394ef97f5f9dad6a9e4e435f771ed..7a18f439db0b8a7d55ffd0368bb0159afa79ab62:/qt/multypo.cpp?ds=inline diff --git a/qt/multypo.cpp b/qt/multypo.cpp index 2584c0c..fff1da7 100644 --- a/qt/multypo.cpp +++ b/qt/multypo.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include @@ -11,39 +13,105 @@ MultypoWindow::MultypoWindow(QWidget *parent) : QWidget(parent), + state(Naming), xiInited(false) { /* Prepare colors */ - setStyleSheet("background-color: black; color: green"); + 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; - if (string == "Escape") + if (string == "Escape") { close(); + return; + } if (!players.contains(device)) { - QLabel *label = new QLabel(this); - layout()->addWidget(label); - players[device] = label; + if (state > Naming) + return; + players[device] = new Player(this); + } + 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()+1; + player->wordComplete(points); + QString readyString = QString("READY: %1 points").arg(points); + if (allPlayersWaiting()) { + nextWord(); + } + } } - players[device]->setText(players[device]->text() + string); } bool MultypoWindow::handleX11Event(XEvent *event) @@ -58,7 +126,7 @@ bool MultypoWindow::handleX11Event(XEvent *event) } /* Which version of XI2? We support 2.0 */ - int major = 2, minor = 0; + int major = 2, minor = 1; if (XIQueryVersion(dpy, &major, &minor) == BadRequest) { qCritical() << "XI2 not available. Server supports" << major << "." << minor; @@ -92,7 +160,13 @@ bool MultypoWindow::handleX11Event(XEvent *event) } else if (xiInited && event->type == GenericEvent && event->xcookie.type == GenericEvent && event->xcookie.extension == xiOpcode - && event->xcookie.evtype == XI_KeyPress && XGetEventData(dpy, &event->xcookie)) { + && event->xcookie.evtype == XI_KeyPress) { + + if (XGetEventData(dpy, &event->xcookie) != True) { + qCritical() << "Error getting event data"; + // FIXME return true; + } + /* Handle XI event */ XIDeviceEvent *d_ev = (XIDeviceEvent*) event->xcookie.data; KeyCode keycode = d_ev->detail;