Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Refactor pointer code. Support layers for pointer. (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored and bluemarvin committed Nov 21, 2018
1 parent 743a8df commit 54b55a0
Show file tree
Hide file tree
Showing 17 changed files with 393 additions and 182 deletions.
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ add_library( # Sets the name of the library.
src/main/cpp/GestureDelegate.cpp
src/main/cpp/LoadingAnimation.cpp
src/main/cpp/JNIUtil.cpp
src/main/cpp/Pointer.cpp
src/main/cpp/Skybox.cpp
src/main/cpp/SplashAnimation.cpp
src/main/cpp/VRBrowser.cpp
Expand Down
32 changes: 32 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.SurfaceTexture;
import android.net.Uri;
import android.opengl.GLES11Ext;
Expand Down Expand Up @@ -605,6 +609,34 @@ void resumeGeckoViewCompositor() {
}, 20);
}

@Keep
@SuppressWarnings("unused")
void renderPointerLayer(final Surface aSurface) {
runOnUiThread(() -> {
try {
Canvas canvas = aSurface.lockHardwareCanvas();
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
final float x = canvas.getWidth() * 0.5f;
final float y = canvas.getHeight() * 0.5f;
final float radius = canvas.getWidth() * 0.4f;
canvas.drawCircle(x, y, radius, paint);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(4);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(x, y, radius, paint);
aSurface.unlockCanvasAndPost(canvas);
}
catch (Exception ex) {
ex.printStackTrace();
}
});
}

