Guest User

Untitled

a guest
Aug 28th, 2017
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. using OpenTK;
  4. using OpenTK.Platform.Android;
  5.  
  6. using Android.Views;
  7. using Android.Content;
  8. using Android.Util;
  9. using Android.Runtime;
  10. using Noesis;
  11. using System.IO;
  12.  
  13. namespace App1
  14. {
  15. public class AndroidXamlProvider : XamlProvider
  16. {
  17. public override Stream LoadXaml(string filename)
  18. {
  19. var memory = new MemoryStream();
  20. using (var res = Android.App.Application.Context.Assets.Open(filename))
  21. {
  22. res.CopyTo(memory);
  23. memory.Position = 0;
  24. }
  25. return memory;
  26. }
  27. }
  28.  
  29. public class AndroidFontProvider : FontProvider
  30. {
  31. public override void ScanFolder(string folder)
  32. {
  33. var strs = Android.App.Application.Context.Assets.List(folder);
  34. foreach(string s in strs)
  35. {
  36. RegisterFont(folder, s);
  37. }
  38. }
  39.  
  40. public override Stream OpenFont(string folder, string id)
  41. {
  42. var memory = new MemoryStream();
  43. using (var res = Android.App.Application.Context.Assets.Open(folder + "/" + id))
  44. {
  45. res.CopyTo(memory); // ReadByte(); //Read(buf, 0, (int)res.Length);
  46. memory.Position = 0;
  47. }
  48. return memory;
  49. }
  50. }
  51.  
  52.  
  53. class GLView1 : AndroidGameView
  54. {
  55. static Noesis.View _view = null;
  56. static Noesis.Renderer _renderer = null;
  57. static bool _eventsAttached = false;
  58. bool inited = false;
  59. public GLView1(Context context) : base(context)
  60. {
  61.  
  62. AutoSetContextOnRenderFrame = true;
  63.  
  64.  
  65. RenderOnUIThread = true;
  66. }
  67.  
  68.  
  69. protected override void OnLoad(System.EventArgs e)
  70. {
  71. base.OnLoad(e);
  72.  
  73.  
  74. Run();
  75. }
  76.  
  77. public override bool OnTouchEvent(MotionEvent e)
  78. {
  79. base.OnTouchEvent(e);
  80.  
  81. var e_x = (int)e.GetX();
  82. var e_y = (int)e.GetY();
  83.  
  84. //this doesn't really work, but sometimes it does
  85.  
  86. if (e.Action == MotionEventActions.Down)
  87. _view.TouchDown(e_x, e_y, 0); //I don't know what touch id is, sorry
  88. if (e.Action == MotionEventActions.Move)
  89. _view.TouchMove(e_x, e_y, 0);
  90. if (e.Action == MotionEventActions.Up)
  91. _view.TouchUp(e_x, e_y, 0);
  92.  
  93.  
  94. return true;
  95. }
  96.  
  97. protected override void CreateFrameBuffer()
  98. {
  99. ContextRenderingApi = OpenTK.Graphics.GLVersion.ES2;
  100.  
  101. // the default GraphicsMode that is set consists of (16, 16, 0, 0, 2, false)
  102. try
  103. {
  104. Android.Util.Log.Verbose("GLCube", "Loading with default settings");
  105.  
  106.  
  107. base.CreateFrameBuffer();
  108. return;
  109. }
  110. catch (Exception ex)
  111. {
  112. Android.Util.Log.Verbose("GLCube", $"{ex}");
  113. }
  114.  
  115.  
  116. try
  117. {
  118. Android.Util.Log.Verbose("GLCube", "Loading with custom Android settings (low mode)");
  119. GraphicsMode = new AndroidGraphicsMode(0, 0, 0, 0, 0, false);
  120.  
  121.  
  122. base.CreateFrameBuffer();
  123. return;
  124. }
  125. catch (Exception ex)
  126. {
  127. Android.Util.Log.Verbose("GLCube", $"{ex}");
  128. }
  129. throw new Exception("Can't load egl, aborting");
  130. }
  131.  
  132.  
  133. DateTime start = DateTime.Now;
  134. protected override void OnRenderFrame(FrameEventArgs e)
  135. {
  136. base.OnRenderFrame(e);
  137.  
  138. if(!inited)
  139. {
  140. Noesis.GUI.Init();
  141. Noesis.GUI.SetResourceProvider(new Provider() { XamlProvider = new AndroidXamlProvider(), FontProvider = new AndroidFontProvider() });
  142.  
  143. // Global theme
  144. {
  145. var theme = (Noesis.ResourceDictionary)Noesis.GUI.LoadXaml("NoesisStyle.xaml");
  146. Noesis.GUI.SetTheme(theme);
  147. }
  148.  
  149. // Data loading
  150. {
  151. var content = (Noesis.Grid)Noesis.GUI.LoadXaml("TextBox.xaml");
  152. _view = Noesis.GUI.CreateView(content);
  153.  
  154. IWindowManager windowManager = Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
  155. var device = windowManager.DefaultDisplay;
  156. Android.Graphics.Point size = new Android.Graphics.Point();
  157. device.GetSize(size);
  158. _view.SetSize(size.X, size.Y);
  159.  
  160. _renderer = _view.Renderer;
  161. _renderer.InitGL(new Noesis.VGOptions() { });
  162. }
  163. inited = true;
  164. }
  165. _view.Update((DateTime.Now - start).TotalSeconds);
  166.  
  167. _renderer.UpdateRenderTree();
  168.  
  169. if (_renderer.NeedsOffscreen())
  170. {
  171. _renderer.RenderOffscreen();
  172. }
  173.  
  174. _renderer.Render();
  175.  
  176. SwapBuffers();
  177. }
  178.  
  179. }
  180. }
RAW Paste Data