Loading

Paste #p8yc5rgth

  1. Index: src/video/sdl_v.cpp
  2. ===================================================================
  3. --- src/video/sdl_v.cpp (revision 27164)
  4. +++ src/video/sdl_v.cpp (working copy)
  5. @@ -545,20 +545,8 @@
  6.  
  7.     switch (ev.type) {
  8.         case SDL_MOUSEMOTION:
  9. -           if (_cursor.fix_at) {
  10. -               int dx = ev.motion.x - _cursor.pos.x;
  11. -               int dy = ev.motion.y - _cursor.pos.y;
  12. -               if (dx != 0 || dy != 0) {
  13. -                   _cursor.delta.x = dx;
  14. -                   _cursor.delta.y = dy;
  15. -                   SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
  16. -               }
  17. -           } else {
  18. -               _cursor.delta.x = ev.motion.x - _cursor.pos.x;
  19. -               _cursor.delta.y = ev.motion.y - _cursor.pos.y;
  20. -               _cursor.pos.x = ev.motion.x;
  21. -               _cursor.pos.y = ev.motion.y;
  22. -               _cursor.dirty = true;
  23. +           if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y)) {
  24. +               SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
  25.             }
  26.             HandleMouseEvents();
  27.             break;
  28. Index: src/video/win32_v.cpp
  29. ===================================================================
  30. --- src/video/win32_v.cpp   (revision 27164)
  31. +++ src/video/win32_v.cpp   (working copy)
  32. @@ -747,25 +747,11 @@
  33.                 SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc);
  34.             }
  35.  
  36. -           if (_cursor.fix_at) {
  37. -               int dx = x - _cursor.pos.x;
  38. -               int dy = y - _cursor.pos.y;
  39. -               if (dx != 0 || dy != 0) {
  40. -                   _cursor.delta.x = dx;
  41. -                   _cursor.delta.y = dy;
  42. -
  43. -                   pt.x = _cursor.pos.x;
  44. -                   pt.y = _cursor.pos.y;
  45. -
  46. -                   ClientToScreen(hwnd, &pt);
  47. -                   SetCursorPos(pt.x, pt.y);
  48. -               }
  49. -           } else {
  50. -               _cursor.delta.x = x - _cursor.pos.x;
  51. -               _cursor.delta.y = y - _cursor.pos.y;
  52. -               _cursor.pos.x = x;
  53. -               _cursor.pos.y = y;
  54. -               _cursor.dirty = true;
  55. +           if (_cursor.UpdateCursorPosition(x, y)) {
  56. +               pt.x = _cursor.pos.x;
  57. +               pt.y = _cursor.pos.y;
  58. +               ClientToScreen(hwnd, &pt);
  59. +               SetCursorPos(pt.x, pt.y);
  60.             }
  61.             MyShowCursor(false);
  62.             HandleMouseEvents();
  63. Index: src/video/allegro_v.cpp
  64. ===================================================================
  65. --- src/video/allegro_v.cpp (revision 27164)
  66. +++ src/video/allegro_v.cpp (working copy)
  67. @@ -388,22 +388,10 @@
  68.     }
  69.  
  70.     /* Mouse movement */
  71. -   int dx = mouse_x - _cursor.pos.x;
  72. -   int dy = mouse_y - _cursor.pos.y;
  73. -   if (dx != 0 || dy != 0) {
  74. -       if (_cursor.fix_at) {
  75. -           _cursor.delta.x = dx;
  76. -           _cursor.delta.y = dy;
  77. -           position_mouse(_cursor.pos.x, _cursor.pos.y);
  78. -       } else {
  79. -           _cursor.delta.x = dx;
  80. -           _cursor.delta.y = dy;
  81. -           _cursor.pos.x = mouse_x;
  82. -           _cursor.pos.y = mouse_y;
  83. -           _cursor.dirty = true;
  84. -       }
  85. -       mouse_action = true;
  86. +   if (_cursor.UpdateCursorPosition(mouse_x, mouse_y)) {
  87. +       position_mouse(_cursor.pos.x, _cursor.pos.y); // TODO when does this take effect?
  88.     }
  89. +   if (_cursor.delta.x != 0 || _cursor.delta.y) mouse_action = true;
  90.  
  91.     static int prev_mouse_z = 0;
  92.     if (prev_mouse_z != mouse_z) {
  93. Index: src/video/cocoa/event.mm
  94. ===================================================================
  95. --- src/video/cocoa/event.mm    (revision 27164)
  96. +++ src/video/cocoa/event.mm    (working copy)
  97. @@ -362,22 +362,8 @@
  98.  
  99.  static void QZ_MouseMovedEvent(int x, int y)
  100.  {
  101. -   if (_cursor.fix_at) {
  102. -       int dx = x - _cursor.pos.x;
  103. -       int dy = y - _cursor.pos.y;
  104. -
  105. -       if (dx != 0 || dy != 0) {
  106. -           _cursor.delta.x += dx;
  107. -           _cursor.delta.y += dy;
  108. -
  109. -           QZ_WarpCursor(_cursor.pos.x, _cursor.pos.y);
  110. -       }
  111. -   } else {
  112. -       _cursor.delta.x = x - _cursor.pos.x;
  113. -       _cursor.delta.y = y - _cursor.pos.y;
  114. -       _cursor.pos.x = x;
  115. -       _cursor.pos.y = y;
  116. -       _cursor.dirty = true;
  117. +   if (_cursor.UpdateCursorPosition(x, y)) {
  118. +       QZ_WarpCursor(_cursor.pos.x, _cursor.pos.y); // TODO when does this take effect?
  119.     }
  120.     HandleMouseEvents();
  121.  }
  122. Index: src/gfx.cpp
  123. ===================================================================
  124. --- src/gfx.cpp (revision 27164)
  125. +++ src/gfx.cpp (working copy)
  126. @@ -1603,6 +1603,51 @@
  127.     SwitchAnimatedCursor();
  128.  }
  129.  
  130. +/**
  131. + * Update cursor position on mouse movement.
  132. + * @param x New X position.
  133. + * @param y New Y position.
  134. + * @return true, when the OS cursor position should be warped back to this->pos.
  135. + */
  136. +bool CursorVars::UpdateCursorPosition(int x, int y)
  137. +{
  138. +   /* Detecting relative mouse movement is somewhat tricky.
  139. +    *  - There may me multiple mouse move events in the video driver queue (esp. when OpenTTD lags a bit).
  140. +    *  - When we request warping the mouse position (return true), a mouse move event is appended at the end of the queue.
  141. +    *
  142. +    * So, when this->fix_at is active, we use the following strategy:
  143. +    *  - The first movement triggers the warp to reset the mouse position.
  144. +    *  - Subsequent events have to compute movement relative to the previous event.
  145. +    *  - The relative movement is finished, when we receive the event matching the warp.
  146. +    */
  147. +
  148. +   if (x == this->pos.x && y == this->pos.y) {
  149. +       /* Warp finished. */
  150. +       this->warped = false;
  151. +   }
  152. +
  153. +   this->delta.x = x - (this->warped ? this->last_position.x : this->pos.x);
  154. +   this->delta.y = y - (this->warped ? this->last_position.y : this->pos.y);
  155. +
  156. +   this->last_position.x = x;
  157. +   this->last_position.y = y;
  158. +
  159. +   bool need_warp = false;
  160. +   if (this->fix_at) {
  161. +       if (!this->warped && (this->delta.x != 0 || this->delta.y != 0)) {
  162. +           /* Trigger warp. */
  163. +           this->warped = true;
  164. +           need_warp = true;
  165. +       }
  166. +   } else if (this->pos.x != x || this->pos.y != y) {
  167. +       this->warped = false; // Cancel warping, we are no longer confining the position.
  168. +       this->dirty = true;
  169. +       this->pos.x = x;
  170. +       this->pos.y = y;
  171. +   }
  172. +   return need_warp;
  173. +}
  174. +
  175.  bool ChangeResInGame(int width, int height)
  176.  {
  177.     return (_screen.width == width && _screen.height == height) || VideoDriver::GetInstance()->ChangeResolution(width, height);
  178. Index: src/gfx_type.h
  179. ===================================================================
  180. --- src/gfx_type.h  (revision 27164)
  181. +++ src/gfx_type.h  (working copy)
  182. @@ -138,6 +138,12 @@
  183.     bool in_window;  ///< mouse inside this window, determines drawing logic
  184.  
  185.     bool vehchain;   ///< vehicle chain is dragged
  186. +
  187. +   bool UpdateCursorPosition(int x, int y);
  188. +
  189. +private:
  190. +   bool warped;
  191. +   Point last_position;
  192.  };
  193.  
  194.  /** Data about how and where to blit pixels. */
  195.  

Comments