in_next.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // in_next.m
  2. #import <AppKit/AppKit.h>
  3. #import <drivers/event_status_driver.h>
  4. #include "../client/client.h"
  5. float mousex, mousey;
  6. float mouse_center_x = 160;
  7. float mouse_center_y = 100;
  8. void PSsetmouse (float x, float y);
  9. void PSshowcursor (void);
  10. void PShidecursor (void);
  11. void PScurrentmouse (int win, float *x, float *y);
  12. extern NSView *vid_view_i;
  13. extern NSWindow *vid_window_i;
  14. qboolean mlooking;
  15. qboolean mouseinitialized;
  16. int mouse_buttons;
  17. int mouse_oldbuttonstate;
  18. int mouseactive;
  19. int mousereset;
  20. int mx_accum, my_accum;
  21. int window_center_x, window_center_y;
  22. int old_mouse_x, old_mouse_y;
  23. cvar_t in_mouse = {"in_mouse", "0", CVAR_ARCHIVE};
  24. cvar_t m_filter = {"m_filter", "0", CVAR_ARCHIVE};
  25. cvar_t freelook = {"in_freelook", "0", CVAR_ARCHIVE};
  26. /*
  27. ===========
  28. IN_ActivateMouse
  29. Called when the window gains focus or changes in some way
  30. ===========
  31. */
  32. void IN_ActivateMouse (void)
  33. {
  34. NSRect r;
  35. if (!mouseinitialized)
  36. return;
  37. if (!in_mouse.value)
  38. return;
  39. r = [vid_window_i frame];
  40. window_center_x = r.size.width / 2;
  41. window_center_y = r.size.height / 2;
  42. if (!mouseactive)
  43. PShidecursor ();
  44. mouseactive = true;
  45. mousereset = true;
  46. }
  47. /*
  48. ===========
  49. IN_DeactivateMouse
  50. Called when the window loses focus
  51. ===========
  52. */
  53. void IN_DeactivateMouse (void)
  54. {
  55. if (!mouseinitialized)
  56. return;
  57. if (mouseactive)
  58. PSshowcursor ();
  59. mouseactive = false;
  60. }
  61. /*
  62. ===========
  63. IN_StartupMouse
  64. ===========
  65. */
  66. void IN_StartupMouse (void)
  67. {
  68. if ( COM_CheckParm ("-nomouse") )
  69. return;
  70. mouseinitialized = true;
  71. mouse_buttons = 3;
  72. IN_ActivateMouse ();
  73. }
  74. /*
  75. ===========
  76. IN_MouseEvent
  77. ===========
  78. */
  79. void IN_MouseEvent (int mstate)
  80. {
  81. int i;
  82. if (!mouseactive)
  83. return;
  84. // perform button actions
  85. for (i=0 ; i<mouse_buttons ; i++)
  86. {
  87. if ( (mstate & (1<<i)) &&
  88. !(mouse_oldbuttonstate & (1<<i)) )
  89. {
  90. Key_Event (K_MOUSE1 + i, true);
  91. }
  92. if ( !(mstate & (1<<i)) &&
  93. (mouse_oldbuttonstate & (1<<i)) )
  94. {
  95. Key_Event (K_MOUSE1 + i, false);
  96. }
  97. }
  98. mouse_oldbuttonstate = mstate;
  99. }
  100. /*
  101. ===========
  102. IN_Accumulate
  103. ===========
  104. */
  105. void IN_Accumulate (void)
  106. {
  107. int dx, dy;
  108. static int old_x, old_y;
  109. if (!mouseinitialized)
  110. return;
  111. if (in_mouse.modified)
  112. {
  113. in_mouse.modified = false;
  114. IN_DeactivateMouse ();
  115. IN_ActivateMouse ();
  116. }
  117. if (!mouseactive)
  118. return;
  119. // [vid_view_i lockFocus];
  120. if (mousereset)
  121. { // we haven't centered cursor yet
  122. mousereset = false;
  123. }
  124. else
  125. {
  126. NSPoint p;
  127. PScurrentmouse ([vid_window_i windowNumber], &mousex, &mousey);
  128. p.x = mousex;
  129. p.y = mousey;
  130. p = [vid_view_i convertPoint:p fromView: nil];
  131. mousex = p.x;
  132. mousey = p.y;
  133. dx = mousex - old_x;
  134. dy = old_y - mousey;
  135. if (!dx && !dy)
  136. return;
  137. mx_accum += dx;
  138. my_accum += dy;
  139. }
  140. // force the mouse to the center, so there's room to move
  141. PSsetmouse (window_center_x, window_center_y);
  142. PScurrentmouse ([vid_window_i windowNumber], &mousex, &mousey);
  143. // PSsetmouse (window_center_x, window_center_y);
  144. old_x = window_center_x;
  145. old_y = window_center_y;
  146. // [vid_view_i unlockFocus];
  147. }
  148. /*
  149. ===========
  150. IN_MouseMove
  151. ===========
  152. */
  153. void IN_MouseMove (usercmd_t *cmd)
  154. {
  155. int mx, my;
  156. int mouse_x, mouse_y;
  157. IN_Accumulate ();
  158. mx = mx_accum;
  159. my = my_accum;
  160. mx_accum = 0;
  161. my_accum = 0;
  162. if (m_filter.value)
  163. {
  164. mouse_x = (mx + old_mouse_x) * 0.5;
  165. mouse_y = (my + old_mouse_y) * 0.5;
  166. }
  167. else
  168. {
  169. mouse_x = mx;
  170. mouse_y = my;
  171. }
  172. old_mouse_x = mx;
  173. old_mouse_y = my;
  174. if (!mx && !my)
  175. return;
  176. if (!mouseactive)
  177. return;
  178. mouse_x *= sensitivity.value;
  179. mouse_y *= sensitivity.value;
  180. // add mouse X/Y movement to cmd
  181. if ( (in_strafe.state & 1) || (lookstrafe.value && mlooking ))
  182. cmd->sidemove += m_side.value * mouse_x;
  183. else
  184. cl.viewangles[YAW] -= m_yaw.value * mouse_x;
  185. if ( (mlooking || freelook.value) && !(in_strafe.state & 1))
  186. {
  187. cl.viewangles[PITCH] += m_pitch.value * mouse_y;
  188. if (cl.viewangles[PITCH] > 80)
  189. cl.viewangles[PITCH] = 80;
  190. if (cl.viewangles[PITCH] < -70)
  191. cl.viewangles[PITCH] = -70;
  192. }
  193. else
  194. {
  195. cmd->forwardmove -= m_forward.value * mouse_y;
  196. }
  197. }
  198. void IN_ShowMouse (void)
  199. {
  200. PSshowcursor ();
  201. }
  202. void IN_HideMouse (void)
  203. {
  204. PShidecursor ();
  205. }
  206. NXEventHandle eventhandle;
  207. NXMouseScaling oldscaling, newscaling;
  208. NXMouseButton oldbutton;
  209. /*
  210. =============
  211. IN_Init
  212. =============
  213. */
  214. void IN_Init (void)
  215. {
  216. Cvar_RegisterVariable (&in_mouse);
  217. Cvar_RegisterVariable (&m_filter);
  218. Cvar_RegisterVariable (&freelook);
  219. Cmd_AddCommand ("showmouse", IN_ShowMouse);
  220. Cmd_AddCommand ("hidemouse", IN_HideMouse);
  221. IN_StartupMouse ();
  222. // open the event status driver
  223. eventhandle = NXOpenEventStatus();
  224. NXGetMouseScaling (eventhandle, &oldscaling);
  225. NXSetMouseScaling (eventhandle, &newscaling);
  226. oldbutton = NXMouseButtonEnabled (eventhandle);
  227. NXEnableMouseButton (eventhandle, 2);
  228. }
  229. /*
  230. =============
  231. IN_Shutdown
  232. =============
  233. */
  234. void IN_Shutdown (void)
  235. {
  236. IN_DeactivateMouse ();
  237. // put mouse scaling back the way it was
  238. NXSetMouseScaling (eventhandle, &oldscaling);
  239. NXEnableMouseButton (eventhandle, oldbutton);
  240. NXCloseEventStatus (eventhandle);
  241. }
  242. void IN_Move (usercmd_t *cmd)
  243. {
  244. IN_MouseMove (cmd);
  245. }
  246. void IN_Commands (void)
  247. {
  248. }
  249. /*
  250. =========================================================================
  251. VIEW CENTERING
  252. =========================================================================
  253. */
  254. void V_StopPitchDrift (void)
  255. {
  256. cl.laststop = cl.time;
  257. cl.nodrift = true;
  258. cl.pitchvel = 0;
  259. }