-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOgreWidget.cpp
More file actions
executable file
·345 lines (275 loc) · 8.85 KB
/
OgreWidget.cpp
File metadata and controls
executable file
·345 lines (275 loc) · 8.85 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
-----------------------------------------------------------------------------------
A QtMeshEditor file
Copyright (c) Fernando Tonon (https://github.com/fernandotonon)
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------------
*/
#include <QDebug>
#include <QTimer>
#include <QCoreApplication>
#include <QTimer>
#include <QNativeGestureEvent>
#include <Ogre.h>
#include "GlobalDefinitions.h"
#include "OgreWidget.h"
#include "Manager.h"
#include "SpaceCamera.h"
#include "EditorViewport.h"
#include "QtInputManager.h"
OgreWidget::OgreWidget( QWidget *parent ):
QWidget( parent )
{
setAttribute( Qt::WA_PaintOnScreen );
setAttribute( Qt::WA_OpaquePaintEvent );
setMinimumSize(100,100);
setFocusPolicy(Qt::ClickFocus); //one click to get focus
initOgreWindow();
}
OgreWidget::~OgreWidget()
{
// Safely clean up OGRE resources
// Order is important: remove listeners first, then destroy camera, then detach render target, then remove viewports
if(mOgreRoot)
{
try {
// Remove frame listener first
mOgreRoot->removeFrameListener(this);
} catch (...) {
// Ignore exceptions during shutdown
}
}
// Destroy camera BEFORE removing viewports (viewport may reference camera)
// This must happen before removing viewports
mCamera.reset();
if(mOgreWindow)
{
try {
// Remove all viewports (they should be cleared after camera destruction)
try {
mOgreWindow->removeAllViewports();
} catch (...) {
// Ignore exceptions during shutdown
}
// Detach render target from root if root still exists
if(mOgreRoot)
{
try {
mOgreRoot->detachRenderTarget(mOgreWindow);
} catch (...) {
// Ignore exceptions during shutdown
}
}
// Deactivate window
try {
mOgreWindow->setActive(false);
} catch (...) {
// Ignore exceptions during shutdown
}
mViewport = nullptr;
mOgreWindow = nullptr;
} catch (...) {
// If something goes wrong, just nullify pointers
mViewport = nullptr;
mOgreWindow = nullptr;
}
}
destroy();
}
int OgreWidget::getIndex() const
{
return (qobject_cast<EditorViewport *>(parent()))->getIndex();
}
QColor OgreWidget::getBackgroundColor() const
{
if(mViewport)
{
Ogre::ColourValue bgColour = mViewport->getBackgroundColour();
return QColor::fromRgbF(bgColour.r, bgColour.g, bgColour.b);
}
else
return QColor(Qt::black);
}
const Ogre::Viewport* OgreWidget::getViewport() const
{ return mViewport; }
void OgreWidget::setBackgroundColor(const QColor& c)
{
Ogre::ColourValue ogreColour;
ogreColour.setAsARGB(c.rgba());
if(mViewport)
mViewport->setBackgroundColour(ogreColour);
}
void OgreWidget::initOgreWindow(void)
{
mOgreRoot = Manager::getSingleton()->getRoot();
// Get the parameters of the window QT created
Ogre::String winHandle;
#ifdef WIN32
// Windows code
winHandle = Ogre::StringConverter::toString((unsigned long)(this->parentWidget()->winId()));
#else
winHandle = Ogre::StringConverter::toString(winId());
#endif
Ogre::NameValuePairList params;
params["externalWindowHandle"] = winHandle;
#ifdef Q_OS_MACOS
params["macAPI"] = "cocoa";
params["macAPICocoaUseNSView"] = "true";
#endif
QString name = "Viewport " + QString::number(getIndex());
while (mOgreRoot->getRenderTarget(name.toStdString())) {
name+=".";
}
mOgreWindow = mOgreRoot->createRenderWindow( name.toStdString().data(),
width(),
height(),
false,
¶ms );
mOgreWindow->setActive(true);
mCamera = std::make_unique<SpaceCamera>(this);
mViewport = mOgreWindow->addViewport( mCamera->getCamera() );
mViewport->setBackgroundColour( Ogre::ColourValue( 0,0,0 ) );
mViewport->setVisibilityMask(SCENE_VISIBILITY_FLAGS);
mOgreRoot->addFrameListener(this);
}
bool OgreWidget::frameStarted(const Ogre::FrameEvent& e)
{
return true;
}
bool OgreWidget::frameRenderingQueued(const Ogre::FrameEvent &e)
{
return true;
}
bool OgreWidget::frameEnded(const Ogre::FrameEvent& e)
{
// Safety check: don't access window if widget is being destroyed
if(mOgreWindow && mOgreRoot)
{
try {
mOgreWindow->windowMovedOrResized();
mOgreWindow->update();
} catch (...) {
// Ignore exceptions during shutdown
return false;
}
}
return true;
}
QPaintEngine* OgreWidget:: paintEngine() const
{ return nullptr; }
void OgreWidget::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
if(e->isAccepted())
{
const QSize &newSize = e->size();
if(mOgreWindow)
{
mOgreWindow->resize(newSize.width(), newSize.height());
mOgreWindow->windowMovedOrResized();
}
if(mCamera)
mCamera->setAspectRatio((Ogre::Real)width()/(Ogre::Real)height());
update();
}
}
void OgreWidget::moveEvent(QMoveEvent *e)
{
QWidget::moveEvent(e);
if(e->isAccepted() && mOgreWindow)
{
mOgreWindow->windowMovedOrResized();
update();
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Mouse & Keyboard event Widget level
void OgreWidget::keyPressEvent(QKeyEvent *e)
{
mCamera->keyPressEvent(e);
e->ignore();
}
void OgreWidget::keyReleaseEvent(QKeyEvent *e)
{ mCamera->keyReleaseEvent(e); }
void OgreWidget::wheelEvent(QWheelEvent *e)
{
QtInputManager::getInstance().wheelEvent(e);
mCamera->wheelEvent(e);
}
bool OgreWidget::event(QEvent *e)
{
if (e->type() == QEvent::NativeGesture) {
auto *gestureEvent = static_cast<QNativeGestureEvent*>(e);
if (gestureEvent->gestureType() == Qt::ZoomNativeGesture) {
int delta = static_cast<int>(gestureEvent->value() * 200 * mCamera->getCameraSpeed());
mCamera->zoomByDelta(delta);
e->accept();
return true;
}
}
return QWidget::event(e);
}
void OgreWidget::mousePressEvent(QMouseEvent *e)
{
QtInputManager::getInstance().mousePressEvent(e);
mCamera->mousePressEvent(e);
if(e->buttons().testFlag(Qt::MiddleButton))
{
QCursor cursor = this->cursor();
cursor.setShape(Qt::SizeAllCursor);
QWidget::setCursor(cursor);
e->accept();
}
else if(e->buttons().testFlag(Qt::LeftButton))
{
QCursor cursor = this->cursor();
cursor.setShape(Qt::ClosedHandCursor);
QWidget::setCursor(cursor);
e->accept();
}
else
{
e->ignore();
}
}
void OgreWidget::mouseMoveEvent(QMouseEvent *e)
{
QtInputManager::getInstance().mouseMoveEvent(e);
mCamera->mouseMoveEvent(e);
}
void OgreWidget::mouseReleaseEvent(QMouseEvent *e)
{
QtInputManager::getInstance().mouseReleaseEvent(e);
mCamera->mouseReleaseEvent(e);
QCursor cursor = this->cursor();
cursor.setShape(Qt::ArrowCursor);
QWidget::setCursor(cursor);
}
void OgreWidget::focusInEvent(QFocusEvent* e)
{
emit focusOnWidget(this);
parentWidget()->update(); //required to show the highlight on the DockWidget
mViewport->setVisibilityMask(SCENE_VISIBILITY_FLAGS | GUI_VISIBILITY_FLAGS);
e->accept();
}
void OgreWidget::focusOutEvent(QFocusEvent* e)
{
parentWidget()->update(); //required to show the highlight on the DockWidget
mViewport->setVisibilityMask(SCENE_VISIBILITY_FLAGS);
e->accept();
}