Skip to content

Commit bcc43d2

Browse files
committed
Migrate GodotGLRenderView & GodotVulkanRenderView to Kotlin - part 2
Final (compiling) step of the migration to Kotlin
1 parent 0bf2eb0 commit bcc43d2

File tree

2 files changed

+191
-224
lines changed

2 files changed

+191
-224
lines changed
Lines changed: 111 additions & 123 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,118 +28,100 @@
2828
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2929
/**************************************************************************/
3030

31-
package org.godotengine.godot.render;
32-
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;
31+
package org.godotengine.godot.render
32+
33+
import android.annotation.SuppressLint
34+
import android.graphics.Bitmap
35+
import android.graphics.BitmapFactory
36+
import android.graphics.PixelFormat
37+
import android.os.Build
38+
import android.text.TextUtils
39+
import android.util.SparseArray
40+
import android.view.KeyEvent
41+
import android.view.MotionEvent
42+
import android.view.PointerIcon
43+
import androidx.annotation.Keep
44+
import org.godotengine.godot.Godot
45+
import org.godotengine.godot.GodotRenderView
46+
import org.godotengine.godot.input.GodotInputHandler
47+
import org.godotengine.godot.xr.XRMode
5348

5449
/**
5550
* A simple GLSurfaceView sub-class that demonstrate how to perform
5651
* OpenGL ES 2.0 rendering into a GL Surface. Note the following important
5752
* details:
5853
*
5954
* - The class must use a custom context factory to enable 2.0 rendering.
60-
* See ContextFactory class definition below.
55+
* See ContextFactory class definition below.
6156
*
6257
* - 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.
58+
* an EGLConfig that supports 3.0. This is done by providing a config
59+
* specification to eglChooseConfig() that has the attribute
60+
* EGL10.ELG_RENDERABLE_TYPE containing the EGL_OPENGL_ES2_BIT flag
61+
* set. See ConfigChooser class definition below.
6762
*
6863
* - 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.
64+
* that matches it exactly (with regards to red/green/blue/alpha channels
65+
* bit depths). Failure to do so would result in an EGL_BAD_MATCH error.
7166
*/
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);
86-
}
67+
internal class GodotGLRenderView(
68+
private val godot: Godot,
69+
godotRenderer: GodotRenderer,
70+
private val inputHandler: GodotInputHandler,
71+
xrMode: XRMode,
72+
useDebugOpengl: Boolean,
73+
shouldBeTranslucent: Boolean
74+
) : GLSurfaceView(
75+
godot.context
76+
), GodotRenderView {
77+
private val customPointerIcons = SparseArray<PointerIcon>()
8778

88-
@Override
89-
public SurfaceView getView() {
90-
return this;
79+
init {
80+
pointerIcon = PointerIcon.getSystemIcon(context, PointerIcon.TYPE_DEFAULT)
81+
init(xrMode, godotRenderer, shouldBeTranslucent, useDebugOpengl)
9182
}
9283

93-
@Override
94-
public GodotInputHandler getInputHandler() {
95-
return inputHandler;
96-
}
84+
override fun getView() = this
85+
86+
override fun getInputHandler() = inputHandler
9787

9888
@SuppressLint("ClickableViewAccessibility")
99-
@Override
100-
public boolean onTouchEvent(MotionEvent event) {
101-
super.onTouchEvent(event);
102-
return inputHandler.onTouchEvent(event);
89+
override fun onTouchEvent(event: MotionEvent): Boolean {
90+
super.onTouchEvent(event)
91+
return inputHandler.onTouchEvent(event)
10392
}
10493

105-
@Override
106-
public boolean onKeyUp(final int keyCode, KeyEvent event) {
107-
return inputHandler.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event);
94+
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
95+
return inputHandler.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event)
10896
}
10997

110-
@Override
111-
public boolean onKeyDown(final int keyCode, KeyEvent event) {
112-
return inputHandler.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
98+
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
99+
return inputHandler.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event)
113100
}
114101

