dolphin/Source/Core/DolphinQt/TAS/StickWidget.cpp

93 lines
2.1 KiB
C++
Raw Normal View History

2018-01-27 14:35:02 +01:00
// Copyright 2018 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
2018-07-07 00:40:15 +02:00
#include "DolphinQt/TAS/StickWidget.h"
2018-02-06 12:10:28 +01:00
#include <algorithm>
2018-08-13 14:41:53 +02:00
#include <cmath>
2018-01-27 14:35:02 +01:00
#include <QMouseEvent>
#include <QPainter>
2018-02-06 12:10:28 +01:00
#include "Common/CommonTypes.h"
2018-01-27 14:35:02 +01:00
2018-02-06 12:10:28 +01: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 14:35:02 +01:00
{
setMouseTracking(false);
2018-08-13 14:41:53 +02:00
setToolTip(tr("Left click to set the stick value.\n"
"Right click to re-center it."));
2018-01-27 14:35:02 +01: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);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
2018-01-27 14:35:02 +01:00
painter.setBrush(Qt::white);
painter.drawEllipse(0, 0, width() - 1, height() - 1);
painter.drawLine(0, height() / 2, width(), height() / 2);
painter.drawLine(width() / 2, 0, width() / 2, height());
// convert from value space to widget space
u16 x = (m_x * width()) / m_max_x;
u16 y = height() - (m_y * height()) / m_max_y;
painter.drawLine(width() / 2, height() / 2, x, y);
painter.setBrush(Qt::blue);
int wh_avg = (width() + height()) / 2;
int radius = wh_avg / 30;
painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2);
}
void StickWidget::mousePressEvent(QMouseEvent* event)
{
handleMouseEvent(event);
}
void StickWidget::mouseMoveEvent(QMouseEvent* event)
{
handleMouseEvent(event);
}
void StickWidget::handleMouseEvent(QMouseEvent* event)
{
2018-08-13 14:41:53 +02: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
int new_x = (event->x() * m_max_x) / width();
int new_y = m_max_y - (event->y() * m_max_y) / height();
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 14:35:02 +01:00
emit ChangedX(m_x);
emit ChangedY(m_y);
update();
}