添加了公交车动画函数

This commit is contained in:
jackfiled 2022-06-28 12:23:55 +08:00
parent 35569b093d
commit 112299b02f
3 changed files with 88 additions and 7 deletions

View File

@ -6,23 +6,59 @@
#define AUTO_BUS_GUI_BUS_WIDGET_H #define AUTO_BUS_GUI_BUS_WIDGET_H
#include "QGraphicsPixmapItem" #include "QGraphicsPixmapItem"
#include "QTransform" #include "QTransform"
#include "QPropertyAnimation"
#include "PosPair.h" #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 class BusWidget
{ {
public: public:
QGraphicsPixmapItem *item; BusItem *item;
explicit BusWidget(); explicit BusWidget();
~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: private:
PosPair *pos_pairs = nullptr; PosPair *pos_pairs = nullptr;
QPropertyAnimation *animation;
int pos = 0;
int node_num = 0;
}; };

View File

@ -2,11 +2,17 @@
// Created by ricardo on 2022/6/28. // Created by ricardo on 2022/6/28.
// //
#include "BusWidget.h" #include "moc_BusWidget.cpp"
BusItem::BusItem(const QPixmap &pixmap) : QGraphicsPixmapItem(pixmap)
{
}
BusWidget::BusWidget() 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; double bus_scale = 0.1;
@ -20,9 +26,48 @@ BusWidget::~BusWidget()
delete item; delete item;
} }
void BusWidget::ResetBusPos(PosPair *s) void BusWidget::ResetBusPos(PosPair *s, int num)
{ {
pos_pairs = s; 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();
}
} }

View File

@ -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); scene->addItem(bus->item);
} }