2015-11-27 08:33:07 +00:00
|
|
|
// Copyright 2015 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-11-27 08:33:07 +00:00
|
|
|
|
2019-06-01 11:55:09 +00:00
|
|
|
#include "DolphinQt/RenderWidget.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
2018-04-13 18:12:13 +00:00
|
|
|
#include <QApplication>
|
2018-09-22 02:26:26 +00:00
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QDropEvent>
|
|
|
|
#include <QFileInfo>
|
2018-04-13 18:12:13 +00:00
|
|
|
#include <QGuiApplication>
|
2018-05-04 12:04:46 +00:00
|
|
|
#include <QIcon>
|
2015-11-27 08:33:07 +00:00
|
|
|
#include <QKeyEvent>
|
2018-09-22 02:26:26 +00:00
|
|
|
#include <QMimeData>
|
2018-04-29 11:35:43 +00:00
|
|
|
#include <QMouseEvent>
|
2018-03-24 22:50:03 +00:00
|
|
|
#include <QPalette>
|
2018-04-13 18:12:13 +00:00
|
|
|
#include <QScreen>
|
2017-07-04 14:58:27 +00:00
|
|
|
#include <QTimer>
|
2019-10-13 01:36:17 +00:00
|
|
|
#include <QWindow>
|
2015-11-27 08:33:07 +00:00
|
|
|
|
2021-10-28 19:57:48 +00:00
|
|
|
#include <imgui.h>
|
2018-10-10 11:41:52 +00:00
|
|
|
|
2019-03-03 04:41:50 +00:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2018-03-24 22:50:03 +00:00
|
|
|
#include "Core/Core.h"
|
2018-09-22 02:26:26 +00:00
|
|
|
#include "Core/State.h"
|
2018-04-29 11:35:43 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Host.h"
|
2019-03-04 19:49:00 +00:00
|
|
|
#include "DolphinQt/QtUtils/ModalMessageBox.h"
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Resources.h"
|
|
|
|
#include "DolphinQt/Settings.h"
|
2015-11-27 08:33:07 +00:00
|
|
|
|
2021-05-09 10:28:04 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
|
|
|
|
2018-10-10 11:41:52 +00:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
|
2021-05-09 10:28:04 +00:00
|
|
|
#ifdef _WIN32
|
2021-12-27 13:19:12 +00:00
|
|
|
#include <Windows.h>
|
2021-05-09 10:28:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-11-27 08:33:07 +00:00
|
|
|
RenderWidget::RenderWidget(QWidget* parent) : QWidget(parent)
|
|
|
|
{
|
2018-05-04 12:04:46 +00:00
|
|
|
setWindowTitle(QStringLiteral("Dolphin"));
|
|
|
|
setWindowIcon(Resources::GetAppIcon());
|
2019-10-25 21:51:05 +00:00
|
|
|
setWindowRole(QStringLiteral("renderer"));
|
2018-09-22 02:26:26 +00:00
|
|
|
setAcceptDrops(true);
|
2018-05-04 12:04:46 +00:00
|
|
|
|
2018-03-24 22:50:03 +00:00
|
|
|
QPalette p;
|
2019-10-13 01:36:17 +00:00
|
|
|
p.setColor(QPalette::Window, Qt::black);
|
2018-03-24 22:50:03 +00:00
|
|
|
setPalette(p);
|
2015-11-27 08:33:07 +00:00
|
|
|
|
|
|
|
connect(Host::GetInstance(), &Host::RequestTitle, this, &RenderWidget::setWindowTitle);
|
2018-03-24 15:40:47 +00:00
|
|
|
connect(Host::GetInstance(), &Host::RequestRenderSize, this, [this](int w, int h) {
|
2019-03-03 04:41:50 +00:00
|
|
|
if (!Config::Get(Config::MAIN_RENDER_WINDOW_AUTOSIZE) || isFullScreen() || isMaximized())
|
2018-03-24 15:40:47 +00:00
|
|
|
return;
|
|
|
|
|
2019-09-21 01:07:02 +00:00
|
|
|
const auto dpr = window()->windowHandle()->screen()->devicePixelRatio();
|
|
|
|
|
|
|
|
resize(w / dpr, h / dpr);
|
2018-03-24 15:40:47 +00:00
|
|
|
});
|
2017-11-19 16:27:07 +00:00
|
|
|
|
2018-03-24 22:50:03 +00:00
|
|
|
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) {
|
2018-10-10 11:41:52 +00:00
|
|
|
if (state == Core::State::Running)
|
|
|
|
SetImGuiKeyMap();
|
2018-03-24 22:50:03 +00:00
|
|
|
});
|
|
|
|
|
2017-11-19 16:27:07 +00:00
|
|
|
// We have to use Qt::DirectConnection here because we don't want those signals to get queued
|
|
|
|
// (which results in them not getting called)
|
|
|
|
connect(this, &RenderWidget::StateChanged, Host::GetInstance(), &Host::SetRenderFullscreen,
|
|
|
|
Qt::DirectConnection);
|
|
|
|
connect(this, &RenderWidget::HandleChanged, Host::GetInstance(), &Host::SetRenderHandle,
|
|
|
|
Qt::DirectConnection);
|
2018-01-26 06:23:24 +00:00
|
|
|
connect(this, &RenderWidget::SizeChanged, Host::GetInstance(), &Host::ResizeSurface,
|
2018-01-20 20:00:28 +00:00
|
|
|
Qt::DirectConnection);
|
2018-06-08 18:47:15 +00:00
|
|
|
connect(this, &RenderWidget::FocusChanged, Host::GetInstance(), &Host::SetRenderFocus,
|
|
|
|
Qt::DirectConnection);
|
2017-11-19 16:27:07 +00:00
|
|
|
|
2017-07-04 14:58:27 +00:00
|
|
|
m_mouse_timer = new QTimer(this);
|
|
|
|
connect(m_mouse_timer, &QTimer::timeout, this, &RenderWidget::HandleCursorTimer);
|
|
|
|
m_mouse_timer->setSingleShot(true);
|
|
|
|
setMouseTracking(true);
|
|
|
|
|
2021-09-23 03:57:52 +00:00
|
|
|
connect(&Settings::Instance(), &Settings::CursorVisibilityChanged, this,
|
2017-06-01 06:49:21 +00:00
|
|
|
&RenderWidget::OnHideCursorChanged);
|
2021-05-09 10:28:04 +00:00
|
|
|
connect(&Settings::Instance(), &Settings::LockCursorChanged, this,
|
|
|
|
&RenderWidget::OnLockCursorChanged);
|
2017-06-01 06:49:21 +00:00
|
|
|
OnHideCursorChanged();
|
2021-05-09 10:28:04 +00:00
|
|
|
OnLockCursorChanged();
|
2018-04-22 08:56:15 +00:00
|
|
|
connect(&Settings::Instance(), &Settings::KeepWindowOnTopChanged, this,
|
|
|
|
&RenderWidget::OnKeepOnTopChanged);
|
|
|
|
OnKeepOnTopChanged(Settings::Instance().IsKeepWindowOnTopEnabled());
|
2017-07-04 14:58:27 +00:00
|
|
|
m_mouse_timer->start(MOUSE_HIDE_DELAY);
|
2018-03-24 22:50:03 +00:00
|
|
|
|
2018-10-03 13:03:22 +00:00
|
|
|
// We need a native window to render into.
|
|
|
|
setAttribute(Qt::WA_NativeWindow);
|
2020-03-11 13:13:42 +00:00
|
|
|
setAttribute(Qt::WA_PaintOnScreen);
|
2019-04-23 03:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPaintEngine* RenderWidget::paintEngine() const
|
|
|
|
{
|
2020-03-11 13:13:42 +00:00
|
|
|
return nullptr;
|
2017-06-01 06:49:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 02:26:26 +00:00
|
|
|
void RenderWidget::dragEnterEvent(QDragEnterEvent* event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1)
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderWidget::dropEvent(QDropEvent* event)
|
|
|
|
{
|
|
|
|
const auto& urls = event->mimeData()->urls();
|
|
|
|
if (urls.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto& url = urls[0];
|
|
|
|
QFileInfo file_info(url.toLocalFile());
|
|
|
|
|
|
|
|
auto path = file_info.filePath();
|
|
|
|
|
|
|
|
if (!file_info.exists() || !file_info.isReadable())
|
|
|
|
{
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::critical(this, tr("Error"), tr("Failed to open '%1'").arg(path));
|
2018-09-22 02:26:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_info.isFile())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
State::LoadAs(path.toStdString());
|
|
|
|
}
|
|
|
|
|
2017-06-01 06:49:21 +00:00
|
|
|
void RenderWidget::OnHideCursorChanged()
|
|
|
|
{
|
2021-05-09 10:28:04 +00:00
|
|
|
UpdateCursor();
|
|
|
|
}
|
2021-09-23 03:57:52 +00:00
|
|
|
|
2021-05-09 10:28:04 +00:00
|
|
|
void RenderWidget::OnLockCursorChanged()
|
|
|
|
{
|
|
|
|
SetCursorLocked(false);
|
|
|
|
UpdateCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling this at any time will set the cursor (image) to the correct state
|
|
|
|
void RenderWidget::UpdateCursor()
|
|
|
|
{
|
|
|
|
if (!Settings::Instance().GetLockCursor())
|
|
|
|
{
|
|
|
|
// Only hide if the cursor is automatically locking (it will hide on lock).
|
|
|
|
// "Unhide" the cursor if we lost focus, otherwise it will disappear when hovering
|
|
|
|
// on top of the game window in the background
|
|
|
|
const bool keep_on_top = (windowFlags() & Qt::WindowStaysOnTopHint) != 0;
|
|
|
|
const bool should_hide =
|
2021-12-31 02:00:39 +00:00
|
|
|
(Settings::Instance().GetCursorVisibility() == Config::ShowCursor::Never) &&
|
2021-12-27 18:53:20 +00:00
|
|
|
(keep_on_top || Config::Get(Config::MAIN_INPUT_BACKGROUND_INPUT) || isActiveWindow());
|
2021-05-09 10:28:04 +00:00
|
|
|
setCursor(should_hide ? Qt::BlankCursor : Qt::ArrowCursor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-23 03:57:52 +00:00
|
|
|
setCursor((m_cursor_locked &&
|
2021-12-31 02:00:39 +00:00
|
|
|
Settings::Instance().GetCursorVisibility() == Config::ShowCursor::Never) ?
|
2021-09-23 03:57:52 +00:00
|
|
|
Qt::BlankCursor :
|
|
|
|
Qt::ArrowCursor);
|
2021-05-09 10:28:04 +00:00
|
|
|
}
|
2015-11-27 08:33:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 08:56:15 +00:00
|
|
|
void RenderWidget::OnKeepOnTopChanged(bool top)
|
|
|
|
{
|
2018-04-22 11:01:18 +00:00
|
|
|
const bool was_visible = isVisible();
|
|
|
|
|
2018-04-22 08:56:15 +00:00
|
|
|
setWindowFlags(top ? windowFlags() | Qt::WindowStaysOnTopHint :
|
|
|
|
windowFlags() & ~Qt::WindowStaysOnTopHint);
|
|
|
|
|
2021-05-09 10:28:04 +00:00
|
|
|
m_dont_lock_cursor_on_show = true;
|
2018-04-22 11:01:18 +00:00
|
|
|
if (was_visible)
|
|
|
|
show();
|
2021-05-09 10:28:04 +00:00
|
|
|
m_dont_lock_cursor_on_show = false;
|
|
|
|
|
|
|
|
UpdateCursor();
|
2018-04-22 08:56:15 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 14:58:27 +00:00
|
|
|
void RenderWidget::HandleCursorTimer()
|
|
|
|
{
|
2021-05-09 10:28:04 +00:00
|
|
|
if (!isActiveWindow())
|
|
|
|
return;
|
2021-09-23 03:57:52 +00:00
|
|
|
if ((!Settings::Instance().GetLockCursor() || m_cursor_locked) &&
|
2021-12-31 02:00:39 +00:00
|
|
|
Settings::Instance().GetCursorVisibility() == Config::ShowCursor::OnMovement)
|
2021-05-09 10:28:04 +00:00
|
|
|
{
|
2017-07-04 14:58:27 +00:00
|
|
|
setCursor(Qt::BlankCursor);
|
2021-05-09 10:28:04 +00:00
|
|
|
}
|
2017-07-04 14:58:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:53:37 +00:00
|
|
|
void RenderWidget::showFullScreen()
|
|
|
|
{
|
|
|
|
QWidget::showFullScreen();
|
2018-04-13 18:12:13 +00:00
|
|
|
|
2019-10-13 01:36:17 +00:00
|
|
|
QScreen* screen = window()->windowHandle()->screen();
|
|
|
|
|
|
|
|
const auto dpr = screen->devicePixelRatio();
|
2018-04-13 18:12:13 +00:00
|
|
|
|
|
|
|
emit SizeChanged(width() * dpr, height() * dpr);
|
2018-03-21 19:53:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 10:28:04 +00:00
|
|
|
// Lock the cursor within the window/widget internal borders, including the aspect ratio if wanted
|
|
|
|
void RenderWidget::SetCursorLocked(bool locked, bool follow_aspect_ratio)
|
|
|
|
{
|
|
|
|
// It seems like QT doesn't scale the window frame correctly with some DPIs
|
|
|
|
// so it might happen that the locked cursor can be on the frame of the window,
|
|
|
|
// being able to resize it, but that is a minor problem.
|
|
|
|
// As a hack, if necessary, we could always scale down the size by 2 pixel, to a min of 1 given
|
|
|
|
// that the size can be 0 already. We probably shouldn't scale axes already scaled by aspect ratio
|
|
|
|
QRect render_rect = geometry();
|
|
|
|
if (parentWidget())
|
|
|
|
{
|
|
|
|
render_rect.moveTopLeft(parentWidget()->mapToGlobal(render_rect.topLeft()));
|
|
|
|
}
|
|
|
|
auto scale = devicePixelRatioF(); // Seems to always be rounded on Win. Should we round results?
|
|
|
|
QPoint screen_offset = QPoint(0, 0);
|
|
|
|
if (window()->windowHandle() && window()->windowHandle()->screen())
|
|
|
|
{
|
|
|
|
screen_offset = window()->windowHandle()->screen()->geometry().topLeft();
|
|
|
|
}
|
|
|
|
render_rect.moveTopLeft(((render_rect.topLeft() - screen_offset) * scale) + screen_offset);
|
|
|
|
render_rect.setSize(render_rect.size() * scale);
|
|
|
|
|
|
|
|
if (follow_aspect_ratio)
|
|
|
|
{
|
|
|
|
// TODO: SetCursorLocked() should be re-called every time this value is changed?
|
|
|
|
// This might cause imprecisions of one pixel (but it won't cause the cursor to go over borders)
|
|
|
|
Common::Vec2 aspect_ratio = g_controller_interface.GetWindowInputScale();
|
|
|
|
if (aspect_ratio.x > 1.f)
|
|
|
|
{
|
|
|
|
const float new_half_width = float(render_rect.width()) / (aspect_ratio.x * 2.f);
|
|
|
|
// Only ceil if it was >= 0.25
|
|
|
|
const float ceiled_new_half_width = std::ceil(std::round(new_half_width * 2.f) / 2.f);
|
|
|
|
const int x_center = render_rect.center().x();
|
|
|
|
// Make a guess on which one to floor and ceil.
|
|
|
|
// For more precision, we should have kept the rounding point scale from above as well.
|
|
|
|
render_rect.setLeft(x_center - std::floor(new_half_width));
|
|
|
|
render_rect.setRight(x_center + ceiled_new_half_width);
|
|
|
|
}
|
|
|
|
if (aspect_ratio.y > 1.f)
|
|
|
|
{
|
|
|
|
const float new_half_height = render_rect.height() / (aspect_ratio.y * 2.f);
|
|
|
|
const float ceiled_new_half_height = std::ceil(std::round(new_half_height * 2.f) / 2.f);
|
|
|
|
const int y_center = render_rect.center().y();
|
|
|
|
render_rect.setTop(y_center - std::floor(new_half_height));
|
|
|
|
render_rect.setBottom(y_center + ceiled_new_half_height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (locked)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
RECT rect;
|
|
|
|
rect.left = render_rect.left();
|
|
|
|
rect.right = render_rect.right();
|
|
|
|
rect.top = render_rect.top();
|
|
|
|
rect.bottom = render_rect.bottom();
|
|
|
|
|
|
|
|
if (ClipCursor(&rect))
|
|
|
|
#else
|
|
|
|
// TODO: implement on other platforms. Probably XGrabPointer on Linux.
|
|
|
|
// The setting is hidden in the UI if not implemented
|
|
|
|
if (false)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
m_cursor_locked = true;
|
|
|
|
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Settings::Instance().GetCursorVisibility() != Config::ShowCursor::Constantly)
|
2021-05-09 10:28:04 +00:00
|
|
|
{
|
|
|
|
setCursor(Qt::BlankCursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
Host::GetInstance()->SetRenderFullFocus(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
ClipCursor(nullptr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (m_cursor_locked)
|
|
|
|
{
|
|
|
|
m_cursor_locked = false;
|
|
|
|
|
|
|
|
if (!Settings::Instance().GetLockCursor())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Center the mouse in the window if it's still active
|
|
|
|
// Leave it where it was otherwise, e.g. a prompt has opened or we alt tabbed.
|
|
|
|
if (isActiveWindow())
|
|
|
|
{
|
|
|
|
cursor().setPos(render_rect.left() + render_rect.width() / 2,
|
|
|
|
render_rect.top() + render_rect.height() / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the cursor or the user won't know the mouse is now unlocked
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
|
|
|
|
Host::GetInstance()->SetRenderFullFocus(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderWidget::SetCursorLockedOnNextActivation(bool locked)
|
|
|
|
{
|
|
|
|
if (Settings::Instance().GetLockCursor())
|
|
|
|
{
|
|
|
|
m_lock_cursor_on_next_activation = locked;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_lock_cursor_on_next_activation = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderWidget::SetWaitingForMessageBox(bool waiting_for_message_box)
|
|
|
|
{
|
|
|
|
if (m_waiting_for_message_box == waiting_for_message_box)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_waiting_for_message_box = waiting_for_message_box;
|
|
|
|
if (!m_waiting_for_message_box && m_lock_cursor_on_next_activation && isActiveWindow())
|
|
|
|
{
|
|
|
|
if (Settings::Instance().GetLockCursor())
|
|
|
|
{
|
|
|
|
SetCursorLocked(true);
|
|
|
|
}
|
|
|
|
m_lock_cursor_on_next_activation = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 08:33:07 +00:00
|
|
|
bool RenderWidget::event(QEvent* event)
|
|
|
|
{
|
2018-10-10 11:41:52 +00:00
|
|
|
PassEventToImGui(event);
|
|
|
|
|
2015-11-27 08:33:07 +00:00
|
|
|
switch (event->type())
|
|
|
|
{
|
|
|
|
case QEvent::KeyPress:
|
|
|
|
{
|
|
|
|
QKeyEvent* ke = static_cast<QKeyEvent*>(event);
|
|
|
|
if (ke->key() == Qt::Key_Escape)
|
|
|
|
emit EscapePressed();
|
2018-01-29 17:35:33 +00:00
|
|
|
|
|
|
|
// The render window might flicker on some platforms because Qt tries to change focus to a new
|
|
|
|
// element when there is none (?) Handling this event before it reaches QWidget fixes the issue.
|
|
|
|
if (ke->key() == Qt::Key_Tab)
|
|
|
|
return true;
|
|
|
|
|
2015-11-27 08:33:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-05-09 10:28:04 +00:00
|
|
|
// Needed in case a new window open and it moves the mouse
|
|
|
|
case QEvent::WindowBlocked:
|
|
|
|
SetCursorLocked(false);
|
|
|
|
break;
|
2017-07-04 14:58:27 +00:00
|
|
|
case QEvent::MouseButtonPress:
|
2021-05-09 10:28:04 +00:00
|
|
|
if (isActiveWindow())
|
2017-07-04 14:58:27 +00:00
|
|
|
{
|
2021-05-09 10:28:04 +00:00
|
|
|
// Lock the cursor with any mouse button click (behave the same as window focus change).
|
|
|
|
// This event is occasionally missed because isActiveWindow is laggy
|
2021-09-24 01:13:28 +00:00
|
|
|
if (Settings::Instance().GetLockCursor())
|
2021-05-09 10:28:04 +00:00
|
|
|
{
|
|
|
|
SetCursorLocked(true);
|
|
|
|
}
|
2021-09-24 01:13:28 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QEvent::MouseMove:
|
|
|
|
// Unhide on movement
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Settings::Instance().GetCursorVisibility() == Config::ShowCursor::OnMovement)
|
2021-09-24 01:13:28 +00:00
|
|
|
{
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
m_mouse_timer->start(MOUSE_HIDE_DELAY);
|
2017-07-04 14:58:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-11-27 08:33:07 +00:00
|
|
|
case QEvent::WinIdChange:
|
2018-10-03 13:03:13 +00:00
|
|
|
emit HandleChanged(reinterpret_cast<void*>(winId()));
|
2015-11-27 08:33:07 +00:00
|
|
|
break;
|
2021-05-09 10:28:04 +00:00
|
|
|
case QEvent::Show:
|
|
|
|
// Don't do if "stay on top" changed (or was true)
|
2021-09-23 03:57:52 +00:00
|
|
|
if (Settings::Instance().GetLockCursor() &&
|
2021-12-31 02:00:39 +00:00
|
|
|
Settings::Instance().GetCursorVisibility() != Config::ShowCursor::Constantly &&
|
2021-05-09 10:28:04 +00:00
|
|
|
!m_dont_lock_cursor_on_show)
|
|
|
|
{
|
|
|
|
// Auto lock when this window is shown (it was hidden)
|
|
|
|
if (isActiveWindow())
|
|
|
|
SetCursorLocked(true);
|
|
|
|
else
|
|
|
|
SetCursorLockedOnNextActivation();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Note that this event in Windows is not always aligned to the window that is highlighted,
|
|
|
|
// it's the window that has keyboard and mouse focus
|
2017-07-13 19:56:33 +00:00
|
|
|
case QEvent::WindowActivate:
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Config::Get(Config::MAIN_PAUSE_ON_FOCUS_LOST) && Core::GetState() == Core::State::Paused)
|
2018-04-20 19:46:42 +00:00
|
|
|
Core::SetState(Core::State::Running);
|
2018-06-08 18:47:15 +00:00
|
|
|
|
2021-05-09 10:28:04 +00:00
|
|
|
UpdateCursor();
|
|
|
|
|
|
|
|
// Avoid "race conditions" with message boxes
|
|
|
|
if (m_lock_cursor_on_next_activation && !m_waiting_for_message_box)
|
|
|
|
{
|
|
|
|
if (Settings::Instance().GetLockCursor())
|
|
|
|
{
|
|
|
|
SetCursorLocked(true);
|
|
|
|
}
|
|
|
|
m_lock_cursor_on_next_activation = false;
|
|
|
|
}
|
|
|
|
|
2018-06-08 18:47:15 +00:00
|
|
|
emit FocusChanged(true);
|
2017-07-13 19:56:33 +00:00
|
|
|
break;
|
|
|
|
case QEvent::WindowDeactivate:
|
2021-05-09 10:28:04 +00:00
|
|
|
SetCursorLocked(false);
|
|
|
|
|
|
|
|
UpdateCursor();
|
|
|
|
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Config::Get(Config::MAIN_PAUSE_ON_FOCUS_LOST) && Core::GetState() == Core::State::Running)
|
2020-05-18 17:38:56 +00:00
|
|
|
{
|
|
|
|
// If we are declared as the CPU thread, it means that the real CPU thread is waiting
|
|
|
|
// for us to finish showing a panic alert (with that panic alert likely being the cause
|
|
|
|
// of this event), so trying to pause the real CPU thread would cause a deadlock
|
|
|
|
if (!Core::IsCPUThread())
|
|
|
|
Core::SetState(Core::State::Paused);
|
|
|
|
}
|
2018-06-08 18:47:15 +00:00
|
|
|
|
|
|
|
emit FocusChanged(false);
|
2015-11-27 08:33:07 +00:00
|
|
|
break;
|
2021-05-09 10:28:04 +00:00
|
|
|
case QEvent::Move:
|
|
|
|
SetCursorLocked(m_cursor_locked);
|
|
|
|
break;
|
2018-01-20 20:00:28 +00:00
|
|
|
case QEvent::Resize:
|
2018-01-26 06:23:24 +00:00
|
|
|
{
|
2021-05-09 10:28:04 +00:00
|
|
|
SetCursorLocked(m_cursor_locked);
|
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
const QResizeEvent* se = static_cast<QResizeEvent*>(event);
|
|
|
|
QSize new_size = se->size();
|
2018-04-13 18:12:13 +00:00
|
|
|
|
2019-10-13 01:36:17 +00:00
|
|
|
QScreen* screen = window()->windowHandle()->screen();
|
2018-05-06 02:42:23 +00:00
|
|
|
|
2019-10-13 01:36:17 +00:00
|
|
|
const auto dpr = screen->devicePixelRatio();
|
2018-04-13 18:12:13 +00:00
|
|
|
|
|
|
|
emit SizeChanged(new_size.width() * dpr, new_size.height() * dpr);
|
2018-01-20 20:00:28 +00:00
|
|
|
break;
|
2018-01-26 06:23:24 +00:00
|
|
|
}
|
2021-05-09 10:28:04 +00:00
|
|
|
// Happens when we add/remove the widget from the main window instead of the dedicated one
|
|
|
|
case QEvent::ParentChange:
|
|
|
|
SetCursorLocked(false);
|
|
|
|
break;
|
2015-11-27 08:33:07 +00:00
|
|
|
case QEvent::WindowStateChange:
|
2021-05-09 10:28:04 +00:00
|
|
|
// Lock the mouse again when fullscreen changes (we might have missed some events)
|
|
|
|
SetCursorLocked(m_cursor_locked || (isFullScreen() && Settings::Instance().GetLockCursor()));
|
2015-11-27 08:33:07 +00:00
|
|
|
emit StateChanged(isFullScreen());
|
|
|
|
break;
|
|
|
|
case QEvent::Close:
|
|
|
|
emit Closed();
|
2021-10-12 00:13:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-11-27 08:33:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QWidget::event(event);
|
|
|
|
}
|
2018-04-29 11:35:43 +00:00
|
|
|
|
2018-10-10 11:41:52 +00:00
|
|
|
void RenderWidget::PassEventToImGui(const QEvent* event)
|
|
|
|
{
|
|
|
|
if (!Core::IsRunningAndStarted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (event->type())
|
|
|
|
{
|
|
|
|
case QEvent::KeyPress:
|
|
|
|
case QEvent::KeyRelease:
|
|
|
|
{
|
|
|
|
// As the imgui KeysDown array is only 512 elements wide, and some Qt keys which
|
|
|
|
// we need to track (e.g. alt) are above this value, we mask the lower 9 bits.
|
|
|
|
// Even masked, the key codes are still unique, so conflicts aren't an issue.
|
|
|
|
// The actual text input goes through AddInputCharactersUTF8().
|
|
|
|
const QKeyEvent* key_event = static_cast<const QKeyEvent*>(event);
|
|
|
|
const bool is_down = event->type() == QEvent::KeyPress;
|
2019-01-25 16:21:38 +00:00
|
|
|
const u32 key = static_cast<u32>(key_event->key() & 0x1FF);
|
2018-10-10 11:41:52 +00:00
|
|
|
auto lock = g_renderer->GetImGuiLock();
|
2019-06-01 11:55:09 +00:00
|
|
|
if (key < std::size(ImGui::GetIO().KeysDown))
|
2018-10-10 11:41:52 +00:00
|
|
|
ImGui::GetIO().KeysDown[key] = is_down;
|
|
|
|
|
|
|
|
if (is_down)
|
|
|
|
{
|
|
|
|
auto utf8 = key_event->text().toUtf8();
|
|
|
|
ImGui::GetIO().AddInputCharactersUTF8(utf8.constData());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::MouseMove:
|
|
|
|
{
|
|
|
|
auto lock = g_renderer->GetImGuiLock();
|
2019-01-25 15:31:58 +00:00
|
|
|
|
|
|
|
// Qt multiplies all coordinates by the scaling factor in highdpi mode, giving us "scaled" mouse
|
|
|
|
// coordinates (as if the screen was standard dpi). We need to update the mouse position in
|
|
|
|
// native coordinates, as the UI (and game) is rendered at native resolution.
|
|
|
|
const float scale = devicePixelRatio();
|
|
|
|
ImGui::GetIO().MousePos.x = static_cast<const QMouseEvent*>(event)->x() * scale;
|
|
|
|
ImGui::GetIO().MousePos.y = static_cast<const QMouseEvent*>(event)->y() * scale;
|
2018-10-10 11:41:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::MouseButtonPress:
|
|
|
|
case QEvent::MouseButtonRelease:
|
|
|
|
{
|
|
|
|
auto lock = g_renderer->GetImGuiLock();
|
|
|
|
const u32 button_mask = static_cast<u32>(static_cast<const QMouseEvent*>(event)->buttons());
|
2019-06-01 11:55:09 +00:00
|
|
|
for (size_t i = 0; i < std::size(ImGui::GetIO().MouseDown); i++)
|
2018-10-10 11:41:52 +00:00
|
|
|
ImGui::GetIO().MouseDown[i] = (button_mask & (1u << i)) != 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderWidget::SetImGuiKeyMap()
|
|
|
|
{
|
2019-06-01 11:55:09 +00:00
|
|
|
static constexpr std::array<std::array<int, 2>, 21> key_map{{
|
|
|
|
{ImGuiKey_Tab, Qt::Key_Tab},
|
|
|
|
{ImGuiKey_LeftArrow, Qt::Key_Left},
|
|
|
|
{ImGuiKey_RightArrow, Qt::Key_Right},
|
|
|
|
{ImGuiKey_UpArrow, Qt::Key_Up},
|
|
|
|
{ImGuiKey_DownArrow, Qt::Key_Down},
|
|
|
|
{ImGuiKey_PageUp, Qt::Key_PageUp},
|
|
|
|
{ImGuiKey_PageDown, Qt::Key_PageDown},
|
|
|
|
{ImGuiKey_Home, Qt::Key_Home},
|
|
|
|
{ImGuiKey_End, Qt::Key_End},
|
|
|
|
{ImGuiKey_Insert, Qt::Key_Insert},
|
|
|
|
{ImGuiKey_Delete, Qt::Key_Delete},
|
|
|
|
{ImGuiKey_Backspace, Qt::Key_Backspace},
|
|
|
|
{ImGuiKey_Space, Qt::Key_Space},
|
|
|
|
{ImGuiKey_Enter, Qt::Key_Return},
|
|
|
|
{ImGuiKey_Escape, Qt::Key_Escape},
|
|
|
|
{ImGuiKey_A, Qt::Key_A},
|
|
|
|
{ImGuiKey_C, Qt::Key_C},
|
|
|
|
{ImGuiKey_V, Qt::Key_V},
|
|
|
|
{ImGuiKey_X, Qt::Key_X},
|
|
|
|
{ImGuiKey_Y, Qt::Key_Y},
|
|
|
|
{ImGuiKey_Z, Qt::Key_Z},
|
|
|
|
}};
|
2018-10-10 11:41:52 +00:00
|
|
|
auto lock = g_renderer->GetImGuiLock();
|
2019-02-01 18:47:07 +00:00
|
|
|
|
|
|
|
for (auto [imgui_key, qt_key] : key_map)
|
|
|
|
ImGui::GetIO().KeyMap[imgui_key] = (qt_key & 0x1FF);
|
2018-10-10 11:41:52 +00:00
|
|
|
}
|