Skip to content
This repository was archived by the owner on Dec 26, 2018. It is now read-only.

Commit fd63221

Browse files
author
Yuya Matsuo
committed
Refactor GearVRActivity.isLookingAt to LookDetector.isLookingAt
1 parent 752582b commit fd63221

File tree

8 files changed

+71
-41
lines changed

8 files changed

+71
-41
lines changed

library/src/main/java/org/meganekkovr/GearVRActivity.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public class GearVRActivity extends VrActivity implements MeganekkoContext {
2929

3030
private static native long nativeSetAppInterface(VrActivity act, String fromPackageNameString, String commandString, String uriString);
3131

32-
private static native boolean isLookingAt(long appPtr, long entityPointer, long geometryComponentPointer);
33-
3432
private static native void getCenterViewRotation(long appPtr, float[] values);
3533

3634
private static native void setClearColorBuffer(long appPtr, boolean clearColorBuffer);
@@ -65,6 +63,7 @@ protected void onCreate(Bundle savedInstanceState) {
6563
setAppPtr(appPtr);
6664

6765
OVRApp.init(appPtr);
66+
LookDetector.init(appPtr);
6867
}
6968

7069
/**
@@ -178,16 +177,7 @@ public Context getContext() {
178177

179178
@Override
180179
public boolean isLookingAt(Entity entity) {
181-
182-
if (!entity.isShown()) {
183-
return false;
184-
}
185-
186-
// Check if entity has geometry
187-
GeometryComponent geometryComponent = entity.getComponent(GeometryComponent.class);
188-
if (geometryComponent == null) return false;
189-
190-
return isLookingAt(getAppPtr(), entity.getNativePointer(), geometryComponent.getNativePointer());
180+
return LookDetector.getInstance().isLookingAt(entity);
191181
}
192182

193183
@Override
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.meganekkovr;
2+
3+
public class LookDetector {
4+
5+
private static LookDetector instance;
6+
private final long appPtr;
7+
8+
private LookDetector(long appPtr) {
9+
this.appPtr = appPtr;
10+
}
11+
12+
public synchronized static void init(long appPtr) {
13+
if (instance != null)
14+
throw new IllegalStateException("init was called twice!");
15+
16+
instance = new LookDetector(appPtr);
17+
}
18+
19+
public static LookDetector getInstance() {
20+
return instance;
21+
}
22+
23+
private static native boolean isLookingAt(long appPtr, long entityPtr, long geometryComponentPtr);
24+
25+
public boolean isLookingAt(Entity entity) {
26+
27+
if (!entity.isShown() || !entity.hasComponent(GeometryComponent.class)) {
28+
return false;
29+
}
30+
31+
GeometryComponent geometry = entity.getComponent(GeometryComponent.class);
32+
return isLookingAt(appPtr, entity.getNativePointer(), geometry.getNativePointer());
33+
}
34+
}

library/src/main/java/org/meganekkovr/LookDetectorComponent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public interface LookListener {
3232
void onLooking(Entity entity, FrameInput frame);
3333
}
3434

35+
private final LookDetector lookDetector = LookDetector.getInstance();
3536
private final LookListener lookListener;
3637
private boolean looking;
3738

@@ -43,7 +44,7 @@ public LookDetectorComponent(LookListener lookListener) {
4344
public void update(FrameInput frame) {
4445

4546
Entity entity = getEntity();
46-
boolean isLookingNow = entity.getApp().isLookingAt(entity);
47+
boolean isLookingNow = lookDetector.isLookingAt(entity);
4748

4849
if (isLookingNow) {
4950

library/src/main/java/org/meganekkovr/MeganekkoApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public boolean onKeyMax(int keyCode, int repeatCount) {
149149
* @return {@code true} if user is looking at. Otherwise {@code false}.
150150
*/
151151
public boolean isLookingAt(Entity entity) {
152-
return context.isLookingAt(entity);
152+
return LookDetector.getInstance().isLookingAt(entity);
153153
}
154154

155155
/**

library/src/main/jni/GearVRActivity.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "Android/JniUtils.h"
1818
#include "Entity.h"
1919
#include "GeometryComponent.h"
20-
#include "LookDetection.h"
2120
#include "util/convert.h"
2221
#include <jni.h>
2322

@@ -197,27 +196,6 @@ jlong Java_org_meganekkovr_GearVRActivity_nativeSetAppInterface(
197196
uriString);
198197
}
199198

200-
jboolean Java_org_meganekkovr_GearVRActivity_isLookingAt(JNIEnv *jni,
201-
jclass clazz,
202-
jlong appPtr,
203-
jlong entityPtr,
204-
jlong geoCompPtr) {
205-
mgn::GearVRActivity *activity =
206-
(mgn::GearVRActivity *)((App *)appPtr)->GetAppInterface();
207-
Matrix4f centerM = activity->GetCenterEyeViewMatrix();
208-
209-
mgn::Entity *entity = reinterpret_cast<mgn::Entity *>(entityPtr);
210-
Matrix4f m = entity->GetWorldModelMatrix();
211-
212-
mgn::GeometryComponent *geoComp =
213-
reinterpret_cast<mgn::GeometryComponent *>(geoCompPtr);
214-
GlGeometry geo = geoComp->GetGeometry();
215-
216-
mgn::IntersectRayBoundsResult result =
217-
mgn::IntersectRayBounds(centerM, m, geo, false);
218-
return result.intersected;
219-
}
220-
221199
void Java_org_meganekkovr_GearVRActivity_getCenterViewRotation(
222200
JNIEnv *jni, jclass clazz, jlong appPtr, jfloatArray values) {
223201
mgn::GearVRActivity *activity =
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#include "LookDetection.h"
16+
#include "LookDetector.h"
17+
#include "App.h"
18+
#include "GeometryComponent.h"
1719

1820
using namespace OVR;
1921

@@ -56,4 +58,27 @@ IntersectRayBoundsResult IntersectRayBounds(const Matrix4f &centerViewMatrix,
5658

5759
return result;
5860
}
59-
}
61+
}
62+
63+
extern "C" {
64+
65+
jboolean Java_org_meganekkovr_LookDetector_isLookingAt(JNIEnv *jni,
66+
jclass clazz,
67+
jlong appPtr,
68+
jlong entityPtr,
69+
jlong geoCompPtr) {
70+
App *app = reinterpret_cast<App *>(appPtr);
71+
Matrix4f viewM = app->GetLastViewMatrix();
72+
73+
mgn::Entity *entity = reinterpret_cast<mgn::Entity *>(entityPtr);
74+
Matrix4f modelM = entity->GetWorldModelMatrix();
75+
76+
mgn::GeometryComponent *geoComp =
77+
reinterpret_cast<mgn::GeometryComponent *>(geoCompPtr);
78+
GlGeometry geo = geoComp->GetGeometry();
79+
80+
mgn::IntersectRayBoundsResult result =
81+
mgn::IntersectRayBounds(viewM, modelM, geo, false);
82+
return result.intersected;
83+
}
84+
} // extern "C"

samplev3/src/main/java/org/meganekkovr/sample/FirstScene.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.meganekkovr.Entity;
1010
import org.meganekkovr.FrameInput;
1111
import org.meganekkovr.JoyButton;
12+
import org.meganekkovr.LookDetector;
1213
import org.meganekkovr.LookDetectorComponent;
1314
import org.meganekkovr.Scene;
1415
import org.meganekkovr.SurfaceRendererComponent;
@@ -85,7 +86,7 @@ public void onLooking(Entity entity, FrameInput frame) {
8586
@Override
8687
public void update(FrameInput frame) {
8788

88-
if (getApp().isLookingAt(planeEntity)) {
89+
if (LookDetector.getInstance().isLookingAt(planeEntity)) {
8990
planeRenderer.color = Color.RED;
9091
} else {
9192
planeRenderer.color = Color.YELLOW;
@@ -105,10 +106,11 @@ public void update(FrameInput frame) {
105106
}
106107

107108
private void onSingleTouchDetected() {
108-
if (getApp().isLookingAt(button)) {
109+
LookDetector lookDetector = LookDetector.getInstance();
110+
if (lookDetector.isLookingAt(button)) {
109111
MyApp app = (MyApp) getApp();
110112
app.onTapButton();
111-
} else if (getApp().isLookingAt(videocam)) {
113+
} else if (lookDetector.isLookingAt(videocam)) {
112114
MyApp app = (MyApp) getApp();
113115
app.onTapVideocam();
114116
}

0 commit comments

Comments
 (0)