Skip to content

Commit

Permalink
Implement multiwindow hover and click border effect.
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Aug 20, 2019
1 parent 35f9ae8 commit 3c65059
Show file tree
Hide file tree
Showing 16 changed files with 455 additions and 163 deletions.
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ add_library( # Sets the name of the library.
src/main/cpp/VRLayer.cpp
src/main/cpp/VRLayerNode.cpp
src/main/cpp/Widget.cpp
src/main/cpp/WidgetBorder.cpp
src/main/cpp/WidgetMover.cpp
src/main/cpp/WidgetPlacement.cpp
src/main/cpp/WidgetResizer.cpp
Expand Down
14 changes: 0 additions & 14 deletions app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -648,20 +648,6 @@ void handleMotionEvent(final int aHandle, final int aDevice, final boolean aPres
if (!isWidgetInputEnabled(widget)) {
widget = null; // Fallback to mRootWidget in order to allow world clicks to dismiss UI.
}
if (widget instanceof WindowWidget) {
WindowWidget window = (WindowWidget) widget;
boolean focused = mWindows.getFocusedWindow() == window;
if (!focused && aPressed) {
// Focus the window when pressed
mWindows.focusWindow(window);
// Discard first click.
return;
} else if (!focused) {
// Do not send hover events to not focused windows.
widget = null;
}
}


float scale = widget != null ? widget.getPlacement().textureScale : 1.0f;
final float x = aX / scale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public WidgetPlacement(Context aContext) {
public float textureScale = 0.7f;
// Widget will be curved if enabled.
public boolean cylinder = true;
public int borderColor = 0;
/*
* Flat surface placements are automatically mapped to curved coordinates.
* If a radius is set it's used for the automatic mapping of the yaw & angle when the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.mozilla.vrbrowser.ui.widgets.prompts.PromptWidget;
import org.mozilla.vrbrowser.ui.widgets.prompts.TextPromptWidget;
import org.mozilla.vrbrowser.utils.InternalPages;
import org.mozilla.vrbrowser.utils.ViewUtils;

import java.util.ArrayList;

Expand Down Expand Up @@ -91,7 +92,14 @@ public class WindowWidget extends UIWidget implements SessionChangeListener,
private Windows.WindowPlacement mWindowPlacement = Windows.WindowPlacement.FRONT;
private float mMaxWindowScale = 3;
private boolean mIsRestored = false;
private WindowDelegate mWindowDelegate;
boolean mActive = false;
boolean mHovered = false;
boolean mClickedAfterFocus = false;

public interface WindowDelegate {
void onFocusRequest(WindowWidget aWindow);
}

public WindowWidget(Context aContext, int windowId, boolean privateMode) {
super(aContext);
Expand Down Expand Up @@ -388,6 +396,7 @@ public void setActiveWindow(boolean active) {
}

TelemetryWrapper.activePlacementEvent(mWindowPlacement.getValue(), mActive);
updateBorder();
}

public SessionStack getSessionStack() {
Expand Down Expand Up @@ -489,6 +498,26 @@ public WidgetPlacement getPlacement() {
@Override
public void handleTouchEvent(MotionEvent aEvent) {
mLastMouseClickPos = new Point((int)aEvent.getX(), (int)aEvent.getY());
if (aEvent.getAction() == MotionEvent.ACTION_DOWN) {
if (!mActive) {
mClickedAfterFocus = true;
updateBorder();
if (mWindowDelegate != null) {
// Focus this window
mWindowDelegate.onFocusRequest(this);
}
// Return to discard first click after focus
return;
}
} else if (aEvent.getAction() == MotionEvent.ACTION_UP || aEvent.getAction() == MotionEvent.ACTION_CANCEL) {
mClickedAfterFocus = false;
updateBorder();
}

if (!mActive) {
// Do not send touch events to not focused windows.
return;
}

if (mView != null) {
super.handleTouchEvent(aEvent);
Expand All @@ -508,6 +537,19 @@ public void handleTouchEvent(MotionEvent aEvent) {

@Override
public void handleHoverEvent(MotionEvent aEvent) {
if (aEvent.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
mHovered = true;
updateBorder();
} else if (aEvent.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
mHovered = false;
updateBorder();
}

if (!mActive) {
// Do not send touch events to not focused windows.
return;
}

if (mView != null) {
super.handleHoverEvent(aEvent);

Expand All @@ -521,6 +563,23 @@ public void handleHoverEvent(MotionEvent aEvent) {
}
}

protected void updateBorder() {
int color = 0;
if (!mActive && !mClickedAfterFocus && mHovered) {
color = ViewUtils.ARGBtoRGBA(getContext().getColor(R.color.window_border_hover));
} else if (mClickedAfterFocus) {
color = ViewUtils.ARGBtoRGBA(getContext().getColor(R.color.window_border_click));
}
if (mWidgetPlacement.borderColor != color) {
mWidgetPlacement.borderColor = color;
mWidgetManager.updateWidget(this);
}
}

public void setWindowDelegate(WindowDelegate aDelegate) {
mWindowDelegate = aDelegate;
}

@Override
public void handleResizeEvent(float aWorldWidth, float aWorldHeight) {
int width = getWindowWidth(aWorldWidth);
Expand Down Expand Up @@ -986,5 +1045,4 @@ public GeckoResult<AllowOrDeny> onLoadRequest(@NonNull GeckoSession session, @No

return GeckoResult.ALLOW;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ private void removeWindow(@NonNull WindowWidget aWindow) {
mPrivateWindows.remove(aWindow);
aWindow.getTopBar().setVisible(false);
aWindow.getTopBar().setDelegate((TopBarWidget.Delegate) null);
aWindow.setWindowDelegate(null);
aWindow.getSessionStack().removeContentListener(this);
aWindow.close();
updateMaxWindowScales();
Expand Down Expand Up @@ -664,6 +665,7 @@ private void updateTopBars() {
private WindowWidget createWindow() {
int newWindowId = sIndex++;
WindowWidget window = new WindowWidget(mContext, newWindowId, mPrivateMode);
window.setWindowDelegate(this::focusWindow);
getCurrentWindows().add(window);
window.getTopBar().setDelegate(this);
window.getSessionStack().addContentListener(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.mozilla.vrbrowser.utils;

import android.graphics.Color;
import android.os.Build;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
Expand Down Expand Up @@ -117,4 +119,7 @@ public static boolean isInsideView(@NotNull View view, int rx, int ry) {
return true;
}

public static int ARGBtoRGBA(int c) {
return (c & 0x00FFFFFF) << 8 | (c & 0xFF000000) >>> 24;
}
}
1 change: 1 addition & 0 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ BrowserWorld::UpdateWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement
widget->SetWorldWidth(newWorldWidth);
}

widget->SetBorderColor(vrb::Color(aPlacement->borderColor));
LayoutWidget(aHandle);
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/cpp/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef VRBROWSER_DEVICE_H
#define VRBROWSER_DEVICE_H

#include <stdint.h>

namespace crow {
namespace device {
typedef uint32_t CapabilityFlags;
Expand Down
60 changes: 56 additions & 4 deletions app/src/main/cpp/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "VRBrowser.h"
#include "WidgetPlacement.h"
#include "WidgetResizer.h"
#include "WidgetBorder.h"
#include "vrb/ConcreteClass.h"

#include "vrb/Color.h"
Expand All @@ -26,6 +27,14 @@

namespace crow {

static const float kFrameSize = 0.02f;
#if defined(OCULUSVR)
static const float kBorder = 0.0f;
#else
static const float kBorder = kFrameSize * 0.15f;
#endif


struct Widget::State {
vrb::RenderContextWeak context;
std::string name;
Expand All @@ -43,6 +52,8 @@ struct Widget::State {
WidgetResizerPtr resizer;
bool resizing;
bool toggleState;
vrb::TogglePtr bordersContainer;
std::vector<WidgetBorderPtr> borders;

State()
: handle(0)
Expand Down Expand Up @@ -169,6 +180,14 @@ struct Widget::State {
resizer = nullptr;
}
}

void RemoveBorder() {
if (bordersContainer) {
bordersContainer->RemoveFromParents();
bordersContainer = nullptr;
}
borders.clear();
}
};

WidgetPtr
Expand All @@ -185,7 +204,7 @@ Widget::Create(vrb::RenderContextPtr& aContext, const int aHandle, const float a
const int32_t aTextureWidth, const int32_t aTextureHeight, const CylinderPtr& aCylinder) {
WidgetPtr result = std::make_shared<vrb::ConcreteClass<Widget, Widget::State> >(aContext);
result->m.min = vrb::Vector(-aWorldWidth * 0.5f, -aWorldHeight * 0.5f, 0.0f);
result->m.max = vrb::Vector(aWorldWidth *0.5f, aWorldHeight * 0.5f, 0.0f);
result->m.max = vrb::Vector(aWorldWidth * 0.5f, aWorldHeight * 0.5f, 0.0f);
result->m.Initialize(aHandle, aTextureWidth, aTextureHeight, nullptr, aCylinder);
return result;
}
Expand Down Expand Up @@ -247,6 +266,7 @@ Widget::SetWorldWidth(float aWorldWidth) const {
const float aspect = (float)width / (float) height;
const float worldHeight = aWorldWidth / aspect;

const float oldWidth = m.max.x() - m.min.x();
m.min = vrb::Vector(-aWorldWidth * 0.5f, -worldHeight * 0.5f, 0.0f);
m.max = vrb::Vector(aWorldWidth *0.5f, worldHeight * 0.5f, 0.0f);

Expand All @@ -260,6 +280,10 @@ Widget::SetWorldWidth(float aWorldWidth) const {
if (m.resizing && m.resizer) {
m.resizer->SetSize(m.min, m.max);
}

if (oldWidth != aWorldWidth) {
m.RemoveBorder();
}
}

void
Expand Down Expand Up @@ -346,7 +370,7 @@ Widget::GetCylinder() const {
}

void
Widget::SetQuad(const QuadPtr& aQuad) const {
Widget::SetQuad(const QuadPtr& aQuad) {
int32_t textureWidth, textureHeight;
GetSurfaceTextureSize(textureWidth, textureHeight);
if (m.cylinder) {
Expand All @@ -362,11 +386,12 @@ Widget::SetQuad(const QuadPtr& aQuad) const {
m.transformContainer->SetTransform(vrb::Matrix::Identity());

m.RemoveResizer();
m.RemoveBorder();
m.UpdateSurface(textureWidth, textureHeight);
}

void
Widget::SetCylinder(const CylinderPtr& aCylinder) const {
Widget::SetCylinder(const CylinderPtr& aCylinder) {
int32_t textureWidth, textureHeight;
GetSurfaceTextureSize(textureWidth, textureHeight);
if (m.quad) {
Expand All @@ -381,6 +406,7 @@ Widget::SetCylinder(const CylinderPtr& aCylinder) const {
m.transform->AddNode(aCylinder->GetRoot());

m.RemoveResizer();
m.RemoveBorder();
m.UpdateSurface(textureWidth, textureHeight);
}

Expand Down Expand Up @@ -461,14 +487,14 @@ void
Widget::HandleResize(const vrb::Vector& aPoint, bool aPressed, bool& aResized, bool &aResizeEnded) {
m.resizer->HandleResizeGestures(aPoint, aPressed, aResized, aResizeEnded);
if (aResized || aResizeEnded) {

m.min = m.resizer->GetResizeMin();
m.max = m.resizer->GetResizeMax();
if (m.quad) {
m.quad->SetWorldSize(m.min, m.max);
} else if (m.cylinder) {
m.UpdateCylinderMatrix();
}
m.RemoveBorder();
}
}

Expand All @@ -492,6 +518,32 @@ Widget::GetCylinderDensity() const {
return m.cylinderDensity;
}

void
Widget::SetBorderColor(const vrb::Color &aColor) {
const bool visible = aColor.Alpha() > 0.0f;
if (!visible && m.bordersContainer) {
m.bordersContainer->ToggleAll(false);
return;
}
if (visible && !m.bordersContainer) {
vrb::RenderContextPtr render = m.context.lock();
vrb::CreationContextPtr create = render->GetRenderThreadCreationContext();
m.bordersContainer = vrb::Toggle::Create(create);
m.borders = WidgetBorder::CreateFrame(create, *this, kFrameSize, kBorder);
for (const WidgetBorderPtr& border: m.borders) {
m.bordersContainer->AddNode(border->GetTransformNode());
}
m.transform->InsertNode(m.bordersContainer, 0);
}

if (visible) {
m.bordersContainer->ToggleAll(true);
for (const WidgetBorderPtr& border: m.borders) {
border->SetColor(aColor);
}
}
}

Widget::Widget(State& aState, vrb::RenderContextPtr& aContext) : m(aState) {
m.context = aContext;
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/cpp/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class Widget {
vrb::NodePtr GetRoot() const;
QuadPtr GetQuad() const;
CylinderPtr GetCylinder() const;
void SetQuad(const QuadPtr& aQuad) const;
void SetCylinder(const CylinderPtr& aCylinder) const;
void SetQuad(const QuadPtr& aQuad);
void SetCylinder(const CylinderPtr& aCylinder);
VRLayerSurfacePtr GetLayer() const;
vrb::TransformPtr GetTransformNode() const;
const WidgetPlacementPtr& GetPlacement() const;
Expand All @@ -74,6 +74,7 @@ class Widget {
void HoverExitResize();
void SetCylinderDensity(const float aDensity);
float GetCylinderDensity() const;
void SetBorderColor(const vrb::Color& aColor);
protected:
struct State;
Widget(State& aState, vrb::RenderContextPtr& aContext);
Expand Down

0 comments on commit 3c65059

Please sign in to comment.