@Keep
@SuppressWarnings("unused")
String getStorageAbsolutePath() {
Expand Down
84 changes: 36 additions & 48 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "LoadingAnimation.h"
#include "Skybox.h"
#include "SplashAnimation.h"
#include "Pointer.h"
#include "Widget.h"
#include "WidgetPlacement.h"
#include "Quad.h"
Expand Down Expand Up @@ -182,7 +183,7 @@ struct BrowserWorld::State {
rootController->AddLight(light);
cullVisitor = CullVisitor::Create(create);
drawList = DrawableList::Create(create);
controllers = ControllerContainer::Create(create);
controllers = ControllerContainer::Create(create, rootTransparent);
externalVR = ExternalVR::Create();
blitter = ExternalBlitter::Create(create);
fadeAnimation = FadeAnimation::Create(create);
Expand Down Expand Up @@ -254,14 +255,13 @@ ThrottleHoverEvent(Controller& aController, const double aTimestamp, const bool

void
BrowserWorld::State::UpdateControllers(bool& aRelayoutWidgets) {
std::vector<Widget*> active;
for (const WidgetPtr& widget: widgets) {
widget->TogglePointer(false);
}
for (Controller& controller: controllers->GetControllers()) {
if (!controller.enabled || (controller.index < 0)) {
continue;
}
if (controller.pointer && !controller.pointer->IsLoaded()) {
controller.pointer->Load(device);
}

if (!(controller.lastButtonState & ControllerDelegate::BUTTON_APP) && (controller.buttonState & ControllerDelegate::BUTTON_APP)) {
VRBrowser::HandleBack();
Expand Down Expand Up @@ -290,12 +290,20 @@ BrowserWorld::State::UpdateControllers(bool& aRelayoutWidgets) {
resizingWidget.reset();
}

if (controller.pointer) {
controller.pointer->SetVisible(hitWidget.get() != nullptr);
controller.pointer->SetHitWidget(hitWidget);
if (hitWidget) {
vrb::Matrix translate = vrb::Matrix::Translation(vrb::Vector(hitPoint.x(), hitPoint.y(), 0.001f));
controller.pointer->SetTransform(hitWidget->GetTransform().PostMultiply(translate));
}
}

const bool pressed = controller.buttonState & ControllerDelegate::BUTTON_TRIGGER ||
controller.buttonState & ControllerDelegate::BUTTON_TOUCHPAD;
const bool wasPressed = controller.lastButtonState & ControllerDelegate::BUTTON_TRIGGER ||
controller.lastButtonState & ControllerDelegate::BUTTON_TOUCHPAD;
if (hitWidget && hitWidget->IsResizing()) {
active.push_back(hitWidget.get());
bool aResized = false, aResizeEnded = false;
hitWidget->HandleResize(hitPoint, pressed, aResized, aResizeEnded);

Expand All @@ -316,7 +324,6 @@ BrowserWorld::State::UpdateControllers(bool& aRelayoutWidgets) {
}
}
else if (hitWidget) {
active.push_back(hitWidget.get());
float theX = 0.0f, theY = 0.0f;
hitWidget->ConvertToWidgetCoordinates(hitPoint, theX, theY);
const uint32_t handle = hitWidget->GetHandle();
Expand Down Expand Up @@ -366,10 +373,6 @@ BrowserWorld::State::UpdateControllers(bool& aRelayoutWidgets) {
}
controller.lastButtonState = controller.buttonState;
}
for (Widget* widget: active) {
widget->TogglePointer(true);
}
active.clear();
if (gestures) {
const int32_t gestureCount = gestures->GetGestureCount();
for (int32_t count = 0; count < gestureCount; count++) {
Expand Down Expand Up @@ -497,7 +500,7 @@ BrowserWorld::InitializeJava(JNIEnv* aEnv, jobject& aActivity, jobject& aAssetMa
m.controllers->LoadControllerModel(index, m.loader, fileName);
}
}
m.controllers->InitializePointer();
m.controllers->InitializeBeam();
m.controllers->SetPointerColor(vrb::Color(VRBrowser::GetPointerColor()));
m.loadingAnimation->LoadModels(m.loader);
m.rootController->AddNode(m.controllers->GetRoot());
Expand Down Expand Up @@ -656,9 +659,6 @@ BrowserWorld::UpdatePointerColor() {
int32_t color = VRBrowser::GetPointerColor();
VRB_LOG("Setting pointer color to: %d:", color);

for (const WidgetPtr& widget: m.widgets) {
widget->SetPointerColor(vrb::Color(color));
}
if (m.controllers)
m.controllers->SetPointerColor(vrb::Color(color));
}
Expand Down Expand Up @@ -714,12 +714,6 @@ BrowserWorld::AddWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement) {

m.widgets.push_back(widget);
UpdateWidget(widget->GetHandle(), aPlacement);

if (!aPlacement->showPointer) {
vrb::NodePtr emptyNode = vrb::Group::Create(m.create);
widget->SetPointerGeometry(emptyNode);
}
widget->SetPointerColor(vrb::Color(VRBrowser::GetPointerColor()));
}

void
Expand Down Expand Up @@ -941,8 +935,8 @@ BrowserWorld::DrawWorld() {
m.skybox->SetTransform(vrb::Matrix::Translation(headPosition));
}
m.rootTransparent->SortNodes([=](const NodePtr& a, const NodePtr& b) {
float da = DistanceToPlane(GetWidgetFromNode(a), headPosition, headDirection);
float db = DistanceToPlane(GetWidgetFromNode(b), headPosition, headDirection);
float da = DistanceToPlane(a, headPosition, headDirection);
float db = DistanceToPlane(b, headPosition, headDirection);
if (da < 0.0f) {
da = std::numeric_limits<float>::max();
}
Expand Down Expand Up @@ -1122,41 +1116,35 @@ BrowserWorld::CreateFloor() {
}

float
BrowserWorld::DistanceToNode(const vrb::NodePtr& aTargetNode, const vrb::Vector& aPosition) const {
ASSERT_ON_RENDER_THREAD(0.0f);
float result = -1;
Node::Traverse(aTargetNode, [&](const NodePtr &aNode, const GroupPtr &aTraversingFrom) {
vrb::TransformPtr transform = std::dynamic_pointer_cast<vrb::Transform>(aNode);
if (transform) {
vrb::Vector targetPos = transform->GetTransform().GetTranslation();
result = (targetPos - aPosition).Magnitude();
return true;
}
return false;
});

return result;
}

WidgetPtr
BrowserWorld::GetWidgetFromNode(const vrb::NodePtr& aNode) const {
BrowserWorld::DistanceToPlane(const vrb::NodePtr& aNode, const vrb::Vector& aPosition, const vrb::Vector& aDirection) const {
WidgetPtr target;
bool pointer = false;
for (const auto & widget: m.widgets) {
if (widget->GetRoot() == aNode) {
return widget;
target = widget;
break;
}
}
if (!target) {
for (Controller& controller: m.controllers->GetControllers()) {
if (controller.pointer && controller.pointer->GetRoot() == aNode) {
target = controller.pointer->GetHitWidget();
pointer = true;
break;
}
}
}
return nullptr;
}

float
BrowserWorld::DistanceToPlane(const WidgetPtr& aWidget, const vrb::Vector& aPosition, const vrb::Vector& aDirection) const {
if (!aWidget) {
if (!target) {
return -1.0f;
}
vrb::Vector result;
bool inside = false;
float distance = -1.0f;
aWidget->GetQuad()->TestIntersection(aPosition, aDirection, result, false, inside, distance);
target->GetQuad()->TestIntersection(aPosition, aDirection, result, false, inside, distance);
if (pointer) {
distance-= 0.001f;
}
return distance;
}

Expand Down
4 changes: 1 addition & 3 deletions app/src/main/cpp/BrowserWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ class BrowserWorld {
void DrawSplashAnimation();
void CreateSkyBox(const std::string& basePath);
void CreateFloor();
float DistanceToNode(const vrb::NodePtr& aNode, const vrb::Vector& aPosition) const;
WidgetPtr GetWidgetFromNode(const vrb::NodePtr& aNode) const;
float DistanceToPlane(const WidgetPtr& aNode, const vrb::Vector& aPosition, const vrb::Vector& aDirection) const;
float DistanceToPlane(const vrb::NodePtr& aNode, const vrb::Vector& aPosition, const vrb::Vector& aDirection) const;
private:
State& m;
BrowserWorld() = delete;
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/cpp/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Controller::operator=(const Controller& aController) {
scrollDeltaX = aController.scrollDeltaX;
scrollDeltaY = aController.scrollDeltaY;
transform = aController.transform;
pointer = aController.pointer;
transformMatrix = aController.transformMatrix;
immersiveName = aController.immersiveName;
immersivePressedState = aController.immersivePressedState;
Expand All @@ -68,9 +69,8 @@ Controller::Reset() {
touchX = touchY = 0.0f;
lastTouchX = lastTouchY = 0.0f;
scrollDeltaX = scrollDeltaY = 0.0f;
if (transform) {
transform = nullptr;
}
transform = nullptr;
pointer = nullptr;
transformMatrix = Matrix::Identity();
immersiveName.clear();
immersivePressedState = 0;
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/cpp/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace crow {

class Pointer;
typedef std::shared_ptr<Pointer> PointerPtr;

static const int kControllerMaxButtonCount = 4;
static const int kControllerMaxAxes = 6;

Expand All @@ -32,6 +35,7 @@ struct Controller {
float scrollDeltaX;
float scrollDeltaY;
vrb::TransformPtr transform;
PointerPtr pointer;
vrb::Matrix transformMatrix;
std::string immersiveName;
uint64_t immersivePressedState;
Expand Down
35 changes: 26 additions & 9 deletions app/src/main/cpp/ControllerContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "ControllerContainer.h"
#include "Controller.h"
#include "Pointer.h"

#include "vrb/ConcreteClass.h"
#include "vrb/Color.h"
Expand All @@ -26,8 +27,9 @@ struct ControllerContainer::State {
std::vector<Controller> list;
CreationContextWeak context;
TogglePtr root;
GroupPtr pointerContainer;
std::vector<GroupPtr> models;
GeometryPtr pointerModel;
GeometryPtr beamModel;
bool visible;

void Initialize(vrb::CreationContextPtr& aContext) {
Expand All @@ -52,8 +54,10 @@ struct ControllerContainer::State {
};

ControllerContainerPtr
ControllerContainer::Create(vrb::CreationContextPtr& aContext) {
return std::make_shared<vrb::ConcreteClass<ControllerContainer, ControllerContainer::State> >(aContext);
ControllerContainer::Create(vrb::CreationContextPtr& aContext, const vrb::GroupPtr& aPointerContainer) {
auto result = std::make_shared<vrb::ConcreteClass<ControllerContainer, ControllerContainer::State> >(aContext);
result->m.pointerContainer = aPointerContainer;
return result;
}


Expand All @@ -70,8 +74,8 @@ ControllerContainer::LoadControllerModel(const int32_t aModelIndex, const ModelL
}

void
ControllerContainer::InitializePointer() {
if (m.pointerModel) {
ControllerContainer::InitializeBeam() {
if (m.beamModel) {
return;
}
CreationContextPtr create = m.context.lock();
Expand Down Expand Up @@ -125,10 +129,10 @@ ControllerContainer::InitializePointer() {
index.push_back(5);
geometry->AddFace(index, uvIndex, index);

m.pointerModel = std::move(geometry);
m.beamModel = std::move(geometry);
for (Controller& controller: m.list) {
if (controller.transform) {
controller.transform->AddNode(m.pointerModel);
controller.transform->AddNode(m.beamModel);
}
}
}
Expand All @@ -139,6 +143,9 @@ ControllerContainer::Reset() {
if (controller.transform) {
controller.transform->RemoveFromParents();
}
if (controller.pointer) {
controller.pointer->GetRoot()->RemoveFromParents();
}
controller.Reset();
}
}
Expand Down Expand Up @@ -167,15 +174,19 @@ ControllerContainer::CreateController(const int32_t aControllerIndex, const int3
m.SetUpModelsGroup(aModelIndex);
CreationContextPtr create = m.context.lock();
controller.transform = Transform::Create(create);
controller.pointer = Pointer::Create(create);
if ((m.models.size() >= aModelIndex) && m.models[aModelIndex]) {
controller.transform->AddNode(m.models[aModelIndex]);
if (m.pointerModel) {
controller.transform->AddNode(m.pointerModel);
if (m.beamModel) {
controller.transform->AddNode(m.beamModel);
}
if (m.root) {
m.root->AddNode(controller.transform);
m.root->ToggleChild(*controller.transform, false);
}
if (m.pointerContainer) {
m.pointerContainer->AddNode(controller.pointer->GetRoot());
}
} else {
VRB_ERROR("Failed to add controller model");
}
Expand Down Expand Up @@ -209,6 +220,9 @@ ControllerContainer::SetVisible(const int32_t aControllerIndex, const bool aVisi
if (controller.transform && m.visible) {
m.root->ToggleChild(*controller.transform, aVisible);
}
if (controller.pointer && !aVisible) {
controller.pointer->SetVisible(false);
}
}

void
Expand Down Expand Up @@ -322,6 +336,9 @@ void ControllerContainer::SetPointerColor(const vrb::Color& aColor) const {
GeometryPtr geometry = std::dynamic_pointer_cast<vrb::Geometry>(controller.transform->GetNode(1));
geometry->GetRenderState()->SetMaterial(aColor, aColor, vrb::Color(0.0f, 0.0f, 0.0f), 0.0f);
}
if (controller.pointer) {
controller.pointer->SetPointerColor(aColor);
}
}
}

Expand Down
Loading

0 comments on commit 54b55a0

Please sign in to comment.