projects
/
multypo.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
add proper states and a sane API for players
[multypo.git]
/
qt
/
player.cpp
diff --git
a/qt/player.cpp
b/qt/player.cpp
index 69e7651432a114a5b6309c11d399e525cfbc715f..485382300e993e7913e1b3508d2ca5f6295f9b31 100644
(file)
--- a/
qt/player.cpp
+++ b/
qt/player.cpp
@@
-3,57
+3,58
@@
#include <QLayout>
#include <QDebug>
#include <QLayout>
#include <QDebug>
-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) : score(0) {
+Player::Player(QWidget* parent) : score(0), state(Naming) {
theLabel = new QLabel (parent);
parent->layout()->addWidget(theLabel);
theLabel = new QLabel (parent);
parent->layout()->addWidget(theLabel);
- modifyable = true;
qDebug() << "Player created";
}
qDebug() << "Player created";
}
-bool Player::hasName() {
- return !name.isEmpty();
-}
-
-QString Player::getName() {
- return name;
-}
-
-QString Player::getCurrentLine() {
- return currentLine;
+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("<READY>");
+ }
+ break;
+ case Waiting:
+ break;
+ case Typing:
+ theLabel->setText(currentWord);
+ break;
+ }
+}
+
+void Player::wordComplete(int points)
+{
+ score += points;
+ state = Waiting;
+ theLabel->setText("<COMPLETE>");
}
}
-
void Player::setWaiting(QString labeltext
) {
-
theLabel->setText(labeltext
);
-
modifyable = false
;
+
QString Player::getCurrentWord(
) {
+
Q_ASSERT(state == Typing
);
+
return currentWord
;
}
}
-void Player::
resetText
() {
- current
Line.clear()
;
-
theLabel->setText(currentLine
);
-
modifyable = true
;
+void Player::
nextWord
() {
+ current
Word = ""
;
+
theLabel->setText(currentWord
);
+
state = Typing
;
}
}
-bool Player::handleKey(QString str) {
- if (!modifyable)
- return false;
- bool newChar = false;
- if (str.length() == 1) {
- currentLine += str;
- newChar = true;
- } else if (!hasName() && str == "Return") {
- // set name
- name = currentLine;
- currentLine = "";
- } else if (str == "BackSpace") {
- currentLine.chop(1);
- }
- theLabel->setText(currentLine);
- return newChar;
+void Player::showScore() {
+ Q_ASSERT(state == Waiting);
+ theLabel->setText(QString ("Spieler %1 hat %2 Punkte.").arg(name).arg(score));
}
}