-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cpp
executable file
·178 lines (159 loc) · 5.21 KB
/
Engine.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <stdlib.h>
#ifdef __MACOSX_CORE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include "Engine.h"
#include "Network.h"
Engine::Engine() :
m_selectedWidget(NULL),
m_newRoundPad(NULL),
m_mouseCursor(new Spiral(Point2D(0, 0), 360, 5, 0, 5)),
m_cursorText(new Text(Point2D(-100, 100), "")),
m_textMode(TEXT_REPLACE),
m_xLine(Point2D(0, 0), Point2D(0, 0)),
m_yLine(Point2D(0, 0), Point2D(0, 0))
{
m_mouseCursor->setColor(Color(51 / (float)255, 153 / (float)255, 1, 1));
m_mouseCursor->setFilled(true);
m_cursorText->setColor(Color(51 / (float)255, 153 / (float)255, 1, 1));
m_xLine.setColor(Color(0.95, 0.95, 0.95, 0.5));
m_xLine.setLineWidth(0.1);
m_yLine.setColor(Color(0.95, 0.95, 0.95, 0.5));
m_yLine.setLineWidth(0.1);
m_parent = this;
}
Engine::~Engine()
{
delete m_mouseCursor;
delete m_cursorText;
}
bool Engine::handleDraw(float x, float y)
{
m_maxRadius = FLT_MAX;
for (std::vector<Widget*>::iterator cit = m_children.begin(); cit != m_children.end(); cit++) {
RoundPad* pad = dynamic_cast<RoundPad*>(*cit);
if (pad)
m_maxRadius = std::min(m_maxRadius,
Point2D::distance(m_mouseDownPos,
pad->getCenter()) - pad->getRadius());
}
if (m_newRoundPad)
m_newRoundPad->setRadius(std::min(m_maxRadius,
Point2D::distance(m_newRoundPad->getCenter(),
Point2D(x, y))));
else
m_newRoundPad = new RoundPad(m_mouseDownPos, std::min(m_maxRadius,
Point2D::distance(m_mouseDownPos,
Point2D(x, y))));
return true;
}
bool Engine::handleDrawEnd(float x, float y)
{
if (m_newRoundPad) {
Widget::getAll()->insert(WidgetData(m_newRoundPad->getUuid(), m_newRoundPad));
this->addChild(m_newRoundPad);
s_network->sendObjectMessage(m_newRoundPad);
m_newRoundPad = NULL;
}
return true;
}
bool Engine::onKeyDown(unsigned char key, float x, float y)
{
switch(key) {
case 127:
if (m_textMode == TEXT_REPLACE && m_selectedWidget &&
(m_selectedWidget = m_selectedWidget->getParent()->removeChild(m_selectedWidget))) {
for (int i = 0; i < 5; i++)
s_network->sendObjectMessage(m_selectedWidget, true);
delete m_selectedWidget;
m_selectedWidget = NULL;
} else if (m_textMode == TEXT_APPEND) {
std::string text;
if (m_selectedWidget && dynamic_cast<RoundPad*>(m_selectedWidget)) {
RoundPad* pad = (RoundPad*)m_selectedWidget;
text = pad->getCommentText()->getText();
pad->setCommentText(text.substr(0, text.length() - 1));
s_network->sendRoundPadTextMessage(pad);
} else {
text = m_cursorText->getText();
m_cursorText->setText(text.substr(0, text.length() - 1));
s_network->sendPeerTextMessage(m_cursorText->getText());
}
}
break;
case 27:
exit(1);
break;
default:
std::string character(1, key);
if (m_selectedWidget && dynamic_cast<RoundPad*>(m_selectedWidget)) {
RoundPad* pad = (RoundPad*)m_selectedWidget;
if (m_textMode == TEXT_REPLACE) {
pad->setCommentText(character);
m_textMode = TEXT_APPEND;
} else
pad->appendCommentText(character);
s_network->sendRoundPadTextMessage(pad);
} else {
if (m_textMode == TEXT_REPLACE) {
m_cursorText->setText(character);
m_textMode = TEXT_APPEND;
} else
m_cursorText->setText(m_cursorText->getText() + character);
s_network->sendPeerTextMessage(m_cursorText->getText());
}
}
glutPostRedisplay( );
return true;
}
void Engine::draw()
{
// clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw the elements
for (int i = 0; i < m_children.size(); i ++)
m_children[i]->draw();
// Draw in-progress new round pad, if any
if (m_newRoundPad)
m_newRoundPad->draw();
Network::PeerMap* peers = s_network->getPeers();
for (Network::PeerMap::iterator pit = peers->begin(); pit != peers->end(); pit++)
pit->second->draw();
m_xLine.draw();
m_yLine.draw();
m_mouseCursor->draw();
m_cursorText->draw();
}
void Engine::setMouseCursorPosition(float x, float y)
{
m_mouseCursor->setCenter(Point2D(x, y));
m_cursorText->setPos(Point2D(x + 10, y + 5));
for (std::vector<Widget *>::iterator cit = m_children.begin(); cit != m_children.end(); cit++)
if ((*cit)->hitTest(x, y)) {
m_xLine.setPoints(Point2D(0, 0), Point2D(0, 0));
m_yLine.setPoints(Point2D(0, 0), Point2D(0, 0));
return;
}
m_xLine.setPoints(Point2D(0, y), Point2D(m_width, y));
m_yLine.setPoints(Point2D(x, 0), Point2D(x, m_height));
return;
}
void Engine::setSize(int w, int h)
{
m_width = w;
m_height = h;
}
void Engine::setCursorText(std::string text)
{
m_cursorText->setText(text);
}