1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-06 12:20:30 +00:00

Add missing display server overrides

Improves the base functionality for the Android platform and helps reduce the amount of spurious error logs emitted.
This commit is contained in:
Fredia Huya-Kouadio
2022-11-27 22:03:28 -08:00
parent 4a82d71d73
commit 7cc47613fe
10 changed files with 166 additions and 7 deletions

View File

@@ -175,6 +175,7 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
public GodotIO io;
public GodotNetUtils netUtils;
public GodotTTS tts;
DirectoryAccessHandler directoryAccessHandler;
public interface ResultCallback {
void callback(int requestCode, int resultCode, Intent data);
@@ -488,7 +489,7 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
netUtils = new GodotNetUtils(activity);
tts = new GodotTTS(activity);
Context context = getContext();
DirectoryAccessHandler directoryAccessHandler = new DirectoryAccessHandler(context);
directoryAccessHandler = new DirectoryAccessHandler(context);
FileAccessHandler fileAccessHandler = new FileAccessHandler(context);
mSensorManager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

View File

@@ -43,8 +43,13 @@ import org.godotengine.godot.xr.regular.RegularFallbackConfigChooser;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.os.Build;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.PointerIcon;
@@ -52,6 +57,8 @@ import android.view.SurfaceView;
import androidx.annotation.Keep;
import java.io.InputStream;
/**
* A simple GLSurfaceView sub-class that demonstrate how to perform
* OpenGL ES 2.0 rendering into a GL Surface. Note the following important
@@ -74,6 +81,7 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView
private final Godot godot;
private final GodotInputHandler inputHandler;
private final GodotRenderer godotRenderer;
private final SparseArray<PointerIcon> customPointerIcons = new SparseArray<>();
public GodotGLRenderView(Context context, Godot godot, XRMode xrMode, boolean p_use_debug_opengl) {
super(context);
@@ -168,13 +176,50 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView
inputHandler.onPointerCaptureChange(false);
}
/**
* Used to configure the PointerIcon for the given type.
*
* Called from JNI
*/
@Keep
@Override
public void configurePointerIcon(int pointerType, String imagePath, float hotSpotX, float hotSpotY) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
try {
Bitmap bitmap = null;
if (!TextUtils.isEmpty(imagePath)) {
if (godot.directoryAccessHandler.filesystemFileExists(imagePath)) {
// Try to load the bitmap from the file system
bitmap = BitmapFactory.decodeFile(imagePath);
} else if (godot.directoryAccessHandler.assetsFileExists(imagePath)) {
// Try to load the bitmap from the assets directory
AssetManager am = getContext().getAssets();
InputStream imageInputStream = am.open(imagePath);
bitmap = BitmapFactory.decodeStream(imageInputStream);
}
}
PointerIcon customPointerIcon = PointerIcon.create(bitmap, hotSpotX, hotSpotY);
customPointerIcons.put(pointerType, customPointerIcon);
} catch (Exception e) {
// Reset the custom pointer icon
customPointerIcons.delete(pointerType);
}
}
}
/**
* called from JNI to change pointer icon
*/
@Keep
@Override
public void setPointerIcon(int pointerType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setPointerIcon(PointerIcon.getSystemIcon(getContext(), pointerType));
PointerIcon pointerIcon = customPointerIcons.get(pointerType);
if (pointerIcon == null) {
pointerIcon = PointerIcon.getSystemIcon(getContext(), pointerType);
}
setPointerIcon(pointerIcon);
}
}

View File

@@ -48,5 +48,7 @@ public interface GodotRenderView {
GodotInputHandler getInputHandler();
void configurePointerIcon(int pointerType, String imagePath, float hotSpotX, float hotSpotY);
void setPointerIcon(int pointerType);
}

View File

@@ -36,7 +36,12 @@ import org.godotengine.godot.vulkan.VkSurfaceView;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.PointerIcon;
@@ -44,10 +49,13 @@ import android.view.SurfaceView;
import androidx.annotation.Keep;
import java.io.InputStream;
public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderView {
private final Godot godot;
private final GodotInputHandler mInputHandler;
private final VkRenderer mRenderer;
private final SparseArray<PointerIcon> customPointerIcons = new SparseArray<>();
public GodotVulkanRenderView(Context context, Godot godot) {
super(context);
@@ -142,13 +150,50 @@ public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderV
mInputHandler.onPointerCaptureChange(hasCapture);
}
/**
* Used to configure the PointerIcon for the given type.
*
* Called from JNI
*/
@Keep
@Override
public void configurePointerIcon(int pointerType, String imagePath, float hotSpotX, float hotSpotY) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
try {
Bitmap bitmap = null;
if (!TextUtils.isEmpty(imagePath)) {
if (godot.directoryAccessHandler.filesystemFileExists(imagePath)) {
// Try to load the bitmap from the file system
bitmap = BitmapFactory.decodeFile(imagePath);
} else if (godot.directoryAccessHandler.assetsFileExists(imagePath)) {
// Try to load the bitmap from the assets directory
AssetManager am = getContext().getAssets();
InputStream imageInputStream = am.open(imagePath);
bitmap = BitmapFactory.decodeStream(imageInputStream);
}
}
PointerIcon customPointerIcon = PointerIcon.create(bitmap, hotSpotX, hotSpotY);
customPointerIcons.put(pointerType, customPointerIcon);
} catch (Exception e) {
// Reset the custom pointer icon
customPointerIcons.delete(pointerType);
}
}
}
/**
* called from JNI to change pointer icon
*/
@Keep
@Override
public void setPointerIcon(int pointerType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setPointerIcon(PointerIcon.getSystemIcon(getContext(), pointerType));
PointerIcon pointerIcon = customPointerIcons.get(pointerType);
if (pointerIcon == null) {
pointerIcon = PointerIcon.getSystemIcon(getContext(), pointerType);
}
setPointerIcon(pointerIcon);
}
}

View File

@@ -79,6 +79,9 @@ class DirectoryAccessHandler(context: Context) {
private val assetsDirAccess = AssetsDirectoryAccess(context)
private val fileSystemDirAccess = FilesystemDirectoryAccess(context)
fun assetsFileExists(assetsPath: String) = assetsDirAccess.fileExists(assetsPath)
fun filesystemFileExists(path: String) = fileSystemDirAccess.fileExists(path)
private fun hasDirId(accessType: AccessType, dirId: Int): Boolean {
return when (accessType) {
ACCESS_RESOURCES -> assetsDirAccess.hasDirId(dirId)