overlay_dialog.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <QKeyEvent>
  4. #include <QScreen>
  5. #include "core/core.h"
  6. #include "core/hid/hid_types.h"
  7. #include "core/hid/input_interpreter.h"
  8. #include "ui_overlay_dialog.h"
  9. #include "yuzu/util/overlay_dialog.h"
  10. namespace {
  11. constexpr float BASE_TITLE_FONT_SIZE = 14.0f;
  12. constexpr float BASE_FONT_SIZE = 18.0f;
  13. constexpr float BASE_WIDTH = 1280.0f;
  14. constexpr float BASE_HEIGHT = 720.0f;
  15. } // Anonymous namespace
  16. OverlayDialog::OverlayDialog(QWidget* parent, Core::System& system, const QString& title_text,
  17. const QString& body_text, const QString& left_button_text,
  18. const QString& right_button_text, Qt::Alignment alignment,
  19. bool use_rich_text_)
  20. : QDialog(parent), ui{std::make_unique<Ui::OverlayDialog>()}, use_rich_text{use_rich_text_} {
  21. ui->setupUi(this);
  22. setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowTitleHint |
  23. Qt::WindowSystemMenuHint | Qt::CustomizeWindowHint);
  24. setWindowModality(Qt::WindowModal);
  25. setAttribute(Qt::WA_TranslucentBackground);
  26. if (use_rich_text) {
  27. InitializeRichTextDialog(title_text, body_text, left_button_text, right_button_text,
  28. alignment);
  29. } else {
  30. InitializeRegularTextDialog(title_text, body_text, left_button_text, right_button_text,
  31. alignment);
  32. }
  33. MoveAndResizeWindow();
  34. // TODO (Morph): Remove this when InputInterpreter no longer relies on the HID backend
  35. if (system.IsPoweredOn()) {
  36. input_interpreter = std::make_unique<InputInterpreter>(system);
  37. StartInputThread();
  38. }
  39. }
  40. OverlayDialog::~OverlayDialog() {
  41. StopInputThread();
  42. }
  43. void OverlayDialog::InitializeRegularTextDialog(const QString& title_text, const QString& body_text,
  44. const QString& left_button_text,
  45. const QString& right_button_text,
  46. Qt::Alignment alignment) {
  47. ui->stackedDialog->setCurrentIndex(0);
  48. ui->label_title->setText(title_text);
  49. ui->label_dialog->setText(body_text);
  50. ui->button_cancel->setText(left_button_text);
  51. ui->button_ok_label->setText(right_button_text);
  52. ui->label_dialog->setAlignment(alignment);
  53. if (title_text.isEmpty()) {
  54. ui->label_title->hide();
  55. ui->verticalLayout_2->setStretch(0, 0);
  56. ui->verticalLayout_2->setStretch(1, 219);
  57. ui->verticalLayout_2->setStretch(2, 82);
  58. }
  59. if (left_button_text.isEmpty()) {
  60. ui->button_cancel->hide();
  61. ui->button_cancel->setEnabled(false);
  62. }
  63. if (right_button_text.isEmpty()) {
  64. ui->button_ok_label->hide();
  65. ui->button_ok_label->setEnabled(false);
  66. }
  67. connect(
  68. ui->button_cancel, &QPushButton::clicked, this,
  69. [this](bool) {
  70. StopInputThread();
  71. QDialog::reject();
  72. },
  73. Qt::QueuedConnection);
  74. connect(
  75. ui->button_ok_label, &QPushButton::clicked, this,
  76. [this](bool) {
  77. StopInputThread();
  78. QDialog::accept();
  79. },
  80. Qt::QueuedConnection);
  81. }
  82. void OverlayDialog::InitializeRichTextDialog(const QString& title_text, const QString& body_text,
  83. const QString& left_button_text,
  84. const QString& right_button_text,
  85. Qt::Alignment alignment) {
  86. ui->stackedDialog->setCurrentIndex(1);
  87. ui->label_title_rich->setText(title_text);
  88. ui->text_browser_dialog->setText(body_text);
  89. ui->button_cancel_rich->setText(left_button_text);
  90. ui->button_ok_rich->setText(right_button_text);
  91. // TODO (Morph/Rei): Replace this with something that works better
  92. ui->text_browser_dialog->setAlignment(alignment);
  93. if (title_text.isEmpty()) {
  94. ui->label_title_rich->hide();
  95. ui->verticalLayout_3->setStretch(0, 0);
  96. ui->verticalLayout_3->setStretch(1, 438);
  97. ui->verticalLayout_3->setStretch(2, 82);
  98. }
  99. if (left_button_text.isEmpty()) {
  100. ui->button_cancel_rich->hide();
  101. ui->button_cancel_rich->setEnabled(false);
  102. }
  103. if (right_button_text.isEmpty()) {
  104. ui->button_ok_rich->hide();
  105. ui->button_ok_rich->setEnabled(false);
  106. }
  107. connect(
  108. ui->button_cancel_rich, &QPushButton::clicked, this,
  109. [this](bool) {
  110. StopInputThread();
  111. QDialog::reject();
  112. },
  113. Qt::QueuedConnection);
  114. connect(
  115. ui->button_ok_rich, &QPushButton::clicked, this,
  116. [this](bool) {
  117. StopInputThread();
  118. QDialog::accept();
  119. },
  120. Qt::QueuedConnection);
  121. }
  122. void OverlayDialog::MoveAndResizeWindow() {
  123. const auto pos = parentWidget()->mapToGlobal(parentWidget()->rect().topLeft());
  124. const auto width = static_cast<float>(parentWidget()->width());
  125. const auto height = static_cast<float>(parentWidget()->height());
  126. // High DPI
  127. const float dpi_scale = qApp->screenAt(pos)->logicalDotsPerInch() / 96.0f;
  128. const auto title_text_font_size = BASE_TITLE_FONT_SIZE * (height / BASE_HEIGHT) / dpi_scale;
  129. const auto body_text_font_size =
  130. BASE_FONT_SIZE * (((width / BASE_WIDTH) + (height / BASE_HEIGHT)) / 2.0f) / dpi_scale;
  131. const auto button_text_font_size = BASE_FONT_SIZE * (height / BASE_HEIGHT) / dpi_scale;
  132. QFont title_text_font(QStringLiteral("MS Shell Dlg 2"), title_text_font_size, QFont::Normal);
  133. QFont body_text_font(QStringLiteral("MS Shell Dlg 2"), body_text_font_size, QFont::Normal);
  134. QFont button_text_font(QStringLiteral("MS Shell Dlg 2"), button_text_font_size, QFont::Normal);
  135. if (use_rich_text) {
  136. ui->label_title_rich->setFont(title_text_font);
  137. ui->text_browser_dialog->setFont(body_text_font);
  138. ui->button_cancel_rich->setFont(button_text_font);
  139. ui->button_ok_rich->setFont(button_text_font);
  140. } else {
  141. ui->label_title->setFont(title_text_font);
  142. ui->label_dialog->setFont(body_text_font);
  143. ui->button_cancel->setFont(button_text_font);
  144. ui->button_ok_label->setFont(button_text_font);
  145. }
  146. QDialog::move(pos);
  147. QDialog::resize(width, height);
  148. }
  149. template <Core::HID::NpadButton... T>
  150. void OverlayDialog::HandleButtonPressedOnce() {
  151. const auto f = [this](Core::HID::NpadButton button) {
  152. if (input_interpreter->IsButtonPressedOnce(button)) {
  153. TranslateButtonPress(button);
  154. }
  155. };
  156. (f(T), ...);
  157. }
  158. void OverlayDialog::TranslateButtonPress(Core::HID::NpadButton button) {
  159. QPushButton* left_button = use_rich_text ? ui->button_cancel_rich : ui->button_cancel;
  160. QPushButton* right_button = use_rich_text ? ui->button_ok_rich : ui->button_ok_label;
  161. // TODO (Morph): Handle QTextBrowser text scrolling
  162. // TODO (Morph): focusPrevious/NextChild() doesn't work well with the rich text dialog, fix it
  163. switch (button) {
  164. case Core::HID::NpadButton::A:
  165. case Core::HID::NpadButton::B:
  166. if (left_button->hasFocus()) {
  167. left_button->click();
  168. } else if (right_button->hasFocus()) {
  169. right_button->click();
  170. }
  171. break;
  172. case Core::HID::NpadButton::Left:
  173. case Core::HID::NpadButton::StickLLeft:
  174. focusPreviousChild();
  175. break;
  176. case Core::HID::NpadButton::Right:
  177. case Core::HID::NpadButton::StickLRight:
  178. focusNextChild();
  179. break;
  180. default:
  181. break;
  182. }
  183. }
  184. void OverlayDialog::StartInputThread() {
  185. if (input_thread_running) {
  186. return;
  187. }
  188. input_thread_running = true;
  189. input_thread = std::thread(&OverlayDialog::InputThread, this);
  190. }
  191. void OverlayDialog::StopInputThread() {
  192. input_thread_running = false;
  193. if (input_thread.joinable()) {
  194. input_thread.join();
  195. }
  196. }
  197. void OverlayDialog::InputThread() {
  198. while (input_thread_running) {
  199. input_interpreter->PollInput();
  200. HandleButtonPressedOnce<Core::HID::NpadButton::A, Core::HID::NpadButton::B,
  201. Core::HID::NpadButton::Left, Core::HID::NpadButton::Right,
  202. Core::HID::NpadButton::StickLLeft,
  203. Core::HID::NpadButton::StickLRight>();
  204. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  205. }
  206. }