X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/b02f9fdbd25d756d6ca73156d0b21a703e17d1de..04ba0d5a99f63208188c4301aa21b743c42141cb:/qt/multypo.cpp diff --git a/qt/multypo.cpp b/qt/multypo.cpp index 638114c..8dea3e0 100644 --- a/qt/multypo.cpp +++ b/qt/multypo.cpp @@ -2,8 +2,9 @@ #include #include -#include +#include #include +#include #include #include @@ -13,23 +14,89 @@ MultypoWindow::MultypoWindow(QWidget *parent) : QWidget(parent), xiInited(false) { - QHBoxLayout *layout = new QHBoxLayout(this); - setLayout(layout); + /* Prepare colors */ + setStyleSheet("background-color: black; color: green; font-size: 30pt"); - mainLabel = new QLabel(this); - layout->addWidget(mainLabel); + /* Prepare conents */ + setLayout(new QVBoxLayout(this)); - setMinimumSize(300, 300); + mainLabel = new QLabel("Hit to quit", this); + layout()->addWidget(mainLabel); + + /* Fullscreen, no cursor */ + setWindowState(Qt::WindowFullScreen); + setCursor(QCursor(Qt::BlankCursor)); + + words.open(stdin,QIODevice::ReadOnly); } 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); + 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; - mainLabel->setText(QString("%1 pressed on %2").arg(string).arg(device)); + + if (string == "Escape") { + close(); + return; + } + + if (!players.contains(device)) { + players[device] = new Player(this); + } + players[device]->handleKey(string); + + if (gameStarted) { // ingame + qDebug() << players[device]->getCurrentLine(); + qDebug() << mainLabel->text(); + if (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 + bool allHaveNames = true; + for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { + if (! it.value()->hasName()) { + allHaveNames = false; + break; + } + } + if (!players.empty() && allHaveNames) { + gameStarted = true; + nextWord(); + } + } } bool MultypoWindow::handleX11Event(XEvent *event) @@ -44,7 +111,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; @@ -70,6 +137,7 @@ bool MultypoWindow::handleX11Event(XEvent *event) qDebug() << "Found master keyboard with ID" << devices[i].deviceid; XISetFocus(dpy, devices[i].deviceid, winId(), CurrentTime); } + XIFreeDeviceInfo(devices); xiInited = true; @@ -77,7 +145,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;