添加了公交车动画函数
This commit is contained in:
parent
35569b093d
commit
112299b02f
|
@ -6,23 +6,59 @@
|
|||
#define AUTO_BUS_GUI_BUS_WIDGET_H
|
||||
#include "QGraphicsPixmapItem"
|
||||
#include "QTransform"
|
||||
#include "QPropertyAnimation"
|
||||
|
||||
#include "PosPair.h"
|
||||
#include "define.h"
|
||||
|
||||
/**
|
||||
* 显示公交车的对象
|
||||
* 继承了QObject QGraphicsPixmapItem
|
||||
* 用以使用qt的动画框架
|
||||
*/
|
||||
class BusItem: public QObject, public QGraphicsPixmapItem
|
||||
{
|
||||
// 调用这个宏使其qt对象化
|
||||
Q_OBJECT
|
||||
// 注册了一个QPointF类型的变量pos
|
||||
// 读取这个变量通过 pos()函数
|
||||
// 写入这个变量通过 setPos()函数
|
||||
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
|
||||
public:
|
||||
explicit BusItem(const QPixmap& pixmap);
|
||||
};
|
||||
|
||||
|
||||
class BusWidget
|
||||
{
|
||||
public:
|
||||
QGraphicsPixmapItem *item;
|
||||
BusItem *item;
|
||||
|
||||
|
||||
explicit BusWidget();
|
||||
|
||||
~BusWidget();
|
||||
|
||||
void ResetBusPos(PosPair *s);
|
||||
/**
|
||||
* 重置公交车
|
||||
* @param s 位置对数组的头指针
|
||||
* @param num 站点数目
|
||||
*/
|
||||
void ResetBusPos(PosPair *s, int num);
|
||||
|
||||
/**
|
||||
* 开始公交车动画
|
||||
* @param direction 动画的方向
|
||||
* @param duration 动画持续的时间 ms
|
||||
*/
|
||||
void StartAnimation(int direction, int duration);
|
||||
|
||||
private:
|
||||
PosPair *pos_pairs = nullptr;
|
||||
QPropertyAnimation *animation;
|
||||
|
||||
int pos = 0;
|
||||
int node_num = 0;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -2,11 +2,17 @@
|
|||
// Created by ricardo on 2022/6/28.
|
||||
//
|
||||
|
||||
#include "BusWidget.h"
|
||||
#include "moc_BusWidget.cpp"
|
||||
|
||||
BusItem::BusItem(const QPixmap &pixmap) : QGraphicsPixmapItem(pixmap)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
BusWidget::BusWidget()
|
||||
{
|
||||
item = new QGraphicsPixmapItem(QPixmap(":/picture/bus.png"));
|
||||
item = new BusItem(QPixmap(":/picture/bus.png"));
|
||||
animation = new QPropertyAnimation(item, "pos");
|
||||
|
||||
// 设置缩放
|
||||
double bus_scale = 0.1;
|
||||
|
@ -20,9 +26,48 @@ BusWidget::~BusWidget()
|
|||
delete item;
|
||||
}
|
||||
|
||||
void BusWidget::ResetBusPos(PosPair *s)
|
||||
void BusWidget::ResetBusPos(PosPair *s, int num)
|
||||
{
|
||||
pos_pairs = s;
|
||||
pos = 0;
|
||||
node_num = num;
|
||||
|
||||
item->setPos(s[0].GetBusPosX(), s[0].GetBusPosY());
|
||||
item->setPos(pos_pairs[0].GetBusPosX(), pos_pairs[0].GetBusPosY());
|
||||
}
|
||||
|
||||
void BusWidget::StartAnimation(int direction, int duration)
|
||||
{
|
||||
bool is_animate = false;
|
||||
|
||||
animation->setDuration(duration);
|
||||
animation->setStartValue(QPointF(pos_pairs[pos].GetBusPosX(), pos_pairs[pos].GetBusPosY()));
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
case BUS_CLOCK_WISE:
|
||||
pos++;
|
||||
is_animate = true;
|
||||
// 处理环绕一圈的情况
|
||||
if(pos == node_num)
|
||||
{
|
||||
pos = pos - node_num;
|
||||
}
|
||||
break;
|
||||
case BUS_COUNTER_CLOCK_WISE:
|
||||
pos--;
|
||||
is_animate = true;
|
||||
// 处理环绕一圈的情况
|
||||
if(pos == -1)
|
||||
{
|
||||
pos = pos + node_num;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if(is_animate)
|
||||
{
|
||||
animation->setEndValue(QPointF(pos_pairs[pos].GetBusPosX(), pos_pairs[pos].GetBusPosY()));
|
||||
animation->start();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ void SceneManager::SetStopScene(int node_number)
|
|||
}
|
||||
|
||||
// 设置公交车图像
|
||||
bus->ResetBusPos(stop_pos_pairs);
|
||||
bus->ResetBusPos(stop_pos_pairs, node_number);
|
||||
scene->addItem(bus->item);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user