-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusernamescreen.cpp
More file actions
136 lines (111 loc) · 3.67 KB
/
usernamescreen.cpp
File metadata and controls
136 lines (111 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//AUTHORS: Matt Dumford, Anthony Phelps
#include "usernamescreen.h"
#include "mainmenu.h"
#include <QLineEdit>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QFont>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QTcpSocket>
// userNameScreen is shown only the first time the app is run.
// it takes in a QString with a QLineEdit to use as the players
// user name for the online high scores and multiplayer.
UsernameScreen::UsernameScreen(MainWindow *mainWindow, QWidget *parent) : QWidget(parent)
{
this->mainWindow = mainWindow;
socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
}
void UsernameScreen::display(int screenWidth, int screenHeight)
{
this->screenWidth = screenWidth;
this->screenHeight = screenHeight;
//set up layout
QFont font("Helvetica", 13);
layout = new QGridLayout();
this->setLayout(layout);
this->setFixedSize(screenWidth, screenHeight);
//create QLineEdit to take user input
QLineEdit *lineEdit = new QLineEdit();
lineEdit->setFixedWidth(screenWidth);
layout->addWidget(lineEdit, 1,0,1,1,Qt::AlignHCenter);
//instruction label
QLabel *label = new QLabel("Please input a username.");
label->setFixedSize(screenWidth, screenHeight/10);
label->setAlignment(Qt::AlignHCenter);
label->setFont(font);
layout->addWidget(label, 0,0,1,1,Qt::AlignHCenter);
//create ok button and add to the layout
QPushButton *okButton = new QPushButton("OK");
okButton->setFixedWidth(screenWidth/2);
layout->addWidget(okButton, 2,0,1,1,Qt::AlignHCenter);
//connect everything
connect(okButton, SIGNAL(clicked()), this, SLOT(okPushed()));
connect(lineEdit, SIGNAL(textEdited(QString)), this, SLOT(newText(QString)));
lineEdit->clearFocus();
this->show();
}
//updates text whenever the user makes changes in the QLineEdit
void UsernameScreen::newText(const QString &t)
{
qDebug() << "new text.";
text = t;
}
//saves text to a file on the sd card and passes the user name to mainWindow
//for easy access in other classes. Also creates main menu.
void UsernameScreen::okPushed()
{
qDebug() << "OK pushed.";
qDebug() << text;
//save text to sdcard/Puzzle Pictures/user.name
QFile file(QDir::rootPath().append("/sdcard/Puzzle Pictures/user.name"));
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out << text;
file.close();
mainWindow->setUserName(text);
qDebug() << "user.name written.";
makeCon();
//go to main menu
MainMenu *mm = new MainMenu(mainWindow, mainWindow);
mm->display(screenWidth, screenHeight);
}
QString UsernameScreen::parseResponse(QString s)
{
QStringList parts = s.split(":");
QString result;
return result;
}
void UsernameScreen::makeCon()
{
qDebug() << "Connecting...";
socket->connectToHost("ganymede.sytes.net", 4848);
if(!socket->waitForConnected(2000))
{
qDebug() << "ERROR: " << socket->errorString();
}
}
void UsernameScreen::connected()
{
qDebug() << "Connected";
socket->write(QString("user:new:"+ mainWindow->getUserName() + ":_:_" + "\n").toUtf8());
}
void UsernameScreen::disconnected()
{
qDebug() << "Disconnected";
}
void UsernameScreen::bytesWritten(qint64 bytes)
{
qDebug() << "Wrote Something: " << bytes << "bytes";
}
void UsernameScreen::readyRead()
{
qDebug() << "Reading...";
parseResponse(socket->readLine(1024));
}