godot/platform/android/java/src/org/godotengine/godot/GodotView.java

171 lines
6.9 KiB
Java
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* GodotView.java */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
package org.godotengine.godot;
import android.annotation.SuppressLint;
2014-02-10 02:10:30 +01:00
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.view.KeyEvent;
import android.view.MotionEvent;
import org.godotengine.godot.input.GodotInputHandler;
import org.godotengine.godot.utils.GLUtils;
import org.godotengine.godot.xr.XRMode;
import org.godotengine.godot.xr.ovr.OvrConfigChooser;
import org.godotengine.godot.xr.ovr.OvrContextFactory;
import org.godotengine.godot.xr.ovr.OvrWindowSurfaceFactory;
import org.godotengine.godot.xr.pancake.PancakeConfigChooser;
import org.godotengine.godot.xr.pancake.PancakeContextFactory;
import org.godotengine.godot.xr.pancake.PancakeFallbackConfigChooser;
2014-02-10 02:10:30 +01:00
/**
* A simple GLSurfaceView sub-class that demonstrate how to perform
* OpenGL ES 2.0 rendering into a GL Surface. Note the following important
* details:
*
* - The class must use a custom context factory to enable 2.0 rendering.
* See ContextFactory class definition below.
*
* - The class must use a custom EGLConfigChooser to be able to select
* an EGLConfig that supports 2.0. This is done by providing a config
* specification to eglChooseConfig() that has the attribute
* EGL10.ELG_RENDERABLE_TYPE containing the EGL_OPENGL_ES2_BIT flag
* set. See ConfigChooser class definition below.
*
* - The class must select the surface's format, then choose an EGLConfig
* that matches it exactly (with regards to red/green/blue/alpha channels
* bit depths). Failure to do so would result in an EGL_BAD_MATCH error.
*/
public class GodotView extends GLSurfaceView {
2014-02-10 02:10:30 +01:00
private static String TAG = GodotView.class.getSimpleName();
2014-02-10 02:10:30 +01:00
private final Godot activity;
private final GodotInputHandler inputHandler;
2014-02-10 02:10:30 +01:00
public GodotView(Godot activity, XRMode xrMode, boolean p_use_gl3, boolean p_use_32_bits, boolean p_use_debug_opengl) {
super(activity);
GLUtils.use_gl3 = p_use_gl3;
GLUtils.use_32 = p_use_32_bits;
GLUtils.use_debug_opengl = p_use_debug_opengl;
this.activity = activity;
this.inputHandler = new GodotInputHandler(this);
init(xrMode, false, 16, 0);
}
2014-02-10 02:10:30 +01:00
public void initInputDevices() {
this.inputHandler.initInputDevices();
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
2018-01-07 15:11:58 +01:00
super.onTouchEvent(event);
2014-02-10 02:10:30 +01:00
return activity.gotTouchEvent(event);
}
2014-02-10 02:10:30 +01:00
@Override
public boolean onKeyUp(final int keyCode, KeyEvent event) {
return inputHandler.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event);
}
@Override
public boolean onKeyDown(final int keyCode, KeyEvent event) {
return inputHandler.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
return inputHandler.onGenericMotionEvent(event) || super.onGenericMotionEvent(event);
}
2014-02-10 02:10:30 +01:00
private void init(XRMode xrMode, boolean translucent, int depth, int stencil) {
2014-06-04 05:31:20 +02:00
setPreserveEGLContextOnPause(true);
setFocusableInTouchMode(true);
switch (xrMode) {
2014-02-10 02:10:30 +01:00
case OVR:
// Replace the default egl config chooser.
setEGLConfigChooser(new OvrConfigChooser());
// Replace the default context factory.
setEGLContextFactory(new OvrContextFactory());
// Replace the default window surface factory.
setEGLWindowSurfaceFactory(new OvrWindowSurfaceFactory());
break;
case PANCAKE:
default:
/* By default, GLSurfaceView() creates a RGB_565 opaque surface.
* If we want a translucent one, we should change the surface's
* format here, using PixelFormat.TRANSLUCENT for GL Surfaces
* is interpreted as any 32-bit surface with alpha by SurfaceFlinger.
*/
if (translucent) {
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
2014-02-10 02:10:30 +01:00
/* Setup the context factory for 2.0 rendering.
* See ContextFactory class definition below
*/
setEGLContextFactory(new PancakeContextFactory());
2014-02-10 02:10:30 +01:00
/* We need to choose an EGLConfig that matches the format of
* our surface exactly. This is going to be done in our
* custom config chooser. See ConfigChooser class definition
* below.
*/
2014-02-10 02:10:30 +01:00
if (GLUtils.use_32) {
setEGLConfigChooser(translucent ?
new PancakeFallbackConfigChooser(8, 8, 8, 8, 24, stencil,
new PancakeConfigChooser(8, 8, 8, 8, 16, stencil)) :
new PancakeFallbackConfigChooser(8, 8, 8, 8, 24, stencil,
new PancakeConfigChooser(5, 6, 5, 0, 16, stencil)));
2014-02-10 02:10:30 +01:00
} else {
setEGLConfigChooser(translucent ?
new PancakeConfigChooser(8, 8, 8, 8, 16, stencil) :
new PancakeConfigChooser(5, 6, 5, 0, 16, stencil));
}
break;
2014-06-04 05:31:20 +02:00
}
2014-02-10 02:10:30 +01:00
/* Set the renderer responsible for frame rendering */
setRenderer(new GodotRenderer());
2014-02-10 02:10:30 +01:00
}
public void onBackPressed() {
activity.onBackPressed();
2014-02-10 02:10:30 +01:00
}
}