UrhoSurface.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading.Tasks;
  4. using Android.App;
  5. using Android.Content;
  6. using Android.Runtime;
  7. using Android.Util;
  8. using Android.Views;
  9. using Android.Widget;
  10. using Org.Libsdl.App;
  11. namespace Urho.Droid
  12. {
  13. /// <summary>
  14. /// A controller that provides a SDLSurface that can be used in any activity.
  15. /// Make sure you handle these events in your Activity:
  16. /// - OnResume
  17. /// - OnPause
  18. /// - OnLowMemory
  19. /// - OnDestroy
  20. /// - DispatchKeyEvent
  21. /// - OnWindowFocusChanged
  22. /// </summary>
  23. public class UrhoSurface
  24. {
  25. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  26. public delegate int SdlCallback(IntPtr context);
  27. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  28. internal static extern void RegisterSdlLauncher(SdlCallback callback);
  29. public static bool IsAlive => SDLActivity.MIsSurfaceReady;
  30. static UrhoSurfacePlaceholder lastPlaceholder;
  31. /// <summary>
  32. /// Creates a view (SurfaceView) that can be added anywhere
  33. /// </summary>
  34. public static UrhoSurfacePlaceholder CreateSurface(Activity activity)
  35. {
  36. lastPlaceholder = new UrhoSurfacePlaceholder(activity) {
  37. LayoutParameters = new ViewGroup.LayoutParams (
  38. ViewGroup.LayoutParams.MatchParent,
  39. ViewGroup.LayoutParams.MatchParent)
  40. };
  41. return lastPlaceholder;
  42. }
  43. public static void OnResume()
  44. {
  45. SDLActivity.OnResume();
  46. }
  47. public static void OnPause()
  48. {
  49. SDLActivity.OnPause();
  50. }
  51. public static void OnLowMemory()
  52. {
  53. SDLActivity.OnLowMemory();
  54. }
  55. public static void OnDestroy()
  56. {
  57. if (lastPlaceholder != null)
  58. {
  59. lastPlaceholder.Stop();
  60. var viewGroup = lastPlaceholder.Parent as ViewGroup;
  61. viewGroup?.RemoveView(lastPlaceholder);
  62. }
  63. }
  64. public static bool DispatchKeyEvent(KeyEvent keyEvent)
  65. {
  66. return SDLActivity.DispatchKeyEvent(keyEvent);
  67. }
  68. public static void OnWindowFocusChanged(bool focus)
  69. {
  70. SDLActivity.OnWindowFocusChanged(focus);
  71. }
  72. /// <summary>
  73. /// The simpliest way to launch a game. It opens a special full-screen activity
  74. /// </summary>
  75. public static void RunInActivity<TApplication>(ApplicationOptions options = null) where TApplication : Application
  76. {
  77. RunInActivity(typeof (TApplication), options);
  78. }
  79. /// <summary>
  80. /// The simpliest way to launch a game. It opens a special full-screen activity
  81. /// </summary>
  82. public static void RunInActivity(Type appType, ApplicationOptions options = null)
  83. {
  84. RunInActivity(() => Application.CreateInstance(appType, options), options);
  85. }
  86. /// <summary>
  87. /// The simpliest way to launch a game. It opens a special full-screen activity
  88. /// </summary>
  89. public static void RunInActivity(Func<Application> applicationFactory, ApplicationOptions opts)
  90. {
  91. SetSdlMain(applicationFactory, true, null);
  92. var context = Android.App.Application.Context;
  93. var intent = new Intent(context, typeof(Urho.Droid.FullscreenUrhoActivity));
  94. intent.AddFlags(ActivityFlags.NewTask);
  95. intent.PutExtra(nameof(ApplicationOptions.OrientationType), (int)(opts?.Orientation ?? ApplicationOptions.OrientationType.Landscape));
  96. context.StartActivity(intent);
  97. }
  98. internal static void SetSdlMain(Func<Application> applicationFactory, bool finishActivityOnExit, SDLSurface surface)
  99. {
  100. SDLActivity.FinishActivityOnNativeExit = finishActivityOnExit;
  101. RegisterSdlLauncher(_ => {
  102. var app = applicationFactory();
  103. app.UrhoSurface = surface;
  104. var code = app.Run();
  105. Log.Warn("URHOSHARP", "App exited: " + code);
  106. return code;
  107. });
  108. }
  109. }
  110. public class UrhoSurfacePlaceholder : FrameLayout
  111. {
  112. bool launching;
  113. public SDLSurface SDLSurface;
  114. public UrhoSurfacePlaceholder(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) {}
  115. public UrhoSurfacePlaceholder(Android.Content.Context context) : base(context) {}
  116. public UrhoSurfacePlaceholder(Android.Content.Context context, IAttributeSet attrs) : base(context, attrs) {}
  117. public UrhoSurfacePlaceholder(Android.Content.Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) {}
  118. public UrhoSurfacePlaceholder(Android.Content.Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) {}
  119. public async Task<Application> Show(Type appType, ApplicationOptions options = null, bool finishActivityOnExit = false)
  120. {
  121. Stop();
  122. launching = true;
  123. if (SDLSurface != null)
  124. {
  125. RemoveView(SDLSurface);
  126. }
  127. SDLSurface = SDLActivity.CreateSurface(Context as Activity);
  128. AddView(SDLSurface, ViewGroup.LayoutParams.MatchParent);
  129. var tcs = new TaskCompletionSource<Application>();
  130. Action startedHandler = null;
  131. startedHandler = () =>
  132. {
  133. Application.Started -= startedHandler;
  134. tcs.TrySetResult(Application.Current);
  135. };
  136. Application.Started += startedHandler;
  137. UrhoSurface.SetSdlMain(() => Application.CreateInstance(appType, options), finishActivityOnExit, SDLSurface);
  138. var app = await tcs.Task;
  139. launching = false;
  140. return app;
  141. }
  142. public async Task<TApplication> Show<TApplication>(ApplicationOptions options = null,
  143. bool finishActivityOnExit = false)
  144. where TApplication : Application
  145. {
  146. var app = await Show(typeof(TApplication), options, finishActivityOnExit);
  147. return (TApplication)app;
  148. }
  149. public void Stop()
  150. {
  151. if (launching)
  152. {
  153. Application.WaitStart();
  154. Console.WriteLine("WARNING: Stop while starting the urho app.");
  155. }
  156. //TODO: make sure Stop() is called in the main Android thread (not in the game thread)
  157. Application.StopAndroid();
  158. }
  159. }
  160. }