| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/TAS/IRWidget.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cmath> |
| 8 | |
| 9 | #include <QMouseEvent> |
| 10 | #include <QPainter> |
| 11 | |
| 12 | #include "Common/CommonTypes.h" |
| 13 | |
| 14 | constexpr int PADDING = 1; |
| 15 | |
| 16 | IRWidget::IRWidget(QWidget* parent) : QWidget(parent) |
| 17 | { |
| 18 | setMouseTracking(false); |
| 19 | setToolTip(tr("Left click to set the IR value.\n" |
| 20 | "Right click to re-center it.")); |
| 21 | |
| 22 | // If the widget gets too small, it will get deformed. |
| 23 | setMinimumSize(QSize(64, 48)); |
| 24 | } |
| 25 | |
| 26 | QSize IRWidget::sizeHint() const |
| 27 | { |
| 28 | return QSize(128, 96); |
| 29 | } |
| 30 | |
| 31 | void IRWidget::SetX(u16 x) |
| 32 | { |
| 33 | m_x = std::min(IR_MAX_X, x); |
| 34 | |
| 35 | update(); |
| 36 | } |
| 37 | |
| 38 | void IRWidget::SetY(u16 y) |
| 39 | { |
| 40 | m_y = std::min(IR_MAX_Y, y); |
| 41 | |
| 42 | update(); |
| 43 | } |
| 44 | |
| 45 | void IRWidget::paintEvent(QPaintEvent* event) |
| 46 | { |
| 47 | QPainter painter(this); |
| 48 | |
| 49 | painter.setRenderHint(QPainter::Antialiasing, true); |
| 50 | painter.setRenderHint(QPainter::SmoothPixmapTransform, true); |
| 51 | |
| 52 | const int w = width() - PADDING * 2; |
| 53 | const int h = height() - PADDING * 2; |
| 54 | |
| 55 | painter.setBrush(Qt::white); |
| 56 | painter.drawRect(PADDING, PADDING, w, h); |
| 57 | |
| 58 | painter.drawLine(PADDING, PADDING + h / 2, PADDING + w, PADDING + h / 2); |
| 59 | painter.drawLine(PADDING + w / 2, PADDING, PADDING + w / 2, PADDING + h); |
| 60 | |
| 61 | // convert from value space to widget space |
| 62 | u16 x = PADDING + ((m_x * w) / IR_MAX_X); |
| 63 | u16 y = PADDING + (h - (m_y * h) / IR_MAX_Y); |
| 64 | |
| 65 | painter.drawLine(PADDING + w / 2, PADDING + h / 2, x, y); |
| 66 | |
| 67 | painter.setBrush(Qt::blue); |
| 68 | int wh_avg = (w + h) / 2; |
| 69 | int radius = wh_avg / 30; |
| 70 | painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2); |
| 71 | } |
| 72 | |
| 73 | void IRWidget::mousePressEvent(QMouseEvent* event) |
| 74 | { |
| 75 | handleMouseEvent(event); |
| 76 | m_ignore_movement = event->button() == Qt::RightButton; |
| 77 | } |
| 78 | |
| 79 | void IRWidget::mouseMoveEvent(QMouseEvent* event) |
| 80 | { |
| 81 | if (!m_ignore_movement) |
| 82 | handleMouseEvent(event); |
| 83 | } |
| 84 | |
| 85 | void IRWidget::handleMouseEvent(QMouseEvent* event) |
| 86 | { |
| 87 | u16 prev_x = m_x; |
| 88 | u16 prev_y = m_y; |
| 89 | |
| 90 | if (event->button() == Qt::RightButton) |
| 91 | { |
| 92 | m_x = std::round(IR_MAX_X / 2.); |
| 93 | m_y = std::round(IR_MAX_Y / 2.); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | // convert from widget space to value space |
| 98 | int new_x = (event->pos().x() * IR_MAX_X) / width(); |
| 99 | int new_y = IR_MAX_Y - (event->pos().y() * IR_MAX_Y) / height(); |
| 100 | |
| 101 | m_x = std::max(0, std::min(static_cast<int>(IR_MAX_X), new_x)); |
| 102 | m_y = std::max(0, std::min(static_cast<int>(IR_MAX_Y), new_y)); |
| 103 | } |
| 104 | |
| 105 | bool changed = false; |
| 106 | if (prev_x != m_x) |
| 107 | { |
| 108 | emit ChangedX(m_x); |
| 109 | changed = true; |
| 110 | } |
| 111 | if (prev_y != m_y) |
| 112 | { |
| 113 | emit ChangedY(m_y); |
| 114 | changed = true; |
| 115 | } |
| 116 | |
| 117 | if (changed) |
| 118 | update(); |
| 119 | } |