GLScreen.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using OpenTK;
  2. using OpenTK.Graphics;
  3. using OpenTK.Input;
  4. using Ryujinx.Graphics.Gal;
  5. using Ryujinx.HLE;
  6. using Ryujinx.HLE.Input;
  7. using System;
  8. namespace Ryujinx
  9. {
  10. public class GLScreen : GameWindow
  11. {
  12. private const int TouchScreenWidth = 1280;
  13. private const int TouchScreenHeight = 720;
  14. private const float TouchScreenRatioX = (float)TouchScreenWidth / TouchScreenHeight;
  15. private const float TouchScreenRatioY = (float)TouchScreenHeight / TouchScreenWidth;
  16. private Switch Ns;
  17. private IGalRenderer Renderer;
  18. private KeyboardState? Keyboard = null;
  19. private MouseState? Mouse = null;
  20. public GLScreen(Switch Ns, IGalRenderer Renderer)
  21. : base(1280, 720,
  22. new GraphicsMode(), "Ryujinx", 0,
  23. DisplayDevice.Default, 3, 3,
  24. GraphicsContextFlags.ForwardCompatible)
  25. {
  26. this.Ns = Ns;
  27. this.Renderer = Renderer;
  28. Location = new Point(
  29. (DisplayDevice.Default.Width / 2) - (Width / 2),
  30. (DisplayDevice.Default.Height / 2) - (Height / 2));
  31. }
  32. protected override void OnLoad(EventArgs e)
  33. {
  34. VSync = VSyncMode.On;
  35. Renderer.FrameBuffer.SetWindowSize(Width, Height);
  36. }
  37. private bool IsGamePadButtonPressedFromString(GamePadState GamePad, string Button)
  38. {
  39. if (Button.ToUpper() == "LTRIGGER" || Button.ToUpper() == "RTRIGGER")
  40. {
  41. return GetGamePadTriggerFromString(GamePad, Button) >= Config.GamePadTriggerThreshold;
  42. }
  43. else
  44. {
  45. return (GetGamePadButtonFromString(GamePad, Button) == ButtonState.Pressed);
  46. }
  47. }
  48. private ButtonState GetGamePadButtonFromString(GamePadState GamePad, string Button)
  49. {
  50. switch (Button.ToUpper())
  51. {
  52. case "A": return GamePad.Buttons.A;
  53. case "B": return GamePad.Buttons.B;
  54. case "X": return GamePad.Buttons.X;
  55. case "Y": return GamePad.Buttons.Y;
  56. case "LSTICK": return GamePad.Buttons.LeftStick;
  57. case "RSTICK": return GamePad.Buttons.RightStick;
  58. case "LSHOULDER": return GamePad.Buttons.LeftShoulder;
  59. case "RSHOULDER": return GamePad.Buttons.RightShoulder;
  60. case "DPADUP": return GamePad.DPad.Up;
  61. case "DPADDOWN": return GamePad.DPad.Down;
  62. case "DPADLEFT": return GamePad.DPad.Left;
  63. case "DPADRIGHT": return GamePad.DPad.Right;
  64. case "START": return GamePad.Buttons.Start;
  65. case "BACK": return GamePad.Buttons.Back;
  66. default: throw new ArgumentException();
  67. }
  68. }
  69. private float GetGamePadTriggerFromString(GamePadState GamePad, string Trigger)
  70. {
  71. switch (Trigger.ToUpper())
  72. {
  73. case "LTRIGGER": return GamePad.Triggers.Left;
  74. case "RTRIGGER": return GamePad.Triggers.Right;
  75. default: throw new ArgumentException();
  76. }
  77. }
  78. private Vector2 GetJoystickAxisFromString(GamePadState GamePad, string Joystick)
  79. {
  80. switch (Joystick.ToUpper())
  81. {
  82. case "LJOYSTICK": return GamePad.ThumbSticks.Left;
  83. case "RJOYSTICK": return new Vector2(-GamePad.ThumbSticks.Right.Y, -GamePad.ThumbSticks.Right.X);
  84. default: throw new ArgumentException();
  85. }
  86. }
  87. protected override void OnUpdateFrame(FrameEventArgs e)
  88. {
  89. HidControllerButtons CurrentButton = 0;
  90. HidJoystickPosition LeftJoystick;
  91. HidJoystickPosition RightJoystick;
  92. int LeftJoystickDX = 0;
  93. int LeftJoystickDY = 0;
  94. int RightJoystickDX = 0;
  95. int RightJoystickDY = 0;
  96. float AnalogStickDeadzone = Config.GamePadDeadzone;
  97. //Keyboard Input
  98. if (Keyboard.HasValue)
  99. {
  100. KeyboardState Keyboard = this.Keyboard.Value;
  101. if (Keyboard[Key.Escape]) this.Exit();
  102. //LeftJoystick
  103. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickUp]) LeftJoystickDY = short.MaxValue;
  104. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickDown]) LeftJoystickDY = -short.MaxValue;
  105. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickLeft]) LeftJoystickDX = -short.MaxValue;
  106. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickRight]) LeftJoystickDX = short.MaxValue;
  107. //LeftButtons
  108. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickButton]) CurrentButton |= HidControllerButtons.KEY_LSTICK;
  109. if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadUp]) CurrentButton |= HidControllerButtons.KEY_DUP;
  110. if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadDown]) CurrentButton |= HidControllerButtons.KEY_DDOWN;
  111. if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadLeft]) CurrentButton |= HidControllerButtons.KEY_DLEFT;
  112. if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadRight]) CurrentButton |= HidControllerButtons.KEY_DRIGHT;
  113. if (Keyboard[(Key)Config.JoyConKeyboard.Left.ButtonMinus]) CurrentButton |= HidControllerButtons.KEY_MINUS;
  114. if (Keyboard[(Key)Config.JoyConKeyboard.Left.ButtonL]) CurrentButton |= HidControllerButtons.KEY_L;
  115. if (Keyboard[(Key)Config.JoyConKeyboard.Left.ButtonZL]) CurrentButton |= HidControllerButtons.KEY_ZL;
  116. //RightJoystick
  117. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickUp]) RightJoystickDY = short.MaxValue;
  118. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickDown]) RightJoystickDY = -short.MaxValue;
  119. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickLeft]) RightJoystickDX = -short.MaxValue;
  120. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickRight]) RightJoystickDX = short.MaxValue;
  121. //RightButtons
  122. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickButton]) CurrentButton |= HidControllerButtons.KEY_RSTICK;
  123. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonA]) CurrentButton |= HidControllerButtons.KEY_A;
  124. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonB]) CurrentButton |= HidControllerButtons.KEY_B;
  125. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonX]) CurrentButton |= HidControllerButtons.KEY_X;
  126. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonY]) CurrentButton |= HidControllerButtons.KEY_Y;
  127. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonPlus]) CurrentButton |= HidControllerButtons.KEY_PLUS;
  128. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonR]) CurrentButton |= HidControllerButtons.KEY_R;
  129. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonZR]) CurrentButton |= HidControllerButtons.KEY_ZR;
  130. }
  131. //Controller Input
  132. if (Config.GamePadEnable)
  133. {
  134. GamePadState GamePad = OpenTK.Input.GamePad.GetState(Config.GamePadIndex);
  135. //LeftButtons
  136. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadUp)) CurrentButton |= HidControllerButtons.KEY_DUP;
  137. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadDown)) CurrentButton |= HidControllerButtons.KEY_DDOWN;
  138. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadLeft)) CurrentButton |= HidControllerButtons.KEY_DLEFT;
  139. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadRight)) CurrentButton |= HidControllerButtons.KEY_DRIGHT;
  140. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.StickButton)) CurrentButton |= HidControllerButtons.KEY_LSTICK;
  141. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.ButtonMinus)) CurrentButton |= HidControllerButtons.KEY_MINUS;
  142. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.ButtonL)) CurrentButton |= HidControllerButtons.KEY_L;
  143. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.ButtonZL)) CurrentButton |= HidControllerButtons.KEY_ZL;
  144. //RightButtons
  145. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonA)) CurrentButton |= HidControllerButtons.KEY_A;
  146. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonB)) CurrentButton |= HidControllerButtons.KEY_B;
  147. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonX)) CurrentButton |= HidControllerButtons.KEY_X;
  148. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonY)) CurrentButton |= HidControllerButtons.KEY_Y;
  149. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.StickButton)) CurrentButton |= HidControllerButtons.KEY_RSTICK;
  150. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonPlus)) CurrentButton |= HidControllerButtons.KEY_PLUS;
  151. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonR)) CurrentButton |= HidControllerButtons.KEY_R;
  152. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonZR)) CurrentButton |= HidControllerButtons.KEY_ZR;
  153. //LeftJoystick
  154. if (GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).X >= AnalogStickDeadzone
  155. || GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).X <= -AnalogStickDeadzone)
  156. LeftJoystickDX = (int)(GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).X * short.MaxValue);
  157. if (GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).Y >= AnalogStickDeadzone
  158. || GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).Y <= -AnalogStickDeadzone)
  159. LeftJoystickDY = (int)(GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).Y * short.MaxValue);
  160. //RightJoystick
  161. if (GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).X >= AnalogStickDeadzone
  162. || GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).X <= -AnalogStickDeadzone)
  163. RightJoystickDX = (int)(GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).X * short.MaxValue);
  164. if (GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).Y >= AnalogStickDeadzone
  165. || GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).Y <= -AnalogStickDeadzone)
  166. RightJoystickDY = (int)(GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).Y * short.MaxValue);
  167. }
  168. LeftJoystick = new HidJoystickPosition
  169. {
  170. DX = LeftJoystickDX,
  171. DY = LeftJoystickDY
  172. };
  173. RightJoystick = new HidJoystickPosition
  174. {
  175. DX = RightJoystickDX,
  176. DY = RightJoystickDY
  177. };
  178. bool HasTouch = false;
  179. //Get screen touch position from left mouse click
  180. //OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  181. if (Focused && Mouse?.LeftButton == ButtonState.Pressed)
  182. {
  183. MouseState Mouse = this.Mouse.Value;
  184. int ScrnWidth = Width;
  185. int ScrnHeight = Height;
  186. if (Width > Height * TouchScreenRatioX)
  187. {
  188. ScrnWidth = (int)(Height * TouchScreenRatioX);
  189. }
  190. else
  191. {
  192. ScrnHeight = (int)(Width * TouchScreenRatioY);
  193. }
  194. int StartX = (Width - ScrnWidth) >> 1;
  195. int StartY = (Height - ScrnHeight) >> 1;
  196. int EndX = StartX + ScrnWidth;
  197. int EndY = StartY + ScrnHeight;
  198. if (Mouse.X >= StartX &&
  199. Mouse.Y >= StartY &&
  200. Mouse.X < EndX &&
  201. Mouse.Y < EndY)
  202. {
  203. int ScrnMouseX = Mouse.X - StartX;
  204. int ScrnMouseY = Mouse.Y - StartY;
  205. int MX = (int)(((float)ScrnMouseX / ScrnWidth) * TouchScreenWidth);
  206. int MY = (int)(((float)ScrnMouseY / ScrnHeight) * TouchScreenHeight);
  207. HidTouchPoint CurrentPoint = new HidTouchPoint
  208. {
  209. X = MX,
  210. Y = MY,
  211. //Placeholder values till more data is acquired
  212. DiameterX = 10,
  213. DiameterY = 10,
  214. Angle = 90
  215. };
  216. HasTouch = true;
  217. Ns.Hid.SetTouchPoints(CurrentPoint);
  218. }
  219. }
  220. if (!HasTouch)
  221. {
  222. Ns.Hid.SetTouchPoints();
  223. }
  224. Ns.Hid.SetJoyconButton(
  225. HidControllerId.CONTROLLER_HANDHELD,
  226. HidControllerLayouts.Handheld_Joined,
  227. CurrentButton,
  228. LeftJoystick,
  229. RightJoystick);
  230. Ns.Hid.SetJoyconButton(
  231. HidControllerId.CONTROLLER_HANDHELD,
  232. HidControllerLayouts.Main,
  233. CurrentButton,
  234. LeftJoystick,
  235. RightJoystick);
  236. Ns.ProcessFrame();
  237. Renderer.RunActions();
  238. }
  239. protected override void OnRenderFrame(FrameEventArgs e)
  240. {
  241. Renderer.FrameBuffer.Render();
  242. Ns.Statistics.RecordSystemFrameTime();
  243. double HostFps = Ns.Statistics.GetSystemFrameRate();
  244. double GameFps = Ns.Statistics.GetGameFrameRate();
  245. Title = $"Ryujinx | Host FPS: {HostFps:0.0} | Game FPS: {GameFps:0.0}";
  246. SwapBuffers();
  247. Ns.Os.SignalVsync();
  248. }
  249. protected override void OnResize(EventArgs e)
  250. {
  251. Renderer.FrameBuffer.SetWindowSize(Width, Height);
  252. }
  253. protected override void OnKeyDown(KeyboardKeyEventArgs e)
  254. {
  255. Keyboard = e.Keyboard;
  256. }
  257. protected override void OnKeyUp(KeyboardKeyEventArgs e)
  258. {
  259. Keyboard = e.Keyboard;
  260. }
  261. protected override void OnMouseDown(MouseButtonEventArgs e)
  262. {
  263. Mouse = e.Mouse;
  264. }
  265. protected override void OnMouseUp(MouseButtonEventArgs e)
  266. {
  267. Mouse = e.Mouse;
  268. }
  269. protected override void OnMouseMove(MouseMoveEventArgs e)
  270. {
  271. Mouse = e.Mouse;
  272. }
  273. }
  274. }