summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/_Win/UI/PlayerContainerWnd.cpp
blob: 9572fca3b3e2b6fac0d358350abfeb9e90bdf5ed (plain)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//==============================================================================
//	Prefix
//==============================================================================

#include "stdafx.h"
#include "SceneView.h"
#include "Doc.h"
#include "StudioProjectSettings.h"
#include "Dispatch.h"
#include "HotKeys.h"
#include "MasterP.h"
#include "StudioApp.h"
#include "IStudioRenderer.h"

//==============================================================================
//	Includes
//==============================================================================

#include "PlatformStrings.h"
#include "PlayerContainerWnd.h"
#include "Qt3DSDMStudioSystem.h"
#include "Core.h"
#include "MainFrm.h"
#include "StudioUtils.h"

#include <QtWidgets/qscrollbar.h>
#include <QtGui/qevent.h>
#include <QtGui/qwindow.h>
#include <QtGui/qscreen.h>

//==============================================================================
//	Class CPlayerContainerWnd
//==============================================================================

//==============================================================================
/**
 *	Constructor: Initializes the object.
 */
//==============================================================================
CPlayerContainerWnd::CPlayerContainerWnd(CSceneView *inSceneView)
    : QScrollArea(inSceneView)
    , m_SceneView(inSceneView)
    , m_PlayerWnd(NULL)
    , m_IsMouseDown(false)
    , m_IsMiddleMouseDown(false)
    , m_ViewMode(VIEW_SCENE)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

//==============================================================================
/**
 *	Destructor: Releases the object.
 */
//==============================================================================
CPlayerContainerWnd::~CPlayerContainerWnd()
{
}

bool CPlayerContainerWnd::ShouldHideScrollBars()
{
    return m_ViewMode == VIEW_EDIT || g_StudioApp.IsAuthorZoom();
}

//==============================================================================
/**
 *	SetPlayerWndPosition: Sets the position of the child player window
 *
 *  Called when the view is scrolled to position the child player window
 *	@param outLeftPresentationEdge  the left edge of the presentation, with the scroll position
 *taken into consideration
 *	@param outTopPresentionEdge		the top edge of the presentation, with the scroll
 *position taken into consideration
 *
 */
//==============================================================================
void CPlayerContainerWnd::SetPlayerWndPosition(long &outLeftPresentationEdge,
                                               long &outTopPresentionEdge)
{
    long theHScrollPosition, theVScrollPosition;
    // Negate to adjust actual client positions
    theHScrollPosition = -horizontalScrollBar()->value();
    theVScrollPosition = -verticalScrollBar()->value();

    QRect theClientRect = rect();

    // Horizontal scrollbar does not exist
    if (m_ClientRect.width() < theClientRect.width()) {
        theHScrollPosition =
                theClientRect.left() + (theClientRect.width() / 2) - (m_ClientRect.width() / 2);
        outLeftPresentationEdge = theHScrollPosition;
    } else // This stays a negated value
        outLeftPresentationEdge = theHScrollPosition;

    // Vertical scrollbar does not exist
    if (m_ClientRect.height() < theClientRect.height()) {
        theVScrollPosition =
                theClientRect.top() + (theClientRect.height() / 2) - (m_ClientRect.height() / 2);
        outTopPresentionEdge = theVScrollPosition;
    } else // This stays a negated value
        outTopPresentionEdge = theVScrollPosition;

    // Move the child player window
    m_PlayerWnd->setGeometry(QRect(QPoint(theHScrollPosition, theVScrollPosition),
                                   m_ClientRect.size()));
}

//==============================================================================
/**
 *	SetScrollRanges: Sets the scroll ranges when the view is being resized
 */
//==============================================================================
void CPlayerContainerWnd::SetScrollRanges()
{

    long theScrollWidth = 0;
    long theScrollHeight = 0;

    if (ShouldHideScrollBars()) {
        horizontalScrollBar()->setRange(0, 0);
        verticalScrollBar()->setRange(0, 0);
        horizontalScrollBar()->setValue(0);
        verticalScrollBar()->setValue(0);
    } else {
        QSize theSize = GetEffectivePresentationSize();

        theScrollWidth = theSize.width();
        theScrollHeight = theSize.height();

        // Set scrollbar ranges
        horizontalScrollBar()->setRange(0, theScrollWidth - width());
        verticalScrollBar()->setRange(0, theScrollHeight - height());
        horizontalScrollBar()->setPageStep(width());
        verticalScrollBar()->setPageStep(height());
        horizontalScrollBar()->setVisible(true);
        verticalScrollBar()->setVisible(true);
    }
}

