| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/TAS/TASCheckBox.h" |
| 5 | |
| 6 | #include <QMouseEvent> |
| 7 | |
| 8 | #include "Core/Movie.h" |
| 9 | #include "Core/System.h" |
| 10 | #include "DolphinQt/QtUtils/QueueOnObject.h" |
| 11 | #include "DolphinQt/TAS/TASInputWindow.h" |
| 12 | |
| 13 | TASCheckBox::TASCheckBox(const QString& text, TASInputWindow* parent) |
| 14 | : QCheckBox(text, parent), m_parent(parent) |
| 15 | { |
| 16 | setTristate(true); |
| 17 | |
| 18 | #if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) |
| 19 | connect(this, &TASCheckBox::checkStateChanged, this, &TASCheckBox::OnUIValueChanged); |
| 20 | #else |
| 21 | connect(this, &TASCheckBox::stateChanged, this, &TASCheckBox::OnUIValueChanged); |
| 22 | #endif |
| 23 | } |
| 24 | |
| 25 | bool TASCheckBox::GetValue() const |
| 26 | { |
| 27 | Qt::CheckState check_state = static_cast<Qt::CheckState>(m_state.GetValue()); |
| 28 | |
| 29 | if (check_state == Qt::PartiallyChecked) |
| 30 | { |
| 31 | const u64 frames_elapsed = |
| 32 | Core::System::GetInstance().GetMovie().GetCurrentFrame() - m_frame_turbo_started; |
| 33 | return static_cast<int>(frames_elapsed % m_turbo_total_frames) < m_turbo_press_frames; |
| 34 | } |
| 35 | |
| 36 | return check_state != Qt::Unchecked; |
| 37 | } |
| 38 | |
| 39 | void TASCheckBox::OnControllerValueChanged(bool new_value) |
| 40 | { |
| 41 | if (m_state.OnControllerValueChanged(new_value ? Qt::Checked : Qt::Unchecked)) |
| 42 | QueueOnObject(this, &TASCheckBox::ApplyControllerValueChange); |
| 43 | } |
| 44 | |
| 45 | void TASCheckBox::mousePressEvent(QMouseEvent* event) |
| 46 | { |
| 47 | if (event->button() != Qt::RightButton) |
| 48 | { |
| 49 | setChecked(!isChecked()); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | if (checkState() == Qt::PartiallyChecked) |
| 54 | { |
| 55 | setCheckState(Qt::Unchecked); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | m_frame_turbo_started = Core::System::GetInstance().GetMovie().GetCurrentFrame(); |
| 60 | m_turbo_press_frames = m_parent->GetTurboPressFrames(); |
| 61 | m_turbo_total_frames = m_turbo_press_frames + m_parent->GetTurboReleaseFrames(); |
| 62 | setCheckState(Qt::PartiallyChecked); |
| 63 | } |
| 64 | |
| 65 | #if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) |
| 66 | void TASCheckBox::OnUIValueChanged(Qt::CheckState new_value) |
| 67 | #else |
| 68 | void TASCheckBox::OnUIValueChanged(int new_value) |
| 69 | #endif |
| 70 | { |
| 71 | m_state.OnUIValueChanged(new_value); |
| 72 | } |
| 73 | |
| 74 | void TASCheckBox::ApplyControllerValueChange() |
| 75 | { |
| 76 | const QSignalBlocker blocker(this); |
| 77 | setCheckState(static_cast<Qt::CheckState>(m_state.ApplyControllerValueChange())); |
| 78 | } |