代码拉取完成,页面将自动刷新
同步操作将从 JiuZhouCangLan/Joystick 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "joystick.h"
#include <QPaintEvent>
#include <QPainter>
#include <QRadialGradient>
#include <QtDebug>
#include <cmath>
#define SPACE 20
#define PI 3.1415926
JoyStick::JoyStick(QWidget *parent): QLabel(parent)
{
}
QPoint JoyStick::getPos()
{
return lastPos;
}
void JoyStick::setRange(uint maxValue)
{
range = static_cast<int>(maxValue);
}
void JoyStick::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QRect rect = this->rect();
// 绘制底圆
QPen pen;
pen.setWidth(1);
pen.setStyle(Qt::SolidLine);
painter.setPen(pen);
painter.drawEllipse(rect.center(), baseRadius, baseRadius);
// 绘制摇杆
pen.setWidth(2);
painter.setPen(pen);
QBrush brush;
brush.setColor(Qt::black);
brush.setStyle(Qt::Dense7Pattern);
painter.setBrush(brush);
painter.drawEllipse(joyPos, joyRadius, joyRadius);
// 绘制摇杆帽光影
QRadialGradient gradient(joyPos, edgeLength / 2, joyPos);
gradient.setColorAt(0, Qt::white);
gradient.setColorAt(1, QColor(50, 50, 50, 100));
painter.setBrush(QBrush(gradient));
painter.setPen(Qt::NoPen);
painter.drawEllipse(joyPos, joyRadius, joyRadius);
}
QSize JoyStick::sizeHint() const
{
return QSize(120, 120);
}
void JoyStick::mouseMoveEvent(QMouseEvent *event)
{
setValue(event->pos());
QLabel::mouseMoveEvent(event);
}
void JoyStick::mouseReleaseEvent(QMouseEvent *event)
{
setValue(rect().center());
QLabel::mouseReleaseEvent(event);
}
void JoyStick::showEvent(QShowEvent *event)
{
QRect rect = this->rect();
setMask(QRegion(rect, QRegion::Ellipse));
edgeLength = qMin(rect.width(), rect.height());
baseRadius = (edgeLength - SPACE * 2) / 2;
joyRadius = baseRadius * 2 / 3;
int capWidth = joyRadius * 2 / 3;
capWidth = qMin(capWidth, SPACE);
stickRadius = joyRadius - capWidth;
joyPos = rect.center();
QLabel::showEvent(event);
}
void JoyStick::mousePressEvent(QMouseEvent *event)
{
setValue(event->pos());
QLabel::mousePressEvent(event);
}
double getDistance(QPoint p1, QPoint p2)
{
int x = p1.x() - p2.x();
int y = p1.y() - p2.y();
return sqrt(x * x + y * y);
}
void JoyStick::setValue(QPoint pos)
{
auto center = this->rect().center();
double distance = getDistance(pos, rect().center());
double availableR = baseRadius - stickRadius;
QPoint offset;
offset.setX(pos.x() - center.x());
offset.setY(pos.y() - center.y());
if(distance < availableR) {
joyPos = pos;
} else {
double cos = offset.x() / distance;
double sin = offset.y() / distance;
offset.setX(static_cast<int>(availableR * cos));
offset.setY(static_cast<int>(availableR * sin));
joyPos = center + offset;
}
if(range < 100) {
offset.setX(static_cast<int>(offset.x() * 100 / availableR));
offset.setY(-static_cast<int>(offset.y() * 100 / availableR));
offset = offset * range / 100;
} else {
offset.setX(static_cast<int>(offset.x() * range / availableR));
offset.setY(-static_cast<int>(offset.y() * range / availableR));
}
if(offset != lastPos) {
emit posChanged(offset);
lastPos = offset;
}
update();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。