X-Git-Url: https://git.ralfj.de/multypo.git/blobdiff_plain/e6207dc52430a9fcbfd7df03f46c2876fc05154b..4978fd909202554270ec12c87d78a990001b17e4:/qt/player.cpp diff --git a/qt/player.cpp b/qt/player.cpp index 4c84eb7..036d547 100644 --- a/qt/player.cpp +++ b/qt/player.cpp @@ -1,55 +1,73 @@ #include "player.h" #include +#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); + theLabel->setTextFormat(Qt::RichText); parent->layout()->addWidget(theLabel); - modifyable = true; + 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: + setLabel(currentWord, BASE_COLOR); + if (str == "Return") { + name = currentWord; + state = Waiting; + setLabel("", READY_COLOR); + } + break; + case Waiting: + break; + case Typing: + setLabel(currentWord, BASE_COLOR); + break; + } } -QString Player::getName() { - return name; +void Player::setLabel(QString body, QString color) +{ + QString text = QString("").arg(color); + if (state >= Waiting) { + text += QString("%1:
").arg(Qt::escape(name)); + } + text += Qt::escape(body); + theLabel->setText(text); } -QString Player::getCurrentLine() { - return currentLine; +void Player::wordComplete(int points) +{ + score += points; + state = Waiting; + setLabel(currentWord, READY_COLOR); } -void Player::setWaiting(QString labeltext) { - theLabel->setText(labeltext); - modifyable = false; +QString Player::getCurrentWord() { + Q_ASSERT(state == Typing); + return currentWord; } -void Player::resetText() { - currentLine.clear(); - theLabel->setText(currentLine); - modifyable = true; +void Player::nextWord() { + Q_ASSERT(state == Waiting); + currentWord = ""; + setLabel("", BASE_COLOR); + state = Typing; } -void Player::handleKey(QString str) { - if (!modifyable) - return; - 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); + setLabel(QString ("%1 Punkte").arg(score), READY_COLOR); }