implement joystick item switching
This commit is contained in:
		
							parent
							
								
									00bfa72455
								
							
						
					
					
						commit
						7a61dc9368
					
				
					 8 changed files with 61 additions and 41 deletions
				
			
		| 
						 | 
					@ -1,6 +1,8 @@
 | 
				
			||||||
#include "itemstore.h"
 | 
					#include "itemstore.h"
 | 
				
			||||||
#include <QJsonArray>
 | 
					#include <QJsonArray>
 | 
				
			||||||
#include <QDebug>
 | 
					#include <QDebug>
 | 
				
			||||||
 | 
					#include "train.h"
 | 
				
			||||||
 | 
					#include "../trainjs.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ItemStore::ItemStore(QObject *parent): QObject(parent)
 | 
					ItemStore::ItemStore(QObject *parent): QObject(parent)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -19,11 +21,54 @@ void ItemStore::addItem(std::shared_ptr<Item> item)
 | 
				
			||||||
    if(!mached)
 | 
					    if(!mached)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        items_.push_back(std::shared_ptr<Item>(item));
 | 
					        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()));
 | 
					        itemAdded(std::weak_ptr<Item>(items_.back()));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    qDebug()<<"Got item: "<<item->id()<<" matched: "<<mached;
 | 
					    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)
 | 
					void ItemStore::addItems(const std::vector<std::shared_ptr<Item>>& itemIn)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    for(unsigned j = 0; j < itemIn.size(); j++)
 | 
					    for(unsigned j = 0; j < itemIn.size(); j++)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,6 +20,10 @@ public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void clear();
 | 
					    void clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private slots:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void jsReqNewItem();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
