VJoyDialog.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // VJoyDialog.cpp : implementation file
  19. //
  20. #include "stdafx.h"
  21. #include "phystest.h"
  22. #include "VJoyDialog.h"
  23. #include "vector2.h"
  24. #include "vector3.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. #define JOYSTICK_UPDATE_COMMAND WM_USER+101
  31. #define SLIDER_RESOLUTION 255
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CVJoyDialog dialog
  34. CVJoyDialog::CVJoyDialog(CWnd* pParent /*=NULL*/)
  35. : CDialog(CVJoyDialog::IDD, pParent)
  36. {
  37. //{{AFX_DATA_INIT(CVJoyDialog)
  38. // NOTE: the ClassWizard will add member initialization here
  39. //}}AFX_DATA_INIT
  40. Controller.Reset();
  41. }
  42. CVJoyDialog::~CVJoyDialog(void)
  43. {
  44. }
  45. void CVJoyDialog::DoDataExchange(CDataExchange* pDX)
  46. {
  47. CDialog::DoDataExchange(pDX);
  48. //{{AFX_DATA_MAP(CVJoyDialog)
  49. DDX_Control(pDX, IDC_TURNZ_SLIDER, m_TurnZSlider);
  50. DDX_Control(pDX, IDC_MOVEZ_SLIDER, m_MoveZSlider);
  51. //}}AFX_DATA_MAP
  52. }
  53. BEGIN_MESSAGE_MAP(CVJoyDialog, CDialog)
  54. //{{AFX_MSG_MAP(CVJoyDialog)
  55. ON_WM_VSCROLL()
  56. ON_WM_DESTROY()
  57. //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59. /*
  60. ** Joystick Window Proc
  61. */
  62. LRESULT CALLBACK JoystickWndProc(HWND hwnd,unsigned int message,WPARAM wparam,LPARAM lparam)
  63. {
  64. Vector2 point;
  65. const int RADIUS = 4;
  66. switch (message)
  67. {
  68. case WM_LBUTTONDOWN:
  69. ::SetCapture(hwnd);
  70. break;
  71. case WM_LBUTTONUP:
  72. ::ReleaseCapture();
  73. break;
  74. case WM_MOUSEMOVE:
  75. if (wparam & MK_LBUTTON) {
  76. float ex,ey,cx,cy;
  77. short x = LOWORD(lparam);
  78. short y = HIWORD(lparam);
  79. RECT rect;
  80. GetClientRect(hwnd,&rect);
  81. if (x > rect.right) x = rect.right;
  82. if (x < rect.left) x = rect.left;
  83. if (y > rect.bottom) y = rect.bottom;
  84. if (y < rect.top) y = rect.top;
  85. ex = (float)(rect.right - rect.left) / 2.0f;
  86. cx = (float)(rect.right + rect.left) / 2.0f;
  87. ey = (float)(rect.bottom - rect.top) / 2.0f;
  88. cy = (float)(rect.bottom + rect.top) / 2.0f;
  89. point.X = ((float)x - cx) / ex;
  90. point.Y = ((float)y - cy) / ey;
  91. ::SendMessage(GetParent(hwnd),JOYSTICK_UPDATE_COMMAND,GetWindowLong(hwnd,GWL_ID),(long)&point);
  92. ::InvalidateRect(hwnd,NULL,FALSE);
  93. ::UpdateWindow(hwnd);
  94. SetProp(hwnd,"XCOORD",(HANDLE)x);
  95. SetProp(hwnd,"YCOORD",(HANDLE)y);
  96. }
  97. break;
  98. case WM_PAINT:
  99. {
  100. RECT rect;
  101. GetClientRect(hwnd,&rect);
  102. HDC hdc = GetDC(hwnd);
  103. FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
  104. FrameRect(hdc,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));
  105. MoveToEx(hdc,rect.right/2,0,NULL);
  106. LineTo(hdc,rect.right/2,rect.bottom);
  107. MoveToEx(hdc,0,rect.bottom/2,NULL);
  108. LineTo(hdc,rect.right,rect.bottom/2);
  109. int x = (int)GetProp(hwnd,"XCOORD");
  110. int y = (int)GetProp(hwnd,"YCOORD");
  111. if (x < RADIUS) x = RADIUS;
  112. if (x > rect.right-RADIUS) x = rect.right-RADIUS;
  113. if (y < RADIUS) y = RADIUS;
  114. if (y > rect.bottom-RADIUS) y = rect.bottom-RADIUS;
  115. MoveToEx(hdc,rect.bottom/2,rect.right/2,NULL);
  116. LineTo(hdc,x,y);
  117. Ellipse(hdc,x-RADIUS,y-RADIUS,x+RADIUS,y+RADIUS);
  118. ReleaseDC(hwnd,hdc);
  119. }
  120. break;
  121. }
  122. WNDPROC oldwndproc = (WNDPROC)GetProp(hwnd,"OldWndProc");
  123. //return CallWindowProc(oldwndproc,hwnd,message,wparam,lparam);
  124. return DefWindowProc (hwnd, message, wparam, lparam);
  125. }
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CVJoyDialog message handlers
  128. BOOL CVJoyDialog::OnInitDialog()
  129. {
  130. CDialog::OnInitDialog();
  131. RECT rect;
  132. HWND movexy_wnd = ::GetDlgItem(m_hWnd,IDC_MOVEXY_STATIC);
  133. long oldproc = SetWindowLong(movexy_wnd,GWL_WNDPROC,(long)JoystickWndProc);
  134. SetProp(movexy_wnd,"OldWndProc",(void*)oldproc);
  135. ::GetClientRect(movexy_wnd,&rect);
  136. SetProp(movexy_wnd,"XCOORD",(HANDLE)(rect.right/2));
  137. SetProp(movexy_wnd,"YCOORD",(HANDLE)(rect.bottom/2));
  138. HWND turnxy_wnd = ::GetDlgItem(m_hWnd,IDC_TURNXY_STATIC);
  139. oldproc = SetWindowLong(turnxy_wnd,GWL_WNDPROC,(long)JoystickWndProc);
  140. SetProp(turnxy_wnd,"OldWndProc",(void*)oldproc);
  141. ::GetClientRect(movexy_wnd,&rect);
  142. SetProp(turnxy_wnd,"XCOORD",(HANDLE)(rect.right/2));
  143. SetProp(turnxy_wnd,"YCOORD",(HANDLE)(rect.bottom/2));
  144. m_MoveZSlider.SetRange(0,SLIDER_RESOLUTION);
  145. m_TurnZSlider.SetRange(0,SLIDER_RESOLUTION);
  146. return TRUE;
  147. }
  148. LRESULT CVJoyDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  149. {
  150. if (message == JOYSTICK_UPDATE_COMMAND) {
  151. Vector2 * point = (Vector2*)lParam;
  152. if (wParam == IDC_MOVEXY_STATIC) {
  153. Controller.Set_Move_Forward(-point->Y);
  154. Controller.Set_Turn_Left(-point->X);
  155. } else if (wParam == IDC_TURNXY_STATIC) {
  156. // Nothing to do here anymore...
  157. }
  158. }
  159. return CDialog::WindowProc(message, wParam, lParam);
  160. }
  161. void CVJoyDialog::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  162. {
  163. if (pScrollBar == GetDlgItem(IDC_MOVEZ_SLIDER)) {
  164. Controller.Set_Move_Up(1.0f - 2.0f * (float)m_MoveZSlider.GetPos() / (float)SLIDER_RESOLUTION);
  165. }
  166. #if 0
  167. else if (pScrollBar == GetDlgItem(IDC_TURNZ_SLIDER)) {
  168. Controller.Set_Turn_Left(1.0f - 2.0f * (float)m_TurnZSlider.GetPos() / (float)SLIDER_RESOLUTION);
  169. }
  170. #endif
  171. CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
  172. }
  173. void CVJoyDialog::OnDestroy()
  174. {
  175. HWND movexy_wnd = ::GetDlgItem(m_hWnd,IDC_MOVEXY_STATIC);
  176. RemoveProp(movexy_wnd,"OldWndProc");
  177. RemoveProp(movexy_wnd,"XCOORD");
  178. RemoveProp(movexy_wnd,"YCOORD");
  179. HWND turnxy_wnd = ::GetDlgItem(m_hWnd,IDC_TURNXY_STATIC);
  180. RemoveProp(turnxy_wnd,"OldWndProc");
  181. RemoveProp(turnxy_wnd,"XCOORD");
  182. RemoveProp(turnxy_wnd,"YCOORD");
  183. CDialog::OnDestroy();
  184. }