//==============================================================================
/**
 *	OnRulerGuideToggled:
 *	Handle scrollbar position when ruler, guide has been toggled
 *
 */
//==============================================================================
void CPlayerContainerWnd::OnRulerGuideToggled()
{
    int scrollAmount = g_StudioApp.GetRenderer().AreGuidesEnabled() ? 16 : -16;
    bool hasHorz = horizontalScrollBar()->isVisible();
    bool hasVert = verticalScrollBar()->isVisible();
    int hscrollPos = 0, vscrollPos = 0;
    if (hasHorz) {
        hscrollPos = qMax(horizontalScrollBar()->value() + scrollAmount, 0);
    }
    if (hasVert) {
        vscrollPos = qMax(verticalScrollBar()->value() + scrollAmount, 0);
    }
    horizontalScrollBar()->setValue(hscrollPos);
    verticalScrollBar()->setValue(vscrollPos);
    m_PlayerWnd->update();
}

qreal CPlayerContainerWnd::fixedDevicePixelRatio() const
{
    // Fix a problem on X11: https://bugreports.qt.io/browse/QTBUG-65570
    qreal ratio = devicePixelRatio();
    if (QWindow *w = window()->windowHandle()) {
        if (QScreen *s = w->screen())
            ratio = s->devicePixelRatio();
    }
    return ratio;
}

//==============================================================================
/**
 *	RecenterClient: Recenters the Client rect in the View's client area.
 */
//==============================================================================
void CPlayerContainerWnd::RecenterClient()
{
    QRect theViewClientRect = rect();
    QSize theClientSize;
    m_ClientRect = theViewClientRect;

    if (!ShouldHideScrollBars()) {
        theClientSize = GetEffectivePresentationSize();

        // Only center if we need to scroll
        if (theClientSize.width() > theViewClientRect.width()) {
            // compute the size of the client rectangle to display
            m_ClientRect.setLeft(
                        (theViewClientRect.width() / 2) - (theClientSize.width() / 2)
                        - (theClientSize.width() % 2));
            m_ClientRect.setRight((theViewClientRect.width() / 2) + (theClientSize.width() / 2));
        }

        if (theClientSize.height() > theViewClientRect.height()) {
            m_ClientRect.setTop(
                        (theViewClientRect.height() / 2) - (theClientSize.height() / 2)
                        - (theClientSize.height() % 2));
            m_ClientRect.setBottom((theViewClientRect.height() / 2) + (theClientSize.height() / 2));
        }
    }

    QRect glRect = m_ClientRect;
    glRect.setWidth(int(fixedDevicePixelRatio() * m_ClientRect.width()));
    glRect.setHeight(int(fixedDevicePixelRatio() * m_ClientRect.height()));
    g_StudioApp.GetRenderer().SetViewRect(glRect);
}

//==============================================================================
/**
 *	OnLButtonDown: Called whenever the user left clicks in the view.
 *	This processes the WM_LBUTTONDOWN message. This message is generated whenever
 *	the user left clicks in the view. Since this could involve selection of an item
 *	in the scene, it may (if the click is in the Client rect) call ProcessMouseClick()
 *	on the Document to perform the selection.
 *	@param inFlags the flags passed in from the message call
 *	@param inPoint the point where the event takes place
 */
