X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/9892f674393394ef97f5f9dad6a9e4e435f771ed..48457df920b6f2c3747baa57366cc23389b3caca:/qt/multypo.cpp diff --git a/qt/multypo.cpp b/qt/multypo.cpp index 2584c0c..5ccfed8 100644 --- a/qt/multypo.cpp +++ b/qt/multypo.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -11,39 +12,99 @@ MultypoWindow::MultypoWindow(QWidget *parent) : QWidget(parent), - xiInited(false) + xiInited(false), + gameStarted(false) { /* Prepare colors */ - setStyleSheet("background-color: black; color: green"); + setStyleSheet("background-color: black; color: green; font-size: 45pt"); /* Prepare conents */ setLayout(new QVBoxLayout(this)); - mainLabel = new QLabel("Hit to quit", this); + mainLabel = new QLabel("Namen eingeben, dann Return", this); layout()->addWidget(mainLabel); /* Fullscreen, no cursor */ setWindowState(Qt::WindowFullScreen); setCursor(QCursor(Qt::BlankCursor)); + + words.open(stdin,QIODevice::ReadOnly); + + typingPlayers = 0; } MultypoWindow::~MultypoWindow() { } +void MultypoWindow::resetPlayerText() { + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + it.value()->resetText(); + } +} + +void MultypoWindow::nextWord() { + QByteArray tmp = words.readLine().trimmed(); + QString word = QString::fromUtf8(tmp); + qDebug() << "New word" << word; + if (word.isEmpty()) { // game over + mainLabel->setText("GAME OVER"); + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + QString tmp = QString ("Spieler %1 hat %2 Punkte.").arg(it.value()->getName()).arg(it.value()->score); + it.value()->setWaiting(tmp); + } + } else { + resetPlayerText(); + mainLabel->setText(word); + typingPlayers = players.size(); + } +} + 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 (gameStarted) + return; + players[device] = new Player(this); + } + bool newChar = players[device]->handleKey(string); + + if (gameStarted) { // ingame + qDebug() << "current player line" << players[device]->getCurrentLine(); + qDebug() << "current word" << mainLabel->text(); + if (newChar && players[device]->getCurrentLine() == mainLabel->text()) { + players[device]->score += typingPlayers; + QString readyString = QString("READY: %1 points").arg(typingPlayers); + players[device]->setWaiting(readyString); + qDebug() << "typingPlayers " << typingPlayers; + typingPlayers--; + qDebug() << "typingPlayers " << typingPlayers; + if (typingPlayers <= 0) { + nextWord(); + } + } + } else { // name entering phase + qDebug() << "checking for game started"; + bool allHaveNames = true; + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + if (! it.value()->hasName()) { + allHaveNames = false; + break; + } + } + qDebug() << "Players empty?" << players.empty() << "All have names?" << allHaveNames; + if (!players.empty() && allHaveNames) { + gameStarted = true; + nextWord(); + } } - players[device]->setText(players[device]->text() + string); } bool MultypoWindow::handleX11Event(XEvent *event) @@ -58,7 +119,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 +153,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;