Skip to content

Commit 2d31db6

Browse files
committed
Migrate GodotGLRenderView & GodotVulkanRenderView to Kotlin - part 2
Final (compiling) step of the migration to Kotlin
1 parent 6ed5241 commit 2d31db6

File tree

2 files changed

+192
-228
lines changed

2 files changed

+192
-228
lines changed
Lines changed: 112 additions & 126 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,121 +28,101 @@
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.os.Build;
44-
import android.text.TextUtils;
45-
import android.util.SparseArray;
46-
import android.view.KeyEvent;
47-
import android.view.MotionEvent;
48-
import android.view.PointerIcon;
49-
import android.view.SurfaceView;
50-
51-
import androidx.annotation.Keep;
52-
53-
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
5448

5549
/**
5650
* A simple GLSurfaceView sub-class that demonstrate how to perform
5751
* OpenGL ES 2.0 rendering into a GL Surface. Note the following important
5852
* details:
5953
*
6054
* - The class must use a custom context factory to enable 2.0 rendering.
61-
* See ContextFactory class definition below.
55+
* See ContextFactory class definition below.
6256
*
6357
* - The class must use a custom EGLConfigChooser to be able to select
64-
* an EGLConfig that supports 3.0. This is done by providing a config
65-
* specification to eglChooseConfig() that has the attribute
66-
* EGL10.ELG_RENDERABLE_TYPE containing the EGL_OPENGL_ES2_BIT flag
67-
* 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.
6862
*
6963
* - The class must select the surface's format, then choose an EGLConfig
70-
* that matches it exactly (with regards to red/green/blue/alpha channels
71-
* 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.
7266
*/
73-
public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView {
74-
private final Godot godot;
75-
private final GodotInputHandler inputHandler;
76-
private final GodotRenderer godotRenderer;
77-
private final SparseArray<PointerIcon> customPointerIcons = new SparseArray<>();
78-
79-
public GodotGLRenderView(Godot godot, GodotRenderer renderer, GodotInputHandler inputHandler, XRMode xrMode, boolean useDebugOpengl) {
80-
super(godot.getContext());
81-
82-
this.godot = godot;
83-
this.inputHandler = inputHandler;
84-
this.godotRenderer = renderer;
67+
internal class GodotGLRenderView(
68+
private val godot: Godot,
69+
godotRenderer: GodotRenderer,
70+
private val inputHandler: GodotInputHandler,
71+
xrMode: XRMode,
72+
useDebugOpengl: Boolean
73+
) : GLSurfaceView(
74+
godot.context
75+
), GodotRenderView {
76+
private val customPointerIcons = SparseArray<PointerIcon>()
77+
78+
init {
8579
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
86-
setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT));
80+
pointerIcon = PointerIcon.getSystemIcon(context, PointerIcon.TYPE_DEFAULT)
8781
}
88-
init(xrMode, false, useDebugOpengl);
82+
init(xrMode, godotRenderer, false, useDebugOpengl)
8983
}
9084

91-
@Override
92-
public SurfaceView getView() {
93-
return this;
94-
}
85+
override fun getView() = this
9586

96-
@Override
97-
public GodotInputHandler getInputHandler() {
98-
return inputHandler;
99-
}
87+
override fun getInputHandler() = inputHandler
10088

10189
@SuppressLint("ClickableViewAccessibility")
102-
@Override
103-
public boolean onTouchEvent(MotionEvent event) {
104-
super.onTouchEvent(event);
105-
return inputHandler.onTouchEvent(event);
90+
override fun onTouchEvent(event: MotionEvent): Boolean {
91+
super.onTouchEvent(event)
92+
return inputHandler.onTouchEvent(event)
10693
}
10794

108-
@Override
109-
public boolean onKeyUp(final int keyCode, KeyEvent event) {
110-
return inputHandler.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event);
95+
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
96+
return inputHandler.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event)
11197
}
11298

113-
@Override
114-
public boolean onKeyDown(final int keyCode, KeyEvent event) {
115-
return inputHandler.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
99+
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
100+
return inputHandler.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event)
116101
}
117102

118-
@Override
119-
public boolean onGenericMotionEvent(MotionEvent event) {
120-
return inputHandler.onGenericMotionEvent(event) || super.onGenericMotionEvent(event);
103+
override fun onGenericMotionEvent(event: MotionEvent): Boolean {
104+
return inputHandler.onGenericMotionEvent(event) || super.onGenericMotionEvent(event)
121105
}
122106

123-
@Override
124-
public boolean onCapturedPointerEvent(MotionEvent event) {
125-
return inputHandler.onGenericMotionEvent(event);
107+
override fun onCapturedPointerEvent(event: MotionEvent): Boolean {
108+
return inputHandler.onGenericMotionEvent(event)
126109
}
127110

128-
@Override
129-
public void onPointerCaptureChange(boolean hasCapture) {
130-
super.onPointerCaptureChange(hasCapture);
131-
inputHandler.onPointerCaptureChange(hasCapture);
111+
override fun onPointerCaptureChange(hasCapture: Boolean) {
112+
super.onPointerCaptureChange(hasCapture)
113+
inputHandler.onPointerCaptureChange(hasCapture)
132114
}
133115

134-
@Override
135-
public void requestPointerCapture() {
116+
override fun requestPointerCapture() {
136117
if (canCapturePointer()) {
137-
super.requestPointerCapture();
138-
inputHandler.onPointerCaptureChange(true);
118+
super.requestPointerCapture()
119+
inputHandler.onPointerCaptureChange(true)
139120
}
140121
}
141122

142-
@Override
143-
public void releasePointerCapture() {
144-
super.releasePointerCapture();
145-
inputHandler.onPointerCaptureChange(false);
123+
override fun releasePointerCapture() {
124+
super.releasePointerCapture()
125+
inputHandler.onPointerCaptureChange(false)
146126
}
147127

148128
/**
@@ -151,28 +131,34 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView
151131
* Called from JNI
152132
*/
153133
@Keep
154-
@Override
155-
public void configurePointerIcon(int pointerType, String imagePath, float hotSpotX, float hotSpotY) {
156-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
134+
override fun configurePointerIcon(
135+
pointerType: Int,
136+
imagePath: String,
137+
hotSpotX: Float,
138+
hotSpotY: Float
139+
) {
140+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
157141
try {
158-
Bitmap bitmap = null;
142+
var bitmap: Bitmap? = null
159143
if (!TextUtils.isEmpty(imagePath)) {
160-
if (godot.getDirectoryAccessHandler().filesystemFileExists(imagePath)) {
144+
if (godot.directoryAccessHandler.filesystemFileExists(imagePath)) {
161145
// Try to load the bitmap from the file system
162-
bitmap = BitmapFactory.decodeFile(imagePath);
163-
} else if (godot.getDirectoryAccessHandler().assetsFileExists(imagePath)) {
146+
bitmap = BitmapFactory.decodeFile(imagePath)
147+
} else if (godot.directoryAccessHandler.assetsFileExists(imagePath)) {
164148
// Try to load the bitmap from the assets directory
165-
AssetManager am = getContext().getAssets();
166-
InputStream imageInputStream = am.open(imagePath);
167-
bitmap = BitmapFactory.decodeStream(imageInputStream);
149+
val am = context.assets
150+
val imageInputStream = am.open(imagePath)
151+
bitmap = BitmapFactory.decodeStream(imageInputStream)
168152
}
169153
}
170154

171-
PointerIcon customPointerIcon = PointerIcon.create(bitmap, hotSpotX, hotSpotY);
172-
customPointerIcons.put(pointerType, customPointerIcon);
173-
} catch (Exception e) {
155+
if (bitmap != null) {
156+
val customPointerIcon = PointerIcon.create(bitmap, hotSpotX, hotSpotY)
157+
customPointerIcons.put(pointerType, customPointerIcon)
158+
}
159+
} catch (e: Exception) {
174160
// Reset the custom pointer icon
175-
customPointerIcons.delete(pointerType);
161+
customPointerIcons.delete(pointerType)
176162
}
177163
}
178164
}
@@ -181,72 +167,72 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView
181167
* called from JNI to change pointer icon
182168
*/
183169
@Keep
184-
@Override
185-
public void setPointerIcon(int pointerType) {
170+
override fun setPointerIcon(pointerType: Int) {
186171
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
187-
PointerIcon pointerIcon = customPointerIcons.get(pointerType);
172+
var pointerIcon = customPointerIcons[pointerType]
188173
if (pointerIcon == null) {
189-
pointerIcon = PointerIcon.getSystemIcon(getContext(), pointerType);
174+
pointerIcon = PointerIcon.getSystemIcon(context, pointerType)
190175
}
191-
setPointerIcon(pointerIcon);
176+
setPointerIcon(pointerIcon)
192177
}
193178
}
194179

195-
@Override
196-
public PointerIcon onResolvePointerIcon(MotionEvent me, int pointerIndex) {
180+
override fun onResolvePointerIcon(me: MotionEvent, pointerIndex: Int): PointerIcon {
197181
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
198-
return getPointerIcon();
182+
return pointerIcon
199183
}
200-
return super.onResolvePointerIcon(me, pointerIndex);
184+
return super.onResolvePointerIcon(me, pointerIndex)
201185
}
202186

203-
private void init(XRMode xrMode, boolean translucent, boolean useDebugOpengl) {
204-
setPreserveEGLContextOnPause(true);
205-
setFocusableInTouchMode(true);
206-
switch (xrMode) {
207-
case OPENXR:
187+
private fun init(xrMode: XRMode, renderer: GodotRenderer, translucent: Boolean, useDebugOpengl: Boolean) {
188+
preserveEGLContextOnPause = true
189+
isFocusableInTouchMode = true
190+
when (xrMode) {
191+
XRMode.OPENXR -> {
208192
// Replace the default egl config chooser.
209-
setEGLConfigChooser(new OvrConfigChooser());
193+
setEGLConfigChooser(OvrConfigChooser())
210194

211195
// Replace the default context factory.
212-
setEGLContextFactory(new OvrContextFactory());
196+
setEGLContextFactory(OvrContextFactory())
213197

214198
// Replace the default window surface factory.
215-
setEGLWindowSurfaceFactory(new OvrWindowSurfaceFactory());
216-
break;
199+
setEGLWindowSurfaceFactory(OvrWindowSurfaceFactory())
200+
}
217201

218-
case REGULAR:
219-
default:
202+
XRMode.REGULAR -> {
220203
/* By default, GLSurfaceView() creates a RGB_565 opaque surface.
221204
* If we want a translucent one, we should change the surface's
222205
* format here, using PixelFormat.TRANSLUCENT for GL Surfaces
223206
* is interpreted as any 32-bit surface with alpha by SurfaceFlinger.
224207
*/
225208
if (translucent) {
226-
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
209+
this.holder.setFormat(PixelFormat.TRANSLUCENT)
227210
}
228211

229212
/* Setup the context factory for 2.0 rendering.
230213
* See ContextFactory class definition below
231214
*/
232-
setEGLContextFactory(new RegularContextFactory(useDebugOpengl));
215+
setEGLContextFactory(
216+
RegularContextFactory(
217+
useDebugOpengl
218+
)
219+
)
233220

234221
/* We need to choose an EGLConfig that matches the format of
235222
* our surface exactly. This is going to be done in our
236223
* custom config chooser. See ConfigChooser class definition
237224
* below.
238225
*/
239-
240226
setEGLConfigChooser(
241-
new RegularFallbackConfigChooser(8, 8, 8, 8, 24, 0,
242-
new RegularConfigChooser(8, 8, 8, 8, 16, 0)));
243-
break;
227+
RegularFallbackConfigChooser(
228+
8, 8, 8, 8, 24, 0,
229+
RegularConfigChooser(8, 8, 8, 8, 16, 0)
230+
)
231+
)
232+
}
244233
}
245-
}
246234

247-
@Override
248-
public void startRenderer() {
249-
/* Set the renderer responsible for frame rendering */
250-
setRenderer(godotRenderer);
235+
// Set the renderer responsible for frame rendering
236+
setRenderer(renderer)
251237
}
252238
}

0 commit comments

Comments
 (0)