void CPlayerContainerWnd::mousePressEvent(QMouseEvent *event)
{
    if ((event->button() == Qt::LeftButton) || (event->button() == Qt::RightButton)) {
        long theToolMode = g_StudioApp.GetToolMode();
        g_StudioApp.GetCore()->GetDispatch()->FireSceneMouseDown(
                    SceneDragSenderType::Matte, event->pos(), theToolMode);
        m_IsMouseDown = true;
    } else if (event->button() == Qt::MiddleButton) {
        const bool theCtrlKeyIsDown = event->modifiers() & Qt::ControlModifier;
        const bool theAltKeyIsDown = event->modifiers() & Qt::AltModifier;

        bool theToolChanged = false;
        if (rect().contains(event->pos()) && !IsDeploymentView()) {
            // If both the control key and the Alt key is not down
            if (!theCtrlKeyIsDown && !theAltKeyIsDown) {
                // press Scroll Wheel Click
                // Do Camera Pan
                g_StudioApp.SetToolMode(STUDIO_TOOLMODE_CAMERA_PAN);
                theToolChanged = true;
            } else if ((theAltKeyIsDown) && (!theCtrlKeyIsDown)) {
                // press Alt-Scroll Wheel Click
                // Do Camera Rotate if we are in 3D Camera
                if (g_StudioApp.GetRenderer().DoesEditCameraSupportRotation(
                            g_StudioApp.GetRenderer().GetEditCamera())) {
                    g_StudioApp.SetToolMode(STUDIO_TOOLMODE_CAMERA_ROTATE);
                    theToolChanged = true;
                }
            }
        }

        if (theToolChanged) {
            Q_EMIT toolChanged();
            m_SceneView->SetViewCursor();

            long theToolMode = g_StudioApp.GetToolMode();
            g_StudioApp.GetCore()->GetDispatch()->FireSceneMouseDown(SceneDragSenderType::Matte,
                                                                     event->pos(), theToolMode);
            m_IsMouseDown = true;
            m_IsMiddleMouseDown = true;
        }
    }
}

//==============================================================================
/**
 * OnLButtonUp: Called whenever the user releases the left mouse button.
 *
 * This processes the WM_LBUTTONUP message. This message is generated whenever
 * the left mouse button. We release the mouse capture to stop dragging.
 *
 * @param inFlags The flags passed in from the message call
 * @param inPoint The point where the event takes place
 */
//==============================================================================
void CPlayerContainerWnd::mouseReleaseEvent(QMouseEvent *event)
{
    if ((event->button() == Qt::LeftButton) || (event->button() == Qt::RightButton)) {
        // Need to commit the current command when we have a mouse up. :)
        g_StudioApp.GetCore()->GetDispatch()->FireSceneMouseUp(SceneDragSenderType::Matte);
        g_StudioApp.GetCore()->CommitCurrentCommand();
        m_IsMouseDown = false;
    } else if (event->button() == Qt::MiddleButton) {
        g_StudioApp.GetCore()->GetDispatch()->FireSceneMouseUp(SceneDragSenderType::Matte);
        g_StudioApp.GetCore()->CommitCurrentCommand();
        if (m_IsMiddleMouseDown) {
            m_IsMouseDown = false;
            m_IsMiddleMouseDown = false;

            const bool theCtrlKeyIsDown = event->modifiers() & Qt::ControlModifier;
            const bool theAltKeyIsDown = event->modifiers() & Qt::AltModifier;

            if (!IsDeploymentView()) {
                if (!theCtrlKeyIsDown && !theAltKeyIsDown) {
                    // none of the modifier key is pressed... reset to previous tool
                    m_SceneView->RestorePreviousTool();
                } else if (theCtrlKeyIsDown && theAltKeyIsDown) {
                    // since both modifier is down... let the ctrl has priority
                    m_SceneView->SetToolOnCtrl();
                }
                m_SceneView->SetViewCursor();
                Q_EMIT toolChanged();
            }
        }
    }
}

//==============================================================================
/**
 * OnMouseMove: Called whenever the user moves the mouse in the window.
 *
 * This processes the WM_MOUSEMOVE message. This message is generated whenever
 * the user moves the mouse in the view. This tells lets the document process it
 * as well since the user could be dragging an object.
 *
 * @param inFlags The flags passed in from the message call
 * @param inPoint The point where the event takes place
 */
//==============================================================================
void CPlayerContainerWnd::mouseMoveEvent(QMouseEvent* event)
{
    if (m_IsMouseDown) {
        QT3DS_PROFILE(OnMouseMove);

        long theModifierKeys = 0;
        if (event->buttons() & Qt::LeftButton)
            theModifierKeys = CHotKeys::MOUSE_LBUTTON | CHotKeys::GetCurrentKeyModifiers();
        else if (event->buttons() & Qt::RightButton)
            theModifierKeys = CHotKeys::MOUSE_RBUTTON | CHotKeys::GetCurrentKeyModifiers();

        long theToolMode = g_StudioApp.GetToolMode();
        g_StudioApp.GetCore()->GetDispatch()->FireSceneMouseDrag(
                    SceneDragSenderType::Matte, event->pos(), theToolMode, theModifierKeys);
    }
}

//==============================================================================
/**
 * OnMouseWheel: Called whenever the mouse wheel.
 *
 * This processes the WM_MOUSEWHEEL message.
 *
 * @param inFlags the flags passed in from the message call
 * @param inPoint the point where the event takes place
 */
