Platform_Win32_Input.cpp
Go to the documentation of this file.00001 //*** Platform_Win32_Input *** 00002 00003 #include "Platform_Win32_Input.h" 00004 #include "Platform_Win32_Input_KeyboardDevice.h" 00005 #include "Platform_Win32_Input_MouseDevice.h" 00006 #include "Platform_Win32_OS.h" 00007 00008 #define WIN32_LEAN_AND_MEAN 00009 #define VC_EXTRALEAN 00010 00011 #define DIRECTINPUT_VERSION 0x0300 00012 #include <dinput.h> 00013 00014 00015 //*** Constructor *** 00016 00017 Platform_Win32_Input::Platform_Win32_Input(Platform_Win32_OS* os): 00018 windowHandle_(os->GetWindowHandle()), 00019 keyboardDevice_(0), 00020 mouseDevice_(0), 00021 cursorCount_(0) 00022 { 00023 Platform::RegisterEventListener(this); 00024 defaultCursor_=LoadCursor(NULL, IDC_ARROW); 00025 } 00026 00027 00028 //*** Destructor *** 00029 00030 Platform_Win32_Input::~Platform_Win32_Input() 00031 { 00032 if (keyboardDevice_) 00033 { 00034 delete keyboardDevice_; 00035 keyboardDevice_=0; 00036 } 00037 00038 if (mouseDevice_) 00039 { 00040 delete mouseDevice_; 00041 mouseDevice_=0; 00042 } 00043 Platform::UnregisterEventListener(this); 00044 00045 for (int i=0; i<cursorCount_; i++) 00046 { 00047 DeleteObject(cursors_[i]); 00048 } 00049 DeleteObject(defaultCursor_); 00050 } 00051 00052 00053 //*** OnCustomEvent *** 00054 00055 void Platform_Win32_Input::OnCustomEvent(const char* eventId,void* userData) 00056 { 00057 if (stricmp(eventId,"OnWmChar")==0) 00058 { 00059 int ascii=*(static_cast<int*>(userData)); 00060 int keycode=*((static_cast<int*>(userData))+1); 00061 if (keyboardDevice_) 00062 { 00063 keyboardDevice_->SetCharPressed((unsigned char)ascii,(unsigned char)keycode); 00064 } 00065 return; 00066 } 00067 00068 if (stricmp(eventId,"OnWmKeyUp")==0) 00069 { 00070 int keycode=*(static_cast<int*>(userData)); 00071 if (keyboardDevice_) 00072 { 00073 keyboardDevice_->SetCharReleased((unsigned char)keycode); 00074 } 00075 return; 00076 } 00077 } 00078 00079 00080 //*** GetKeyboardDevice *** 00081 00082 const Platform_Input_KeyboardDevice* Platform_Win32_Input::GetKeyboardDevice() 00083 { 00084 if (!keyboardDevice_) 00085 { 00086 keyboardDevice_=new Platform_Win32_Input_KeyboardDevice(); 00087 } 00088 00089 return keyboardDevice_; 00090 } 00091 00092 00093 //*** GetMouseDevice *** 00094 00095 const Platform_Input_MouseDevice* Platform_Win32_Input::GetMouseDevice() 00096 { 00097 if (!mouseDevice_) 00098 { 00099 mouseDevice_=new Platform_Win32_Input_MouseDevice(windowHandle_); 00100 } 00101 00102 return mouseDevice_; 00103 } 00104 00105 00106 00107 //*** CreateMouseCursor *** 00108 00109 int Platform_Win32_Input::CreateMouseCursor(int width, int height, int hotspotX, int hotspotY, unsigned short* colorData, unsigned char* alphaData) 00110 { 00111 if (cursorCount_>=MaxCursors) 00112 { 00113 return 0; 00114 } 00115 00116 //Create the AND and XOR masks for the bitmap 00117 00118 HDC hDC = ::GetDC(NULL); 00119 HDC hAndMaskDC = ::CreateCompatibleDC(hDC); 00120 HDC hXorMaskDC = ::CreateCompatibleDC(hDC); 00121 00122 HBITMAP hAndMask = ::CreateCompatibleBitmap(hDC,width,height); 00123 HBITMAP hXorMask = ::CreateCompatibleBitmap(hDC,width,height); 00124 00125 //Select the bitmaps to DC 00126 HBITMAP hOldAndMaskBitmap = (HBITMAP)::SelectObject(hAndMaskDC,hAndMask); 00127 HBITMAP hOldXorMaskBitmap = (HBITMAP)::SelectObject(hXorMaskDC,hXorMask); 00128 00129 //Scan each pixel of the source bitmap and create the masks 00130 for (int y=0; y<height; y++) 00131 { 00132 for (int x=0; x<width; x++) 00133 { 00134 unsigned short c=colorData[x+y*width]; 00135 unsigned char r=(unsigned char)((c & 0xf800)>>8); 00136 unsigned char g=(unsigned char)((c & 0x07e0)>>3); 00137 unsigned char b=(unsigned char)((c & 0x001f)<<3); 00138 unsigned char a=alphaData[x+y*width]; 00139 00140 if (a<128) 00141 { 00142 ::SetPixel(hAndMaskDC,x,y,RGB(255,255,255)); 00143 ::SetPixel(hXorMaskDC,x,y,RGB(0,0,0)); 00144 } 00145 else 00146 { 00147 ::SetPixel(hAndMaskDC,x,y,RGB(0,0,0)); 00148 ::SetPixel(hXorMaskDC,x,y,RGB(r,g,b)); 00149 } 00150 00151 } 00152 } 00153 00154 ::SelectObject(hAndMaskDC,hOldAndMaskBitmap); 00155 ::SelectObject(hXorMaskDC,hOldXorMaskBitmap); 00156 00157 ::DeleteDC(hXorMaskDC); 00158 ::DeleteDC(hAndMaskDC); 00159 00160 ::ReleaseDC(NULL,hDC); 00161 00162 00163 00164 //Create the cursor using the masks and the hotspot values provided 00165 ICONINFO iconinfo = {0}; 00166 iconinfo.fIcon = FALSE; 00167 iconinfo.xHotspot = hotspotX; 00168 iconinfo.yHotspot = hotspotY; 00169 iconinfo.hbmMask = hAndMask; 00170 iconinfo.hbmColor = hXorMask; 00171 00172 cursors_[cursorCount_] = CreateIconIndirect(&iconinfo); 00173 cursorCount_++; 00174 00175 return cursorCount_; 00176 } 00177 00178 00179 //*** SetMouseCursor *** 00180 00181 void Platform_Win32_Input::SetMouseCursor(int handle) 00182 { 00183 if (handle<=0 || handle>cursorCount_) 00184 { 00185 SetCursor(0); 00186 Platform::SendEvent_CustomEvent("SetCursor",0); 00187 } 00188 else 00189 { 00190 SetCursor(cursors_[handle-1]); 00191 Platform::SendEvent_CustomEvent("SetCursor",cursors_[handle-1]); 00192 } 00193 } 00194 00195 00196 //*** SetDefaultMouseCursor *** 00197 00198 void Platform_Win32_Input::SetDefaultMouseCursor() 00199 { 00200 SetCursor(defaultCursor_); 00201 Platform::SendEvent_CustomEvent("SetCursor",defaultCursor_); 00202 } 00203
Reproduction/republishing of any material on this site without permission is strictly prohibited.
