show player name while typing
authorRalf Jung <post@ralfj.de>
Thu, 17 Jul 2014 12:55:06 +0000 (14:55 +0200)
committerRalf Jung <post@ralfj.de>
Thu, 17 Jul 2014 12:55:06 +0000 (14:55 +0200)
play.sh
qt/multypo.cpp
qt/multypo.h
qt/player.cpp
qt/player.h
words.txt

diff --git a/play.sh b/play.sh
index 128a25d93bf509feb9e786467b3c21daee9e17e1..b427eeed67fc92378f8721f178ee342dbe533efd 100755 (executable)
--- 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
index d58d247728286c90c3225df9dd2f0e72d83536f6..fff1da762ff487233d996cf39f28c70df1df1a17 100644 (file)
@@ -5,6 +5,7 @@
 #include <QVBoxLayout>
 #include <QLabel>
 #include <QMap>
+#include <QTextDocument>
 
 #include <X11/Xlib.h>
 #include <X11/extensions/XInput2.h>
@@ -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("<font color=%1>%2</span>").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<int, Player*>::Iterator it = players.begin(); it != players.end(); ++it) {
                        it.value()->showScore();
                }
@@ -49,7 +56,7 @@ void MultypoWindow::nextWord() {
                for (QMap<int, Player*>::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);
index 3117c9f1b0fa5b08de4e42e6e8988322a91ba6a3..66728a6d8264352068f53bb381ed595afed3ba4c 100644 (file)
@@ -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
index 485382300e993e7913e1b3508d2ca5f6295f9b31..c75bb4614ec71fb004c41e8dda348dddb55f3452 100644 (file)
@@ -2,9 +2,11 @@
 
 #include <QLayout>
 #include <QDebug>
+#include <QTextDocument>
 
 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("<READY>");
+                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("<font color=%1>").arg(color);
+    if (!header.isEmpty()) {
+        text += QString("<font size=-3>%1</font><br>").arg(Qt::escape(header));
+    }
+    text += Qt::escape(body);
+    theLabel->setText(text);
+}
+
 void Player::wordComplete(int points)
 {
     score += points;
     state = Waiting;
-    theLabel->setText("<COMPLETE>");
+    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("<Eingabe erscheint hier>", 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);
 }
index 3817866e14641d36b790ad35d1fc14a473fdd095..f28028f6687a7316f9832e6cb0af1725e28c6ab7 100644 (file)
@@ -4,6 +4,11 @@
 #include <QString>
 #include <QLabel>
 
+#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
index 916333c818c34f99189281de453f04c58e4467a3..fa036d0377e515282ddef1d6a2fc472eeadbdd37 100644 (file)
--- a/words.txt
+++ b/words.txt
@@ -1,4 +1,4 @@
 garden
-tree
-dddddddddddddddddddddddddd
+trees
+tests
 chamber