Skip to content

Commit 1d48357

Browse files
committed
ISSUE-2082: Add repeat support for bindings API
1 parent fcfa401 commit 1d48357

3 files changed

Lines changed: 41 additions & 35 deletions

File tree

library/private/interactor_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ class interactor_impl : public interactor
4848
interactor& initBindings() override;
4949
interactor& addBinding(const interaction_bind_t& bind, std::vector<std::string> commands,
5050
std::string group = std::string(), documentation_callback_t documentationCallback = nullptr,
51-
BindingType type = BindingType::OTHER, bool notify = true) override;
51+
BindingType type = BindingType::OTHER, bool notify = true, bool repeat = false) override;
5252
interactor& addBinding(const interaction_bind_t& bind, std::string command,
5353
std::string group = std::string(), documentation_callback_t documentationCallback = nullptr,
54-
BindingType type = BindingType::OTHER, bool notify = true) override;
54+
BindingType type = BindingType::OTHER, bool notify = true, bool repeat = false) override;
5555
interactor& removeBinding(const interaction_bind_t& bind) override;
5656
std::vector<std::string> getBindGroups() const override;
5757
std::vector<interaction_bind_t> getBindsForGroup(std::string group) const override;

library/public/interactor.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,13 @@ class F3D_EXPORT interactor
187187
*
188188
* If notify is true, a notification is triggered when pressing the binding
189189
*
190+
* If repeat is true, the binding is continuously applied when holding down the key
191+
*
190192
* Adding commands for an existing bind will throw a interactor::already_exists_exception.
191193
*/
192194
virtual interactor& addBinding(const interaction_bind_t& bind, std::vector<std::string> commands,
193195
std::string group = {}, documentation_callback_t documentationCallback = nullptr,
194-
BindingType type = BindingType::OTHER, bool notify = true) = 0;
196+
BindingType type = BindingType::OTHER, bool notify = true, bool repeat = false) = 0;
195197

196198
/**
197199
* See addBinding
@@ -202,17 +204,17 @@ class F3D_EXPORT interactor
202204
*/
203205
virtual interactor& addBinding(const interaction_bind_t& bind, std::string command,
204206
std::string group = {}, documentation_callback_t documentationCallback = nullptr,
205-
BindingType type = BindingType::OTHER, bool notify = true) = 0;
207+
BindingType type = BindingType::OTHER, bool notify = true, bool repeat = false) = 0;
206208

207209
/**
208210
* Convenience initializer list signature for add binding method
209211
*/
210212
interactor& addBinding(const interaction_bind_t& bind, std::initializer_list<std::string> list,
211213
std::string group = {}, documentation_callback_t documentationCallback = nullptr,
212-
BindingType type = BindingType::OTHER, bool notify = true)
214+
BindingType type = BindingType::OTHER, bool notify = true, bool repeat = false)
213215
{
214216
return this->addBinding(bind, std::vector<std::string>(list), std::move(group),
215-
std::move(documentationCallback), type, notify);
217+
std::move(documentationCallback), type, notify, repeat);
216218
}
217219

218220
/**

library/src/interactor_impl.cxx

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class interactor_impl::internals
6161
documentation_callback_t DocumentationCallback;
6262
BindingType Type;
6363
bool Notify;
64+
bool Repeat;
6465
};
6566

6667
struct CommandCallbacks
@@ -519,36 +520,39 @@ class interactor_impl::internals
519520
// invalidating any references/iterators into it.
520521
const BindingCommands binding = commandsIt->second;
521522

522-
for (const std::string& command : binding.CommandVector)
523+
if (binding.Repeat || rwi->GetRepeatCount() <= 1)
523524
{
524-
std::string commandWithArgs = command;
525-
if (!argsString.empty())
525+
for (const std::string& command : binding.CommandVector)
526526
{
527-
commandWithArgs.push_back(' ');
528-
commandWithArgs.append(argsString);
529-
};
530-
try
531-
{
532-
// XXX: Ignore the boolean return of triggerCommand,
533-
// error is already logged by triggerCommand
534-
this->Interactor.triggerCommand(commandWithArgs);
535-
}
536-
catch (const f3d::interactor::command_runtime_exception& ex)
537-
{
538-
log::error(
539-
"Interaction: error running command: \"" + commandWithArgs + "\": " + ex.what());
527+
std::string commandWithArgs = command;
528+
if (!argsString.empty())
529+
{
530+
commandWithArgs.push_back(' ');
531+
commandWithArgs.append(argsString);
532+
};
533+
try
534+
{
535+
// XXX: Ignore the boolean return of triggerCommand,
536+
// error is already logged by triggerCommand
537+
this->Interactor.triggerCommand(commandWithArgs);
538+
}
539+
catch (const f3d::interactor::command_runtime_exception& ex)
540+
{
541+
log::error(
542+
"Interaction: error running command: \"" + commandWithArgs + "\": " + ex.what());
543+
}
540544
}
541-
}
542545

543-
if (binding.Notify && binding.DocumentationCallback)
544-
{
545-
// trigger notification
546-
vtkRenderWindow* renWin = this->Window.GetRenderWindow();
547-
vtkF3DRenderer* ren =
548-
vtkF3DRenderer::SafeDownCast(renWin->GetRenderers()->GetFirstRenderer());
546+
if (binding.Notify && binding.DocumentationCallback)
547+
{
548+
// trigger notification
549+
vtkRenderWindow* renWin = this->Window.GetRenderWindow();
550+
vtkF3DRenderer* ren =
551+
vtkF3DRenderer::SafeDownCast(renWin->GetRenderers()->GetFirstRenderer());
549552

550-
auto [desc, value] = binding.DocumentationCallback();
551-
ren->AddNotification(desc, value, bind.format(), 3.0);
553+
auto [desc, value] = binding.DocumentationCallback();
554+
ren->AddNotification(desc, value, bind.format(), 3.0);
555+
}
552556
}
553557
}
554558

@@ -1734,10 +1738,10 @@ interactor& interactor_impl::initBindings()
17341738
//----------------------------------------------------------------------------
17351739
interactor& interactor_impl::addBinding(const interaction_bind_t& bind,
17361740
std::vector<std::string> commands, std::string group,
1737-
documentation_callback_t documentationCallback, BindingType type, bool notify)
1741+
documentation_callback_t documentationCallback, BindingType type, bool notify, bool repeat)
17381742
{
17391743
const auto [it, success] = this->Internals->Bindings.insert(
1740-
{ bind, { std::move(commands), std::move(documentationCallback), type, notify } });
1744+
{ bind, { std::move(commands), std::move(documentationCallback), type, notify, repeat } });
17411745
if (!success)
17421746
{
17431747
throw interactor::already_exists_exception(
@@ -1759,10 +1763,10 @@ interactor& interactor_impl::addBinding(const interaction_bind_t& bind,
17591763

17601764
//----------------------------------------------------------------------------
17611765
interactor& interactor_impl::addBinding(const interaction_bind_t& bind, std::string command,
1762-
std::string group, documentation_callback_t documentationCallback, BindingType type, bool notify)
1766+
std::string group, documentation_callback_t documentationCallback, BindingType type, bool notify, bool repeat)
17631767
{
17641768
return this->addBinding(bind, std::vector<std::string>{ std::move(command) }, std::move(group),
1765-
std::move(documentationCallback), type, notify);
1769+
std::move(documentationCallback), type, notify, repeat);
17661770
}
17671771

17681772
//----------------------------------------------------------------------------

0 commit comments

Comments
 (0)