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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
2.5 KiB
C++
Raw Normal View History

2018-02-06 04:10:28 -07:00
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2018-02-06 04:10:28 -07:00
2018-07-06 16:40:15 -06:00
#include "DolphinQt/TAS/IRWidget.h"
2018-02-06 04:10:28 -07:00
#include <algorithm>
2018-08-13 06:41:53 -06:00
#include <cmath>
2018-02-06 04:10:28 -07:00
#include <QMouseEvent>
#include <QPainter>
#include "Common/CommonTypes.h"
constexpr int PADDING = 1;
2018-02-06 04:10:28 -07:00
IRWidget::IRWidget(QWidget* parent) : QWidget(parent)
{
setMouseTracking(false);
2018-08-13 06:41:53 -06:00
setToolTip(tr("Left click to set the IR value.\n"
"Right click to re-center it."));
// If the widget gets too small, it will get deformed.
setMinimumSize(QSize(64, 48));
2018-02-06 04:10:28 -07:00
}
void IRWidget::SetX(u16 x)
{
2018-02-12 13:31:40 -07:00
m_x = std::min(ir_max_x, x);
2018-02-06 04:10:28 -07:00
update();
}
void IRWidget::SetY(u16 y)
{
2018-02-12 13:31:40 -07:00
m_y = std::min(ir_max_y, y);
2018-02-06 04:10:28 -07:00
update();
}
void IRWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
const int w = width() - PADDING * 2;
const int h = height() - PADDING * 2;
2018-02-06 04:10:28 -07:00
painter.setBrush(Qt::white);
painter.drawRect(PADDING, PADDING, w, h);
2018-02-06 04:10:28 -07:00
painter.drawLine(PADDING, PADDING + h / 2, PADDING + w, PADDING + h / 2);
painter.drawLine(PADDING + w / 2, PADDING, PADDING + w / 2, PADDING + h);
2018-02-06 04:10:28 -07:00
// convert from value space to widget space
u16 x = PADDING + ((m_x * w) / ir_max_x);
u16 y = PADDING + (h - (m_y * h) / ir_max_y);
2018-02-06 04:10:28 -07:00
painter.drawLine(PADDING + w / 2, PADDING + h / 2, x, y);
2018-02-06 04:10:28 -07:00
painter.setBrush(Qt::blue);
int wh_avg = (w + h) / 2;
2018-02-06 04:10:28 -07:00
int radius = wh_avg / 30;
painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2);
}
void IRWidget::mousePressEvent(QMouseEvent* event)
{
handleMouseEvent(event);
2019-03-25 17:38:25 -06:00
m_ignore_movement = event->button() == Qt::RightButton;
2018-02-06 04:10:28 -07:00
}
void IRWidget::mouseMoveEvent(QMouseEvent* event)
{
2019-03-25 17:38:25 -06:00
if (!m_ignore_movement)
handleMouseEvent(event);
2018-02-06 04:10:28 -07:00
}
void IRWidget::handleMouseEvent(QMouseEvent* event)
{
u16 prev_x = m_x;
u16 prev_y = m_y;
2018-08-13 06:41:53 -06:00
if (event->button() == Qt::RightButton)
{
m_x = std::round(ir_max_x / 2.);
m_y = std::round(ir_max_y / 2.);
}
else
{
// convert from widget space to value space
int new_x = (event->pos().x() * ir_max_x) / width();
int new_y = ir_max_y - (event->pos().y() * ir_max_y) / height();
2018-08-13 06:41:53 -06:00
m_x = std::max(0, std::min(static_cast<int>(ir_max_x), new_x));
m_y = std::max(0, std::min(static_cast<int>(ir_max_y), new_y));
}
2018-02-06 04:10:28 -07: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-02-06 04:10:28 -07:00
}