/* Copyright (c) 2014 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "inputcontroller/InputPlayback.h" #include #include #define stemobject_implementation InputPlayback stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); InputPlayback * InputPlayback_create(InputController * inputController, InputSession * inputSession) { stemobject_create_implementation(init, inputController, inputSession) } bool InputPlayback_init(InputPlayback * self, InputController * inputController, InputSession * inputSession) { call_super(init, self); self->inputController = inputController; self->inputSession = inputSession; self->eventDispatcher = EventDispatcher_create(self); self->frameIndex = 0; self->lastFrameIndex = 0; self->eventIndex = 0; return true; } void InputPlayback_dispose(InputPlayback * self) { EventDispatcher_dispose(self->eventDispatcher); call_super(dispose, self); } void InputPlayback_step(InputPlayback * self, double referenceTime) { while (self->eventIndex < self->inputSession->eventCount && self->frameIndex - self->lastFrameIndex >= self->inputSession->events[self->eventIndex].frameDelta) { struct InputSessionEvent event = self->inputSession->events[self->eventIndex]; switch (event.eventType) { case INPUT_SESSION_EVENT_ACTION_DOWN: { struct InputController_event eventData = {self->inputSession->actions[event.actionIndex], referenceTime, InputMap_genericUnknownBinding()}; EventDispatcher_dispatchEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_ACTION_DOWN), &eventData); break; } case INPUT_SESSION_EVENT_ACTION_UP: { struct InputController_event eventData = {self->inputSession->actions[event.actionIndex], referenceTime, InputMap_genericUnknownBinding()}; EventDispatcher_dispatchEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_ACTION_UP), &eventData); break; } case INPUT_SESSION_EVENT_ACTION_REPEAT: { struct InputController_event eventData = {self->inputSession->actions[event.actionIndex], referenceTime, InputMap_genericUnknownBinding()}; EventDispatcher_dispatchEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_ACTION_REPEAT), &eventData); break; } case INPUT_SESSION_EVENT_MOTION: { struct InputController_motionEvent eventData = {.actionID = self->inputSession->actions[event.actionIndex], .timestamp = referenceTime, .valueX = event.valueX, .valueY = event.valueY, .rawValueX = event.rawValueX, .rawValueY = event.rawValueY}; EventDispatcher_dispatchEvent(self->inputController->eventDispatcher, ATOM(INPUT_CONTROLLER_EVENT_MOTION), &eventData); break; } } self->lastFrameIndex = self->frameIndex; self->eventIndex++; if (self->eventIndex >= self->inputSession->eventCount) { EventDispatcher_dispatchEvent(self->eventDispatcher, ATOM(INPUT_PLAYBACK_EVENT_PLAYBACK_COMPLETE), NULL); } } self->frameIndex++; } void InputPlayback_skipToNextEventFrame(InputPlayback * self) { if (self->eventIndex < self->inputSession->eventCount) { self->frameIndex = self->lastFrameIndex + self->inputSession->events[self->eventIndex].frameDelta; } } void InputPlayback_rewind(InputPlayback * self) { self->frameIndex = 0; self->lastFrameIndex = 0; self->eventIndex = 0; }