2018-01-27 13:35:02 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-01-27 13:35:02 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/TAS/StickWidget.h"
|
2018-02-06 11:10:28 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-08-13 12:41:53 +00:00
|
|
|
#include <cmath>
|
2018-01-27 13:35:02 +00:00
|
|
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
|
2018-02-06 11:10:28 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2018-01-27 13:35:02 +00:00
|
|
|
|
2020-02-17 14:07:18 +00:00
|
|
|
constexpr int PADDING = 1;
|
|
|
|
|
2018-02-06 11:10:28 +00:00
|
|
|
StickWidget::StickWidget(QWidget* parent, u16 max_x, u16 max_y)
|
|
|
|
: QWidget(parent), m_max_x(max_x), m_max_y(max_y)
|
2018-01-27 13:35:02 +00:00
|
|
|
{
|
|
|
|
setMouseTracking(false);
|
2018-08-13 12:41:53 +00:00
|
|
|
setToolTip(tr("Left click to set the stick value.\n"
|
|
|
|
"Right click to re-center it."));
|
2020-02-17 14:07:18 +00:00
|
|
|
|
|
|
|
// If the widget gets too small, it will get deformed.
|
|
|
|
setMinimumSize(QSize(64, 64));
|
2018-01-27 13:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickWidget::SetX(u16 x)
|
|
|
|
{
|
|
|
|
m_x = std::min(m_max_x, x);
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickWidget::SetY(u16 y)
|
|
|
|
{
|
|
|
|
m_y = std::min(m_max_y, y);
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickWidget::paintEvent(QPaintEvent* event)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
|
2018-08-13 13:05:30 +00:00
|
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
|
|
|
|
|
2020-02-17 14:07:18 +00:00
|
|
|
const int diameter = std::min(width(), height()) - PADDING * 2;
|
|
|
|
|
2021-09-20 13:49:51 +00:00
|
|
|
// inscribe the StickWidget inside a square
|
|
|
|
painter.fillRect(PADDING, PADDING, diameter, diameter, Qt::lightGray);
|
|
|
|
|
2018-01-27 13:35:02 +00:00
|
|
|
painter.setBrush(Qt::white);
|
2020-02-17 14:07:18 +00:00
|
|
|
painter.drawEllipse(PADDING, PADDING, diameter, diameter);
|
2018-01-27 13:35:02 +00:00
|
|
|
|
2020-02-17 14:07:18 +00:00
|
|
|
painter.drawLine(PADDING, PADDING + diameter / 2, PADDING + diameter, PADDING + diameter / 2);
|
|
|
|
painter.drawLine(PADDING + diameter / 2, PADDING, PADDING + diameter / 2, PADDING + diameter);
|
2018-01-27 13:35:02 +00:00
|
|
|
|
|
|
|
// convert from value space to widget space
|
2020-02-17 14:07:18 +00:00
|
|
|
u16 x = PADDING + ((m_x * diameter) / m_max_x);
|
|
|
|
u16 y = PADDING + (diameter - (m_y * diameter) / m_max_y);
|
2018-01-27 13:35:02 +00:00
|
|
|
|
2020-02-17 14:07:18 +00:00
|
|
|
painter.drawLine(PADDING + diameter / 2, PADDING + diameter / 2, x, y);
|
2018-01-27 13:35:02 +00:00
|
|
|
|
|
|
|
painter.setBrush(Qt::blue);
|
2020-02-17 14:07:18 +00:00
|
|
|
int neutral_radius = diameter / 30;
|
|
|
|
painter.drawEllipse(x - neutral_radius, y - neutral_radius, neutral_radius * 2,
|
|
|
|
neutral_radius * 2);
|
2018-01-27 13:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickWidget::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
handleMouseEvent(event);
|
2019-03-25 23:38:25 +00:00
|
|
|
m_ignore_movement = event->button() == Qt::RightButton;
|
2018-01-27 13:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickWidget::mouseMoveEvent(QMouseEvent* event)
|
|
|
|
{
|
2019-03-25 23:38:25 +00:00
|
|
|
if (!m_ignore_movement)
|
|
|
|
handleMouseEvent(event);
|
2018-01-27 13:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickWidget::handleMouseEvent(QMouseEvent* event)
|
|
|
|
{
|
2023-04-06 05:47:42 +00:00
|
|
|
u16 prev_x = m_x;
|
|
|
|
u16 prev_y = m_y;
|
|
|
|
|
2018-08-13 12:41:53 +00:00
|
|
|
if (event->button() == Qt::RightButton)
|
|
|
|
{
|
|
|
|
m_x = std::round(m_max_x / 2.);
|
|
|
|
m_y = std::round(m_max_y / 2.);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// convert from widget space to value space
|
2021-04-25 00:18:28 +00:00
|
|
|
int new_x = (event->pos().x() * m_max_x) / width();
|
|
|
|
int new_y = m_max_y - (event->pos().y() * m_max_y) / height();
|
2018-08-13 12:41:53 +00:00
|
|
|
|
|
|
|
m_x = std::max(0, std::min(static_cast<int>(m_max_x), new_x));
|
|
|
|
m_y = std::max(0, std::min(static_cast<int>(m_max_y), new_y));
|
|
|
|
}
|
2018-01-27 13:35:02 +00:00
|
|
|
|
2023-04-06 05:47:42 +00:00
|
|
|
bool changed = false;
|
|
|
|
if (prev_x != m_x)
|
|
|
|
{
|
|
|
|
emit ChangedX(m_x);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (prev_y != m_y)
|
|
|
|
{
|
|
|
|
emit ChangedY(m_y);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
update();
|
2018-01-27 13:35:02 +00:00
|
|
|
}
|