overlay_dialog.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include <memory>
  6. #include <thread>
  7. #include <QDialog>
  8. #include "common/common_types.h"
  9. class InputInterpreter;
  10. namespace Core {
  11. class System;
  12. }
  13. namespace Core::HID {
  14. enum class NpadButton : u64;
  15. }
  16. namespace Ui {
  17. class OverlayDialog;
  18. }
  19. /**
  20. * An OverlayDialog is an interactive dialog that accepts controller input (while a game is running)
  21. * This dialog attempts to replicate the look and feel of the Nintendo Switch's overlay dialogs and
  22. * provide some extra features such as embedding HTML/Rich Text content in a QTextBrowser.
  23. * The OverlayDialog provides 2 modes: one to embed regular text into a QLabel and another to embed
  24. * HTML/Rich Text content into a QTextBrowser.
  25. */
  26. class OverlayDialog final : public QDialog {
  27. Q_OBJECT
  28. public:
  29. explicit OverlayDialog(QWidget* parent, Core::System& system, const QString& title_text,
  30. const QString& body_text, const QString& left_button_text,
  31. const QString& right_button_text,
  32. Qt::Alignment alignment = Qt::AlignCenter, bool use_rich_text_ = false);
  33. ~OverlayDialog() override;
  34. private:
  35. /**
  36. * Initializes a text dialog with a QLabel storing text.
  37. * Only use this for short text as the dialog buttons would be squashed with longer text.
  38. *
  39. * @param title_text Title text to be displayed
  40. * @param body_text Main text to be displayed
  41. * @param left_button_text Left button text. If empty, the button is hidden and disabled
  42. * @param right_button_text Right button text. If empty, the button is hidden and disabled
  43. * @param alignment Main text alignment
  44. */
  45. void InitializeRegularTextDialog(const QString& title_text, const QString& body_text,
  46. const QString& left_button_text,
  47. const QString& right_button_text, Qt::Alignment alignment);
  48. /**
  49. * Initializes a text dialog with a QTextBrowser storing text.
  50. * This is ideal for longer text or rich text content. A scrollbar is shown for longer text.
  51. *
  52. * @param title_text Title text to be displayed
  53. * @param body_text Main text to be displayed
  54. * @param left_button_text Left button text. If empty, the button is hidden and disabled
  55. * @param right_button_text Right button text. If empty, the button is hidden and disabled
  56. * @param alignment Main text alignment
  57. */
  58. void InitializeRichTextDialog(const QString& title_text, const QString& body_text,
  59. const QString& left_button_text, const QString& right_button_text,
  60. Qt::Alignment alignment);
  61. /// Moves and resizes the dialog to be fully overlayed on top of the parent window.
  62. void MoveAndResizeWindow();
  63. /**
  64. * Handles button presses and converts them into keyboard input.
  65. *
  66. * @tparam HIDButton The list of buttons that can be converted into keyboard input.
  67. */
  68. template <Core::HID::NpadButton... T>
  69. void HandleButtonPressedOnce();
  70. /**
  71. * Translates a button press to focus or click either the left or right buttons.
  72. *
  73. * @param button The button press to process.
  74. */
  75. void TranslateButtonPress(Core::HID::NpadButton button);
  76. void StartInputThread();
  77. void StopInputThread();
  78. /// The thread where input is being polled and processed.
  79. void InputThread();
  80. void keyPressEvent(QKeyEvent* e) override;
  81. std::unique_ptr<Ui::OverlayDialog> ui;
  82. bool use_rich_text;
  83. std::unique_ptr<InputInterpreter> input_interpreter;
  84. std::thread input_thread;
  85. std::atomic<bool> input_thread_running{};
  86. };