From 5cd4007aaa4f28caf031de76ca28e5e1a4c382d7 Mon Sep 17 00:00:00 2001 From: Dante Ruiz Date: Tue, 18 Apr 2017 21:53:23 +0100 Subject: [PATCH] got input recording working --- interface/src/Application.cpp | 7 ++ .../src/controllers/InputRecorder.cpp | 80 +++++++++++++++---- .../src/controllers/InputRecorder.h | 14 +++- .../src/controllers/ScriptingInterface.cpp | 16 ++-- .../src/controllers/UserInputMapper.cpp | 7 +- .../impl/endpoints/ActionEndpoint.cpp | 18 +++-- 6 files changed, 105 insertions(+), 37 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 88278034d0..357bceb120 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -77,6 +77,7 @@ #include #include #include +#include #include #include #include @@ -2731,6 +2732,9 @@ void Application::keyPressEvent(QKeyEvent* event) { if (isMeta) { auto offscreenUi = DependencyManager::get(); offscreenUi->load("Browser.qml"); + } else if (isOption) { + controller::InputRecorder* inputRecorder = controller::InputRecorder::getInstance(); + inputRecorder->togglePlayback(); } break; @@ -2929,6 +2933,9 @@ void Application::keyPressEvent(QKeyEvent* event) { Menu::getInstance()->setIsOptionChecked(MenuOption::FirstPerson, !isFirstPersonChecked); Menu::getInstance()->setIsOptionChecked(MenuOption::ThirdPerson, isFirstPersonChecked); cameraMenuChanged(); + } else if (isOption) { + controller::InputRecorder* inputRecorder = controller::InputRecorder::getInstance(); + inputRecorder->toggleRecording(); } break; } diff --git a/libraries/controllers/src/controllers/InputRecorder.cpp b/libraries/controllers/src/controllers/InputRecorder.cpp index b60dd2387e..3ef6ef38ba 100644 --- a/libraries/controllers/src/controllers/InputRecorder.cpp +++ b/libraries/controllers/src/controllers/InputRecorder.cpp @@ -8,72 +8,122 @@ #include "InputRecorder.h" +#include +#include +#include +#include namespace controller { + + void poseToJsonObject(const Pose pose) { + } InputRecorder::InputRecorder() {} InputRecorder::~InputRecorder() {} - InputRecorder& InputRecorder::getInstance() { + InputRecorder* InputRecorder::getInstance() { static InputRecorder inputRecorder; - return inputRecorder; + return &inputRecorder; } void InputRecorder::startRecording() { _recording = true; + _playback = false; _framesRecorded = 0; _poseStateList.clear(); _actionStateList.clear(); - qDebug() << "-------------> input recording starting <---------------"; + } + + void InputRecorder::saveRecording() { + QJsonObject data; + data["frameCount"] = _framesRecorded; + + QJsonArray actionArrayList; + QJsonArray poseArrayList; + for(const ActionStates actionState _actionStateList) { + QJsonArray actionArray; + for (const float value, actionState) { + actionArray.append(value); + } + actionArrayList.append(actionArray); + } + + for (const PoseStates poseState, _poseStateList) { + QJsonArray poseArray; + for (const Pose pose, poseState) { + + } + poseArrayList.append(poseArray); + } + + } + + void InputRecorder::loadRecording() { } void InputRecorder::stopRecording() { _recording = false; - qDebug() << "--------------> input recording stopping <-----------------"; } void InputRecorder::startPlayback() { _playback = true; _recording = false; - qDebug() << "-----------------> starting playback <---------------"; } void InputRecorder::stopPlayback() { _playback = false; - _recording = false; } void InputRecorder::setActionState(controller::Action action, float value) { if (_recording) { - qDebug() << "-----------------> setiing action state <---------------"; - _actionStateList[_framesRecorded][toInt(action)] = value; + _currentFrameActions[toInt(action)] += value; } } void InputRecorder::setActionState(controller::Action action, const controller::Pose pose) { if (_recording) { - qDebug() << "-----------------> setiing Pose state <---------------"; - _poseStateList[_framesRecorded][toInt(action)] = pose; + _currentFramePoses[toInt(action)] = pose; } } + void InputRecorder::resetFrame() { + if (_recording) { + for(auto& channel : _currentFramePoses) { + channel = Pose(); + } + + for(auto& channel : _currentFrameActions) { + channel = 0.0f; + } + } + } + float InputRecorder::getActionState(controller::Action action) { - return _actionStateList[_playCount][toInt(action)]; + if (_actionStateList.size() > 0 ) { + return _actionStateList[_playCount][toInt(action)]; + } + + return 0.0f; } controller::Pose InputRecorder::getPoseState(controller::Action action) { - return _poseStateList[_playCount][toInt(action)]; + if (_poseStateList.size() > 0) { + return _poseStateList[_playCount][toInt(action)]; + } + + return Pose(); } void InputRecorder::frameTick() { if (_recording) { _framesRecorded++; + _poseStateList.push_back(_currentFramePoses); + _actionStateList.push_back(_currentFrameActions); } if (_playback) { - if (_playCount < _framesRecorded) { - _playCount++; - } else { + _playCount++; + if (_playCount == _framesRecorded) { _playCount = 0; } } diff --git a/libraries/controllers/src/controllers/InputRecorder.h b/libraries/controllers/src/controllers/InputRecorder.h index ff5d2fa6bc..f9ec944ff7 100644 --- a/libraries/controllers/src/controllers/InputRecorder.h +++ b/libraries/controllers/src/controllers/InputRecorder.h @@ -25,11 +25,17 @@ namespace controller { InputRecorder(); ~InputRecorder(); - static InputRecorder& getInstance(); + static InputRecorder* getInstance(); + + void saveRecording(); + void loadRecording(); void startRecording(); void startPlayback(); void stopPlayback(); void stopRecording(); + void toggleRecording() { _recording = !_recording; } + void togglePlayback() { _playback = !_playback; } + void resetFrame(); bool isRecording() { return _recording; } bool isPlayingback() { return _playback; } void setActionState(controller::Action action, float value); @@ -40,8 +46,10 @@ namespace controller { private: bool _recording { false }; bool _playback { false }; - std::vector _poseStateList; - std::vector _actionStateList; + std::vector _poseStateList = std::vector(); + std::vector _actionStateList = std::vector(); + PoseStates _currentFramePoses = PoseStates(toInt(Action::NUM_ACTIONS)); + ActionStates _currentFrameActions = ActionStates(toInt(Action::NUM_ACTIONS)); int _framesRecorded { 0 }; int _playCount { 0 }; diff --git a/libraries/controllers/src/controllers/ScriptingInterface.cpp b/libraries/controllers/src/controllers/ScriptingInterface.cpp index c9b8810463..f09e366df8 100644 --- a/libraries/controllers/src/controllers/ScriptingInterface.cpp +++ b/libraries/controllers/src/controllers/ScriptingInterface.cpp @@ -156,23 +156,23 @@ namespace controller { } void ScriptingInterface::startInputRecording() { - auto inputRecorder = InputRecorder::getInstance(); - inputRecorder.startRecording(); + InputRecorder* inputRecorder = InputRecorder::getInstance(); + inputRecorder->startRecording(); } void ScriptingInterface::stopInputRecording() { - auto inputRecorder = InputRecorder::getInstance(); - inputRecorder.stopRecording(); + InputRecorder* inputRecorder = InputRecorder::getInstance(); + inputRecorder->stopRecording(); } void ScriptingInterface::startInputPlayback() { - auto inputRecorder = InputRecorder::getInstance(); - inputRecorder.startPlayback(); + InputRecorder* inputRecorder = InputRecorder::getInstance(); + inputRecorder->startPlayback(); } void ScriptingInterface::stopInputPlayback() { - auto inputRecorder = InputRecorder::getInstance(); - inputRecorder.stopPlayback(); + InputRecorder* inputRecorder = InputRecorder::getInstance(); + inputRecorder->stopPlayback(); } bool ScriptingInterface::triggerHapticPulseOnDevice(unsigned int device, float strength, float duration, controller::Hand hand) const { diff --git a/libraries/controllers/src/controllers/UserInputMapper.cpp b/libraries/controllers/src/controllers/UserInputMapper.cpp index 43990b3bf0..be10c46538 100755 --- a/libraries/controllers/src/controllers/UserInputMapper.cpp +++ b/libraries/controllers/src/controllers/UserInputMapper.cpp @@ -243,10 +243,11 @@ void fixBisectedAxis(float& full, float& negative, float& positive) { void UserInputMapper::update(float deltaTime) { Locker locker(_lock); - auto inputRecorder = InputRecorder::getInstance(); + InputRecorder* inputRecorder = InputRecorder::getInstance(); static uint64_t updateCount = 0; ++updateCount; + inputRecorder->resetFrame(); // Reset the axis state for next loop for (auto& channel : _actionStates) { channel = 0.0f; @@ -298,7 +299,7 @@ void UserInputMapper::update(float deltaTime) { emit inputEvent(input.id, value); } } - inputRecorder.frameTick(); + inputRecorder->frameTick(); } Input::NamedVector UserInputMapper::getAvailableInputs(uint16 deviceID) const { @@ -509,6 +510,7 @@ bool UserInputMapper::applyRoute(const Route::Pointer& route, bool force) { } // If the source hasn't been written yet, defer processing of this route + auto inputRecorder = InputRecorder::getInstance(); auto source = route->source; auto sourceInput = source->getInput(); if (sourceInput.device == STANDARD_DEVICE && !force && source->writeable()) { @@ -572,7 +574,6 @@ bool UserInputMapper::applyRoute(const Route::Pointer& route, bool force) { } } // no filters yet for pose - qDebug() << "--------------> applying destination <----------------"; destination->apply(value, source); } else { // Fetch the value, may have been overriden by previous loopback routes diff --git a/libraries/controllers/src/controllers/impl/endpoints/ActionEndpoint.cpp b/libraries/controllers/src/controllers/impl/endpoints/ActionEndpoint.cpp index c3c3631118..6c14533f02 100644 --- a/libraries/controllers/src/controllers/impl/endpoints/ActionEndpoint.cpp +++ b/libraries/controllers/src/controllers/impl/endpoints/ActionEndpoint.cpp @@ -16,23 +16,25 @@ using namespace controller; void ActionEndpoint::apply(float newValue, const Pointer& source) { + InputRecorder* inputRecorder = InputRecorder::getInstance(); + if(inputRecorder->isPlayingback()) { + newValue = inputRecorder->getActionState(Action(_input.getChannel())); + } + _currentValue += newValue; if (_input != Input::INVALID_INPUT) { auto userInputMapper = DependencyManager::get(); userInputMapper->deltaActionState(Action(_input.getChannel()), newValue); } - auto inputRecorder = InputRecorder::getInstance(); - inputRecorder.setActionState(Action(_input.getChannel()), _currentValue); + inputRecorder->setActionState(Action(_input.getChannel()), newValue); } void ActionEndpoint::apply(const Pose& value, const Pointer& source) { _currentPose = value; - auto inputRecorder = InputRecorder::getInstance(); - inputRecorder.setActionState(Action(_input.getChannel()), _currentPose); - qDebug << "<--------------- destination"; - if (inputRecorder.isPlayingback()) { - qDebug() << "-------------> playing back <--------------"; - _currentPose = inputRecorder.getPoseState(Action(_input.getChannel())); + InputRecorder* inputRecorder = InputRecorder::getInstance(); + inputRecorder->setActionState(Action(_input.getChannel()), _currentPose); + if (inputRecorder->isPlayingback()) { + _currentPose = inputRecorder->getPoseState(Action(_input.getChannel())); } if (!_currentPose.isValid()) {