| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef HAS_LIBMGBA |
| 5 | |
| 6 | #include "DolphinQt/GBAWidget.h" |
| 7 | |
| 8 | #include <fmt/format.h> |
| 9 | |
| 10 | #include <QAction> |
| 11 | #include <QCloseEvent> |
| 12 | #include <QContextMenuEvent> |
| 13 | #include <QDragEnterEvent> |
| 14 | #include <QDropEvent> |
| 15 | #include <QFileDialog> |
| 16 | #include <QIcon> |
| 17 | #include <QMenu> |
| 18 | #include <QMimeData> |
| 19 | #include <QMouseEvent> |
| 20 | #include <QPainter> |
| 21 | |
| 22 | #include "AudioCommon/AudioCommon.h" |
| 23 | #include "Core/Config/MainSettings.h" |
| 24 | #include "Core/Core.h" |
| 25 | #include "Core/CoreTiming.h" |
| 26 | #include "Core/HW/GBAPad.h" |
| 27 | #include "Core/HW/SI/SI.h" |
| 28 | #include "Core/HW/SI/SI_Device.h" |
| 29 | #include "Core/Movie.h" |
| 30 | #include "Core/NetPlayProto.h" |
| 31 | #include "Core/System.h" |
| 32 | #include "DolphinQt/QtUtils/DolphinFileDialog.h" |
| 33 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 34 | #include "DolphinQt/Resources.h" |
| 35 | #include "DolphinQt/Settings.h" |
| 36 | #include "DolphinQt/Settings/GameCubePane.h" |
| 37 | |
| 38 | static void RestartCore(const std::weak_ptr<HW::GBA::Core>& core, std::string_view rom_path = {}) |
| 39 | { |
| 40 | Core::RunOnCPUThread(Core::System::GetInstance(), [core, rom_path = std::string(rom_path)] { |
| 41 | if (auto core_ptr = core.lock()) |
| 42 | { |
| 43 | auto& info = Config::MAIN_GBA_ROM_PATHS[core_ptr->GetCoreInfo().device_number]; |
| 44 | core_ptr->Stop(); |
| 45 | Config::SetCurrent(info, rom_path); |
| 46 | auto& system = Core::System::GetInstance(); |
| 47 | auto& core_timing = system.GetCoreTiming(); |
| 48 | if (core_ptr->Start(core_timing.GetTicks())) |
| 49 | return; |
| 50 | Config::SetCurrent(info, Config::GetBase(info)); |
| 51 | core_ptr->Start(core_timing.GetTicks()); |
| 52 | } |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | static void QueueEReaderCard(const std::weak_ptr<HW::GBA::Core>& core, std::string_view card_path) |
| 57 | { |
| 58 | Core::RunOnCPUThread(Core::System::GetInstance(), [core, card_path = std::string(card_path)] { |
| 59 | if (auto core_ptr = core.lock()) |
| 60 | core_ptr->EReaderQueueCard(card_path); |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | GBAWidget::GBAWidget(std::weak_ptr<HW::GBA::Core> core, const HW::GBA::CoreInfo& info, |
| 65 | const std::optional<NetPlay::PadDetails>& netplay_pad) |
| 66 | : QWidget(nullptr, LoadWindowFlags(netplay_pad ? netplay_pad->local_pad : info.device_number)), |
| 67 | m_core(std::move(core)), m_core_info(info), m_local_pad(info.device_number), |
| 68 | m_is_local_pad(true), m_volume(0), m_muted(false), m_force_disconnect(false), m_moving(false), |
| 69 | m_interframe_blending(false) |
| 70 | { |
| 71 | bool visible = true; |
| 72 | if (netplay_pad) |
| 73 | { |
| 74 | m_netplayer_name = netplay_pad->player_name; |
| 75 | m_is_local_pad = netplay_pad->is_local; |
| 76 | m_local_pad = netplay_pad->local_pad; |
| 77 | visible = !netplay_pad->hide_gba; |
| 78 | } |
| 79 | |
| 80 | setWindowIcon(Resources::GetAppIcon()); |
| 81 | setAcceptDrops(true); |
| 82 | resize(m_core_info.width, m_core_info.height); |
| 83 | setVisible(visible); |
| 84 | |
| 85 | SetVolume(100); |
| 86 | if (!visible) |
| 87 | ToggleMute(); |
| 88 | |
| 89 | LoadSettings(); |
| 90 | UpdateTitle(); |
| 91 | } |
| 92 | |
| 93 | GBAWidget::~GBAWidget() |
| 94 | { |
| 95 | SaveSettings(); |
| 96 | } |
| 97 | |
| 98 | void GBAWidget::GameChanged(const HW::GBA::CoreInfo& info) |
| 99 | { |
| 100 | m_core_info = info; |
| 101 | m_previous_frame = QImage(); |
| 102 | UpdateTitle(); |
| 103 | update(); |
| 104 | } |
| 105 | |
| 106 | void GBAWidget::SetVideoBuffer(std::span<const u32> video_buffer) |
| 107 | { |
| 108 | m_previous_frame = std::move(m_last_frame); |
| 109 | if (video_buffer.size() == static_cast<size_t>(m_core_info.width * m_core_info.height)) |
| 110 | { |
| 111 | m_last_frame = QImage(reinterpret_cast<const uchar*>(video_buffer.data()), m_core_info.width, |
| 112 | m_core_info.height, QImage::Format_ARGB32) |
| 113 | .convertToFormat(QImage::Format_RGB32) |
| 114 | .rgbSwapped(); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | m_last_frame = QImage(); |
| 119 | } |
| 120 | update(); |
| 121 | } |
| 122 | |
| 123 | void GBAWidget::SetVolume(int volume) |
| 124 | { |
| 125 | m_muted = false; |
| 126 | m_volume = std::clamp(volume, 0, 100); |
| 127 | UpdateVolume(); |
| 128 | } |
| 129 | |
| 130 | void GBAWidget::VolumeDown() |
| 131 | { |
| 132 | SetVolume(m_volume - 10); |
| 133 | } |
| 134 | |
| 135 | void GBAWidget::VolumeUp() |
| 136 | { |
| 137 | SetVolume(m_volume + 10); |
| 138 | } |
| 139 | |
| 140 | bool GBAWidget::IsMuted() |
| 141 | { |
| 142 | return m_muted; |
| 143 | } |
| 144 | |
| 145 | void GBAWidget::ToggleMute() |
| 146 | { |
| 147 | m_muted = !m_muted; |
| 148 | UpdateVolume(); |
| 149 | } |
| 150 | |
| 151 | void GBAWidget::ToggleDisconnect() |
| 152 | { |
| 153 | if (!CanControlCore()) |
| 154 | return; |
| 155 | |
| 156 | m_force_disconnect = !m_force_disconnect; |
| 157 | |
| 158 | Core::RunOnCPUThread(Core::System::GetInstance(), |
| 159 | [core = m_core, force_disconnect = m_force_disconnect] { |
| 160 | if (auto core_ptr = core.lock()) |
| 161 | core_ptr->SetForceDisconnect(force_disconnect); |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | void GBAWidget::LoadROM() |
| 166 | { |
| 167 | if (!CanControlCore()) |
| 168 | return; |
| 169 | |
| 170 | std::string rom_path = GameCubePane::GetOpenGBARom(""); |
| 171 | if (rom_path.empty()) |
| 172 | return; |
| 173 | |
| 174 | RestartCore(m_core, rom_path); |
| 175 | } |
| 176 | |
| 177 | void GBAWidget::UnloadROM() |
| 178 | { |
| 179 | if (!CanControlCore() || !m_core_info.has_rom) |
| 180 | return; |
| 181 | |
| 182 | RestartCore(m_core); |
| 183 | } |
| 184 | |
| 185 | void GBAWidget::PromptForEReaderCards() |
| 186 | { |
| 187 | const QStringList card_paths = DolphinFileDialog::getOpenFileNames( |
| 188 | this, tr("Select e-Reader Cards"), QString(), tr("e-Reader Cards (*.raw);;All Files (*)"), |
| 189 | nullptr, QFileDialog::Options()); |
| 190 | |
| 191 | for (const QString& card_path : card_paths) |
| 192 | QueueEReaderCard(m_core, QDir::toNativeSeparators(card_path).toStdString()); |
| 193 | } |
| 194 | |
| 195 | void GBAWidget::ResetCore() |
| 196 | { |
| 197 | if (!CanResetCore()) |
| 198 | return; |
| 199 | |
| 200 | Pad::SetGBAReset(m_local_pad, true); |
| 201 | } |
| 202 | |
| 203 | void GBAWidget::DoState(bool export_state) |
| 204 | { |
| 205 | if (!CanControlCore() && !export_state) |
| 206 | return; |
| 207 | |
| 208 | QString state_path = QDir::toNativeSeparators( |
| 209 | (export_state ? DolphinFileDialog::getSaveFileName : DolphinFileDialog::getOpenFileName)( |
| 210 | this, tr("Select a File"), QString(), |
| 211 | tr("mGBA Save States (*.ss0 *.ss1 *.ss2 *.ss3 *.ss4 " |
| 212 | "*.ss5 *.ss6 *.ss7 *.ss8 *.ss9);;" |
| 213 | "All Files (*)"), |
| 214 | nullptr, QFileDialog::Options())); |
| 215 | |
| 216 | if (state_path.isEmpty()) |
| 217 | return; |
| 218 | |
| 219 | Core::RunOnCPUThread(Core::System::GetInstance(), |
| 220 | [export_state, core = m_core, state_path = state_path.toStdString()] { |
| 221 | if (auto core_ptr = core.lock()) |
| 222 | { |
| 223 | if (export_state) |
| 224 | core_ptr->ExportState(state_path); |
| 225 | else |
| 226 | core_ptr->ImportState(state_path); |
| 227 | } |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | void GBAWidget::ImportExportSave(bool export_save) |
| 232 | { |
| 233 | if (!CanControlCore() && !export_save) |
| 234 | return; |
| 235 | if (!m_core_info.has_rom) |
| 236 | return; |
| 237 | |
| 238 | QString save_path = QDir::toNativeSeparators( |
| 239 | (export_save ? DolphinFileDialog::getSaveFileName : |
| 240 | DolphinFileDialog::getOpenFileName)(this, tr("Select a File"), QString(), |
| 241 | tr("Save Game Files (*.sav);;" |
| 242 | "All Files (*)"), |
| 243 | nullptr, QFileDialog::Options())); |
| 244 | |
| 245 | if (save_path.isEmpty()) |
| 246 | return; |
| 247 | |
| 248 | Core::RunOnCPUThread(Core::System::GetInstance(), |
| 249 | [export_save, core = m_core, save_path = save_path.toStdString()] { |
| 250 | if (auto core_ptr = core.lock()) |
| 251 | { |
| 252 | if (export_save) |
| 253 | core_ptr->ExportSave(save_path); |
| 254 | else |
| 255 | core_ptr->ImportSave(save_path); |
| 256 | } |
| 257 | }); |
| 258 | } |
| 259 | |
| 260 | void GBAWidget::Resize(int scale) |
| 261 | { |
| 262 | showNormal(); |
| 263 | resize(m_core_info.width * scale, m_core_info.height * scale); |
| 264 | } |
| 265 | |
| 266 | bool GBAWidget::IsBorderless() const |
| 267 | { |
| 268 | return windowFlags().testFlag(Qt::FramelessWindowHint) || |
| 269 | windowState().testFlag(Qt::WindowFullScreen); |
| 270 | } |
| 271 | |
| 272 | void GBAWidget::SetBorderless(bool enable) |
| 273 | { |
| 274 | if (windowState().testFlag(Qt::WindowFullScreen)) |
| 275 | { |
| 276 | if (!enable) |
| 277 | setWindowState((windowState() ^ Qt::WindowFullScreen) | Qt::WindowMaximized); |
| 278 | } |
| 279 | else if (windowState().testFlag(Qt::WindowMaximized)) |
| 280 | { |
| 281 | if (enable) |
| 282 | setWindowState((windowState() ^ Qt::WindowMaximized) | Qt::WindowFullScreen); |
| 283 | } |
| 284 | else if (windowFlags().testFlag(Qt::FramelessWindowHint) != enable) |
| 285 | { |
| 286 | QRect saved_geometry = geometry(); |
| 287 | setWindowFlag(Qt::FramelessWindowHint, enable); |
| 288 | setGeometry(saved_geometry); |
| 289 | show(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | bool GBAWidget::IsAlwaysOnTop() const |
| 294 | { |
| 295 | return windowFlags().testFlag(Qt::WindowStaysOnTopHint); |
| 296 | } |
| 297 | |
| 298 | void GBAWidget::SetAlwaysOnTop(bool enable) |
| 299 | { |
| 300 | if (windowFlags().testFlag(Qt::WindowStaysOnTopHint) == enable) |
| 301 | return; |
| 302 | setWindowFlag(Qt::WindowStaysOnTopHint, enable); |
| 303 | show(); |
| 304 | } |
| 305 | |
| 306 | void GBAWidget::UpdateTitle() |
| 307 | { |
| 308 | std::string title = fmt::format("GBA{}", m_core_info.device_number + 1); |
| 309 | if (!m_netplayer_name.empty()) |
| 310 | title += " " + m_netplayer_name; |
| 311 | |
| 312 | if (!m_core_info.game_title.empty()) |
| 313 | title += " | " + m_core_info.game_title; |
| 314 | |
| 315 | if (m_muted) |
| 316 | title += " | Muted"; |
| 317 | else |
| 318 | title += fmt::format(" | Volume {}%", m_volume); |
| 319 | |
| 320 | setWindowTitle(QString::fromStdString(title)); |
| 321 | } |
| 322 | |
| 323 | void GBAWidget::UpdateVolume() |
| 324 | { |
| 325 | int volume = m_muted ? 0 : m_volume * 256 / 100; |
| 326 | auto& system = Core::System::GetInstance(); |
| 327 | system.GetSoundStream()->GetMixer()->SetGBAVolume(m_core_info.device_number, volume, volume); |
| 328 | UpdateTitle(); |
| 329 | } |
| 330 | |
| 331 | Qt::WindowFlags GBAWidget::LoadWindowFlags(int device_number) |
| 332 | { |
| 333 | const QSettings& settings = Settings::GetQSettings(); |
| 334 | const QString key = QStringLiteral("gbawidget/flags%1").arg(device_number + 1); |
| 335 | return settings.contains(key) ? Qt::WindowFlags{settings.value(key).toInt()} : Qt::WindowFlags{}; |
| 336 | } |
| 337 | |
| 338 | void GBAWidget::LoadSettings() |
| 339 | { |
| 340 | const QSettings& settings = Settings::GetQSettings(); |
| 341 | QString key = QStringLiteral("gbawidget/geometry%1").arg(m_local_pad + 1); |
| 342 | if (settings.contains(key)) |
| 343 | restoreGeometry(settings.value(key).toByteArray()); |
| 344 | |
| 345 | key = QStringLiteral("gbawidget/interframeblending%1").arg(m_local_pad + 1); |
| 346 | if (settings.contains(key)) |
| 347 | m_interframe_blending = settings.value(key).toBool(); |
| 348 | } |
| 349 | |
| 350 | void GBAWidget::SaveSettings() |
| 351 | { |
| 352 | QSettings& settings = Settings::GetQSettings(); |
| 353 | settings.setValue(QStringLiteral("gbawidget/flags%1").arg(m_local_pad + 1), |
| 354 | static_cast<int>(windowFlags())); |
| 355 | settings.setValue(QStringLiteral("gbawidget/geometry%1").arg(m_local_pad + 1), saveGeometry()); |
| 356 | settings.setValue(QStringLiteral("gbawidget/interframeblending%1").arg(m_local_pad + 1), |
| 357 | m_interframe_blending); |
| 358 | } |
| 359 | |
| 360 | bool GBAWidget::CanControlCore() |
| 361 | { |
| 362 | return !Core::System::GetInstance().GetMovie().IsMovieActive() && !NetPlay::IsNetPlayRunning(); |
| 363 | } |
| 364 | |
| 365 | bool GBAWidget::CanResetCore() |
| 366 | { |
| 367 | return m_is_local_pad; |
| 368 | } |
| 369 | |
| 370 | void GBAWidget::closeEvent(QCloseEvent* event) |
| 371 | { |
| 372 | event->ignore(); |
| 373 | } |
| 374 | |
| 375 | void GBAWidget::contextMenuEvent(QContextMenuEvent* event) |
| 376 | { |
| 377 | auto* menu = new QMenu(this); |
| 378 | connect(menu, &QMenu::triggered, menu, &QMenu::deleteLater); |
| 379 | |
| 380 | auto* disconnect_action = |
| 381 | new QAction(m_force_disconnect ? tr("Dis&connected") : tr("&Connected"), menu); |
| 382 | disconnect_action->setEnabled(CanControlCore()); |
| 383 | disconnect_action->setCheckable(true); |
| 384 | disconnect_action->setChecked(!m_force_disconnect); |
| 385 | connect(disconnect_action, &QAction::triggered, this, &GBAWidget::ToggleDisconnect); |
| 386 | |
| 387 | auto* load_action = new QAction(tr("L&oad ROM..."), menu); |
| 388 | load_action->setEnabled(CanControlCore()); |
| 389 | connect(load_action, &QAction::triggered, this, &GBAWidget::LoadROM); |
| 390 | |
| 391 | auto* unload_action = new QAction(tr("&Unload ROM"), menu); |
| 392 | unload_action->setEnabled(CanControlCore() && m_core_info.has_rom); |
| 393 | connect(unload_action, &QAction::triggered, this, &GBAWidget::UnloadROM); |
| 394 | |
| 395 | auto* card_action = new QAction(tr("&Scan e-Reader Card(s)..."), menu); |
| 396 | card_action->setEnabled(CanControlCore() && m_core_info.has_ereader); |
| 397 | connect(card_action, &QAction::triggered, this, &GBAWidget::PromptForEReaderCards); |
| 398 | |
| 399 | auto* reset_action = new QAction(tr("&Reset"), menu); |
| 400 | reset_action->setEnabled(CanResetCore()); |
| 401 | connect(reset_action, &QAction::triggered, this, &GBAWidget::ResetCore); |
| 402 | |
| 403 | // i18n: Noun (i.e. the data saved by the game) |
| 404 | auto* savefile_menu = new QMenu(tr("Save Game"), menu); |
| 405 | auto* save_import_action = new QAction(tr("&Import Save Game..."), savefile_menu); |
| 406 | save_import_action->setEnabled(CanControlCore() && m_core_info.has_rom); |
| 407 | connect(save_import_action, &QAction::triggered, this, [this] { ImportExportSave(false); }); |
| 408 | auto* save_export_action = new QAction(tr("&Export Save Game..."), savefile_menu); |
| 409 | save_export_action->setEnabled(m_core_info.has_rom); |
| 410 | connect(save_export_action, &QAction::triggered, this, [this] { ImportExportSave(true); }); |
| 411 | |
| 412 | auto* state_menu = new QMenu(tr("Save State"), menu); |
| 413 | auto* import_action = new QAction(tr("&Import State..."), state_menu); |
| 414 | import_action->setEnabled(CanControlCore()); |
| 415 | connect(import_action, &QAction::triggered, this, [this] { DoState(false); }); |
| 416 | auto* export_state = new QAction(tr("&Export State..."), state_menu); |
| 417 | connect(export_state, &QAction::triggered, this, [this] { DoState(true); }); |
| 418 | |
| 419 | auto* mute_action = new QAction(tr("&Mute"), menu); |
| 420 | mute_action->setCheckable(true); |
| 421 | mute_action->setChecked(m_muted); |
| 422 | connect(mute_action, &QAction::triggered, this, &GBAWidget::ToggleMute); |
| 423 | |
| 424 | auto* options_menu = new QMenu(tr("Options"), menu); |
| 425 | |
| 426 | auto* size_menu = new QMenu(tr("Window Size"), options_menu); |
| 427 | auto* x1_action = new QAction(tr("&1x"), size_menu); |
| 428 | connect(x1_action, &QAction::triggered, this, [this] { Resize(1); }); |
| 429 | auto* x2_action = new QAction(tr("&2x"), size_menu); |
| 430 | connect(x2_action, &QAction::triggered, this, [this] { Resize(2); }); |
| 431 | auto* x3_action = new QAction(tr("&3x"), size_menu); |
| 432 | connect(x3_action, &QAction::triggered, this, [this] { Resize(3); }); |
| 433 | auto* x4_action = new QAction(tr("&4x"), size_menu); |
| 434 | connect(x4_action, &QAction::triggered, this, [this] { Resize(4); }); |
| 435 | |
| 436 | auto* borderless_action = new QAction(tr("&Borderless Window"), options_menu); |
| 437 | borderless_action->setCheckable(true); |
| 438 | borderless_action->setChecked(IsBorderless()); |
| 439 | connect(borderless_action, &QAction::triggered, this, [this] { SetBorderless(!IsBorderless()); }); |
| 440 | |
| 441 | auto* topmost_action = new QAction(tr("Always on &Top"), options_menu); |
| 442 | topmost_action->setCheckable(true); |
| 443 | topmost_action->setChecked(IsAlwaysOnTop()); |
| 444 | connect(topmost_action, &QAction::triggered, this, [this] { SetAlwaysOnTop(!IsAlwaysOnTop()); }); |
| 445 | |
| 446 | auto* blending_action = new QAction(tr("&Interframe Blending"), options_menu); |
| 447 | blending_action->setCheckable(true); |
| 448 | blending_action->setChecked(m_interframe_blending); |
| 449 | connect(blending_action, &QAction::triggered, this, |
| 450 | [this] { m_interframe_blending = !m_interframe_blending; }); |
| 451 | |
| 452 | menu->addAction(disconnect_action); |
| 453 | menu->addSeparator(); |
| 454 | menu->addAction(load_action); |
| 455 | menu->addAction(unload_action); |
| 456 | menu->addAction(card_action); |
| 457 | menu->addAction(reset_action); |
| 458 | menu->addSeparator(); |
| 459 | menu->addMenu(savefile_menu); |
| 460 | menu->addMenu(state_menu); |
| 461 | menu->addSeparator(); |
| 462 | menu->addAction(mute_action); |
| 463 | menu->addSeparator(); |
| 464 | menu->addMenu(options_menu); |
| 465 | |
| 466 | savefile_menu->addAction(save_import_action); |
| 467 | savefile_menu->addAction(save_export_action); |
| 468 | |
| 469 | state_menu->addAction(import_action); |
| 470 | state_menu->addAction(export_state); |
| 471 | |
| 472 | options_menu->addMenu(size_menu); |
| 473 | options_menu->addSeparator(); |
| 474 | options_menu->addAction(borderless_action); |
| 475 | options_menu->addAction(topmost_action); |
| 476 | options_menu->addAction(blending_action); |
| 477 | |
| 478 | size_menu->addAction(x1_action); |
| 479 | size_menu->addAction(x2_action); |
| 480 | size_menu->addAction(x3_action); |
| 481 | size_menu->addAction(x4_action); |
| 482 | |
| 483 | menu->move(event->globalPos()); |
| 484 | menu->show(); |
| 485 | } |
| 486 | |
| 487 | void GBAWidget::mouseDoubleClickEvent(QMouseEvent* event) |
| 488 | { |
| 489 | SetBorderless(!IsBorderless()); |
| 490 | } |
| 491 | |
| 492 | void GBAWidget::mousePressEvent(QMouseEvent* event) |
| 493 | { |
| 494 | if (event->button() != Qt::MouseButton::LeftButton || |
| 495 | !windowFlags().testFlag(Qt::FramelessWindowHint)) |
| 496 | return; |
| 497 | m_moving = true; |
| 498 | m_move_pos = event->pos(); |
| 499 | } |
| 500 | |
| 501 | void GBAWidget::mouseReleaseEvent(QMouseEvent* event) |
| 502 | { |
| 503 | if (event->button() != Qt::MouseButton::LeftButton) |
| 504 | return; |
| 505 | m_moving = false; |
| 506 | } |
| 507 | |
| 508 | void GBAWidget::mouseMoveEvent(QMouseEvent* event) |
| 509 | { |
| 510 | if (!m_moving) |
| 511 | return; |
| 512 | auto event_pos = event->globalPosition().toPoint(); |
| 513 | move(event_pos - m_move_pos - (geometry().topLeft() - pos())); |
| 514 | } |
| 515 | |
| 516 | void GBAWidget::paintEvent(QPaintEvent* event) |
| 517 | { |
| 518 | QPainter painter(this); |
| 519 | painter.fillRect(QRect(QPoint(), size()), Qt::black); |
| 520 | |
| 521 | const QRect src_rect(0, 0, m_core_info.width, m_core_info.height); |
| 522 | QRect target_rect{}; |
| 523 | if (size() == QSize(m_core_info.width, m_core_info.height)) |
| 524 | { |
| 525 | target_rect = QRect(0, 0, m_core_info.width, m_core_info.height); |
| 526 | } |
| 527 | else if (static_cast<float>(m_core_info.width) / m_core_info.height > |
| 528 | static_cast<float>(width()) / height()) |
| 529 | { |
| 530 | int new_height = width() * m_core_info.height / m_core_info.width; |
| 531 | target_rect = QRect(0, (height() - new_height) / 2, width(), new_height); |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | int new_width = height() * m_core_info.width / m_core_info.height; |
| 536 | target_rect = QRect((width() - new_width) / 2, 0, new_width, height()); |
| 537 | } |
| 538 | |
| 539 | if (m_interframe_blending && m_previous_frame.size() == src_rect.size()) |
| 540 | { |
| 541 | painter.drawImage(target_rect, m_previous_frame, src_rect); |
| 542 | painter.setOpacity(0.5); |
| 543 | } |
| 544 | if (m_last_frame.size() == src_rect.size()) |
| 545 | { |
| 546 | painter.drawImage(target_rect, m_last_frame, src_rect); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | void GBAWidget::dragEnterEvent(QDragEnterEvent* event) |
| 551 | { |
| 552 | if (CanControlCore() && event->mimeData()->hasUrls()) |
| 553 | event->acceptProposedAction(); |
| 554 | } |
| 555 | |
| 556 | void GBAWidget::dropEvent(QDropEvent* event) |
| 557 | { |
| 558 | if (!CanControlCore()) |
| 559 | return; |
| 560 | |
| 561 | for (const QUrl& url : event->mimeData()->urls()) |
| 562 | { |
| 563 | QFileInfo file_info(url.toLocalFile()); |
| 564 | QString path = file_info.filePath(); |
| 565 | |
| 566 | if (!file_info.isFile()) |
| 567 | continue; |
| 568 | |
| 569 | if (!file_info.exists() || !file_info.isReadable()) |
| 570 | { |
| 571 | ModalMessageBox::critical(this, tr("Error"), tr("Failed to open '%1'").arg(path)); |
| 572 | continue; |
| 573 | } |
| 574 | |
| 575 | if (file_info.suffix() == QStringLiteral("raw")) |
| 576 | QueueEReaderCard(m_core, path.toStdString()); |
| 577 | else |
| 578 | RestartCore(m_core, path.toStdString()); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | GBAWidgetController::~GBAWidgetController() |
| 583 | { |
| 584 | m_widget->deleteLater(); |
| 585 | } |
| 586 | |
| 587 | void GBAWidgetController::Create(std::weak_ptr<HW::GBA::Core> core, const HW::GBA::CoreInfo& info) |
| 588 | { |
| 589 | std::optional<NetPlay::PadDetails> netplay_pad; |
| 590 | if (NetPlay::IsNetPlayRunning()) |
| 591 | { |
| 592 | const NetPlay::PadDetails details = NetPlay::GetPadDetails(info.device_number); |
| 593 | if (details.local_pad < 4) |
| 594 | netplay_pad = details; |
| 595 | } |
| 596 | m_widget = new GBAWidget(std::move(core), info, netplay_pad); |
| 597 | } |
| 598 | |
| 599 | void GBAWidgetController::GameChanged(const HW::GBA::CoreInfo& info) |
| 600 | { |
| 601 | m_widget->GameChanged(info); |
| 602 | } |
| 603 | |
| 604 | void GBAWidgetController::FrameEnded(std::span<const u32> video_buffer) |
| 605 | { |
| 606 | m_widget->SetVideoBuffer(video_buffer); |
| 607 | } |
| 608 | #endif // HAS_LIBMGBA |