Skip to content

Commit

Permalink
Interactor: Adding a simple command API
Browse files Browse the repository at this point in the history
  • Loading branch information
mwestphal committed Oct 27, 2024
1 parent 558cebf commit fed9629
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions library/private/interactor_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class interactor_impl : public interactor
interactor& setKeyPressCallBack(std::function<bool(int, std::string)> callBack) override;
interactor& setDropFilesCallBack(std::function<bool(std::vector<std::string>)> callBack) override;

interactor& command(const std::string& command) override;

unsigned long createTimerCallBack(double time, std::function<void()> callBack) override;
void removeTimerCallBack(unsigned long id) override;

Expand Down
2 changes: 2 additions & 0 deletions library/public/interactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class F3D_EXPORT interactor
virtual interactor& setDropFilesCallBack(
std::function<bool(std::vector<std::string>)> callBack) = 0;

virtual interactor& command(const std::string& command) = 0;

/**
* Use this method to create your own timer callback. You callback will be called once every time
* ms. Return an id to use in removeTimeCallBack.
Expand Down
85 changes: 85 additions & 0 deletions library/src/interactor_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
#include <vtkVersion.h>
#include <vtksys/SystemTools.hxx>

#include <algorithm>
#include <chrono>
#include <cmath>
#include <map>
#include <vector>

#include "camera.h"

Expand Down Expand Up @@ -764,4 +766,87 @@ void interactor_impl::UpdateRendererAfterInteraction()
{
this->Internals->Style->UpdateRendererAfterInteraction();
}

//----------------------------------------------------------------------------
interactor& interactor_impl::command(const std::string& commandString)

Check warning on line 771 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L771

Added line #L771 was not covered by tests
{
// Split command with space
std::string temp;
std::stringstream stringstream{ commandString };
std::vector<std::string> results;
while (std::getline(stringstream, temp, ' '))

Check warning on line 777 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L774-L777

Added lines #L774 - L777 were not covered by tests
{
results.push_back(temp);

Check warning on line 779 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L779

Added line #L779 was not covered by tests
}

size_t nTokens = results.size();
if (nTokens > 3 || nTokens == 0)

Check warning on line 783 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L782-L783

Added lines #L782 - L783 were not covered by tests
{
log::error("Command: \"", commandString, "\" contains incorrect number of tokens, ignoring");
return *this;

Check warning on line 786 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L785-L786

Added lines #L785 - L786 were not covered by tests
}

std::string command = results[0];
std::string option = nTokens >= 1 ? results[1] : "";
std::string value = nTokens >= 2 ? results[2] : "";

Check warning on line 791 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L789-L791

Added lines #L789 - L791 were not covered by tests

try
{
if (command == "set")

Check warning on line 795 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L795

Added line #L795 was not covered by tests
{
this->Internals->Options.setAsString(option, value);

Check warning on line 797 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L797

Added line #L797 was not covered by tests
}
else if (command == "inc")

Check warning on line 799 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L799

Added line #L799 was not covered by tests
{
// TODO
}
else if (command == "dec")

Check warning on line 803 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L803

Added line #L803 was not covered by tests
{
// TODO
}
else if (command == "toggle")

Check warning on line 807 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L807

Added line #L807 was not covered by tests
{
this->Internals->Options.toggle(option);

Check warning on line 809 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L809

Added line #L809 was not covered by tests
}
else if (command == "reset")

Check warning on line 811 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L811

Added line #L811 was not covered by tests
{
this->Internals->Options.reset(option);

Check warning on line 813 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L813

Added line #L813 was not covered by tests
}
else if (command == "unset")

Check warning on line 815 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L815

Added line #L815 was not covered by tests
{
this->Internals->Options.removeValue(option);

Check warning on line 817 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L817

Added line #L817 was not covered by tests
}
else if (command == "print")

Check warning on line 819 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L819

Added line #L819 was not covered by tests
{
log::info(this->Internals->Options.getAsString(option));

Check warning on line 821 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L821

Added line #L821 was not covered by tests
}
else if (command == "alias")

Check warning on line 823 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L823

Added line #L823 was not covered by tests
{
// TODO
}
else
{
log::error("Command: \"", command, "\" is not recognized, ignoring");

Check warning on line 829 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L829

Added line #L829 was not covered by tests
}
}
catch (const f3d::options::incompatible_exception&)

Check warning on line 832 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L832

Added line #L832 was not covered by tests
{
log::error("Command: provided value:\"", value, "\" is not compatible with option:\"", option,

Check warning on line 834 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L834

Added line #L834 was not covered by tests
"\", ignoring");
}
catch (const f3d::options::inexistent_exception&)

Check warning on line 837 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L836-L837

Added lines #L836 - L837 were not covered by tests
{
log::error("Command: provided option:\"", option, "\" does not exists, ignoring");
}
catch (const f3d::options::no_value_exception&)

Check warning on line 841 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L839-L841

Added lines #L839 - L841 were not covered by tests
{
log::error("Command: provided option:\"", option, "\" has not value, ignoring");
}
catch (const f3d::options::parsing_exception&)

Check warning on line 845 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L843-L845

Added lines #L843 - L845 were not covered by tests
{
log::error("Command: provided value:\"", value, "\" cannot be parse for option:\"", option,

Check warning on line 847 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L847

Added line #L847 was not covered by tests
"\", ignoring");
}
return *this;
}

Check warning on line 851 in library/src/interactor_impl.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/interactor_impl.cxx#L849-L851

Added lines #L849 - L851 were not covered by tests
}

0 comments on commit fed9629

Please sign in to comment.