mousei.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. {
  2. Free Pascal port of the OpenPTC C++ library.
  3. Copyright (C) 2001-2006 Nikolay Nikolov ([email protected])
  4. Original C++ version by Glenn Fiedler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. }
  17. Constructor TWin32Mouse.Create(window : HWND; thread : DWord; multithreaded : Boolean; EventQueue : TEventQueue; FullScreen : Boolean; ConsoleWidth, ConsoleHeight : Integer);
  18. Begin
  19. Inherited Create(window, thread);
  20. FEventQueue := EventQueue;
  21. FFullScreen := FullScreen;
  22. FConsoleWidth := ConsoleWidth;
  23. FConsoleHeight := ConsoleHeight;
  24. FPreviousMousePositionSaved := False;
  25. { enable buffering }
  26. FEnabled := True;
  27. End;
  28. Procedure TWin32Mouse.SetWindowArea(WindowX1, WindowY1, WindowX2, WindowY2 : Integer);
  29. Begin
  30. FWindowX1 := WindowX1;
  31. FWindowY1 := WindowY1;
  32. FWindowX2 := WindowX2;
  33. FWindowY2 := WindowY2;
  34. End;
  35. Procedure TWin32Mouse.enable;
  36. Begin
  37. { enable buffering }
  38. FEnabled := True;
  39. End;
  40. Procedure TWin32Mouse.disable;
  41. Begin
  42. { disable buffering }
  43. FEnabled := False;
  44. End;
  45. Function TWin32Mouse.WndProc(hWnd : HWND; message : DWord; wParam : WPARAM; lParam : LPARAM) : LRESULT;
  46. Var
  47. fwKeys : Integer;
  48. xPos, yPos : Integer;
  49. LButton, MButton, RButton : Boolean;
  50. TranslatedXPos, TranslatedYPos : Integer;
  51. PTCMouseButtonState : TPTCMouseButtonState;
  52. WindowRect : RECT;
  53. button : TPTCMouseButton;
  54. before, after : Boolean;
  55. cstate : TPTCMouseButtonState;
  56. Begin
  57. Result := 0;
  58. { check enabled flag }
  59. If Not FEnabled Then
  60. Exit;
  61. If (message = WM_MOUSEMOVE) Or
  62. (message = WM_LBUTTONDOWN) Or (message = WM_LBUTTONUP) Or (message = WM_LBUTTONDBLCLK) Or
  63. (message = WM_MBUTTONDOWN) Or (message = WM_MBUTTONUP) Or (message = WM_MBUTTONDBLCLK) Or
  64. (message = WM_RBUTTONDOWN) Or (message = WM_RBUTTONUP) Or (message = WM_RBUTTONDBLCLK) Then
  65. Begin
  66. fwKeys := wParam; {MK_LBUTTON or MK_MBUTTON or MK_RBUTTON or MK_CONTROL or MK_SHIFT}
  67. xPos := lParam And $FFFF;
  68. yPos := (lParam Shr 16) And $FFFF;
  69. LButton := (fwKeys And MK_LBUTTON) <> 0;
  70. MButton := (fwKeys And MK_MBUTTON) <> 0;
  71. RButton := (fwKeys And MK_RBUTTON) <> 0;
  72. If Not FFullScreen Then
  73. Begin
  74. GetClientRect(hWnd, WindowRect);
  75. FWindowX1 := WindowRect.left;
  76. FWindowY1 := WindowRect.top;
  77. FWindowX2 := WindowRect.right - 1;
  78. FWindowY2 := WindowRect.bottom - 1;
  79. End;
  80. If (xPos >= FWindowX1) And (yPos >= FWindowY1) And
  81. (xPos <= FWindowX2) And (yPos <= FWindowY2) Then
  82. Begin
  83. If FWindowX2 <> FWindowX1 Then
  84. TranslatedXPos := (xPos - FWindowX1) * (FConsoleWidth - 1) Div (FWindowX2 - FWindowX1)
  85. Else { avoid div by zero }
  86. TranslatedXPos := 0;
  87. If FWindowY2 <> FWindowY1 Then
  88. TranslatedYPos := (yPos - FWindowY1) * (FConsoleHeight - 1) Div (FWindowY2 - FWindowY1)
  89. Else { avoid div by zero }
  90. TranslatedYPos := 0;
  91. { Just in case... }
  92. If TranslatedXPos < 0 Then
  93. TranslatedXPos := 0;
  94. If TranslatedYPos < 0 Then
  95. TranslatedYPos := 0;
  96. If TranslatedXPos >= FConsoleWidth Then
  97. TranslatedXPos := FConsoleWidth - 1;
  98. If TranslatedYPos >= FConsoleHeight Then
  99. TranslatedYPos := FConsoleHeight - 1;
  100. If Not LButton Then
  101. PTCMouseButtonState := []
  102. Else
  103. PTCMouseButtonState := [PTCMouseButton1];
  104. If RButton Then
  105. PTCMouseButtonState := PTCMouseButtonState + [PTCMouseButton2];
  106. If MButton Then
  107. PTCMouseButtonState := PTCMouseButtonState + [PTCMouseButton3];
  108. If Not FPreviousMousePositionSaved Then
  109. Begin
  110. FPreviousMouseX := TranslatedXPos; { first DeltaX will be 0 }
  111. FPreviousMouseY := TranslatedYPos; { first DeltaY will be 0 }
  112. FPreviousMouseButtonState := [];
  113. End;
  114. { movement? }
  115. If (TranslatedXPos <> FPreviousMouseX) Or (TranslatedYPos <> FPreviousMouseY) Then
  116. FEventQueue.AddEvent(TPTCMouseEvent.Create(TranslatedXPos, TranslatedYPos, TranslatedXPos - FPreviousMouseX, TranslatedYPos - FPreviousMouseY, FPreviousMouseButtonState));
  117. { button presses/releases? }
  118. cstate := FPreviousMouseButtonState;
  119. For button := Low(button) To High(button) Do
  120. Begin
  121. before := button In FPreviousMouseButtonState;
  122. after := button In PTCMouseButtonState;
  123. If after And (Not before) Then
  124. Begin
  125. { button was pressed }
  126. cstate := cstate + [button];
  127. FEventQueue.AddEvent(TPTCMouseButtonEvent.Create(TranslatedXPos, TranslatedYPos, 0, 0, cstate, True, button));
  128. End
  129. Else
  130. If before And (Not after) Then
  131. Begin
  132. { button was released }
  133. cstate := cstate - [button];
  134. FEventQueue.AddEvent(TPTCMouseButtonEvent.Create(TranslatedXPos, TranslatedYPos, 0, 0, cstate, False, button));
  135. End;
  136. End;
  137. FPreviousMouseX := TranslatedXPos;
  138. FPreviousMouseY := TranslatedYPos;
  139. FPreviousMouseButtonState := PTCMouseButtonState;
  140. FPreviousMousePositionSaved := True;
  141. End;
  142. End;
  143. End;