Skip to content

Commit 5ebcd66

Browse files
committed
Migrate GodotGLRenderView & GodotVulkanRenderView to Kotlin - part 2
Final (compiling) step of the migration to Kotlin
1 parent b4fd273 commit 5ebcd66

File tree

3 files changed

+132
-288
lines changed

3 files changed

+132
-288
lines changed

platform/android/java/lib/src/org/godotengine/godot/GodotRenderView.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,11 @@
3131
package org.godotengine.godot;
3232

3333
import org.godotengine.godot.input.GodotInputHandler;
34-
import org.godotengine.godot.utils.DeviceUtils;
3534

3635
import android.view.SurfaceView;
3736

3837
public interface GodotRenderView {
3938
SurfaceView getView();
4039

4140
GodotInputHandler getInputHandler();
42-
43-
void configurePointerIcon(int pointerType, String imagePath, float hotSpotX, float hotSpotY);
44-
45-
void setPointerIcon(int pointerType);
46-
47-
/**
48-
* @return true if pointer capture is supported.
49-
*/
50-
default boolean canCapturePointer() {
51-
// Pointer capture is not supported on native XR devices.
52-
return !DeviceUtils.isNativeXRDevice(getView().getContext()) && getInputHandler().canCapturePointer();
53-
}
5441
}
Lines changed: 80 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**************************************************************************/
2-
/* GodotGLRenderView.java */
2+
/* GodotGLRenderView.kt */
33
/**************************************************************************/
44
/* This file is part of: */
55
/* GODOT ENGINE */
@@ -28,215 +28,147 @@
2828
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2929
/**************************************************************************/
3030

31-
package org.godotengine.godot.render;
31+
package org.godotengine.godot.render
3232

33-
import org.godotengine.godot.Godot;
34-
import org.godotengine.godot.GodotRenderView;
35-
import org.godotengine.godot.input.GodotInputHandler;
36-
import org.godotengine.godot.xr.XRMode;
37-
38-
import android.annotation.SuppressLint;
39-
import android.content.res.AssetManager;
40-
import android.graphics.Bitmap;
41-
import android.graphics.BitmapFactory;
42-
import android.graphics.PixelFormat;
43-
import android.text.TextUtils;
44-
import android.util.SparseArray;
45-
import android.view.KeyEvent;
46-
import android.view.MotionEvent;
47-
import android.view.PointerIcon;
48-
import android.view.SurfaceView;
49-
50-
import androidx.annotation.Keep;
51-
52-
import java.io.InputStream;
33+
import android.annotation.SuppressLint
34+
import android.graphics.PixelFormat
35+
import android.view.KeyEvent
36+
import android.view.MotionEvent
37+
import android.view.PointerIcon
38+
import org.godotengine.godot.Godot
39+
import org.godotengine.godot.GodotRenderView
40+
import org.godotengine.godot.input.GodotInputHandler
41+
import org.godotengine.godot.xr.XRMode
5342

