implement joystick item switching
This commit is contained in:
parent
00bfa72455
commit
7a61dc9368
8 changed files with 61 additions and 41 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue