UrhoSurface.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  4. using Android.App;
  5. using Android.Content;
  6. using Android.Util;
  7. using Android.Views;
  8. using Java.Lang;
  9. using Org.Libsdl.App;
  10. namespace Urho.Droid
  11. {
  12. /// <summary>
  13. /// A controller that provides a SDLSurface that can be used in any activity.
  14. /// Make sure you handle these events in your Activity:
  15. /// - OnResume
  16. /// - OnPause
  17. /// - OnLowMemory
  18. /// - OnDestroy
  19. /// - DispatchKeyEvent
  20. /// - OnWindowFocusChanged
  21. /// </summary>
  22. public class UrhoSurface : IUrhoSurface
  23. {
  24. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  25. public delegate int SdlCallback(IntPtr context);
  26. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  27. internal static extern void RegisterSdlLauncher(SdlCallback callback);
  28. public SDLSurface SdlSurface { get; set; }
  29. UrhoSurface(SDLSurface sdlSurface)
  30. {
  31. SdlSurface = sdlSurface;
  32. }
  33. public void Remove()
  34. {
  35. var vg = SdlSurface?.Parent as ViewGroup;
  36. if (SdlSurface != null && vg != null)
  37. {
  38. //vg.RemoveView(SdlSurface);
  39. SdlSurface.Enabled = false;
  40. SdlSurface.Visibility = ViewStates.Gone;
  41. }
  42. }
  43. public bool IsAlive => SDLActivity.MIsSurfaceReady;
  44. /// <summary>
  45. /// Creates a view (SurfaceView) that can be added anywhere
  46. /// </summary>
  47. public static SDLSurface CreateSurface<TApplication>(Activity activity, ApplicationOptions options = null, bool finishActivtiyOnExit = false) where TApplication : Application
  48. {
  49. return CreateSurface(activity, typeof (TApplication), options, finishActivtiyOnExit);
  50. }
  51. /// <summary>
  52. /// Creates a view (SurfaceView) that can be added anywhere
  53. /// </summary>
  54. public static SDLSurface CreateSurface(Activity activity, Type appType, ApplicationOptions options = null, bool finishActivtiyOnExit = false)
  55. {
  56. return CreateSurface(activity, () => Application.CreateInstance(appType, options), finishActivtiyOnExit);
  57. }
  58. /// <summary>
  59. /// Creates a view (SurfaceView) that can be added anywhere
  60. /// </summary>
  61. public static SDLSurface CreateSurface(Activity activity, Func<Application> applicationFactory, bool finishActivtiyOnExit = false)
  62. {
  63. var surface = SDLActivity.CreateSurface(activity);
  64. SetSdlMain(applicationFactory, finishActivtiyOnExit, surface);
  65. return surface;
  66. }
  67. public static void OnResume()
  68. {
  69. SDLActivity.OnResume();
  70. }
  71. public static void OnPause()
  72. {
  73. SDLActivity.OnPause();
  74. }
  75. public static void OnLowMemory()
  76. {
  77. SDLActivity.OnLowMemory();
  78. }
  79. public static void OnDestroy()
  80. {
  81. SDLActivity.OnDestroy();
  82. }
  83. public static bool DispatchKeyEvent(KeyEvent keyEvent)
  84. {
  85. return SDLActivity.DispatchKeyEvent(keyEvent);
  86. }
  87. public static void OnWindowFocusChanged(bool focus)
  88. {
  89. SDLActivity.OnWindowFocusChanged(focus);
  90. }
  91. /// <summary>
  92. /// The simpliest way to launch a game. It opens a special full-screen activity
  93. /// </summary>
  94. public static void RunInActivity<TApplication>(ApplicationOptions options = null) where TApplication : Application
  95. {
  96. RunInActivity(typeof (TApplication), options);
  97. }
  98. /// <summary>
  99. /// The simpliest way to launch a game. It opens a special full-screen activity
  100. /// </summary>
  101. public static void RunInActivity(Type appType, ApplicationOptions options = null)
  102. {
  103. RunInActivity(() => Application.CreateInstance(appType, options));
  104. }
  105. /// <summary>
  106. /// The simpliest way to launch a game. It opens a special full-screen activity
  107. /// </summary>
  108. public static void RunInActivity(Func<Application> applicationFactory)
  109. {
  110. SetSdlMain(applicationFactory, true, null);
  111. var context = Android.App.Application.Context;
  112. var intent = new Intent(context, typeof(Org.Libsdl.App.UrhoActivity));
  113. intent.AddFlags(ActivityFlags.NewTask);
  114. context.StartActivity(intent);
  115. }
  116. static void SetSdlMain(Func<Application> applicationFactory, bool finishActivityOnExit, SDLSurface surface)
  117. {
  118. SDLActivity.FinishActivityOnNativeExit = finishActivityOnExit;
  119. RegisterSdlLauncher(_ => {
  120. var app = applicationFactory();
  121. app.UrhoSurface = new UrhoSurface(surface);
  122. var code = app.Run();
  123. Log.Warn("URHOSHARP", "App exited: " + code);
  124. return code;
  125. });
  126. }
  127. }
  128. }