X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/e8b07da1c5251e412f80fdeb94fb327b641fc956..318e322c3cd72e6a7f692c6f7c7770c0e15720c8:/qt/player.cpp diff --git a/qt/player.cpp b/qt/player.cpp index 871ecb9..4853823 100644 --- a/qt/player.cpp +++ b/qt/player.cpp @@ -1,46 +1,60 @@ #include "player.h" #include +#include -static QString colorToString(QColor col) -{ - 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')); -} - -Player::Player(QWidget* parent) { +Player::Player(QWidget* parent) : score(0), state(Naming) { theLabel = new QLabel (parent); parent->layout()->addWidget(theLabel); + qDebug() << "Player created"; } - -bool Player::hasName() { - return !name.isNull(); +void Player::handleKey(QString str) +{ + // 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: + theLabel->setText(currentWord); + if (str == "Return") { + name = currentWord; + state = Waiting; + theLabel->setText(""); + } + break; + case Waiting: + break; + case Typing: + theLabel->setText(currentWord); + break; + } } -QString Player::getName() { - return name; +void Player::wordComplete(int points) +{ + score += points; + state = Waiting; + theLabel->setText(""); } -QString Player::getCurrentLine() { - return currentLine; +QString Player::getCurrentWord() { + Q_ASSERT(state == Typing); + return currentWord; } -void Player::resetText() { - currentLine.clear(); - theLabel->setText(currentLine); +void Player::nextWord() { + currentWord = ""; + theLabel->setText(currentWord); + state = Typing; } -void Player::handleKey(QString str) { - if (str.length() == 1) { - currentLine += str; - } else if (!hasName() && str == "Return") { - // set name - name = currentLine; - currentLine = ""; - } else if (str == "BackSpace") { - currentLine.chop(1); - } - theLabel->setText(currentLine); +void Player::showScore() { + Q_ASSERT(state == Waiting); + theLabel->setText(QString ("Spieler %1 hat %2 Punkte.").arg(name).arg(score)); }