add proper states and a sane API for players
[multypo.git] / qt / player.cpp
1 #include "player.h"
2
3 #include <QLayout>
4 #include <QDebug>
5
6 Player::Player(QWidget* parent) : score(0), state(Naming) {
7         theLabel = new QLabel (parent);
8         parent->layout()->addWidget(theLabel);
9         qDebug() << "Player created";
10 }
11
12 void Player::handleKey(QString str)
13 {
14     // edit "current line"
15     if (str.length() == 1) {
16         currentWord += str;
17     }
18     else if (str == "BackSpace") {
19         currentWord.chop(1);
20     }
21     // see if this does anything useful
22     switch (state) {
23         case Naming:
24             theLabel->setText(currentWord);
25             if (str == "Return") {
26                 name = currentWord;
27                 state = Waiting;
28                 theLabel->setText("<READY>");
29             }
30             break;
31         case Waiting:
32             break;
33         case Typing:
34             theLabel->setText(currentWord);
35             break;
36     }
37 }
38
39 void Player::wordComplete(int points)
40 {
41     score += points;
42     state = Waiting;
43     theLabel->setText("<COMPLETE>");
44 }
45
46 QString Player::getCurrentWord() {
47     Q_ASSERT(state == Typing);
48         return currentWord;
49 }
50
51 void Player::nextWord() {
52         currentWord = "";
53     theLabel->setText(currentWord);
54         state = Typing;
55 }
56
57 void Player::showScore() {
58     Q_ASSERT(state == Waiting);
59     theLabel->setText(QString ("Spieler %1 hat %2 Punkte.").arg(name).arg(score));
60 }