5443
/**
5544
* A simple GLSurfaceView sub-class that demonstrate how to perform
5645
* OpenGL ES 2.0 rendering into a GL Surface. Note the following important
5746
* details:
5847
*
5948
* - The class must use a custom context factory to enable 2.0 rendering.
60-
* See ContextFactory class definition below.
49+
* See ContextFactory class definition below.
6150
*
6251
* - The class must use a custom EGLConfigChooser to be able to select
63-
* an EGLConfig that supports 3.0. This is done by providing a config
64-
* specification to eglChooseConfig() that has the attribute
65-
* EGL10.ELG_RENDERABLE_TYPE containing the EGL_OPENGL_ES2_BIT flag
66-
* set. See ConfigChooser class definition below.
52+
* an EGLConfig that supports 3.0. This is done by providing a config
53+
* specification to eglChooseConfig() that has the attribute
54+
* EGL10.ELG_RENDERABLE_TYPE containing the EGL_OPENGL_ES2_BIT flag
55+
* set. See ConfigChooser class definition below.
6756
*
6857
* - The class must select the surface's format, then choose an EGLConfig
69-
* that matches it exactly (with regards to red/green/blue/alpha channels
70-
* bit depths). Failure to do so would result in an EGL_BAD_MATCH error.
58+
* that matches it exactly (with regards to red/green/blue/alpha channels
59+
* bit depths). Failure to do so would result in an EGL_BAD_MATCH error.
7160
*/
72-
public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView {
73-
private final Godot godot;
74-
private final GodotInputHandler inputHandler;
75-
private final GodotRenderer godotRenderer;
76-
private final SparseArray<PointerIcon> customPointerIcons = new SparseArray<>();
77-
78-
public GodotGLRenderView(Godot godot, GodotRenderer renderer, GodotInputHandler inputHandler, XRMode xrMode, boolean useDebugOpengl, boolean shouldBeTranslucent) {
79-
super(godot.getContext());
80-
81-
this.godot = godot;
82-
this.inputHandler = inputHandler;
83-
this.godotRenderer = renderer;
84-
setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));
85-
init(xrMode, shouldBeTranslucent, useDebugOpengl);
61+
internal class GodotGLRenderView(
62+
private val godot: Godot,
63+
godotRenderer: GodotRenderer,
64+
private val inputHandler: GodotInputHandler,
65+
xrMode: XRMode,
66+
useDebugOpengl: Boolean,
67+
shouldBeTranslucent: Boolean
68+
) : GLSurfaceView(
69+
godot.context
70+
), GodotRenderView {
71+
init {
72+
pointerIcon = PointerIcon.getSystemIcon(context, PointerIcon.TYPE_DEFAULT)
73+
init(xrMode, godotRenderer, shouldBeTranslucent, useDebugOpengl)
8674
}
8775

88-
@Override
89-
public SurfaceView getView() {
90-
return this;
91-
}
76+
override fun getView() = this
9277

93-
@Override
94-
public GodotInputHandler getInputHandler() {
95-
return inputHandler;
96-
}
78+
override fun getInputHandler() = inputHandler
9779

9880
@SuppressLint("ClickableViewAccessibility")
99-
@Override
100-
public boolean onTouchEvent(MotionEvent event) {
101-
super.onTouchEvent(event);
102-
return inputHandler.onTouchEvent(event);
103-
}
104-
105-
@Override
106-
public boolean onKeyUp(final int keyCode, KeyEvent event) {
107-
return inputHandler.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event);
108-
}
109-
110-
@Override
111-
public boolean onKeyDown(final int keyCode, KeyEvent event) {
112-
return inputHandler.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
81+
override fun onTouchEvent(event: MotionEvent): Boolean {
82+
super.onTouchEvent(event)
83+
return inputHandler.onTouchEvent(event)
11384
}
11485

115-
@Override
116-
public boolean onGenericMotionEvent(MotionEvent event) {
117-
return inputHandler.onGenericMotionEvent(event) || super.onGenericMotionEvent(event);
86+
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
87+
return inputHandler.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event)
11888
}
11989

120-
@Override
121-
public boolean onCapturedPointerEvent(MotionEvent event) {
122-
return inputHandler.onGenericMotionEvent(event);
90+
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
91+
return inputHandler.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event)
12392
}
12493

