From: Ralf Jung Date: Thu, 17 Jul 2014 12:55:06 +0000 (+0200) Subject: show player name while typing X-Git-Url: https://git.ralfj.de/multypo.git/commitdiff_plain/7a18f439db0b8a7d55ffd0368bb0159afa79ab62 show player name while typing --- diff --git a/play.sh b/play.sh index 128a25d..b427eee 100755 --- a/play.sh +++ b/play.sh @@ -12,7 +12,7 @@ fi export LC_ALL=C # dont do unicode stuff in egrep while true; do - sort -R "$WORDSFILE" | sed 's/\/.*//' | egrep '^[A-Za-z]{5,}$' | head -n "$WORDSTOPLAY" | tr [:upper:] [:lower:] | qt/multypo + sort -R "$WORDSFILE" | sed 's/\/.*//' | egrep '^[A-Za-z]{5,}$' | head -n "$WORDSTOPLAY" | tr [:upper:] [:lower:] | qt/multypo echo "Press Ctrl-C to quit" sleep 3 done diff --git a/qt/multypo.cpp b/qt/multypo.cpp index d58d247..fff1da7 100644 --- a/qt/multypo.cpp +++ b/qt/multypo.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -16,12 +17,13 @@ MultypoWindow::MultypoWindow(QWidget *parent) : xiInited(false) { /* Prepare colors */ - setStyleSheet("background-color: black; color: green; font-size: 45pt"); + setStyleSheet("background-color: black; font-size: 45pt"); /* Prepare conents */ setLayout(new QVBoxLayout(this)); - - mainLabel = new QLabel("Namen eingeben, dann Return", this); + mainLabel = new QLabel(this); + mainLabel->setTextFormat(Qt::RichText); + setLabel("Namen eingeben, dann Return", BASE_COLOR); layout()->addWidget(mainLabel); /* Fullscreen, no cursor */ @@ -35,12 +37,17 @@ 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; + qDebug() << "New word:" << currentWord; if (currentWord.isEmpty()) { // game over - mainLabel->setText("GAME OVER"); + setLabel("GAME OVER", READY_COLOR); for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { it.value()->showScore(); } @@ -49,7 +56,7 @@ void MultypoWindow::nextWord() { for (QMap::Iterator it = players.begin(); it != players.end(); ++it) { it.value()->nextWord(); } - mainLabel->setText(currentWord); + setLabel(currentWord, BASE_COLOR); state = Playing; } } @@ -90,14 +97,12 @@ void MultypoWindow::handleKeyPress(int device, QString string) if (state == Naming) { // someone's still naming (or nobody's there yet) - qDebug() << "checking for game started"; if (!players.empty() && allPlayersWaiting()) { + qDebug() << "Game starting"; nextWord(); } } else if (state == Playing) { // all players are waiting or typing - qDebug() << "current player line" << player->getCurrentWord(); - qDebug() << "current word" << currentWord; if (player->getState() == Player::Typing && player->getCurrentWord() == currentWord) { int points = typingPlayers()+1; player->wordComplete(points); diff --git a/qt/multypo.h b/qt/multypo.h index 3117c9f..66728a6 100644 --- a/qt/multypo.h +++ b/qt/multypo.h @@ -28,6 +28,7 @@ private: void nextWord(); bool allPlayersWaiting(); int typingPlayers(); + void setLabel(QString text, QString color); State state; // naming iff (no players or any player is naming) QString currentWord; // defined iff state == Playing diff --git a/qt/player.cpp b/qt/player.cpp index 4853823..c75bb46 100644 --- a/qt/player.cpp +++ b/qt/player.cpp @@ -2,9 +2,11 @@ #include #include +#include Player::Player(QWidget* parent) : score(0), state(Naming) { theLabel = new QLabel (parent); + theLabel->setTextFormat(Qt::RichText); parent->layout()->addWidget(theLabel); qDebug() << "Player created"; } @@ -21,26 +23,36 @@ void Player::handleKey(QString str) // see if this does anything useful switch (state) { case Naming: - theLabel->setText(currentWord); + setLabel(currentWord, BASE_COLOR); if (str == "Return") { name = currentWord; state = Waiting; - theLabel->setText(""); + setLabel(name, READY_COLOR); } break; case Waiting: break; case Typing: - theLabel->setText(currentWord); + setLabel(currentWord, BASE_COLOR, name+":"); break; } } +void Player::setLabel(QString body, QString color, QString header) +{ + QString text = QString("").arg(color); + if (!header.isEmpty()) { + text += QString("%1
").arg(Qt::escape(header)); + } + text += Qt::escape(body); + theLabel->setText(text); +} + void Player::wordComplete(int points) { score += points; state = Waiting; - theLabel->setText(""); + setLabel(currentWord, READY_COLOR, name+":"); } QString Player::getCurrentWord() { @@ -49,12 +61,13 @@ QString Player::getCurrentWord() { } void Player::nextWord() { + Q_ASSERT(state == Waiting); currentWord = ""; - theLabel->setText(currentWord); + setLabel("", BASE_COLOR, name+":"); state = Typing; } void Player::showScore() { Q_ASSERT(state == Waiting); - theLabel->setText(QString ("Spieler %1 hat %2 Punkte.").arg(name).arg(score)); + setLabel(QString ("Spieler %1 hat %2 Punkte.").arg(name).arg(score), READY_COLOR); } diff --git a/qt/player.h b/qt/player.h index 3817866..f28028f 100644 --- a/qt/player.h +++ b/qt/player.h @@ -4,6 +4,11 @@ #include #include +#define BASE_COLOR "green" +#define READY_COLOR "orange" +#define BASE_SIZE "45pt" +#define SMALL_SIZE "25pt" + class Player { public: @@ -31,7 +36,7 @@ private: State state; - void updateLabel(); + void setLabel(QString body, QString color, QString header = ""); }; #endif // PLAYER_H diff --git a/words.txt b/words.txt index 916333c..fa036d0 100644 --- a/words.txt +++ b/words.txt @@ -1,4 +1,4 @@ garden -tree -dddddddddddddddddddddddddd +trees +tests chamber