115-
@Override
116-
public boolean onGenericMotionEvent(MotionEvent event) {
117-
return inputHandler.onGenericMotionEvent(event) || super.onGenericMotionEvent(event);
102+
override fun onGenericMotionEvent(event: MotionEvent): Boolean {
103+
return inputHandler.onGenericMotionEvent(event) || super.onGenericMotionEvent(event)
118104
}
119105

120-
@Override
121-
public boolean onCapturedPointerEvent(MotionEvent event) {
122-
return inputHandler.onGenericMotionEvent(event);
106+
override fun onCapturedPointerEvent(event: MotionEvent): Boolean {
107+
return inputHandler.onGenericMotionEvent(event)
123108
}
124109

125-
@Override
126-
public void onPointerCaptureChange(boolean hasCapture) {
127-
super.onPointerCaptureChange(hasCapture);
128-
inputHandler.onPointerCaptureChange(hasCapture);
110+
override fun onPointerCaptureChange(hasCapture: Boolean) {
111+
super.onPointerCaptureChange(hasCapture)
112+
inputHandler.onPointerCaptureChange(hasCapture)
129113
}
130114

131-
@Override
132-
public void requestPointerCapture() {
115+
override fun requestPointerCapture() {
133116
if (canCapturePointer()) {
134-
super.requestPointerCapture();
135-
inputHandler.onPointerCaptureChange(true);
117+
super.requestPointerCapture()
118+
inputHandler.onPointerCaptureChange(true)
136119
}
137120
}
138121

139-
@Override
140-
public void releasePointerCapture() {
141-
super.releasePointerCapture();
142-
inputHandler.onPointerCaptureChange(false);
122+
override fun releasePointerCapture() {
123+
super.releasePointerCapture()
124+
inputHandler.onPointerCaptureChange(false)
143125
}
144126

145127
/**
@@ -148,95 +130,101 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView
148130
* Called from JNI
149131
*/
150132
@Keep
151-
@Override
152-
public void configurePointerIcon(int pointerType, String imagePath, float hotSpotX, float hotSpotY) {
133+
override fun configurePointerIcon(
134+
pointerType: Int,
135+
imagePath: String,
136+
hotSpotX: Float,
137+
hotSpotY: Float
138+
) {
153139
try {
154-
Bitmap bitmap = null;
140+
var bitmap: Bitmap? = null
155141
if (!TextUtils.isEmpty(imagePath)) {
156-
if (godot.getDirectoryAccessHandler().filesystemFileExists(imagePath)) {
142+
if (godot.directoryAccessHandler.filesystemFileExists(imagePath)) {
157143
// Try to load the bitmap from the file system
158-
bitmap = BitmapFactory.decodeFile(imagePath);
159-
} else if (godot.getDirectoryAccessHandler().assetsFileExists(imagePath)) {
144+
bitmap = BitmapFactory.decodeFile(imagePath)
145+
} else if (godot.directoryAccessHandler.assetsFileExists(imagePath)) {
160146
// 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);
147+
val am = context.assets
148+
val imageInputStream = am.open(imagePath)
149+
bitmap = BitmapFactory.decodeStream(imageInputStream)
164150
}
165151
}
166152

167-
PointerIcon customPointerIcon = PointerIcon.create(bitmap, hotSpotX, hotSpotY);
168-
customPointerIcons.put(pointerType, customPointerIcon);
169-
} catch (Exception e) {
153+
if (bitmap != null) {
154+
val customPointerIcon = PointerIcon.create(bitmap, hotSpotX, hotSpotY)
155+
customPointerIcons.put(pointerType, customPointerIcon)
156+
}
157+
} catch (e: Exception) {
170158
// Reset the custom pointer icon
171-
customPointerIcons.delete(pointerType);
159+
customPointerIcons.delete(pointerType)
172160
}
173161
}
174162

175163
/**
176164
* called from JNI to change pointer icon
177165
*/
178166
@Keep
179-
@Override
180-
public void setPointerIcon(int pointerType) {
181-
PointerIcon pointerIcon = customPointerIcons.get(pointerType);
167+
override fun setPointerIcon(pointerType: Int) {
168+
var pointerIcon = customPointerIcons[pointerType]
182169
if (pointerIcon == null) {
183-
pointerIcon = PointerIcon.getSystemIcon(getContext(), pointerType);
170+
pointerIcon = PointerIcon.getSystemIcon(context, pointerType)
184171
}
185-
setPointerIcon(pointerIcon);
172+
setPointerIcon(pointerIcon)
186173
}
187174

188-
@Override
189-
public PointerIcon onResolvePointerIcon(MotionEvent me, int pointerIndex) {
190-
return getPointerIcon();
175+
override fun onResolvePointerIcon(me: MotionEvent, pointerIndex: Int): PointerIcon {
176+
return pointerIcon
191177
}
192178

193-
private void init(XRMode xrMode, boolean translucent, boolean useDebugOpengl) {
194-
setPreserveEGLContextOnPause(true);
195-
setFocusableInTouchMode(true);
196-
switch (xrMode) {
197-
case OPENXR:
179+
private fun init(xrMode: XRMode, renderer: GodotRenderer, translucent: Boolean, useDebugOpengl: Boolean) {
180+
preserveEGLContextOnPause = true
181+
isFocusableInTouchMode = true
182+
when (xrMode) {
183+
XRMode.OPENXR -> {
198184
// Replace the default egl config chooser.
199-
setEGLConfigChooser(new OvrConfigChooser());
185+
setEGLConfigChooser(OvrConfigChooser())
200186

201187
// Replace the default context factory.
202-
setEGLContextFactory(new OvrContextFactory());
188+
setEGLContextFactory(OvrContextFactory())
203189

204190
// Replace the default window surface factory.
205-
setEGLWindowSurfaceFactory(new OvrWindowSurfaceFactory());
206-
break;
191+
setEGLWindowSurfaceFactory(OvrWindowSurfaceFactory())
192+
}
207193

208-
case REGULAR:
209-
default:
194+
XRMode.REGULAR -> {
210195
/* By default, GLSurfaceView() creates a RGB_565 opaque surface.
211196
* If we want a translucent one, we should change the surface's
212197
* format here, using PixelFormat.TRANSLUCENT for GL Surfaces
213198
* is interpreted as any 32-bit surface with alpha by SurfaceFlinger.
214199
*/
215200
if (translucent) {
216-
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
201+
this.holder.setFormat(PixelFormat.TRANSLUCENT)
217202
}
218203

219204
/* Setup the context factory for 2.0 rendering.
220205
* See ContextFactory class definition below
221206
*/
222-
setEGLContextFactory(new RegularContextFactory(useDebugOpengl));
207+
setEGLContextFactory(
208+
RegularContextFactory(
209+
useDebugOpengl
210+
)
211+
)
223212

224213
/* We need to choose an EGLConfig that matches the format of
225214
* our surface exactly. This is going to be done in our
226215
* custom config chooser. See ConfigChooser class definition
227216
* below.
228217
*/
229-
230218
setEGLConfigChooser(
231-
new RegularFallbackConfigChooser(8, 8, 8, 8, 24, 0,
232-
new RegularConfigChooser(8, 8, 8, 8, 16, 0)));
233-
break;
219+
RegularFallbackConfigChooser(
220+
8, 8, 8, 8, 24, 0,
221+
RegularConfigChooser(8, 8, 8, 8, 16, 0)
222+
)
223+
)
224+
}
234225
}
235-
}
236226

237-
@Override
238-
public void startRenderer() {
239-
/* Set the renderer responsible for frame rendering */
240-
setRenderer(godotRenderer);
227+
// Set the renderer responsible for frame rendering
228+
setRenderer(renderer)
241229
}
242230
}

0 commit comments

Comments
 (0)