Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor for qt6 #24

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions include/vsgQt/ViewerWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ namespace vsgQt
void mousePressEvent(QMouseEvent*) override;
void mouseReleaseEvent(QMouseEvent*) override;
void resizeEvent(QResizeEvent*) override;
void moveEvent(QMoveEvent*) override;
void wheelEvent(QWheelEvent*) override;

/// convert Qt's window coordinate into Vulkan/VSG ones by scaling by the devicePixelRatio()
int32_t convert_coord(int c) const { return static_cast<int32_t>(std::round(static_cast<float>(c) * devicePixelRatio())); }
int32_t convert_coord(unsigned int c) const { return static_cast<int32_t>(std::round(static_cast<float>(c) * devicePixelRatio())); }
int32_t convert_coord(float c) const { return static_cast<int32_t>(std::round(c * devicePixelRatio())); }
template<typename T>
int32_t convert_coord(T c) const { return static_cast<int32_t>(std::round(static_cast<qreal>(c) * devicePixelRatio())); }

std::pair<vsg::ButtonMask, uint32_t> convertMouseButtons(QMouseEvent* e) const;
std::pair<int32_t, int32_t> convertMousePosition(QMouseEvent* e) const;

private:
bool _initialized = false;
Expand Down
35 changes: 20 additions & 15 deletions src/vsgQt/ViewerWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
</editor-fold> */

#include <vsg/all.h>
#ifdef vsgXchange_FOUND
# include <vsgXchange/all.h>
#endif

#include <QPlatformSurfaceEvent>
#include <QWindow>
Expand All @@ -30,6 +27,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
# include <QVulkanInstance>
#endif

#include <iostream>


using namespace vsgQt;

const char* instanceExtensionSurfaceName()
Expand Down Expand Up @@ -296,8 +296,9 @@ void ViewerWindow::mouseMoveEvent(QMouseEvent* e)
vsg::clock::time_point event_time = vsg::clock::now();

auto [mask, button] = convertMouseButtons(e);
auto [x, y] = convertMousePosition(e);

windowAdapter->bufferedEvents.push_back(vsg::MoveEvent::create(windowAdapter, event_time, convert_coord(e->x()), convert_coord(e->y()), mask));
windowAdapter->bufferedEvents.push_back(vsg::MoveEvent::create(windowAdapter, event_time, x, y, mask));
}

void ViewerWindow::mousePressEvent(QMouseEvent* e)
Expand All @@ -307,27 +308,22 @@ void ViewerWindow::mousePressEvent(QMouseEvent* e)
vsg::clock::time_point event_time = vsg::clock::now();

auto [mask, button] = convertMouseButtons(e);
auto [x, y] = convertMousePosition(e);

windowAdapter->bufferedEvents.push_back(vsg::ButtonPressEvent::create(windowAdapter, event_time, convert_coord(e->x()), convert_coord(e->y()), mask, button));
windowAdapter->bufferedEvents.push_back(vsg::ButtonPressEvent::create(windowAdapter, event_time, x, y, mask, button));
}


void ViewerWindow::mouseReleaseEvent(QMouseEvent* e)
{
if (!windowAdapter) return;

vsg::clock::time_point event_time = vsg::clock::now();

auto [mask, button] = convertMouseButtons(e);
auto [x, y] = convertMousePosition(e);

windowAdapter->bufferedEvents.push_back(vsg::ButtonReleaseEvent::create(windowAdapter, event_time, convert_coord(e->x()), convert_coord(e->y()), mask, button));
}

void ViewerWindow::moveEvent(QMoveEvent*)
{
if (!windowAdapter) return;

vsg::clock::time_point event_time = vsg::clock::now();
windowAdapter->bufferedEvents.push_back(vsg::ConfigureWindowEvent::create(windowAdapter, event_time, convert_coord(x()), convert_coord(y()), convert_coord(width()), convert_coord(height())));
windowAdapter->bufferedEvents.push_back(vsg::ButtonReleaseEvent::create(windowAdapter, event_time, x, y, mask, button));
}

void ViewerWindow::wheelEvent(QWheelEvent* e)
Expand Down Expand Up @@ -356,4 +352,13 @@ std::pair<vsg::ButtonMask, uint32_t> ViewerWindow::convertMouseButtons(QMouseEve
}

return {static_cast<vsg::ButtonMask>(mask), button};
}
}

std::pair<int32_t, int32_t> ViewerWindow::convertMousePosition(QMouseEvent* e) const
{
#if QT_VERSION_MAJOR == 6
return {convert_coord(e->position().x()), convert_coord(e->position().y())};
#else
return {convert_coord(e->x()), convert_coord(e->y())};
#endif
}