116 lines
2.2 KiB
C++
116 lines
2.2 KiB
C++
#include "trainjs.h"
|
|
#include <QDebug>
|
|
|
|
#include "items/train.h"
|
|
|
|
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()
|
|
{
|
|
}
|
|
|
|
void TrainJs::init()
|
|
{
|
|
QJoysticks* jsmanager = QJoysticks::getInstance();
|
|
jsmanager->updateInterfaces();
|
|
const QList<QJoystickDevice*> joysticks = jsmanager->inputDevices();
|
|
|
|
qDebug()<<"loading joysticks number attached: "<<jsmanager->count();
|
|
|
|
for(int id = 0; id < joysticks.count(); ++id)
|
|
{
|
|
QJoystickDevice *joystick = joysticks[id];
|
|
if(joystick->name == JOYSTICK_NAME)
|
|
{
|
|
for(int axis = 0; axis < joystick->axes.count(); ++axis)
|
|
{
|
|
js_.push_back(std::shared_ptr<TrainJs>(new TrainJs(id, axis)));
|
|
qDebug()<<"matched joystick: "<<joystick->name<<" axis "<<axis;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<std::shared_ptr<TrainJs>> TrainJs::getJsDevices()
|
|
{
|
|
return js_;
|
|
}
|
|
|
|
std::weak_ptr<Item> TrainJs::getItem()
|
|
{
|
|
return item_;
|
|
}
|
|
void TrainJs::setItem(std::weak_ptr<Item> item)
|
|
{
|
|
item_ = item;
|
|
}
|
|
|
|
bool TrainJs::itemIsSet()
|
|
{
|
|
return !item_.expired();
|
|
}
|
|
|
|
void TrainJs::axisChanged(const int id, const int axis, const qreal value)
|
|
{
|
|
if(id == id_ && axis == axis_)
|
|
{
|
|
if(std::shared_ptr<Item> workitem = item_.lock())
|
|
{
|
|
int8_t newValue = value*14;
|
|
if(newValue != workitem->getValue())
|
|
workitem->setValue(newValue);
|
|
}
|
|
else
|
|
{
|
|
reqNewItem();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void TrainJs::buttonChanged(const int id, const int button, const bool pressed)
|
|
{
|
|
if(id == id_ && button == axis_)
|
|
{
|
|
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();
|
|
}
|
|
handleRelease = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void TrainJs::longpressTimeout()
|
|
{
|
|
reqNewItem();
|
|
handleRelease = false;
|
|
}
|
|
|
|
|