New lua versions
[ryzomcore.git] / studio / src / plugins / object_viewer / scene / camera_control.cpp
blob4443833d0e45e3e47acaaa224893f31566c80a36
1 /*
2 Object Viewer Qt
3 Copyright (C) 2010 Dzmitry Kamiahin <dnk-88@tut.by>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdpch.h"
21 #include "camera_control.h"
23 // STL includes
25 // Qt includes
27 // NeL includes
28 #include "nel/misc/debug.h"
29 #include "nel/3d/u_driver.h"
30 #include "nel/3d/u_scene.h"
31 #include <nel/3d/u_camera.h>
32 #include <nel/3d/u_3d_mouse_listener.h>
34 // Project includes
35 #include "modules.h"
36 #include "object_viewer_constants.h"
38 static int camId = 0;
40 namespace NLQT
43 CCameraItem::CCameraItem(const QString &name):
44 _cameraFocal(75),
45 _speed(5.0),
46 _active(false),
47 _name(name)
49 _camera = Modules::objView().getScene()->createCamera();
50 _camera.setTransformMode (NL3D::UTransformable::DirectMatrix);
51 reset();
54 CCameraItem::~CCameraItem()
56 Modules::objView().getScene()->deleteCamera(_camera);
59 void CCameraItem::setActive(bool active)
61 if (active)
63 sint w = Modules::objView().getDriver()->getWindowWidth();
64 sint h = Modules::objView().getDriver()->getWindowHeight();
65 _camera.setPerspective(_cameraFocal * float(NLMISC::Pi) / 180.f, float(w) / h, 0.1f, 1000);
66 Modules::objView().getScene()->setCam(_camera);
67 setupListener();
69 else
71 _hotSpot = Modules::objView().get3dMouseListener()->getHotSpot();
73 _active = active;
76 void CCameraItem::setSpeed(float value)
78 _speed = value;
79 Modules::objView().get3dMouseListener()->setSpeed(_speed);
82 void CCameraItem::reset()
84 _hotSpot = NLMISC::CVector(0, 0, 0);
85 float radius=10.f;
87 // Setup camera
88 _camera.lookAt(_hotSpot + NLMISC::CVector(0.57735f, 0.57735f, 0.57735f) * radius, _hotSpot);
90 if (_active)
91 setupListener();
94 void CCameraItem::setupListener()
96 NL3D::U3dMouseListener *_mouseListener = Modules::objView().get3dMouseListener();
97 _mouseListener->setMatrix (_camera.getMatrix());
98 _mouseListener->setFrustrum (_camera.getFrustum());
99 _mouseListener->setViewport (NL3D::CViewport());
100 _mouseListener->setHotSpot (_hotSpot);
101 Modules::objView().get3dMouseListener()->setSpeed(_speed);
104 CCameraControl::CCameraControl(QWidget *parent)
105 : QObject(parent),
106 _currentCamera(0)
108 _camToolBar = new QToolBar(tr("CameraControl"), parent);
110 _fpsAction = _camToolBar->addAction(tr("Camera frs"));
111 _fpsAction->setIcon(QIcon(Constants::ICON_CAMERA_FPS));
112 _fpsAction->setStatusTip(tr("Set firstPerson camera mode"));
113 _fpsAction->setCheckable(true);
115 _edit3dAction = _camToolBar->addAction(tr("Camera 3dEdit"));
116 _edit3dAction->setIcon(QIcon(Constants::ICON_CAMERA_3DEDIT));
117 _edit3dAction->setStatusTip(tr("Set edit3d camera mode"));
118 _edit3dAction->setCheckable(true);
120 QActionGroup *cameraModeGroup = new QActionGroup(this);
121 cameraModeGroup->addAction(_fpsAction);
122 cameraModeGroup->addAction(_edit3dAction);
123 _edit3dAction->setChecked(true);
125 connect(_fpsAction, SIGNAL(triggered()), this, SLOT(setFirstPersonMode()));
126 connect(_edit3dAction, SIGNAL(triggered()), this, SLOT(setEditMode()));
128 _renderModeMenu = new QMenu(tr("Render Mode"), _camToolBar);
129 _renderModeMenu->setIcon(QIcon(":/images/polymode.png"));
130 _camToolBar->addAction(_renderModeMenu->menuAction());
131 connect(_renderModeMenu->menuAction(), SIGNAL(triggered()), this, SLOT(setRenderMode()));
133 QSignalMapper *modeMapper = new QSignalMapper(this);
135 _pointRenderModeAction = _renderModeMenu->addAction(tr("Point mode"));
136 _pointRenderModeAction->setIcon(QIcon(":/images/rmpoints.png"));
137 _pointRenderModeAction->setStatusTip(tr("Set point render mode"));
138 connect(_pointRenderModeAction, SIGNAL(triggered()), modeMapper, SLOT(map()));
139 modeMapper->setMapping(_pointRenderModeAction, 0);
141 _lineRenderModeAction = _renderModeMenu->addAction(tr("Line mode"));
142 _lineRenderModeAction->setStatusTip(tr("Set line render mode"));
143 _lineRenderModeAction->setIcon(QIcon(":/images/rmline.png"));
144 connect(_lineRenderModeAction, SIGNAL(triggered()), modeMapper, SLOT(map()));
145 modeMapper->setMapping(_lineRenderModeAction, 1);
147 _fillRenderModeAction = _renderModeMenu->addAction(tr("Fill mode"));
148 _fillRenderModeAction->setIcon(QIcon(":/images/rmfill.png"));
149 _fillRenderModeAction->setStatusTip(tr("Set fill render mode"));
150 connect(_fillRenderModeAction, SIGNAL(triggered()), modeMapper, SLOT(map()));
151 modeMapper->setMapping(_fillRenderModeAction, 2);
153 connect(modeMapper, SIGNAL(mapped(int)), this, SLOT(setRenderMode(int)));
155 _camToolBar->addSeparator();
156 _speedLabel = new QLabel(tr("Speed:"), _camToolBar);
157 _camToolBar->addWidget(_speedLabel);
158 _speedSpinBox = new QSpinBox(_camToolBar);
159 _speedSpinBox->setMinimum(1);
160 _speedSpinBox->setMaximum(1000);
161 _camToolBar->addWidget(_speedSpinBox);
162 connect(_speedSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setSpeed(int)));
164 _camToolBar->addSeparator();
165 _addCamAction = _camToolBar->addAction(tr("Create camera"));
166 _addCamAction->setIcon(QIcon(Constants::ICON_CAMERA_ADD));
167 _addCamAction->setStatusTip(tr("Create new camera"));
168 connect(_addCamAction, SIGNAL(triggered()), this, SLOT(addCamera()));
170 _delCamAction = _camToolBar->addAction(tr("Delete camera"));
171 _delCamAction->setIcon(QIcon(Constants::ICON_CAMERA_DEL));
172 _delCamAction->setStatusTip(tr("Delete current camera"));
173 connect(_delCamAction, SIGNAL(triggered()), this, SLOT(delCamera()));
175 _listCamComboBox = new QComboBox(_camToolBar);
176 connect(_listCamComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeCamera(int)));
177 _listCamComboBox->setCurrentIndex(createCamera(tr("defaultCamera")));
178 _camToolBar->addWidget(_listCamComboBox);
180 _camToolBar->addSeparator();
181 _resetCamAction = _camToolBar->addAction(tr("Reset camera"));
182 _resetCamAction->setIcon(QIcon(Constants::ICON_RESET_CAMERA));
183 _resetCamAction->setStatusTip(tr("Reset current camera"));
184 //_resetCamAction->setShortcut(tr("Ctrl+R"));
185 connect(_resetCamAction, SIGNAL(triggered()), this, SLOT(resetCamera()));
188 CCameraControl::~CCameraControl()
190 for(size_t i = 0; i < _cameraList.size(); ++i)
191 delete _cameraList[i];
192 _cameraList.clear();
195 void CCameraControl::setEditMode()
197 Modules::objView().get3dMouseListener()->setMouseMode(NL3D::U3dMouseListener::edit3d);
200 void CCameraControl::setFirstPersonMode()
202 Modules::objView().get3dMouseListener()->setMouseMode(NL3D::U3dMouseListener::firstPerson);
205 void CCameraControl::addCamera()
207 _listCamComboBox->setCurrentIndex(createCamera(tr("%1_Camera").arg(++camId)));
210 void CCameraControl::delCamera()
212 int index = _listCamComboBox->currentIndex();
213 _listCamComboBox->setCurrentIndex(index - 1);
215 _listCamComboBox->removeItem(index);
216 delete _cameraList[index];
217 _cameraList.erase(_cameraList.begin() + index);
220 void CCameraControl::setSpeed(int value)
222 nlassert(_currentCamera);
223 _currentCamera->setSpeed(value);
226 void CCameraControl::changeCamera(int index)
228 if (_currentCamera)
229 _currentCamera->setActive(false);
231 if (index == 0)
232 _delCamAction->setEnabled(false);
233 else
234 _delCamAction->setEnabled(true);
236 _currentCamera = _cameraList[index];
238 nlassert(_currentCamera);
239 _currentCamera->setActive(true);
240 _speedSpinBox->setValue(int(_currentCamera->getSpeed()));
243 void CCameraControl::setRenderMode(int value)
245 switch (value)
247 case 0:
248 Modules::objView().getDriver()->setPolygonMode (NL3D::UDriver::Point);
249 break;
250 case 1:
251 Modules::objView().getDriver()->setPolygonMode (NL3D::UDriver::Line);
252 break;
253 case 2:
254 Modules::objView().getDriver()->setPolygonMode (NL3D::UDriver::Filled);
255 break;
259 void CCameraControl::setRenderMode()
261 switch (Modules::objView().getDriver()->getPolygonMode())
263 case NL3D::UDriver::Filled:
264 Modules::objView().getDriver()->setPolygonMode (NL3D::UDriver::Line);
265 break;
266 case NL3D::UDriver::Line:
267 Modules::objView().getDriver()->setPolygonMode (NL3D::UDriver::Point);
268 break;
269 case NL3D::UDriver::Point:
270 Modules::objView().getDriver()->setPolygonMode (NL3D::UDriver::Filled);
271 break;
275 void CCameraControl::resetCamera()
277 nlassert(_currentCamera);
278 _currentCamera->reset();
281 int CCameraControl::createCamera(const QString &name)
283 CCameraItem *newCamera = new CCameraItem(name);
284 _cameraList.push_back(newCamera);
285 _listCamComboBox->addItem(newCamera->getName());
286 return _cameraList.size() - 1;
289 } /* namespace NLQT */