signals:
 | 
					signals:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void itemDeleted(ItemData item);
 | 
					    void itemDeleted(ItemData item);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,5 +14,5 @@ void Turnout::setValue(int8_t value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    Item::setValue(value);
 | 
					    Item::setValue(value);
 | 
				
			||||||
    if(micro)
 | 
					    if(micro)
 | 
				
			||||||
        micro->tunoutSetDirection(turnoutId_, value);
 | 
							micro->tunoutSetDirection(turnoutId_, value > 0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,13 +5,9 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TrainJs::TrainJs(int id, int axis): id_(id), axis_(axis)
 | 
					TrainJs::TrainJs(int id, int axis): id_(id), axis_(axis)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	longpressTimer_.setSingleShot(true);
 | 
					 | 
				
			||||||
	longpressTimer_.setInterval(LONGPRESS_TIME);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	QJoysticks* jsmanager = QJoysticks::getInstance();
 | 
						QJoysticks* jsmanager = QJoysticks::getInstance();
 | 
				
			||||||
	connect(jsmanager, &QJoysticks::axisChanged, this, &TrainJs::axisChanged);
 | 
						connect(jsmanager, &QJoysticks::axisChanged, this, &TrainJs::axisChanged);
 | 
				
			||||||
	connect(jsmanager, &QJoysticks::buttonChanged, this, &TrainJs::buttonChanged);
 | 
						connect(jsmanager, &QJoysticks::buttonChanged, this, &TrainJs::buttonChanged);
 | 
				
			||||||
	connect(&longpressTimer_, &QTimer::timeout, this, &TrainJs::longpressTimeout);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TrainJs::~TrainJs()
 | 
					TrainJs::~TrainJs()
 | 
				
			||||||
| 
						 | 
					@ -53,6 +49,8 @@ std::weak_ptr<Item> TrainJs::getItem()
 | 
				
			||||||
void TrainJs::setItem(std::weak_ptr<Item> item)
 | 
					void TrainJs::setItem(std::weak_ptr<Item> item)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	item_ = item;
 | 
						item_ = item;
 | 
				
			||||||
 | 
						wantsNewItem = false;
 | 
				
			||||||
 | 
						inhibitUntillZero = true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool TrainJs::itemIsSet()
 | 
					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())
 | 
							if(std::shared_ptr<Item> workitem = item_.lock())
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			int8_t newValue = value*14;
 | 
								int8_t newValue = value*14;
 | 
				
			||||||
			if(newValue != workitem->getValue())
 | 
								if(newValue == 0)
 | 
				
			||||||
 | 
									inhibitUntillZero = false;
 | 
				
			||||||
 | 
								if(newValue != workitem->getValue() && !inhibitUntillZero)
 | 
				
			||||||
				workitem->setValue(newValue);
 | 
									workitem->setValue(newValue);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
 | 
								wantsNewItem = true;
 | 
				
			||||||
			reqNewItem();
 | 
								reqNewItem();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -85,32 +86,14 @@ void TrainJs::buttonChanged(const int id, const int button, const bool pressed)
 | 
				
			||||||
		if(pressed)
 | 
							if(pressed)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			handleRelease = true;
 | 
								handleRelease = true;
 | 
				
			||||||
			longpressTimer_.start();
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else if(handleRelease)
 | 
							else if(handleRelease)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			longpressTimer_.stop();
 | 
								wantsNewItem = true;
 | 
				
			||||||
			if(std::shared_ptr<Item> workitem = item_.lock())
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				if(Train* train = dynamic_cast<Train*>(workitem.get()))
 | 
					 | 
				
			||||||
				{
 | 
					 | 
				
			||||||
					if(train->getValue() == 0)
 | 
					 | 
				
			||||||
						train->reverse();
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			else
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				reqNewItem();
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			handleRelease = false;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void TrainJs::longpressTimeout()
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
			reqNewItem();
 | 
								reqNewItem();
 | 
				
			||||||
			handleRelease = false;
 | 
								handleRelease = false;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,15 +13,15 @@ class TrainJs: public QObject
 | 
				
			||||||
	Q_OBJECT
 | 
						Q_OBJECT
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	static constexpr char JOYSTICK_NAME[] = "UVOS UsbJoy";
 | 
						static constexpr char JOYSTICK_NAME[] = "UVOS UsbJoy";
 | 
				
			||||||
	static constexpr int LONGPRESS_TIME = 500;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	inline static std::vector<std::shared_ptr<TrainJs>> js_;
 | 
						inline static std::vector<std::shared_ptr<TrainJs>> js_;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	int id_ = -1;
 | 
						int id_ = -1;
 | 
				
			||||||
	int axis_ = -1;
 | 
						int axis_ = -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QTimer longpressTimer_;
 | 
					 | 
				
			||||||
	bool handleRelease = false;
 | 
						bool handleRelease = false;
 | 
				
			||||||
 | 
						bool wantsNewItem = true;
 | 
				
			||||||
 | 
						bool inhibitUntillZero = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	std::weak_ptr<Item> item_;
 | 
						std::weak_ptr<Item> item_;
 | 
				
			||||||
	TrainJs(int id, int axis);
 | 
						TrainJs(int id, int axis);
 | 
				
			||||||
| 
						 | 
					@ -30,7 +30,6 @@ private:
 | 
				
			||||||
private slots:
 | 
					private slots:
 | 
				
			||||||
	void axisChanged(const int id, const int axis, const qreal value);
 | 
						void axisChanged(const int id, const int axis, const qreal value);
 | 
				
			||||||
	void buttonChanged(const int id, const int button, const bool pressed);
 | 
						void buttonChanged(const int id, const int button, const bool pressed);
 | 
				
			||||||
	void longpressTimeout();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
signals:
 | 
					signals:
 | 
				
			||||||
	void reqNewItem();
 | 
						void reqNewItem();
 | 
				
			||||||
| 
						 | 
					@ -42,6 +41,7 @@ public:
 | 
				
			||||||
	std::weak_ptr<Item> getItem();
 | 
						std::weak_ptr<Item> getItem();
 | 
				
			||||||
	void setItem(std::weak_ptr<Item>);
 | 
						void setItem(std::weak_ptr<Item>);
 | 
				
			||||||
	bool itemIsSet();
 | 
						bool itemIsSet();
 | 
				
			||||||
 | 
						bool getWantsNewItem() {return wantsNewItem;}
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // TRAINJS_H
 | 
					#endif // TRAINJS_H
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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));
 | 
					                widgets_.back()->setShortcuts(QKeySequence(Qt::Key_R), QKeySequence(Qt::Key_F), QKeySequence(Qt::Key_V));
 | 
				
			||||||
            else if(train->getTrainId() == 4)
 | 
					            else if(train->getTrainId() == 4)
 | 
				
			||||||
                widgets_.back()->setShortcuts(QKeySequence(Qt::Key_T), QKeySequence(Qt::Key_G), QKeySequence(Qt::Key_B));
 | 
					                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)
 | 
					        else if(turnout)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,7 +23,6 @@ private:
 | 
				
			||||||
signals:
 | 
					signals:
 | 
				
			||||||
    void deleteRequest(const ItemData& item);
 | 
					    void deleteRequest(const ItemData& item);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    explicit ItemScrollBox(QWidget *parent = nullptr);
 | 
					    explicit ItemScrollBox(QWidget *parent = nullptr);
 | 
				
			||||||
    ~ItemScrollBox();
 | 
					    ~ItemScrollBox();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -73,7 +73,6 @@ void ItemWidget::deleteItem()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void ItemWidget::setValue(int8_t value)
 | 
					void ItemWidget::setValue(int8_t value)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	moveToValue(value);
 | 
					 | 
				
			||||||
	if(auto workingItem = item_.lock())
 | 
						if(auto workingItem = item_.lock())
 | 
				
			||||||
		workingItem->setValue(value);
 | 
							workingItem->setValue(value);
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue