fix score
[multypo.git] / qt / player.cpp
index 40497881224caaea2026fc0549dd4d9e605eb02f..036d547d4482405524cbb37f53e7020eba2cb6b7 100644 (file)
@@ -1,28 +1,73 @@
 #include "player.h"
 
 #include <QLayout>
 #include "player.h"
 
 #include <QLayout>
+#include <QDebug>
+#include <QTextDocument>
 
 
-static QString colorToString(QColor col)
+Player::Player(QWidget* parent) : score(0), state(Naming) {
+       theLabel = new QLabel (parent);
+    theLabel->setTextFormat(Qt::RichText);
+       parent->layout()->addWidget(theLabel);
+       qDebug() << "Player created";
+}
+
+void Player::handleKey(QString str)
 {
 {
-       return QString("#%1%2%3").arg(col.red(), 2, 16, QChar('0'))
-                       .arg(col.green(), 2, 16, QChar('0'))
-                       .arg(col.blue(), 2, 16, QChar('0'));
+    // edit "current line"
+    if (str.length() == 1) {
+        currentWord += str;
+    }
+    else if (str == "BackSpace") {
+        currentWord.chop(1);
+    }
+    // see if this does anything useful
+    switch (state) {
+        case Naming:
+            setLabel(currentWord, BASE_COLOR);
+            if (str == "Return") {
+                name = currentWord;
+                state = Waiting;
+                setLabel("<Bereit>", READY_COLOR);
+            }
+            break;
+        case Waiting:
+            break;
+        case Typing:
+            setLabel(currentWord, BASE_COLOR);
+            break;
+    }
 }
 
 }
 
-Player::Player(QWidget* parent) {
-       theLabel = new QLabel (parent);
-       parent->layout()->addWidget(theLabel);
+void Player::setLabel(QString body, QString color)
+{
+    QString text = QString("<font color=%1>").arg(color);
+    if (state >= Waiting) {
+        text += QString("<font size=-3>%1:</font><br>").arg(Qt::escape(name));
+    }
+    text += Qt::escape(body);
+    theLabel->setText(text);
+}
+
+void Player::wordComplete(int points)
+{
+    score += points;
+    state = Waiting;
+    setLabel(currentWord, READY_COLOR);
+}
+
+QString Player::getCurrentWord() {
+    Q_ASSERT(state == Typing);
+       return currentWord;
+}
+
+void Player::nextWord() {
+    Q_ASSERT(state == Waiting);
+       currentWord = "";
+    setLabel("<Warte auf Eingabe>", BASE_COLOR);
+       state = Typing;
 }
 
 }
 
-void Player::handleKey(QString str) {
-       if (str.length() == 1) {
-               currentLine += str;
-       } else if (name.isNull() && str == "Return") {
-               // set name
-               name = currentLine;
-               currentLine = "";
-       } else if (str == "BackSpace") {
-               currentLine.chop(1);
-       }
-       theLabel->setText(currentLine);
+void Player::showScore() {
+    Q_ASSERT(state == Waiting);
+    setLabel(QString ("%1 Punkte").arg(score), READY_COLOR);
 }
 }