implement joystick item switching
This commit is contained in:
@ -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++)
|
||||
|
Reference in New Issue
Block a user