Files
gcs-nf/App/ToolsUI/RemoteControl/joystick.cpp
T
2021-04-29 09:54:21 +08:00

300 lines
6.6 KiB
C++

#include "joystick.h"
#include "QApplication"
JoyStick::JoyStick(QWidget *parent) :
QWidget(parent)
{
setMinimumSize(QSize(10, 10));
resize(200, 200);
setFocus();
m_dir[0] = NORMAL;
m_dir[1] = NORMAL;
}
JoyStick::~JoyStick()
{
}
int16_t JoyStick::Limit(int16_t pos, int16_t min, int16_t max)
{
return ( pos <= min)?(min):((pos >= max)?(max):(pos));
}
//输入范围是800~2200
void JoyStick::setValue(uint16_t ch, uint16_t value)//输入跟随
{
if((value > m_max[ch])||(value < m_min[ch]))
{
emit error(Rate_Error);
//return;
if(value > m_max[ch])
{
value = m_max[ch];
}
if(value < m_min[ch])
{
value = m_min[ch];
}
}
m_channel[ch] = value;
//qDebug() << "setvalue" << m_channel[0] << m_channel[1];
m_pos[ch] = m_dir[ch] *(float)((m_channel[ch] - m_min[ch]) * 170.0f)/(m_max[ch] - m_min[ch]) - m_dir[ch] * 85.0f;
update();
}
void JoyStick::setRecovery(uint16_t ch, bool state)
{
m_recovery[ch] = state;
update();
}
void JoyStick::setTrim(uint16_t ch, int16_t value)
{
m_trim[ch] = Limit(value,-30,30);
update();
}
void JoyStick::setMaximum(uint16_t ch, uint16_t value)
{
if(value < m_min[ch])
{
emit error(Max_Error);
return;
}
m_max[ch] = Limit(value,200,2800);
update();
}
void JoyStick::setMinimum(uint16_t ch, uint16_t value)
{
if(value > m_max[ch])
{
emit error(Min_Error);
return;
}
m_min[ch] = Limit(value,200,2800);
update();
}
void JoyStick::setDirection(uint16_t ch, int8_t value)
{
m_dir[ch] = Limit(value,REVERSE,NORMAL);
update();
}
void JoyStick::mouseReleaseEvent(QMouseEvent *e)
{
//Q_UNUSED(e);
if(m_recovery[0] == true && !(e->modifiers() & Qt::ShiftModifier) )
{
m_pos[0] = m_trim[0];
}
else
{
m_pos[0] = Limit(e->x() - width()/2,-85,85) + m_trim[0];
}
if(m_recovery[1] == true && !(e->modifiers() & Qt::ShiftModifier) )
{
m_pos[1] = m_trim[1];
}
else
{
m_pos[1] = Limit(e->y() - height()/2,-85,85) + m_trim[1];
}
//qDebug() << m_pos[0] << m_pos[1];
if(m_dir[0] == NORMAL)
{
m_channel[0] = m_min[0] + (m_max[0] - m_min[0])/170.0f * (85 + m_pos[0]);
}
else
{
m_channel[0] = 3000 - (m_min[0] + (m_max[0] - m_min[0])/170.0f * (85 + m_pos[0]));
}
if(m_dir[1] == NORMAL)
{
m_channel[1] = m_min[1] + (m_max[1] - m_min[1])/170.0f * (85 + m_pos[1]);
}
else
{
m_channel[1] = 3000 - (m_min[1] + (m_max[1] - m_min[1])/170.0f * (85 + m_pos[1]));
}
emit positionChange(m_channel[0],m_channel[1]);
update();
}
void JoyStick::mousePressEvent(QMouseEvent *e)
{
m_pos[0] = Limit(e->x() - width()/2,-85,85) + m_trim[0];
m_pos[1] = Limit(e->y() - height()/2,-85,85) + m_trim[1];
//qDebug() << pos[0] << pos[1] << e;
if(m_dir[0] == NORMAL)
{
m_channel[0] = m_min[0] + (m_max[0] - m_min[0])/170.0f * (85 + m_pos[0]);
}
else
{
m_channel[0] = 3000 - (m_min[0] + (m_max[0] - m_min[0])/170.0f * (85 + m_pos[0]));
}
if(m_dir[1] == NORMAL)
{
m_channel[1] = m_min[1] + (m_max[1] - m_min[1])/170.0f * (85 + m_pos[1]);
}
else
{
m_channel[1] = 3000 - (m_min[1] + (m_max[1] - m_min[1])/170.0f * (85 + m_pos[1]));
}
emit positionChange(m_channel[0],m_channel[1]);
update();
}
void JoyStick::mouseMoveEvent(QMouseEvent *e)
{
m_pos[0] = Limit(e->x() - width()/2,-85,85) + m_trim[0];
m_pos[1] = Limit(e->y() - height()/2,-85,85) + m_trim[1];
//qDebug() << m_pos[0] << m_pos[1] << e;
if(m_dir[0] == NORMAL)
{
m_channel[0] = m_min[0] + (m_max[0] - m_min[0])/170.0f * (85 + m_pos[0]);
}
else
{
m_channel[0] = 3000 - (m_min[0] + (m_max[0] - m_min[0])/170.0f * (85 + m_pos[0]));
}
if(m_dir[1] == NORMAL)
{
m_channel[1] = m_min[1] + (m_max[1] - m_min[1])/170.0f * (85 + m_pos[1]);
}
else
{
m_channel[1] = 3000 - (m_min[1] + (m_max[1] - m_min[1])/170.0f * (85 + m_pos[1]));
}
emit positionChange(m_channel[0],m_channel[1]);
update();
}
void JoyStick::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); /* 使用反锯齿(如果可用) */
painter.translate(width() / 2, height() / 2); /* 坐标变换为窗体中心 */
int side = qMin(width(), height());
painter.scale(side / 1200.0, side / 1200.0); /* 比例缩放 */
painter.setPen(Qt::NoPen);
drawStick(&painter);
}
void JoyStick::drawStick(QPainter *painter)
{
//qDebug() << m_pos[0] << m_pos[1];
QPen pen;
QRectF First(-500,-500,1000,1000);
painter->save();
painter->setBrush(QColor("#212121"));
painter->drawRect(First);
painter->restore();
QRectF Rect(-500,-500,1000,1000);
pen.setColor(Qt::black);
pen.setWidth(30);
painter->save();
painter->setPen(pen);
painter->drawRect(Rect);
painter->restore();
pen.setColor(Qt::black);
pen.setWidth(20);
pen.setStyle(Qt::DashLine);
painter->save();
painter->setPen(pen);
painter->drawEllipse(Rect);
pen.setWidth(10);
pen.setColor(QColor("#FFFFFF"));
painter->setPen(pen);
for(int i = 0;i<5;i++)
{
painter->drawLine(QPoint(-450,i*100),QPoint(-500,i*100));
painter->drawLine(QPoint(-450,i*-100),QPoint(-500,i*-100));
painter->drawLine(QPoint( 450,i*100),QPoint( 500,i*100));
painter->drawLine(QPoint( 450,i*-100),QPoint( 500,i*-100));
painter->drawLine(QPoint(i*100,-450),QPoint(i*100,-500));
painter->drawLine(QPoint(i*-100,-450),QPoint(i*-100,-500));
painter->drawLine(QPoint(i*100, 450),QPoint(i*100, 500));
painter->drawLine(QPoint(i*-100, 450),QPoint(i*-100, 500));
}
painter->restore();
painter->save();
//画中间红线
pen.setColor(QColor("#DC143C"));
pen.setWidth(5);
pen.setStyle(Qt::DashLine);
painter->setPen(pen);
// 画横线
painter->drawLine(QPoint(-500,m_pos[1]*6),QPoint(500,m_pos[1]*6));
//画竖线
painter->drawLine(QPoint(m_pos[0]*6,-500),QPoint(m_pos[0]*6,500));
painter->restore();
//===
painter->save();
painter->translate(m_pos[0]*6,m_pos[1]*6);
//画中间的球
painter->setBrush(QColor("#9E9E9E"));
painter->drawEllipse(-90,-90,180,180);
//====
painter->setBrush(QColor("#1F1F1F"));
painter->drawEllipse(-75,-75,150,150);
//====
painter->setBrush(QColor("#FFFAFA"));
painter->drawEllipse(-50,-50,100,100);
painter->restore();
}