-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer.cpp
137 lines (119 loc) · 3.64 KB
/
viewer.cpp
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
137
//
// viewer Widget
//
#include <QComboBox>
#include <QLabel>
#include <QGridLayout>
#include <QFileDialog>
#include "viewer.h"
//
// Constructor
//
viewer::viewer()
{
// Set window title
setWindowTitle(tr("Lumpy"));
// Create new OpenGL widget
ogl = new opengl;
// First Person
QPushButton* toggleFirstPerson = new QPushButton("First Person");
toggleFirstPerson->setCheckable(true);
// Resolution (precision) dropdown
res = new QComboBox();
res->addItem("Low");
res->addItem("Medium");
res->addItem("High");
res->setCurrentIndex(2);
// Cursor size slider
cursorSize = new QSlider(Qt::Horizontal);
cursorSize->setRange(5,50);
cursorSize->setValue(20);
// Pause/resume button
light = new QPushButton("Pause");
// Cursor buttons
QPushButton* left = new QPushButton(QString::fromUtf8("\u2190"));
QPushButton* up = new QPushButton(QString::fromUtf8("\u2191"));
QPushButton* right = new QPushButton(QString::fromUtf8("\u2192"));
QPushButton* down = new QPushButton(QString::fromUtf8("\u2193"));
draw = new QPushButton("Draw");
// Reset & Quit buttons
QPushButton* reset = new QPushButton("Reset");
QPushButton* quit = new QPushButton("Quit");
// Set layout of child widgets
QGridLayout* layout = new QGridLayout;
layout->addWidget(ogl,0,0,10,1);
layout->addWidget(toggleFirstPerson,0,2,1,3);
layout->addWidget(new QLabel("Resolution (reset)"),1,1);
layout->addWidget(res,1,2,1,3);
layout->addWidget(new QLabel("Cursor size"),2,1);
layout->addWidget(cursorSize,2,2,1,3);
layout->addWidget(up,5,3);
layout->addWidget(left,6,2);
layout->addWidget(draw,6,3);
layout->addWidget(right,6,4);
layout->addWidget(down,7,3);
layout->addWidget(reset,9,1);
layout->addWidget(quit,9,2,1,3);
// Manage resizing
layout->setColumnStretch(0,100);
layout->setColumnMinimumWidth(0,100);
layout->setRowStretch(9,100);
setLayout(layout);
// Connect valueChanged() signals to ogl
connect(toggleFirstPerson,SIGNAL(clicked(bool)) , this,SLOT(setFirstPerson(bool)));
connect(res,SIGNAL(currentIndexChanged(int)) , ogl,SLOT(setPrecision(int)));
connect(cursorSize,SIGNAL(valueChanged(int)) , ogl,SLOT(setCursorSize(int)));
// Connect cursor signals
connect(left, SIGNAL(pressed()),this,SLOT(moveLeft()));
connect(up, SIGNAL(pressed()),this,SLOT(moveUp()));
connect(right,SIGNAL(pressed()),this,SLOT(moveRight()));
connect(down, SIGNAL(pressed()),this,SLOT(moveDown()));
connect(draw, SIGNAL(pressed()),ogl,SLOT(cursorDown()));
// Connect reset() and quit() signals
connect(reset,SIGNAL(pressed()) , this,SLOT(resetThings()));
connect(quit,SIGNAL(pressed()) , qApp,SLOT(quit()));
// Force keyboard focus to ogl
ogl->setFocus();
}
//
// Reset things
//
void viewer::resetThings()
{
res->setCurrentIndex(2);
cursorSize->setValue(20);
ogl->reset(); // May be redundant...
}
//
// Handle cursor interaction
//
void viewer::moveLeft() {ogl->moveCursorBy(-1, 0);}
void viewer::moveRight() {ogl->moveCursorBy( 1, 0);}
void viewer::moveUp() {ogl->moveCursorBy( 0, -1);}
void viewer::moveDown() {ogl->moveCursorBy( 0, 1);}
void viewer::keyPressEvent(QKeyEvent * event)
{
switch(event->key())
{
case Qt::Key_A:
moveLeft();
break;
case Qt::Key_D:
moveRight();
break;
case Qt::Key_W:
moveUp();
break;
case Qt::Key_S:
moveDown();
break;
case Qt::Key_Space:
ogl->cursorDown();
break;
}
}
void viewer::setFirstPerson(bool ifp)
{
ogl->setFirstPerson(ifp);
draw->setVisible(!ifp);
}