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

78 lines
1.7 KiB
C++
Raw Normal View History

2018-02-06 11:10:28 +00:00
// Copyright 2018 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
2018-07-06 22:40:15 +00:00
#include "DolphinQt/TAS/IRWidget.h"
2018-02-06 11:10:28 +00:00
#include <algorithm>
#include <QMouseEvent>
#include <QPainter>
#include "Common/CommonTypes.h"
IRWidget::IRWidget(QWidget* parent) : QWidget(parent)
{
setMouseTracking(false);
}
void IRWidget::SetX(u16 x)
{
2018-02-12 20:31:40 +00:00
m_x = std::min(ir_max_x, x);
2018-02-06 11:10:28 +00:00
update();
}
void IRWidget::SetY(u16 y)
{
2018-02-12 20:31:40 +00:00
m_y = std::min(ir_max_y, y);
2018-02-06 11:10:28 +00:00
update();
}
void IRWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setBrush(Qt::white);
painter.drawRect(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
2018-02-12 20:31:40 +00:00
u16 x = width() - (m_x * width()) / ir_max_x;
u16 y = (m_y * height()) / ir_max_y;
2018-02-06 11:10:28 +00:00
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 IRWidget::mousePressEvent(QMouseEvent* event)
{
handleMouseEvent(event);
}
void IRWidget::mouseMoveEvent(QMouseEvent* event)
{
handleMouseEvent(event);
}
void IRWidget::handleMouseEvent(QMouseEvent* event)
{
// convert from widget space to value space
2018-02-12 20:31:40 +00:00
int new_x = ir_max_x - (event->x() * ir_max_x) / width();
int new_y = (event->y() * ir_max_y) / height();
2018-02-06 11:10:28 +00:00
2018-02-12 20:31:40 +00: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 11:10:28 +00:00
emit ChangedX(m_x);
emit ChangedY(m_y);
update();
}