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

Add Android widgets #4

Merged
merged 2 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;

public class PlatformActivity extends NativeActivity {
static String LOGTAG = "VRBrowser";
private SurfaceView mSurfaceView;
private FrameLayout mFrameLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -22,11 +25,19 @@ protected void onCreate(Bundle savedInstanceState) {

getWindow().takeSurface(null);
getWindow().takeInputQueue(null);

mFrameLayout = new FrameLayout(this);
SurfaceView surfaceView = new SurfaceView(this);
surfaceView.setClickable(true);
surfaceView.getHolder().addCallback(this);
surfaceView.setZOrderOnTop(true);
surfaceView.setBackgroundColor(Color.BLUE);
mFrameLayout.addView(surfaceView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

setContentView(mFrameLayout);
}

setContentView(surfaceView);
protected void addWidget(View aView, int aWidth, int aHeight) {
mFrameLayout.addView(aView, 0, new FrameLayout.LayoutParams(aWidth, aHeight));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public void setSurfaceTexture(SurfaceTexture aTexture, final int aWidth, final i
}

@Override
public void onTouchEvent(MotionEvent aEvent) {
public void handleTouchEvent(MotionEvent aEvent) {
mSession.getPanZoomController().onTouchEvent(aEvent);
}

@Override
public void onHoverEvent(MotionEvent aEvent) {
public void handleHoverEvent(MotionEvent aEvent) {
mSession.getPanZoomController().onMotionEvent(aEvent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ static void dispatch(Widget aWidget, int aDevice, boolean aPressed, int aX, int
/*source*/ InputDevice.SOURCE_TOUCHSCREEN,
/*flags*/ 0);
if (hover) {
aWidget.onHoverEvent(event);
aWidget.handleHoverEvent(event);
return;
}
aWidget.onTouchEvent(event);
aWidget.handleTouchEvent(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
package org.mozilla.vrbrowser;

import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.SurfaceTexture;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Keep;
import android.util.Log;
import android.view.View;

import org.mozilla.gecko.GeckoSession;

Expand Down Expand Up @@ -71,12 +71,19 @@ void createWidget(final int aType, final int aHandle, SurfaceTexture aTexture, i
mTargetUrl = "";
}
widget = mCurrentBrowser;
} else if (aType == Widget.URLBar) {
widget = (Widget) getLayoutInflater().inflate(R.layout.url, null);
}

if (widget != null) {
widget.setSurfaceTexture(aTexture, aWidth, aHeight);
mWidgets.put(aHandle, widget);
}

if (aType != Widget.Browser) {
// Add hidden UI widget to the platform window for invalidation
addWidget((View) widget, aWidth, aHeight);
}
}

@Keep
Expand Down
8 changes: 4 additions & 4 deletions app/src/common/shared/org/mozilla/vrbrowser/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

package org.mozilla.vrbrowser;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.view.MotionEvent;

interface Widget {
public interface Widget {
int Browser = 0;
int URLBar = 1;
void setSurfaceTexture(SurfaceTexture aTexture, final int aWidth, final int aHeight);
void onTouchEvent(MotionEvent aEvent);
void onHoverEvent(MotionEvent aEvent);
void handleTouchEvent(MotionEvent aEvent);
void handleHoverEvent(MotionEvent aEvent);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */


package org.mozilla.vrbrowser.ui;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.SurfaceTexture;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.view.Surface;

class UISurfaceTextureRenderer {
private int mTextureWidth;
private int mTextureHeight;
private SurfaceTexture mSurfaceTexture;
private Surface mSurface;
private Canvas mSurfaceCanvas;

UISurfaceTextureRenderer(SurfaceTexture aTexture, int aWidth, int aHeight) {
mTextureWidth = aWidth;
mTextureHeight = aHeight;
mSurfaceTexture = aTexture;
mSurfaceTexture.setDefaultBufferSize(aWidth, aHeight);
mSurface = new Surface(mSurfaceTexture);
}

void release() {
if(mSurface != null){
mSurface.release();
}
if(mSurfaceTexture != null){
mSurfaceTexture.release();
}
mSurface = null;
mSurfaceTexture = null;
}

Canvas drawBegin() {
mSurfaceCanvas = null;
if (mSurface != null) {
try {
mSurfaceCanvas = mSurface.lockCanvas(null);
//mSurfaceCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
}
catch (Exception e){
e.printStackTrace();
}
}
return mSurfaceCanvas;
}

void drawEnd() {
if(mSurfaceCanvas != null) {
mSurface.unlockCanvasAndPost(mSurfaceCanvas);
}
mSurfaceCanvas = null;
}

int width() {
return mTextureWidth;
}

int height() {
return mTextureHeight;
}

}
83 changes: 83 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/ui/UIWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.vrbrowser.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.FrameLayout;

import org.mozilla.vrbrowser.Widget;

public class UIWidget extends FrameLayout implements Widget {
UISurfaceTextureRenderer mRenderer;

public UIWidget(Context aContext) {
super(aContext);
}

public UIWidget(Context aContext, AttributeSet aAttrs) {
super(aContext, aAttrs);
}

public UIWidget(Context aContext, AttributeSet aAttrs, int aDefStyle) {
super(aContext, aAttrs, aDefStyle);
}

@Override
public void setSurfaceTexture(SurfaceTexture aTexture, final int aWidth, final int aHeight) {
if (mRenderer != null) {
mRenderer.release();
}
if (aTexture != null) {
mRenderer = new UISurfaceTextureRenderer(aTexture, aWidth, aHeight);
}
setWillNotDraw(mRenderer == null);
}

@Override
public void handleTouchEvent(MotionEvent aEvent) {
this.dispatchTouchEvent(aEvent);
}

@Override
public void handleHoverEvent(MotionEvent aEvent) {
this.dispatchHoverEvent(aEvent);
}


@Override
public void draw(Canvas aCanvas) {
if (mRenderer == null) {
super.draw(aCanvas);
return;
}
Canvas textureCanvas = mRenderer.drawBegin();
if(textureCanvas != null) {
// set the proper scale
float xScale = textureCanvas.getWidth() / (float)aCanvas.getWidth();
textureCanvas.scale(xScale, xScale);
// draw the view to SurfaceTexture
super.draw(textureCanvas);
}
mRenderer.drawEnd();
}

@Override
public ViewParent invalidateChildInParent(int[] aLocation, Rect aDirty) {
ViewParent parent = super.invalidateChildInParent(aLocation, aDirty);
if (parent != null) {
// TODO: transform rect and use invalidate(dirty)
invalidate();
}
return parent;
}

}
44 changes: 44 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/ui/URLBarWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.vrbrowser.ui;

import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.ImageButton;

import org.mozilla.vrbrowser.R;

public class URLBarWidget extends UIWidget {
private ImageButton mBackButton;
private ImageButton mReloadButton;
private EditText mURL;

public URLBarWidget(Context aContext) {
super(aContext);
}

public URLBarWidget(Context aContext, AttributeSet aAttrs) {
super(aContext, aAttrs);
}

public URLBarWidget(Context aContext, AttributeSet aAttrs, int aDefStyle) {
super(aContext, aAttrs, aDefStyle);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
mBackButton = findViewById(R.id.backButton);
mReloadButton = findViewById(R.id.reloadButton);
mURL = findViewById(R.id.urlBar);
mURL.setRawInputType(InputType.TYPE_NULL);
mURL.setTextIsSelectable(false);
mURL.setCursorVisible(false);
mURL.setText("http://");
}
}
11 changes: 9 additions & 2 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ using namespace vrb;
namespace {

// Must be kept in sync with Widget.java
static const int BrowserWidgetType = 0;
static const int WidgetTypeBrowser = 0;
static const int WidgetTypeURLBar = 1;

static const char* kDispatchCreateWidgetName = "dispatchCreateWidget";
static const char* kDispatchCreateWidgetSignature = "(IILandroid/graphics/SurfaceTexture;II)V";
Expand Down Expand Up @@ -155,10 +156,16 @@ struct BrowserWorld::State {
root->AddLight(light);
cullVisitor = CullVisitor::Create(contextWeak);
drawList = DrawableList::Create(contextWeak);
WidgetPtr browser = Widget::Create(contextWeak, BrowserWidgetType);

WidgetPtr browser = Widget::Create(contextWeak, WidgetTypeBrowser);
browser->SetTransform(Matrix::Position(Vector(0.0f, -3.0f, -18.0f)));
root->AddNode(browser->GetRoot());
widgets.push_back(std::move(browser));

WidgetPtr urlbar = Widget::Create(contextWeak, WidgetTypeURLBar, 1920, 200, 9.0f);
urlbar->SetTransform(Matrix::Position(Vector(0.0f, 7.5f, -18.0f)));
root->AddNode(urlbar->GetRoot());
widgets.push_back(std::move(urlbar));
}
};

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/cpp/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ Widget::Create(vrb::ContextWeak aContext, const int32_t aType) {
return result;
}

WidgetPtr
Widget::Create(vrb::ContextWeak aContext, const int aType, const int32_t aWidth, const int32_t aHeight, float aWorldWidth) {
WidgetPtr result = std::make_shared<vrb::ConcreteClass<Widget, Widget::State> >(aContext);
result->m.textureWidth = aWidth;
result->m.textureHeight = aHeight;
const float aspect = (float)aWidth / (float)aHeight;
result->m.windowMin = vrb::Vector(-aWorldWidth, 0.0f, 0.0f);
result->m.windowMax = vrb::Vector(aWorldWidth, aWorldWidth/aspect * 2.0f, 0.0f);
result->m.Initialize(aType);
return result;
}

WidgetPtr
Widget::Create(vrb::ContextWeak aContext, const int aType, const int32_t aWidth, const int32_t aHeight, const vrb::Vector& aMin, const vrb::Vector& aMax) {
WidgetPtr result = std::make_shared<vrb::ConcreteClass<Widget, Widget::State> >(aContext);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef std::shared_ptr<Widget> WidgetPtr;
class Widget {
public:
static WidgetPtr Create(vrb::ContextWeak aContext, const int aType);
static WidgetPtr Create(vrb::ContextWeak aContext, const int aType, const int32_t aWidth, const int32_t aHeight, float aWorldWidth);
static WidgetPtr Create(vrb::ContextWeak aContext, const int aType, const int32_t aWidth, const int32_t aHeight, const vrb::Vector& aMin, const vrb::Vector& aMax);
int32_t GetType() const;
uint32_t GetHandle() const;
Expand Down
Loading