125-
@Override
126-
public void onPointerCaptureChange(boolean hasCapture) {
127-
super.onPointerCaptureChange(hasCapture);
128-
inputHandler.onPointerCaptureChange(hasCapture);
94+
override fun onGenericMotionEvent(event: MotionEvent): Boolean {
95+
return inputHandler.onGenericMotionEvent(event) || super.onGenericMotionEvent(event)
12996
}
13097

131-
@Override
132-
public void requestPointerCapture() {
133-
if (canCapturePointer()) {
134-
super.requestPointerCapture();
135-
inputHandler.onPointerCaptureChange(true);
136-
}
98+
override fun onCapturedPointerEvent(event: MotionEvent): Boolean {
99+
return inputHandler.onGenericMotionEvent(event)
137100
}
138101

139-
@Override
140-
public void releasePointerCapture() {
141-
super.releasePointerCapture();
142-
inputHandler.onPointerCaptureChange(false);
102+
override fun onPointerCaptureChange(hasCapture: Boolean) {
103+
super.onPointerCaptureChange(hasCapture)
104+
inputHandler.onPointerCaptureChange(hasCapture)
143105
}
144106

145-
/**
146-
* Used to configure the PointerIcon for the given type.
147-
*
148-
* Called from JNI
149-
*/
150-
@Keep
151-
@Override
152-
public void configurePointerIcon(int pointerType, String imagePath, float hotSpotX, float hotSpotY) {
153-
try {
154-
Bitmap bitmap = null;
155-
if (!TextUtils.isEmpty(imagePath)) {
156-
if (godot.getDirectoryAccessHandler().filesystemFileExists(imagePath)) {
157-
// Try to load the bitmap from the file system
158-
bitmap = BitmapFactory.decodeFile(imagePath);
159-
} else if (godot.getDirectoryAccessHandler().assetsFileExists(imagePath)) {
160-
// Try to load the bitmap from the assets directory
161-
AssetManager am = getContext().getAssets();
162-
InputStream imageInputStream = am.open(imagePath);
163-
bitmap = BitmapFactory.decodeStream(imageInputStream);
164-
}
165-
}
166-
167-
PointerIcon customPointerIcon = PointerIcon.create(bitmap, hotSpotX, hotSpotY);
168-
customPointerIcons.put(pointerType, customPointerIcon);
169-
} catch (Exception e) {
170-
// Reset the custom pointer icon
171-
customPointerIcons.delete(pointerType);
107+
override fun requestPointerCapture() {
108+
if (godot.canCapturePointer()) {
109+
super.requestPointerCapture()
110+
inputHandler.onPointerCaptureChange(true)
172111
}
173112
}
174113

175-
/**
176-
* called from JNI to change pointer icon
177-
*/
178-
@Keep
179-
@Override
180-
public void setPointerIcon(int pointerType) {
181-
PointerIcon pointerIcon = customPointerIcons.get(pointerType);
182-
if (pointerIcon == null) {
183-
pointerIcon = PointerIcon.getSystemIcon(getContext(), pointerType);
184-
}
185-
setPointerIcon(pointerIcon);
114+
override fun releasePointerCapture() {
115+
super.releasePointerCapture()
116+
inputHandler.onPointerCaptureChange(false)
186117
}
187118

188-
@Override
189-
public PointerIcon onResolvePointerIcon(MotionEvent me, int pointerIndex) {
190-
return getPointerIcon();
119+
override fun onResolvePointerIcon(me: MotionEvent, pointerIndex: Int): PointerIcon {
120+
return pointerIcon
191121
}
192122

193-
private void init(XRMode xrMode, boolean translucent, boolean useDebugOpengl) {
194-
setPreserveEGLContextOnPause(true);
195-
setFocusableInTouchMode(true);
196-
switch (xrMode) {
197-
case OPENXR:
123+
private fun init(xrMode: XRMode, renderer: GodotRenderer, translucent: Boolean, useDebugOpengl: Boolean) {
124+
preserveEGLContextOnPause = true
125+
isFocusableInTouchMode = true
126+
when (xrMode) {
127+
XRMode.OPENXR -> {
198128
// Replace the default egl config chooser.
199-
setEGLConfigChooser(new OvrConfigChooser());
129+
setEGLConfigChooser(OvrConfigChooser())
200130

201131
// Replace the default context factory.
202-
setEGLContextFactory(new OvrContextFactory());
132+
setEGLContextFactory(OvrContextFactory())
203133

204134
// Replace the default window surface factory.
205-
setEGLWindowSurfaceFactory(new OvrWindowSurfaceFactory());
206-
break;
135+
setEGLWindowSurfaceFactory(OvrWindowSurfaceFactory())
136+
}
207137

208-
case REGULAR:
209-
default:
138+
XRMode.REGULAR -> {
210139
/* By default, GLSurfaceView() creates a RGB_565 opaque surface.
211140
* If we want a translucent one, we should change the surface's
212141
* format here, using PixelFormat.TRANSLUCENT for GL Surfaces
213142
* is interpreted as any 32-bit surface with alpha by SurfaceFlinger.
214143
*/
215144
if (translucent) {
216-
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
145+
this.holder.setFormat(PixelFormat.TRANSLUCENT)
217146
}
218147

219148
/* Setup the context factory for 2.0 rendering.
220149
* See ContextFactory class definition below
221150
*/
222-
setEGLContextFactory(new RegularContextFactory(useDebugOpengl));
151+
setEGLContextFactory(
152+
RegularContextFactory(
153+
useDebugOpengl
154+
)
155+
)
223156

224157
/* We need to choose an EGLConfig that matches the format of
225158
* our surface exactly. This is going to be done in our
226159
* custom config chooser. See ConfigChooser class definition
227160
* below.
228161
*/
229-
230162
setEGLConfigChooser(
231-
new RegularFallbackConfigChooser(8, 8, 8, 8, 24, 0,
232-
new RegularConfigChooser(8, 8, 8, 8, 16, 0)));
233-
break;
163+
RegularFallbackConfigChooser(
164+
8, 8, 8, 8, 24, 0,
165+
RegularConfigChooser(8, 8, 8, 8, 16, 0)
166+
)
167+
)
168+
}
234169
}
235-
}
236170

237-
@Override
238-
public void startRenderer() {
239-
/* Set the renderer responsible for frame rendering */
240-
setRenderer(godotRenderer);
171+
// Set the renderer responsible for frame rendering
172+
setRenderer(renderer)
241173
}
242174
}

0 commit comments

Comments
 (0)