-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1744878
commit cff6ae8
Showing
2 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// | ||
// Created by ksuprynowicz on 25.01.25. | ||
// | ||
|
||
// | ||
// | ||
// Created by Bradley Austin Davis on 2015/12/03 | ||
// Derived from interface/src/GLCanvas.cpp created by Stephen Birarda on 8/14/13. | ||
// Copyright 2013-2015 High Fidelity, Inc. | ||
// | ||
// Distributed under the Apache License, Version 2.0. | ||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
// | ||
|
||
#include "VKWidget.h" | ||
|
||
#include "Config.h" | ||
|
||
#include <mutex> | ||
|
||
#include <QtGlobal> | ||
#include <QtCore/QMimeData> | ||
#include <QtCore/QUrl> | ||
#include <QtCore/QCoreApplication> | ||
|
||
#include <QtGui/QKeyEvent> | ||
#include <QtGui/QPaintEngine> | ||
#include <QtGui/QWindow> | ||
|
||
#include "Context.h" | ||
|
||
class GLPaintEngine : public QPaintEngine { | ||
bool begin(QPaintDevice *pdev) override { return true; } | ||
bool end() override { return true; } | ||
void updateState(const QPaintEngineState &state) override { } | ||
void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) override { } | ||
Type type() const override { return OpenGL2; } | ||
}; | ||
|
||
VKWidget::VKWidget() { | ||
setAttribute(Qt::WA_AcceptTouchEvents); | ||
setAttribute(Qt::WA_NativeWindow); | ||
setAttribute(Qt::WA_PaintOnScreen); | ||
setAttribute(Qt::WA_NoSystemBackground); | ||
setAttribute(Qt::WA_InputMethodEnabled); | ||
setAutoFillBackground(false); | ||
grabGesture(Qt::PinchGesture); | ||
setAcceptDrops(true); | ||
_paintEngine = new GLPaintEngine(); | ||
} | ||
|
||
VKWidget::~VKWidget() { | ||
delete _paintEngine; | ||
_paintEngine = nullptr; | ||
} | ||
|
||
int VKWidget::getDeviceWidth() const { | ||
return width() * (windowHandle() ? (float)windowHandle()->devicePixelRatio() : 1.0f); | ||
} | ||
|
||
int VKWidget::getDeviceHeight() const { | ||
return height() * (windowHandle() ? (float)windowHandle()->devicePixelRatio() : 1.0f); | ||
} | ||
|
||
void VKWidget::createContext(QOpenGLContext* shareContext) { | ||
_context = new gl::Context(); | ||
_context->setWindow(windowHandle()); | ||
_context->create(shareContext); | ||
_context->makeCurrent(); | ||
_context->clear(); | ||
_context->doneCurrent(); | ||
} | ||
|
||
void VKWidget::swapBuffers() { | ||
_context->swapBuffers(); | ||
} | ||
|
||
bool VKWidget::makeCurrent() { | ||
vks::Context::makeCurrent(_context->qglContext(), windowHandle()); | ||
return _context->makeCurrent(); | ||
} | ||
|
||
QOpenGLContext* VKWidget::qglContext() { | ||
return _context->qglContext(); | ||
} | ||
|
||
void VKWidget::doneCurrent() { | ||
_context->doneCurrent(); | ||
} | ||
|
||
QVariant VKWidget::inputMethodQuery(Qt::InputMethodQuery query) const { | ||
// TODO: for now we just use top left corner for an IME popup location, but in the future its position could be calculated | ||
// for a given entry field. | ||
if (query == Qt::ImCursorRectangle) { | ||
int x = 50; | ||
int y = 50; | ||
return QRect(x, y, 10, 10); | ||
} | ||
return QWidget::inputMethodQuery(query); | ||
} | ||
|
||
bool VKWidget::event(QEvent* event) { | ||
switch (event->type()) { | ||
case QEvent::MouseMove: | ||
case QEvent::MouseButtonPress: | ||
case QEvent::MouseButtonRelease: | ||
case QEvent::MouseButtonDblClick: | ||
case QEvent::KeyPress: | ||
case QEvent::KeyRelease: | ||
case QEvent::FocusIn: | ||
case QEvent::FocusOut: | ||
case QEvent::Resize: | ||
case QEvent::TouchBegin: | ||
case QEvent::TouchEnd: | ||
case QEvent::TouchUpdate: | ||
case QEvent::Gesture: | ||
case QEvent::Wheel: | ||
case QEvent::DragEnter: | ||
case QEvent::Drop: | ||
if (QCoreApplication::sendEvent(QCoreApplication::instance(), event)) { | ||
return true; | ||
} | ||
break; | ||
case QEvent::InputMethod: | ||
if (QCoreApplication::sendEvent(QCoreApplication::instance(), event)) { | ||
return true; | ||
} | ||
break; | ||
case QEvent::InputMethodQuery: | ||
if (QCoreApplication::sendEvent(QCoreApplication::instance(), event)) { | ||
return true; | ||
} | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
return QWidget::event(event); | ||
} | ||
|
||
bool VKWidget::nativeEvent(const QByteArray &eventType, void *message, long *result) { | ||
#ifdef Q_OS_WIN32 | ||
MSG* win32message = static_cast<MSG*>(message); | ||
switch (win32message->message) { | ||
case WM_ERASEBKGND: | ||
*result = 1L; | ||
return TRUE; | ||
|
||
default: | ||
break; | ||
} | ||
#endif | ||
return QWidget::nativeEvent(eventType, message, result); | ||
} | ||
|
||
QPaintEngine* VKWidget::paintEngine() const { | ||
return _paintEngine; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Created by ksuprynowicz on 25.01.25. | ||
// | ||
|
||
// | ||
// | ||
// Created by Bradley Austin Davis on 2015/12/03 | ||
// Derived from interface/src/GLCanvas.cpp created by Stephen Birarda on 8/14/13. | ||
// Copyright 2013-2015 High Fidelity, Inc. | ||
// | ||
// Distributed under the Apache License, Version 2.0. | ||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
// | ||
|
||
|
||
#pragma once | ||
|
||
#include <QtWidgets/QWidget> | ||
|
||
#include "Context.h" | ||
|
||
/// customized canvas that simply forwards requests/events to the singleton application | ||
class VKWidget : public QWidget { | ||
Q_OBJECT | ||
|
||
public: | ||
VKWidget(); | ||
~VKWidget(); | ||
int getDeviceWidth() const; | ||
int getDeviceHeight() const; | ||
QSize getDeviceSize() const { return QSize(getDeviceWidth(), getDeviceHeight()); } | ||
QPaintEngine* paintEngine() const override; | ||
void createContext(QOpenGLContext* shareContext = nullptr); | ||
bool makeCurrent(); | ||
void doneCurrent(); | ||
void swapBuffers(); | ||
vks::Context* context() { return _context; } | ||
virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const override; | ||
|
||
protected: | ||
virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override; | ||
virtual bool event(QEvent* event) override; | ||
vks::Context* _context { nullptr }; | ||
|
||
private: | ||
QPaintEngine* _paintEngine { nullptr }; | ||
bool _vsyncSupported { false }; | ||
}; |