Qt: Experiment with alternate widget while a game isn't running.

This commit is contained in:
BearOso 2024-08-02 18:10:03 -05:00
parent e92b93ca9c
commit 8b1d67397e
2 changed files with 52 additions and 19 deletions

View File

@ -22,6 +22,27 @@
static EmuSettingsWindow *g_emu_settings_window = nullptr; static EmuSettingsWindow *g_emu_settings_window = nullptr;
class DefaultBackground
: public QWidget
{
public:
DefaultBackground(QWidget *parent)
: QWidget(parent)
{
}
void paintEvent(QPaintEvent *event) override
{
QPainter paint(this);
QLinearGradient gradient(0.0, 0.0, 0.0, event->rect().toRectF().height());
gradient.setColorAt(0.0, QColor(0, 0, 128));
gradient.setColorAt(1.0, QColor(0, 0, 0));
paint.setBrush(QBrush(gradient));
paint.drawRect(0, 0, event->rect().width(), event->rect().height());
}
};
EmuMainWindow::EmuMainWindow(EmuApplication *app) EmuMainWindow::EmuMainWindow(EmuApplication *app)
: app(app) : app(app)
{ {
@ -33,12 +54,11 @@ EmuMainWindow::EmuMainWindow(EmuApplication *app)
mouse_timer.setTimerType(Qt::CoarseTimer); mouse_timer.setTimerType(Qt::CoarseTimer);
mouse_timer.setInterval(1000); mouse_timer.setInterval(1000);
mouse_timer.callOnTimeout([&] { mouse_timer.callOnTimeout([&] {
if (cursor_visible && isActivelyDrawing()) if (cursor_visible && isActivelyDrawing()) {
{ if (canvas)
if (canvas) canvas->setCursor(QCursor(Qt::BlankCursor));
canvas->setCursor(QCursor(Qt::BlankCursor)); cursor_visible = false;
cursor_visible = false; mouse_timer.stop();
mouse_timer.stop();
} }
}); });
} }
@ -71,10 +91,20 @@ void EmuMainWindow::destroyCanvas()
widget->deinit(); widget->deinit();
delete widget; delete widget;
} }
canvas = nullptr;
} }
bool EmuMainWindow::createCanvas() bool EmuMainWindow::createCanvas()
{ {
auto fallback = [this]() -> bool {
QMessageBox::warning(
this, tr("Unable to Start Display Driver"),
tr("Unable to create a %1 context. Attempting to use qt.")
.arg(QString::fromUtf8(app->config->display_driver)));
app->config->display_driver = "qt";
return createCanvas();
};
if (app->config->display_driver != "vulkan" && if (app->config->display_driver != "vulkan" &&
app->config->display_driver != "opengl" && app->config->display_driver != "opengl" &&
app->config->display_driver != "qt") app->config->display_driver != "qt")
@ -94,7 +124,7 @@ bool EmuMainWindow::createCanvas()
if (!canvas->createContext()) if (!canvas->createContext())
{ {
delete canvas; delete canvas;
return false; return fallback();
} }
} }
else if (app->config->display_driver == "opengl") else if (app->config->display_driver == "opengl")
@ -121,7 +151,7 @@ bool EmuMainWindow::createCanvas()
if (!canvas->createContext()) if (!canvas->createContext())
{ {
delete canvas; delete canvas;
return false; return fallback();
} }
} }
else if (app->config->display_driver == "opengl") else if (app->config->display_driver == "opengl")
@ -141,17 +171,12 @@ bool EmuMainWindow::createCanvas()
void EmuMainWindow::recreateCanvas() void EmuMainWindow::recreateCanvas()
{ {
if (!canvas)
return;
app->suspendThread(); app->suspendThread();
destroyCanvas(); destroyCanvas();
createCanvas();
if (!createCanvas())
{
QMessageBox::warning(this,
tr("Unable to Start Display Driver"),
tr("Unable to create a %1 context. Attempting to use qt.").arg(QString::fromUtf8(app->config->display_driver)));
app->config->display_driver = "qt";
createCanvas();
}
app->unsuspendThread(); app->unsuspendThread();
} }
@ -362,6 +387,8 @@ void EmuMainWindow::createWidgets()
if (app->config->main_window_width != 0 && app->config->main_window_height != 0) if (app->config->main_window_width != 0 && app->config->main_window_height != 0)
resize(app->config->main_window_width, app->config->main_window_height); resize(app->config->main_window_width, app->config->main_window_height);
setCentralWidget(new DefaultBackground(this));
} }
void EmuMainWindow::resizeToMultiple(int multiple) void EmuMainWindow::resizeToMultiple(int multiple)
@ -453,6 +480,12 @@ bool EmuMainWindow::openFile(std::string filename)
setCoreActionsEnabled(true); setCoreActionsEnabled(true);
if (!isFullScreen() && app->config->fullscreen_on_open) if (!isFullScreen() && app->config->fullscreen_on_open)
toggleFullscreen(); toggleFullscreen();
if (!canvas)
if (!createCanvas())
return false;
QApplication::sync();
app->startGame(); app->startGame();
mouse_timer.start(); mouse_timer.start();
return true; return true;

View File

@ -39,8 +39,8 @@ class EmuMainWindow : public QMainWindow
void gameChanging(); void gameChanging();
void toggleMouseGrab(); void toggleMouseGrab();
std::vector<std::string> getDisplayDeviceList(); std::vector<std::string> getDisplayDeviceList();
EmuApplication *app; EmuApplication *app = nullptr;
EmuCanvas *canvas; EmuCanvas *canvas = nullptr;
private: private:
void idle(); void idle();