implement joystick item switching

This commit is contained in:
uvos 2022-02-02 13:36:30 +01:00
parent 00bfa72455
commit 7a61dc9368
8 changed files with 61 additions and 41 deletions

View File

@ -1,6 +1,8 @@
#include "itemstore.h"
#include <QJsonArray>
#include <QDebug>
#include "train.h"
#include "../trainjs.h"
ItemStore::ItemStore(QObject *parent): QObject(parent)
{
@ -19,11 +21,54 @@ void ItemStore::addItem(std::shared_ptr<Item> item)
if(!mached)
{
items_.push_back(std::shared_ptr<Item>(item));
if(dynamic_cast<Train*>(item.get()))
{
std::vector<std::shared_ptr<TrainJs>> joysticks = TrainJs::getJsDevices();
for(auto joystick: joysticks)
{
if(!joystick->itemIsSet())
{
joystick->setItem(item);
connect(joystick.get(), &TrainJs::reqNewItem, this, &ItemStore::jsReqNewItem);
break;
}
}
}
itemAdded(std::weak_ptr<Item>(items_.back()));
}
qDebug()<<"Got item: "<<item->id()<<" matched: "<<mached;
}
void ItemStore::jsReqNewItem()
{
if(items_.empty())
return;
qDebug()<<__func__<<"new item requested";
std::vector<std::shared_ptr<TrainJs>> joysticks = TrainJs::getJsDevices();
for(auto joystick: joysticks)
{
if(joystick->getWantsNewItem())
{
std::shared_ptr<Item> oldItem = joystick->getItem().lock();
for(size_t i = 0; i < items_.size(); ++i)
{
if(!oldItem || *items_[i] == *oldItem)
{
if(i+1 < items_.size())
joystick->setItem(items_[i+1]);
else
joystick->setItem(items_[0]);
break;
}
}
}
}
}
void ItemStore::addItems(const std::vector<std::shared_ptr<Item>>& itemIn)
{
for(unsigned j = 0; j < itemIn.size(); j++)

View File

@ -20,6 +20,10 @@ public:
void clear();
private slots:
void jsReqNewItem();
signals:
void itemDeleted(ItemData item);

View File

@ -14,5 +14,5 @@ void Turnout::setValue(int8_t value)
{
Item::setValue(value);
if(micro)
micro->tunoutSetDirection(turnoutId_, value);
micro->tunoutSetDirection(turnoutId_, value > 0);
}

View File

@ -5,13 +5,9 @@
TrainJs::TrainJs(int id, int axis): id_(id), axis_(axis)
{
longpressTimer_.setSingleShot(true);
longpressTimer_.setInterval(LONGPRESS_TIME);
QJoysticks* jsmanager = QJoysticks::getInstance();
connect(jsmanager, &QJoysticks::axisChanged, this, &TrainJs::axisChanged);
connect(jsmanager, &QJoysticks::buttonChanged, this, &TrainJs::buttonChanged);
connect(&longpressTimer_, &QTimer::timeout, this, &TrainJs::longpressTimeout);
}
TrainJs::~TrainJs()
@ -53,6 +49,8 @@ std::weak_ptr<Item> TrainJs::getItem()
void TrainJs::setItem(std::weak_ptr<Item> item)
{
item_ = item;
wantsNewItem = false;
inhibitUntillZero = true;
}
bool TrainJs::itemIsSet()
@ -67,11 +65,14 @@ void TrainJs::axisChanged(const int id, const int axis, const qreal value)
if(std::shared_ptr<Item> workitem = item_.lock())
{
int8_t newValue = value*14;
if(newValue != workitem->getValue())
if(newValue == 0)
inhibitUntillZero = false;
if(newValue != workitem->getValue() && !inhibitUntillZero)
workitem->setValue(newValue);
}
else
{
wantsNewItem = true;
reqNewItem();
}
}
@ -85,32 +86,14 @@ void TrainJs::buttonChanged(const int id, const int button, const bool pressed)
if(pressed)
{
handleRelease = true;
longpressTimer_.start();
}
else if(handleRelease)
{
longpressTimer_.stop();
if(std::shared_ptr<Item> workitem = item_.lock())
{
if(Train* train = dynamic_cast<Train*>(workitem.get()))
{
if(train->getValue() == 0)
train->reverse();
}
}
else
{
reqNewItem();
}
wantsNewItem = true;
reqNewItem();
handleRelease = false;
}
}
}
void TrainJs::longpressTimeout()
{
reqNewItem();
handleRelease = false;
}

View File

@ -13,15 +13,15 @@ class TrainJs: public QObject
Q_OBJECT
private:
static constexpr char JOYSTICK_NAME[] = "UVOS UsbJoy";
static constexpr int LONGPRESS_TIME = 500;
inline static std::vector<std::shared_ptr<TrainJs>> js_;
int id_ = -1;
int axis_ = -1;
QTimer longpressTimer_;
bool handleRelease = false;
bool wantsNewItem = true;
bool inhibitUntillZero = true;
std::weak_ptr<Item> item_;
TrainJs(int id, int axis);
@ -30,7 +30,6 @@ private:
private slots:
void axisChanged(const int id, const int axis, const qreal value);
void buttonChanged(const int id, const int button, const bool pressed);
void longpressTimeout();
signals:
void reqNewItem();
@ -42,6 +41,7 @@ public:
std::weak_ptr<Item> getItem();
void setItem(std::weak_ptr<Item>);
bool itemIsSet();
bool getWantsNewItem() {return wantsNewItem;}
};
#endif // TRAINJS_H

View File

@ -38,16 +38,6 @@ void ItemScrollBox::addItem(std::weak_ptr<Item> item)
widgets_.back()->setShortcuts(QKeySequence(Qt::Key_R), QKeySequence(Qt::Key_F), QKeySequence(Qt::Key_V));
else if(train->getTrainId() == 4)
widgets_.back()->setShortcuts(QKeySequence(Qt::Key_T), QKeySequence(Qt::Key_G), QKeySequence(Qt::Key_B));
std::vector<std::shared_ptr<TrainJs>> joysticks = TrainJs::getJsDevices();
for(auto joystick: joysticks)
{
if(!joystick->itemIsSet())
{
joystick->setItem(item);
break;
}
}
}
else if(turnout)
{

View File

@ -23,7 +23,6 @@ private:
signals:
void deleteRequest(const ItemData& item);
public:
explicit ItemScrollBox(QWidget *parent = nullptr);
~ItemScrollBox();

View File

@ -73,7 +73,6 @@ void ItemWidget::deleteItem()
void ItemWidget::setValue(int8_t value)
{
moveToValue(value);
if(auto workingItem = item_.lock())
workingItem->setValue(value);
else