//==============================================================================
void CPlayerContainerWnd::wheelEvent(QWheelEvent* event)
{
    // Note : Mouse wheel is a special animal of the scene drag tool. We dont change the tool
    // so as not to affect the toolbar button and the view cursor. This will just do the zoom
    // and the cursor is not changed.

    const bool theCtrlKeyIsDown = event->modifiers() & Qt::ControlModifier;
    const bool theAltKeyIsDown = event->modifiers() & Qt::AltModifier;

    // Mouse Wheel zooms the camera
    if (!theCtrlKeyIsDown && !theAltKeyIsDown && !IsDeploymentView()) {
        g_StudioApp.GetCore()->GetDispatch()->FireSceneMouseWheel(
                    SceneDragSenderType::Matte, event->delta(), STUDIO_TOOLMODE_CAMERA_ZOOM);
    } else {
        QScrollArea::wheelEvent(event);
    }
}

void CPlayerContainerWnd::scrollContentsBy(int, int)
{
    long x, y;
    SetPlayerWndPosition(x, y);
}

//==============================================================================
/**
 *	Set the view mode of the current scene view, whether we are in editing mode
 *	or deployment mode. For editing mode, we want to use the full scene area without
 *	any matte area.
 *	@param inViewMode	the view mode of this scene
 */
void CPlayerContainerWnd::SetViewMode(EViewMode inViewMode)
{
    m_ViewMode = inViewMode;
    m_SceneView->RecheckSizingMode();
    if (m_ViewMode == VIEW_SCENE) {
        // switching from edit mode to deployment mode, release the edit camera tool and set it to
        // object move
        long theCurrentToolSettings = g_StudioApp.GetToolMode();
        bool theIsCameraTool = (theCurrentToolSettings & STUDIO_CAMERATOOL_MASK ? true : false);
        if (theIsCameraTool) {
            g_StudioApp.SetToolMode(STUDIO_TOOLMODE_MOVE);
            m_SceneView->SetToolMode(STUDIO_TOOLMODE_MOVE);
        }
    }
}

//==============================================================================
/**
 *	return the view mode of the current scene view, whether we are in editing mode
 *	or deployment mode. For editing mode, we want to use the full scene area without
 *	any matte area.
 *	@return  the current view mode
 */
CPlayerContainerWnd::EViewMode CPlayerContainerWnd::GetViewMode()
{
    return m_ViewMode;
}

//==============================================================================
/**
 *	Checks whether we are in deployment view mode.
 *	@return true if is in deployment view mode, else false
 */
bool CPlayerContainerWnd::IsDeploymentView()
{
    return m_ViewMode == VIEW_SCENE ? true : false;
}

QSize CPlayerContainerWnd::GetEffectivePresentationSize() const
{
    CPt cSize = g_StudioApp.GetCore()->GetStudioProjectSettings()->GetPresentationSize();
    QSize theSize(cSize.x, cSize.y);

    // If we have guides, resize the window with enough space for the guides as well as the
    // presentation
    // This is a very dirty hack because we are of course hardcoding the size of the guides.
    // If the size of the guides never changes, the bet paid off.
    if (g_StudioApp.GetRenderer().AreGuidesEnabled())
        theSize += QSize(32, 32);

    return theSize / fixedDevicePixelRatio();
}

//==============================================================================

void CPlayerContainerWnd::SetPlayerWnd(CPlayerWnd *inPlayerWnd)
{
    m_PlayerWnd = inPlayerWnd;
    viewport()->setBackgroundRole(QPalette::Dark);
    m_PlayerWnd->SetContainerWnd(this);
    m_PlayerWnd->setParent(viewport());
    m_PlayerWnd->setVisible(true);
    m_PlayerWnd->resize(m_PlayerWnd->sizeHint());
}

//==============================================================================
/**
 *	OnSize: Handles the WM_SIZE message
 *
 *	Recenters the Client and recaluclates the matte when a resize message is
 *	generated.
 *
 *	@param  nType	Specifies the type of resizing requested.
 *	@param	cx		Specifies the new width of the client area.
 *	@param	cy		Specifies the new height of the client area.
 */
//==============================================================================
void CPlayerContainerWnd::resizeEvent(QResizeEvent* event)
{
    QAbstractScrollArea::resizeEvent(event);

    SetScrollRanges();
}