New lua versions
[ryzomcore.git] / studio / src / plugins / zone_painter / qnel_widget.cpp
blob55881edb3f91d75222af1cb75f6ca597b680328d
1 // Object Viewer Qt - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Winch Gate Property Limited
6 // Copyright (C) 2011 Matt RAYKOWSKI (sfb) <matt.raykowski@gmail.com>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "qnel_widget.h"
23 // STL includes
25 // Qt includes
26 #include <QtCore/QTimer>
27 #include <QtGui/QResizeEvent>
29 // NeL includes
30 #include <nel/misc/event_server.h>
31 #include <nel/misc/debug.h>
32 #include <nel/3d/u_driver.h>
33 #include <nel/3d/driver_user.h>
35 namespace NLQT
38 QNLWidget::QNLWidget(QWidget *parent)
39 : QNeLWidget(parent),
40 m_driver(NULL),
41 m_initialized(false),
42 m_interval(25)
44 setMouseTracking(true);
45 setFocusPolicy(Qt::StrongFocus);
47 init();
48 #ifdef Q_OS_LINUX
49 makeCurrent();
50 #endif
51 m_mainTimer = new QTimer(this);
52 connect(m_mainTimer, SIGNAL(timeout()), this, SLOT(updateRender()));
55 QNLWidget::~QNLWidget()
57 release();
60 void QNLWidget::init()
62 // create the driver
63 m_driver = NL3D::UDriver::createDriver(NULL, false, NULL);
64 nlassert(m_driver);
66 // initialize the nel 3d viewport
67 m_driver->setDisplay((nlWindow)winId(), NL3D::UDriver::CMode(width(), height(), 32));
69 // set the cache size for the font manager(in bytes)
70 m_driver->setFontManagerMaxMemory(2097152);
72 m_initialized = true;
75 void QNLWidget::release()
77 m_mainTimer->stop();
78 delete m_mainTimer;
79 if (m_initialized)
81 m_driver->release();
82 delete m_driver;
83 m_driver = NULL;
87 void QNLWidget::setInterval(int msec)
89 m_interval = msec;
90 m_mainTimer->setInterval(msec);
93 void QNLWidget::setBackgroundColor(NLMISC::CRGBA backgroundColor)
95 m_backgroundColor = backgroundColor;
98 void QNLWidget::updateRender()
100 if (isVisible())
102 if (m_initialized)
103 m_driver->EventServer.pump();
104 Q_EMIT updateData();
106 // Calc FPS
107 static sint64 lastTime = NLMISC::CTime::getPerformanceTime ();
108 sint64 newTime = NLMISC::CTime::getPerformanceTime ();
109 m_fps = float(1.0 / NLMISC::CTime::ticksToSecond (newTime-lastTime));
110 lastTime = newTime;
112 if (m_initialized && !m_driver->isLost())
114 //_driver->activate();
115 m_driver->clearBuffers(m_backgroundColor);
116 Q_EMIT updatePreRender();
118 Q_EMIT updatePostRender();
119 // swap 3d buffers
120 m_driver->swapBuffers();
125 void QNLWidget::showEvent(QShowEvent *showEvent)
127 QWidget::showEvent(showEvent);
128 m_driver->activate();
129 m_mainTimer->start(m_interval);
132 void QNLWidget::hideEvent(QHideEvent *hideEvent)
134 m_mainTimer->stop();
135 QWidget::hideEvent(hideEvent);
138 #if defined(NL_OS_WINDOWS)
140 typedef bool (*winProc)(NL3D::IDriver *driver, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
142 bool QNLWidget::winEvent(MSG *message, long *result)
144 if (m_driver && m_driver->isActive())
146 NL3D::IDriver *driver = dynamic_cast<NL3D::CDriverUser *>(m_driver)->getDriver();
147 if (driver)
149 winProc proc = (winProc)driver->getWindowProc();
150 return proc(driver, message->hwnd, message->message, message->wParam, message->lParam);
154 return false;
157 #elif defined(NL_OS_MAC)
159 typedef bool (*cocoaProc)(NL3D::IDriver *, const void *e);
161 bool QNLWidget::macEvent(EventHandlerCallRef caller, EventRef event)
163 if(caller)
164 nlerror("You are using QtCarbon! Only QtCocoa supported, please upgrade Qt");
166 if (m_driver && m_driver->isActive())
168 NL3D::IDriver *driver = dynamic_cast<NL3D::CDriverUser *>(m_driver)->getDriver();
169 if (driver)
171 cocoaProc proc = (cocoaProc)driver->getWindowProc();
172 return proc(driver, event);
176 return false;
179 #elif defined(NL_OS_UNIX)
181 typedef bool (*x11Proc)(NL3D::IDriver *drv, XEvent *e);
183 bool QNLWidget::x11Event(XEvent *event)
185 if (m_driver && m_driver->isActive())
187 NL3D::IDriver *driver = dynamic_cast<NL3D::CDriverUser *>(m_driver)->getDriver();
188 if (driver)
190 x11Proc proc = (x11Proc)driver->getWindowProc();
191 return proc(driver, event);
195 return false;
197 #endif
199 } /* namespace NLQT */