Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

input_mouse.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: input_mouse.cpp,v 1.1 2001/03/06 15:09:22 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #include "Core/precomp.h"
00016 #include <windows.h>
00017 #include "API/Display/Display/display.h"
00018 #include "input_mouse.h"
00019 #include "API/Core/System/cl_assert.h"
00020 #include "Display/Display/DirectDraw/displaycard_win32compatible.h"
00021 
00022 #ifndef WM_MOUSEWHEEL
00023 #define WM_MOUSEWHEEL WM_MOUSELAST+1 
00024 #endif
00025 
00026 /******************************
00027         CL_Mouse_Win32
00028 ******************************/
00029 
00030 #define NUM_MBUTTONS 3
00031 
00032 CL_Mouse_Win32::CL_Mouse_Win32()
00033 {
00034         cursor = new CL_InputCursor_Mouse_Win32();
00035         axes = new CL_InputAxis_Mouse_Win32[2];
00036 
00037         buttons = new CL_InputButton_Mouse_Win32*[NUM_MBUTTONS];
00038         for (int i=0; i<NUM_MBUTTONS; i++) buttons[i] = new CL_InputButton_Mouse_Win32(i);
00039 
00040         CL_System_Win32::add_listener(this);
00041 }
00042 
00043 CL_Mouse_Win32::~CL_Mouse_Win32()
00044 {
00045         CL_System_Win32::remove_listener(this);
00046 
00047         delete cursor;
00048 
00049         for (int i=0; i<NUM_MBUTTONS; i++) delete buttons[i];
00050         delete[] buttons;
00051 }
00052 
00053 char *CL_Mouse_Win32::get_name() const
00054 {
00055         return "Standard mouse";
00056 }
00057 
00058 int CL_Mouse_Win32::get_num_buttons() const
00059 {
00060         return NUM_MBUTTONS;
00061 }
00062 
00063 CL_InputButton *CL_Mouse_Win32::get_button(int button_num)
00064 {
00065         return buttons[button_num];
00066 }
00067 
00068 int CL_Mouse_Win32::get_num_axes() const
00069 {
00070         return 2;
00071 }
00072 
00073 CL_InputAxis *CL_Mouse_Win32::get_axis(int axis_num)
00074 {
00075         cl_assert(((unsigned int) axis_num)<2);
00076         return &axes[axis_num];
00077 }
00078 
00079 int CL_Mouse_Win32::get_num_hats() const
00080 {
00081         return 0;
00082 }
00083 
00084 CL_InputHat *CL_Mouse_Win32::get_hat(int hat_num)
00085 {
00086         return NULL;
00087 }
00088 
00089 int CL_Mouse_Win32::get_num_cursors() const
00090 {
00091         return 1;
00092 }
00093 
00094 CL_InputCursor *CL_Mouse_Win32::get_cursor(int cursor_num)
00095 {
00096         return cursor;
00097 }
00098 
00099 void CL_Mouse_Win32::keep_alive()
00100 {
00101         if (CL_Display::get_current_card() == NULL) return;
00102 
00103         POINT cursor_pos;
00104         GetCursorPos(&cursor_pos);
00105 
00106         CL_DisplayCard_Win32Compatible *win32card =
00107                 (CL_DisplayCard_Win32Compatible *) CL_Display::get_current_card();
00108 
00109         BOOL res = ScreenToClient(win32card->get_hwnd(), &cursor_pos);
00110 
00111         if (cursor->x != cursor_pos.x ||
00112                 cursor->y != cursor_pos.y)
00113         {
00114                 cursor->x = cursor_pos.x;
00115                 cursor->y = cursor_pos.y;
00116 
00117                 CL_Input::sig_mouse_move(this, cursor->x, cursor->y);
00118         }
00119 
00120         axes[0].center = float(CL_Display::get_width())/2.0f;
00121         axes[1].center = float(CL_Display::get_height())/2.0f;
00122         
00123         axes[0].pos = cursor_pos.x;
00124         axes[1].pos = cursor_pos.y;
00125 }
00126 
00127 bool CL_Mouse_Win32::received_event(UINT uMsg, WPARAM wParam, LPARAM lParam)
00128 {
00129         if (CL_Display::get_current_card() == NULL) return false;
00130 
00131         bool up = false;
00132         switch (uMsg)
00133         {
00134         case WM_LBUTTONUP:
00135         case WM_MBUTTONUP:
00136         case WM_RBUTTONUP:
00137                 up = true;
00138                 // fall through
00139         case WM_LBUTTONDOWN:
00140         case WM_MBUTTONDOWN:
00141         case WM_RBUTTONDOWN:
00142                 {
00143                         int button_states = wParam;
00144 
00145                         for (int i=0; i<NUM_MBUTTONS; i++)
00146                         {
00147                                 if (buttons[i] == NULL) continue;
00148 
00149                                 // This code didn't work on Sphairs mouse:
00150                                 // bool new_state = (button_states&(1<<i)) ? true : false;
00151 
00152                                 // Hardcoding the bitposition instead:
00153                                 bool new_state = false;
00154                                 if(i == 0)
00155                                         new_state = (button_states & 1) ? true : false;
00156                                 else if(i == 1)
00157                                         new_state = (button_states & 16) ? true : false;
00158                                 else if(i == 2)
00159                                         new_state = (button_states & 2) ? true : false;
00160 
00161                                 if (new_state != buttons[i]->button_state)
00162                                 {
00163                                         buttons[i]->button_state = new_state;
00164 
00165                                         CL_Key key;
00166                                         key.ascii = -1;
00167                                         key.id = CL_MOUSE_LEFTBUTTON + i;
00168                                         key.x = cursor->x;
00169                                         key.y = cursor->y;
00170 
00171                                         if (up == false)
00172                                         {
00173                                                 key.state = CL_Key::Pressed;
00174                                                 CL_Input::sig_button_press(this, key);
00175                                         }
00176                                         else
00177                                         {
00178                                                 key.state = CL_Key::Released;
00179                                                 CL_Input::sig_button_release(this, key);
00180                                         }
00181                                 }
00182                         }
00183                 }
00184                 break;
00185 
00186         case WM_MOUSEMOVE:
00187                 {
00188                         int x = LOWORD(lParam);
00189                         int y = HIWORD(lParam);
00190 
00191                         if (cursor->x != x ||
00192                                 cursor->y != y)
00193                         {
00194                                 cursor->x = x;
00195                                 cursor->y = y;
00196 
00197                                 CL_Input::sig_mouse_move(this, cursor->x, cursor->y);
00198                         }
00199 
00200                         axes[0].center = float(CL_Display::get_width())/2.0f;
00201                         axes[1].center = float(CL_Display::get_height())/2.0f;
00202                         
00203                         axes[0].pos = x;
00204                         axes[1].pos = y;
00205                 }
00206                 break;
00207 
00208         case WM_MOUSEWHEEL:
00209                 {
00210                         int keyflags = LOWORD(wParam);                  /* key flags */ 
00211                         short delta = (short) HIWORD(wParam);   /* wheel rotation */
00212                         short x_pos = (short) LOWORD(lParam);   /* horizontal position of pointer */
00213                         short y_pos = (short) HIWORD(lParam);   /* vertical position of pointer */ 
00214 
00215                         // TODO: Add a Modifier field to CL_Key and put keyflags into it
00216 
00217                         CL_Key key;
00218                         key.ascii = -1;
00219                         if(delta > 0)
00220                                 key.id = CL_MOUSE_WHEELUP;
00221                         else
00222                                 key.id = CL_MOUSE_WHEELDOWN;
00223                         key.x = x_pos;
00224                         key.y = y_pos;
00225 
00226                         key.state = CL_Key::Pressed;
00227                         CL_Input::sig_button_press(this, key);
00228 
00229                         key.state = CL_Key::Released;
00230                         CL_Input::sig_button_release(this, key);
00231                 }
00232                 break;
00233 
00234         default:
00235                 return false;
00236         }
00237 
00238         return true;
00239 }
00240 
00241 /*******************************
00242          CL_InputButton_Mouse_Win32
00243 *******************************/
00244 
00245 CL_InputButton_Mouse_Win32::CL_InputButton_Mouse_Win32(int _key)
00246 {
00247         key = (1<<_key);
00248         button_state = false;
00249 }
00250 
00251 CL_InputButton_Mouse_Win32::~CL_InputButton_Mouse_Win32()
00252 {
00253 }
00254 
00255 bool CL_InputButton_Mouse_Win32::is_pressed()
00256 {
00257         return button_state;
00258 }
00259 
00260 /*******************************
00261          CL_InputCursor_Mouse_Win32
00262 *******************************/
00263 
00264 CL_InputCursor_Mouse_Win32::CL_InputCursor_Mouse_Win32()
00265 {
00266         x = 0;
00267         y = 0;
00268 }
00269 
00270 CL_InputCursor_Mouse_Win32::~CL_InputCursor_Mouse_Win32()
00271 {
00272 }
00273 
00274 float CL_InputCursor_Mouse_Win32::get_x()
00275 {
00276         POINT cursor_pos;
00277         GetCursorPos(&cursor_pos);
00278 
00279         CL_DisplayCard_Win32Compatible *win32card =
00280                 (CL_DisplayCard_Win32Compatible *) CL_Display::get_current_card();
00281 
00282         ScreenToClient(win32card->get_hwnd(), &cursor_pos);
00283 
00284         return cursor_pos.x;
00285 }
00286 
00287 float CL_InputCursor_Mouse_Win32::get_y()
00288 {
00289         POINT cursor_pos;
00290         GetCursorPos(&cursor_pos);
00291 
00292         CL_DisplayCard_Win32Compatible *win32card =
00293                 (CL_DisplayCard_Win32Compatible *) CL_Display::get_current_card();
00294 
00295         ScreenToClient(win32card->get_hwnd(), &cursor_pos);
00296 
00297         return cursor_pos.y;
00298 }
00299 
00300 float CL_InputCursor_Mouse_Win32::get_max_x()
00301 {
00302         if (CL_Display::get_current_card() == NULL) return -1;
00303         return CL_Display::get_width();
00304 }
00305 
00306 float CL_InputCursor_Mouse_Win32::get_max_y()
00307 {
00308         if (CL_Display::get_current_card() == NULL) return -1;
00309         return CL_Display::get_height();
00310 }
00311 
00312 /*******************************
00313          CL_InputAxis_Mouse_Win32
00314 *******************************/
00315 
00316 CL_InputAxis_Mouse_Win32::CL_InputAxis_Mouse_Win32()
00317 {
00318         pos = 0;
00319         center = 1;
00320 }
00321 
00322 CL_InputAxis_Mouse_Win32::~CL_InputAxis_Mouse_Win32()
00323 {
00324 }
00325 
00326 float CL_InputAxis_Mouse_Win32::get_pos()
00327 {
00328         return (pos - center) / center;
00329 }

Generated at Wed Apr 4 19:54:00 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001