| 1 | /* |
| 2 | Copyright 2014 Jan Adamec, Michalis Kamburelis. |
| 3 | |
| 4 | This file is part of "Castle Game Engine". |
| 5 | |
| 6 | "Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 | included in this distribution, for details about the copyright. |
| 8 | |
| 9 | "Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | |
| 13 | ---------------------------------------------------------------------------- |
| 14 | */ |
| 15 | |
| 16 | #include <QtWidgets> |
| 17 | #include <QtOpenGL> |
| 18 | |
| 19 | #include "glwidget.h" |
| 20 | #include "mainwindow.h" |
| 21 | |
| 22 | GLWidget *g_pThis = NULL; |
| 23 | |
| 24 | GLWidget::GLWidget(const QSurfaceFormat &format, QWidget *parent) : |
| 25 | QOpenGLWidget(parent) |
| 26 | { |
| 27 | g_pThis = this; |
| 28 | m_bAfterInit = false; |
| 29 | m_bLimitFPS = true; |
| 30 | m_bNeedsDisplay = false; |
| 31 | setFormat(format); |
| 32 | setMouseTracking(true); |
| 33 | setFocusPolicy(Qt::StrongFocus); // accept key strokes |
| 34 | |
| 35 | QTimer *pUpdateTimer = new QTimer(this); |
| 36 | connect(pUpdateTimer, SIGNAL(timeout()), this, SLOT(OnUpdateTimer())); |
| 37 | pUpdateTimer->start(20); // 50 fps |
| 38 | } |
| 39 | |
| 40 | GLWidget::~GLWidget() |
| 41 | { |
| 42 | CGE_Close(true); |
| 43 | g_pThis = NULL; |
| 44 | } |
| 45 | |
| 46 | void GLWidget::OpenScene(QString const &sFilename) |
| 47 | { |
| 48 | m_sSceneToOpen = sFilename; |
| 49 | if (!m_bAfterInit) |
| 50 | return; |
| 51 | |
| 52 | CGE_LoadSceneFromFile(sFilename.toUtf8()); |
| 53 | double dPixRatio = devicePixelRatioF(); |
| 54 | CGE_Resize(width()*dPixRatio, height()*dPixRatio); |
| 55 | ((MainWindow*)g_pThis->parent())->UpdateAfterSceneLoaded(); |
| 56 | } |
| 57 | |
| 58 | QSize GLWidget::minimumSizeHint() const |
| 59 | { |
| 60 | return QSize(200, 200); |
| 61 | } |
| 62 | |
| 63 | QSize GLWidget::sizeHint() const |
| 64 | { |
| 65 | return QSize(400, 400); |
| 66 | } |
| 67 | |
| 68 | int CDECL GLWidget::OpenGlLibraryCallback(int eCode, int iParam1, int iParam2, const char *szParam) |
| 69 | { |
| 70 | if (g_pThis == NULL || !g_pThis->m_bAfterInit) return 0; |
| 71 | |
| 72 | switch (eCode) |
| 73 | { |
| 74 | case ecgelibNeedsDisplay: |
| 75 | g_pThis->m_bNeedsDisplay = true; |
| 76 | return 1; |
| 77 | |
| 78 | case ecgelibSetMouseCursor: |
| 79 | { |
| 80 | QCursor aNewCur; |
| 81 | switch (iParam1) |
| 82 | { |
| 83 | case ecgecursorWait: aNewCur.setShape(Qt::WaitCursor); break; |
| 84 | case ecgecursorHand: aNewCur.setShape(Qt::PointingHandCursor); break; |
| 85 | case ecgecursorText: aNewCur.setShape(Qt::IBeamCursor); break; |
| 86 | case ecgecursorNone: aNewCur.setShape(Qt::BlankCursor); break; |
| 87 | default: aNewCur.setShape(Qt::ArrowCursor); |
| 88 | } |
| 89 | g_pThis->setCursor(aNewCur); |
| 90 | } |
| 91 | return 1; |
| 92 | |
| 93 | case ecgelibNavigationTypeChanged: |
| 94 | ((MainWindow*)g_pThis->parent())->UpdateNavigationButtons(); |
| 95 | return 1; |
| 96 | |
| 97 | case ecgelibSetMousePosition: |
| 98 | { |
| 99 | QPoint ptNew = g_pThis->mapToGlobal(QPoint(iParam1, g_pThis->height() - 1 - iParam2)); |
| 100 | QCursor::setPos(ptNew.x(), ptNew.y()); |
| 101 | } |
| 102 | return 1; |
| 103 | |
| 104 | case ecgelibWarning: |
| 105 | { |
| 106 | QString sWarning = QString::fromUtf8(szParam); |
| 107 | ((MainWindow*)g_pThis->parent())->AddNewWarning(sWarning); |
| 108 | } |
| 109 | return 1; |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | void GLWidget::initializeGL() |
| 115 | { |
| 116 | double dPixRatio = devicePixelRatioF(); |
| 117 | // Get config dir, see https://stackoverflow.com/questions/4369661/qt-how-to-save-a-configuration-file-on-multiple-platforms |
| 118 | QString configDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); |
| 119 | CGE_Initialize(configDir.toUtf8()); |
| 120 | CGE_Open(ecgeofLog, width()*dPixRatio, height()*dPixRatio, logicalDpiY()); |
| 121 | CGE_SetUserInterface(false); |
| 122 | CGE_SetLibraryCallbackProc(OpenGlLibraryCallback); |
| 123 | m_bAfterInit = true; |
| 124 | if (!m_sSceneToOpen.isEmpty()) |
| 125 | OpenScene(m_sSceneToOpen); |
| 126 | } |
| 127 | |
| 128 | void GLWidget::OnUpdateTimer() |
| 129 | { |
| 130 | if (!m_bAfterInit) return; |
| 131 | |
| 132 | CGE_Update(); |
| 133 | if (!m_bLimitFPS || m_bNeedsDisplay) |
| 134 | { |
| 135 | m_bNeedsDisplay = false; |
| 136 | updateGL(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void GLWidget::updateGL() |
| 141 | { |
| 142 | update(); // TODO: Qt6 really? |
| 143 | } |
| 144 | |
| 145 | void GLWidget::paintGL() |
| 146 | { |
| 147 | if (!m_bAfterInit) return; |
| 148 | |
| 149 | CGE_Render(); |
| 150 | } |
| 151 | |
| 152 | void GLWidget::resizeGL(int width, int height) |
| 153 | { |
| 154 | if (m_bAfterInit && width > 0 && height > 0) |
| 155 | CGE_Resize(width, height); |
| 156 | } |
| 157 | |
| 158 | QPoint GLWidget::PointFromMousePoint(const QPoint& pt) |
| 159 | { |
| 160 | QPoint ptNew(pt.x(), height() - 1 - pt.y()); |
| 161 | ptNew *= devicePixelRatioF(); |
| 162 | return ptNew; |
| 163 | } |
| 164 | |
| 165 | QPoint GLWidget::PointFromMousePoint(const QPointF& pt) |
| 166 | { |
| 167 | QPoint ptNew(pt.x(), height() - 1 - pt.y()); |
| 168 | ptNew *= devicePixelRatioF(); |
| 169 | return ptNew; |
| 170 | } |
| 171 | |
| 172 | void GLWidget::mousePressEvent(QMouseEvent *event) |
| 173 | { |
| 174 | if (!m_bAfterInit) return; |
| 175 | |
| 176 | QPoint pt(PointFromMousePoint(event->pos())); |
| 177 | CGE_MouseDown(pt.x(), pt.y(), event->button()==Qt::LeftButton, 0); |
| 178 | } |
| 179 | |
| 180 | void GLWidget::mouseMoveEvent(QMouseEvent *event) |
| 181 | { |
| 182 | if (!m_bAfterInit) return; |
| 183 | |
| 184 | QPoint pt(PointFromMousePoint(event->pos())); |
| 185 | CGE_Motion(pt.x(), pt.y(), 0); |
| 186 | } |
| 187 | |
| 188 | void GLWidget::mouseReleaseEvent(QMouseEvent *event) |
| 189 | { |
| 190 | if (!m_bAfterInit) return; |
| 191 | |
| 192 | QPoint pt(PointFromMousePoint(event->pos())); |
| 193 | CGE_MouseUp(pt.x(), pt.y(), event->button()==Qt::LeftButton, 0, true); |
| 194 | } |
| 195 | |
| 196 | #ifndef QT_NO_WHEELEVENT |
| 197 | void GLWidget::wheelEvent(QWheelEvent *event) |
| 198 | { |
| 199 | if (event->angleDelta().y() != 0) |
| 200 | CGE_MouseWheel(event->angleDelta().y(), true); |
| 201 | else |
| 202 | CGE_MouseWheel(event->angleDelta().x(), false); |
| 203 | |
| 204 | if (m_bNeedsDisplay) |
| 205 | { |
| 206 | m_bNeedsDisplay = false; |
| 207 | updateGL(); |
| 208 | } |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | void GLWidget::keyPressEvent(QKeyEvent *event) |
| 213 | { |
| 214 | if (event->key() == Qt::Key_Escape && CGE_GetVariableInt(ecgevarMouseLook)==1) |
| 215 | { |
| 216 | CGE_SetVariableInt(ecgevarMouseLook, 0); |
| 217 | CGE_SetVariableInt(ecgevarCrossHair, 0); |
| 218 | event->accept(); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | CGE_KeyDown(QKeyToCgeKey(event->key())); |
| 223 | } |
| 224 | |
| 225 | void GLWidget::keyReleaseEvent(QKeyEvent *event) |
| 226 | { |
| 227 | CGE_KeyUp(QKeyToCgeKey(event->key())); |
| 228 | } |
| 229 | |
| 230 | int GLWidget::QKeyToCgeKey(int qKey) |
| 231 | { |
| 232 | if (qKey >= Qt::Key_0 && qKey <= Qt::Key_9) |
| 233 | return kcge_0 + qKey - Qt::Key_0; |
| 234 | if (qKey >= Qt::Key_A && qKey <= Qt::Key_Z) |
| 235 | return kcge_A + qKey - Qt::Key_A; |
| 236 | if (qKey >= Qt::Key_F1 && qKey <= Qt::Key_F12) |
| 237 | return kcge_F1 + qKey - Qt::Key_F1; |
| 238 | |
| 239 | switch (qKey) |
| 240 | { |
| 241 | case Qt::Key_Print: return kcge_PrintScreen; |
| 242 | case Qt::Key_CapsLock: return kcge_CapsLock; |
| 243 | case Qt::Key_ScrollLock: return kcge_ScrollLock; |
| 244 | case Qt::Key_NumLock: return kcge_NumLock; |
| 245 | case Qt::Key_Pause: return kcge_Pause; |
| 246 | case Qt::Key_Apostrophe: return kcge_Apostrophe; |
| 247 | case Qt::Key_Semicolon: return kcge_Semicolon; |
| 248 | case Qt::Key_Backspace: return kcge_BackSpace; |
| 249 | case Qt::Key_Tab: return kcge_Tab; |
| 250 | case Qt::Key_Slash: return kcge_Slash; |
| 251 | case Qt::Key_QuoteLeft: return kcge_BackQuote; |
| 252 | case Qt::Key_Minus: return kcge_Minus; |
| 253 | case Qt::Key_Return: return kcge_Enter; |
| 254 | case Qt::Key_Equal: return kcge_Equal; |
| 255 | case Qt::Key_Backslash: return kcge_BackSlash; |
| 256 | case Qt::Key_Shift: return kcge_Shift; |
| 257 | case Qt::Key_Control: return kcge_Ctrl; |
| 258 | case Qt::Key_Alt: return kcge_Alt; |
| 259 | case Qt::Key_Plus: return kcge_Plus; |
| 260 | case Qt::Key_Escape: return kcge_Escape; |
| 261 | case Qt::Key_Space: return kcge_Space; |
| 262 | case Qt::Key_PageUp: return kcge_PageUp; |
| 263 | case Qt::Key_PageDown: return kcge_PageDown; |
| 264 | case Qt::Key_End: return kcge_End; |
| 265 | case Qt::Key_Home: return kcge_Home; |
| 266 | case Qt::Key_Left: return kcge_Left; |
| 267 | case Qt::Key_Up: return kcge_Up; |
| 268 | case Qt::Key_Right: return kcge_Right; |
| 269 | case Qt::Key_Down: return kcge_Down; |
| 270 | case Qt::Key_Insert: return kcge_Insert; |
| 271 | case Qt::Key_Delete: return kcge_Delete; |
| 272 | case Qt::Key_BracketLeft: return kcge_LeftBracket; |
| 273 | case Qt::Key_BracketRight: return kcge_RightBracket; |
| 274 | case Qt::Key_Enter: return kcge_Numpad_Enter; |
| 275 | case Qt::Key_Comma: return kcge_Comma; |
| 276 | case Qt::Key_Period: return kcge_Period; |
| 277 | |
| 278 | default: return kcge_None; |
| 279 | } |
| 280 | } |