summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/_Win/UI/EditCameraBar.cpp
blob: 4f658da41dfe35a3a1040c020b2386b36d08333f (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
/****************************************************************************
**
** Copyright (C) 2006 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"

//==============================================================================
//	Includes
//==============================================================================
#include "EditCameraBar.h"
#include "MainFrm.h"
#include "SceneView.h"
#include "StudioPreferences.h"
#include "StudioApp.h"
#include "IStudioRenderer.h"

#include <QtWidgets/qcombobox.h>
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qabstractitemview.h>
#include <QtWidgets/qlistview.h>

//==============================================================================
/**
 *	Constructor
 */
CEditCameraBar::CEditCameraBar(QWidget* parent)
    : QToolBar(parent)
    , m_SceneView(nullptr)
    , m_ActiveCameraIndex(-1)
{
    OnCustomizeToolbar();
    auto activated = static_cast<void(QComboBox::*)(int)>(&QComboBox::activated);
    connect(m_CameraSelector, activated, this, &CEditCameraBar::OnCameraChanged);
}

//==============================================================================
/**
 *	Destructor
 */
CEditCameraBar::~CEditCameraBar()
{
    delete m_CameraSelector;
}

//==============================================================================
/**
 *	Setup the list of edit cameras into the camera combo box
 *	@param inCameras	the container that holds the edit cameras
 */
void CEditCameraBar::SetupCameras()
{
    m_CameraSelector->clear();
    Q3DStudio::IStudioRenderer &theRenderer = g_StudioApp.getRenderer();
    QStringList theCameraNames;
    theRenderer.GetEditCameraList(theCameraNames);
    int idx = 1;
    for (const QString &str : qAsConst(theCameraNames)) {
        m_CameraSelector->addItem(str, QVariant((int)idx));
        idx++;
    }

    m_CameraSelector->addItem(tr("Scene Camera View"));
    m_CameraSelector->setItemData(m_CameraSelector->count() - 1, 0);

    m_CameraSelector->insertSeparator(m_CameraSelector->count() - 1);
    // adding a 1px spacing, else the separator will disappear sometimes (QComboBox bug)
    qobject_cast<QListView*>(m_CameraSelector->view())->setSpacing(1);

    long thePreferredView = CStudioPreferences::GetPreferredStartupView();
    long theNumItems = m_CameraSelector->count();
    if (thePreferredView == -1) {
        // deployment view
        m_CameraSelector->setCurrentIndex(theNumItems - 1);
        HandleCameraChanged(theNumItems - 1); // set to the last one
    } else {
        int theIndex;
        if (thePreferredView < theNumItems - 2)
            theIndex = thePreferredView;
        else // possibly from old content where cameras are removed
            theIndex = 0;
        m_CameraSelector->setCurrentIndex(theIndex);
        HandleCameraChanged(theIndex);
    }
}

//==============================================================================
/**
 *	Callback method when the camera is changed from the camera selection combo box
 */
void CEditCameraBar::OnCameraChanged()
{
    HandleCameraChanged(m_CameraSelector->currentIndex());
}

void CEditCameraBar::setCameraIndex(int inIndex)
{
    m_CameraSelector->setCurrentIndex(inIndex);
    OnCameraChanged();
}

//==============================================================================
/**
 *	Handle the switching of the current edit camera
 *	@param inIndex	the index of the to-be-activated camera in the combo box
 */
void CEditCameraBar::HandleCameraChanged(int inIndex)
{
    Q3DStudio::IStudioRenderer &theRenderer = g_StudioApp.getRenderer();
    QStringList theCameraNames;
    theRenderer.GetEditCameraList(theCameraNames);
    int theNumCameras = theCameraNames.size();
    if (inIndex < theNumCameras) {
        theRenderer.SetEditCamera(inIndex);
        m_ActiveCameraIndex = inIndex;
        if (m_SceneView)
            m_SceneView->setViewMode(CPlayerContainerWnd::VIEW_EDIT);
    } else if (inIndex > theNumCameras) {
        theRenderer.SetEditCamera(-1);
        m_ActiveCameraIndex = inIndex;
        if (m_SceneView)
            m_SceneView->setViewMode(CPlayerContainerWnd::VIEW_SCENE);
    } else {
        m_CameraSelector->setCurrentIndex(m_ActiveCameraIndex);
    }
    if (m_SceneView)
        m_SceneView->onEditCameraChanged();

    CMainFrame *theMainFrame = g_StudioApp.m_pMainWnd;
    ASSERT(theMainFrame != nullptr);

    // if the current tool is camera rotate and has been switch to 2d camera
    // set the tool to camera pan
    if (theMainFrame != nullptr) {
        long theToolMode = g_StudioApp.GetToolMode();
        if (!theRenderer.DoesEditCameraSupportRotation(theRenderer.GetEditCamera())
                && theToolMode == STUDIO_TOOLMODE_CAMERA_ROTATE) {
            g_StudioApp.SetToolMode(STUDIO_TOOLMODE_CAMERA_PAN);
            m_SceneView->setViewCursor(); // Just set cursor, we don't want to update previous tool
        }

        // Trigger for tool changed. Changing between deployment/edit camera can change the tool
        theMainFrame->OnUpdateToolChange();
    }
}

//==============================================================================
/**
 *	Set the current scene view. This scene view is notified when there is a camera
 *	changed.
 *	@param inSceneView	the scene view object
 */
void CEditCameraBar::SetSceneView(CSceneView *inSceneView)
{
    m_SceneView = inSceneView;
}

//==============================================================================
/**
 *	Enable/Disable the edit camera selector combo box.
 *	@param inFlag	true to enable the camera selector combo box, false to disable
 */
void CEditCameraBar::Enable(bool inFlag)
{
    m_CameraSelector->setEnabled(inFlag);
}

//==============================================================================
/**
 *	When the active camera is changed, the display string needs to be changed. Hence
 *	find which entry is the one which is modified and update with the new string
 *	@param inCamera	the camera that has been modified
 */
void CEditCameraBar::onEditCameraChanged()
{
    using qt3ds::QT3DSI32;
    QT3DSI32 cameraIndex = g_StudioApp.getRenderer().GetEditCamera();
    long theNumEntry = m_CameraSelector->count();
    for (long theIndex = 0; theIndex < theNumEntry; ++theIndex) {
        if (m_CameraSelector->itemData(theIndex).toInt() == cameraIndex) {
            if (theIndex != m_CameraSelector->currentIndex()) {
                m_CameraSelector->setCurrentIndex(theIndex);
                HandleCameraChanged(theIndex);
            }
            break;
        }
    }
}

//==============================================================================
/**
 *	Callback while creating the toolbar in MainFrm. This allows the toolbar to add
 *	other controls to it. For this toolbar, we want to add a descriptor and a camera
 *	selector dropdown combobox.
 */
//==============================================================================
void CEditCameraBar::OnCustomizeToolbar()
{
    // Create the combo box
    addWidget(m_CameraSelector = new QComboBox);
    // We need to specify accessibleName and objectName for the combobox, as it's in the toolbar,
    // and we want to use a different style for it.
    m_CameraSelector->setAccessibleName(QStringLiteral("cameraSelector"));
    m_CameraSelector->setObjectName(QStringLiteral("cameraSelector"));
    m_CameraSelector->setMinimumWidth(145);
}