Skip to content

Commit

Permalink
VROInputControllerBase updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dthian committed Jul 29, 2020
1 parent ee440b0 commit a391ae7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 43 deletions.
44 changes: 7 additions & 37 deletions ViroRenderer/VROInputControllerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,16 @@
#include "VROTime.h"
#include "VROPortal.h"

static bool sSceneBackgroundAdd = true;
static bool sHasHoverEvents = true;

VROInputControllerBase::VROInputControllerBase(std::shared_ptr<VRODriver> driver) :
_driver(driver) {
_scene = nullptr;
}

/**
* OnButtonEvents are triggered on both controller-level and node-level event delegates whenever
* button-specific events has been triggered on the underlying hardware (corresponding
* VROInputController).
*
* For the case of ClickDown and ClickUp events, delegates are triggered with a unique deviceId
* and sourceId. For the case of Clicked Events, delegates are only triggered iff the last ClickDown
* input event matches the last ClickUp input event in terms of deviceId and sourceId. Note that
* these are always triggered after a clickdown-clickup as a separate event.
*
* NOTE: It is important to know the slight difference into how delegates are triggered.
*
* Controller-Level Event Delegate: When triggered, contains ButtonEvents of ALL deviceIDs at
* that point in time. This is to provide developers a single point from which to base business
* logic that may be dependent on multiple inputs at multiple times.
*
* Node-Level Event Delegates: When triggered, contains the ButtonEvents coming from
* the controller (with a unique device Id) that has focused on the node. Thus, ClickDown,
* ClickUp, Clicked events were done intentionally for that node, from a given controller.
*/
void VROInputControllerBase::onButtonEvent(std::vector<VROEventDelegate::ButtonEvent> &events) {
std::map<int, std::vector<VROEventDelegate::ButtonEvent>> eventsByControllerId;
std::map<int, VROEventDelegate::ButtonEvent> clickedEvents;
std::vector<VROEventDelegate::ButtonEvent> clickedEventsAll;

// For each button event:
// - populate it with the cached hit point done for this controller id
// - if it's a clickDown event, update lastTrackedButtonEvent (used for determining clicked)
// - if it's a clickUp event, compare with lastTrackedButtonEvent
for (VROEventDelegate::ButtonEvent &event : events) {
// If no required hit result is found, we can't trigger button events.
if (_deviceControllerState.find(event.deviceId) == _deviceControllerState.end()) {
Expand Down Expand Up @@ -147,11 +120,9 @@ void VROInputControllerBase::onButtonEvent(std::vector<VROEventDelegate::ButtonE
}
}

void VROInputControllerBase::onMove(std::vector<VROEventDelegate::MoveEvent> &events, bool shouldUpdatehitTests) {
shouldUpdatehitTests = shouldUpdatehitTests || sHasHoverEvents;

void VROInputControllerBase::onMove(std::vector<VROEventDelegate::MoveEvent> &events) {
// Update all device input's transforms.
if (shouldUpdatehitTests && _scene != nullptr) {
if (_scene != nullptr) {
for (VROEventDelegate::MoveEvent event : events) {
VROVector3f origin = event.pos;
VROVector3f controllerForward = event.rot.getMatrix().multiply(kBaseForward);
Expand All @@ -169,9 +140,6 @@ void VROInputControllerBase::onMove(std::vector<VROEventDelegate::MoveEvent> &ev
onHover(events);
}

/**
* Hovered Events are only tracked per device Id.
*/
void VROInputControllerBase::onHover(std::vector<VROEventDelegate::MoveEvent> &events) {
if (!sHasHoverEvents) {
return;
Expand Down Expand Up @@ -217,14 +185,16 @@ void VROInputControllerBase::onHover(std::vector<VROEventDelegate::MoveEvent> &e

// Also notify the last hovered node that we have hovered 'off'.
std::shared_ptr<VRONode> lastTrackedOnHoveredNode = _deviceControllerState[event.deviceId].lastTrackedOnHoveredNode;
std::shared_ptr<VRONode> lastTrackedOnHoverEventNode = _deviceControllerState[event.deviceId].lastTrackedOnHoverEventNode;
if (lastTrackedOnHoveredNode != onHoveredNode && lastTrackedOnHoveredNode != nullptr) {
event.isHovering = false;
std::vector<VROEventDelegate::HoverEvent> events2 = {event};
if (lastTrackedOnHoveredNode->getEventDelegate() != nullptr) {
lastTrackedOnHoveredNode->getEventDelegate()->onHover(events2);
if (lastTrackedOnHoverEventNode != nullptr && lastTrackedOnHoverEventNode->getEventDelegate() != nullptr) {
lastTrackedOnHoverEventNode->getEventDelegate()->onHover(events2);
}
}
_deviceControllerState[event.deviceId].lastTrackedOnHoveredNode = onHoverEventNode;
_deviceControllerState[event.deviceId].lastTrackedOnHoveredNode = onHoveredNode;
_deviceControllerState[event.deviceId].lastTrackedOnHoverEventNode = onHoverEventNode;
}
}

Expand Down
13 changes: 11 additions & 2 deletions ViroRenderer/VROInputControllerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
For example, VROInputControllerDaydream maps the onTouchPadClick event onto
a Viro onPrimaryClick event type within VROInputControllerBase, of which then notifies all
VROEventDelegates about such an event.
NOTE: There is a slight difference into how delegates are triggered.
1) Controller-Level Event Delegate: When triggered, contains the event of ALL deviceIDs sources at
that point in time. This is to provide developers a single snapshot point of events, where the
events are triggered on the Controller itself via internal delegates.
2) Node-Level Event Delegates: When triggered, contains the events pertaining to the node
that can handle the event via getNodeToHandleEvent(). This is applied for Click and Hover events
only, where for example, click events are triggered on the Node.
*/
class VROInputControllerBase {
public:
Expand Down Expand Up @@ -115,7 +124,7 @@ class VROInputControllerBase {
}

void onButtonEvent(std::vector<VROEventDelegate::ButtonEvent> &events);
void onMove(std::vector<VROEventDelegate::MoveEvent> &events, bool shouldUpdateHitTests);
void onMove(std::vector<VROEventDelegate::MoveEvent> &events);
void onHover(std::vector<VROEventDelegate::MoveEvent> &events);

/*
Expand Down Expand Up @@ -154,13 +163,13 @@ class VROInputControllerBase {
*/
std::shared_ptr<VROHitTestResult> hitResult;


/*
Tracked Events for state
*/
std::shared_ptr<VROEventDelegate::ButtonEvent> lastTrackedButtonEvent;
std::shared_ptr<VROEventDelegate::HoverEvent> lastTrackedHoverEvent;
std::shared_ptr<VRONode> lastTrackedOnHoveredNode;
std::shared_ptr<VRONode> lastTrackedOnHoverEventNode;

} TouchControllerLeft, TouchControllerRight;

Expand Down
8 changes: 4 additions & 4 deletions ViroRenderer/VRONode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,16 +627,16 @@ void VRONode::applyConstraints(const VRORenderContext &context, VROMatrix4f pare
updated = true;
}

/*
/*
std::shared_ptr<VROPencil> p = context.getPencil();
VROVector3f s = VROVector3f(_worldUmbrellaBoundingBox.getMinX(), _worldUmbrellaBoundingBox.getMinY(), _worldUmbrellaBoundingBox.getMinZ());
VROVector3f l = VROVector3f(_worldUmbrellaBoundingBox.getMaxX(), _worldUmbrellaBoundingBox.getMaxY(), _worldUmbrellaBoundingBox.getMaxZ());
VROVector3f s = VROVector3f(_worldBoundingBox.getMinX(), _worldBoundingBox.getMinY(), _worldBoundingBox.getMinZ());
VROVector3f l = VROVector3f(_worldBoundingBox.getMaxX(), _worldBoundingBox.getMaxY(), _worldBoundingBox.getMaxZ());
p->draw({s.x, s.y, s.z}, {l.x, s.y, s.z});
p->draw({l.x, s.y, s.z}, {l.x, l.y, s.z});
p->draw({l.x, l.y, s.z}, {s.x, l.y, s.z});
p->draw({s.x, l.y, s.z}, {s.x, s.y, s.z});
*/
*/
/*
Move down the tree.
*/
Expand Down

0 comments on commit a391ae7

Please sign in to comment.