|
Blender
V2.59
|
00001 /* 00002 * Sensor for mouse input 00003 * 00004 * 00005 * $Id: SCA_MouseSensor.cpp 35169 2011-02-25 13:32:11Z jesterking $ 00006 * 00007 * ***** BEGIN GPL LICENSE BLOCK ***** 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License 00011 * as published by the Free Software Foundation; either version 2 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software Foundation, 00021 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00022 * 00023 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00024 * All rights reserved. 00025 * 00026 * The Original Code is: all of this file. 00027 * 00028 * Contributor(s): José I. Romero (cleanup and fixes) 00029 * 00030 * ***** END GPL LICENSE BLOCK ***** 00031 */ 00032 00038 #include <stddef.h> 00039 00040 #include "SCA_MouseSensor.h" 00041 #include "SCA_EventManager.h" 00042 #include "SCA_MouseManager.h" 00043 #include "SCA_LogicManager.h" 00044 #include "SCA_IInputDevice.h" 00045 #include "ConstExpr.h" 00046 #include <iostream> 00047 00048 /* ------------------------------------------------------------------------- */ 00049 /* Native functions */ 00050 /* ------------------------------------------------------------------------- */ 00051 00052 SCA_MouseSensor::SCA_MouseSensor(SCA_MouseManager* eventmgr, 00053 int startx,int starty, 00054 short int mousemode, 00055 SCA_IObject* gameobj) 00056 : SCA_ISensor(gameobj,eventmgr), 00057 m_x(startx), 00058 m_y(starty) 00059 { 00060 m_mousemode = mousemode; 00061 m_triggermode = true; 00062 00063 UpdateHotkey(this); 00064 Init(); 00065 } 00066 00067 void SCA_MouseSensor::Init() 00068 { 00069 m_val = (m_invert)?1:0; /* stores the latest attribute */ 00070 m_reset = true; 00071 } 00072 00073 SCA_MouseSensor::~SCA_MouseSensor() 00074 { 00075 /* Nothing to be done here. */ 00076 } 00077 00078 void SCA_MouseSensor::UpdateHotkey(void *self) 00079 { 00080 // gosh, this function is so damn stupid 00081 // its here because of a design mistake in the mouse sensor, it should only 00082 // have 3 trigger modes (button, wheel, move), and let the user set the 00083 // hotkey separately, like the other sensors. but instead it has a mode for 00084 // each friggin key and i have to update the hotkey based on it... genius! 00085 SCA_MouseSensor* sensor = reinterpret_cast<SCA_MouseSensor*>(self); 00086 00087 switch (sensor->m_mousemode) { 00088 case KX_MOUSESENSORMODE_LEFTBUTTON: 00089 sensor->m_hotkey = SCA_IInputDevice::KX_LEFTMOUSE; 00090 break; 00091 case KX_MOUSESENSORMODE_MIDDLEBUTTON: 00092 sensor->m_hotkey = SCA_IInputDevice::KX_MIDDLEMOUSE; 00093 break; 00094 case KX_MOUSESENSORMODE_RIGHTBUTTON: 00095 sensor->m_hotkey = SCA_IInputDevice::KX_RIGHTMOUSE; 00096 break; 00097 case KX_MOUSESENSORMODE_WHEELUP: 00098 sensor->m_hotkey = SCA_IInputDevice::KX_WHEELUPMOUSE; 00099 break; 00100 case KX_MOUSESENSORMODE_WHEELDOWN: 00101 sensor->m_hotkey = SCA_IInputDevice::KX_WHEELDOWNMOUSE; 00102 break; 00103 default: 00104 ; /* ignore, no hotkey */ 00105 } 00106 } 00107 00108 CValue* SCA_MouseSensor::GetReplica() 00109 { 00110 SCA_MouseSensor* replica = new SCA_MouseSensor(*this); 00111 // this will copy properties and so on... 00112 replica->ProcessReplica(); 00113 replica->Init(); 00114 00115 return replica; 00116 } 00117 00118 00119 00120 bool SCA_MouseSensor::IsPositiveTrigger() 00121 { 00122 bool result = (m_val != 0); 00123 if (m_invert) 00124 result = !result; 00125 00126 return result; 00127 } 00128 00129 00130 00131 short int SCA_MouseSensor::GetModeKey() 00132 { 00133 return m_mousemode; 00134 } 00135 00136 00137 00138 SCA_IInputDevice::KX_EnumInputs SCA_MouseSensor::GetHotKey() 00139 { 00140 return m_hotkey; 00141 } 00142 00143 00144 00145 bool SCA_MouseSensor::Evaluate() 00146 { 00147 bool result = false; 00148 bool reset = m_reset && m_level; 00149 SCA_IInputDevice* mousedev = ((SCA_MouseManager *)m_eventmgr)->GetInputDevice(); 00150 00151 m_reset = false; 00152 switch (m_mousemode) { 00153 case KX_MOUSESENSORMODE_LEFTBUTTON: 00154 case KX_MOUSESENSORMODE_MIDDLEBUTTON: 00155 case KX_MOUSESENSORMODE_RIGHTBUTTON: 00156 case KX_MOUSESENSORMODE_WHEELUP: 00157 case KX_MOUSESENSORMODE_WHEELDOWN: 00158 { 00159 const SCA_InputEvent& mevent = mousedev->GetEventValue(m_hotkey); 00160 switch (mevent.m_status){ 00161 case SCA_InputEvent::KX_JUSTACTIVATED: 00162 m_val = 1; 00163 result = true; 00164 break; 00165 case SCA_InputEvent::KX_JUSTRELEASED: 00166 m_val = 0; 00167 result = true; 00168 break; 00169 case SCA_InputEvent::KX_ACTIVE: 00170 if (m_val == 0) 00171 { 00172 m_val = 1; 00173 if (m_level) 00174 result = true; 00175 } 00176 break; 00177 default: 00178 if (m_val == 1) 00179 { 00180 m_val = 0; 00181 result = true; 00182 } 00183 break; 00184 } 00185 break; 00186 } 00187 case KX_MOUSESENSORMODE_MOVEMENT: 00188 { 00189 const SCA_InputEvent& eventX = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEX); 00190 const SCA_InputEvent& eventY = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEY); 00191 00192 if (eventX.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 00193 eventY.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 00194 eventX.m_status == SCA_InputEvent::KX_ACTIVE || 00195 eventY.m_status == SCA_InputEvent::KX_ACTIVE) 00196 { 00197 m_val = 1; 00198 result = true; 00199 } 00200 else if (eventX.m_status == SCA_InputEvent::KX_JUSTRELEASED || 00201 eventY.m_status == SCA_InputEvent::KX_JUSTRELEASED ) 00202 { 00203 m_val = 0; 00204 result = true; 00205 } 00206 else //KX_NO_IMPUTSTATUS 00207 { 00208 if (m_val == 1) 00209 { 00210 m_val = 0; 00211 result = true; 00212 } 00213 } 00214 00215 break; 00216 } 00217 default: 00218 ; /* error */ 00219 } 00220 00221 if (reset) 00222 // force an event 00223 result = true; 00224 return result; 00225 } 00226 00227 void SCA_MouseSensor::setX(short x) 00228 { 00229 m_x = x; 00230 } 00231 00232 void SCA_MouseSensor::setY(short y) 00233 { 00234 m_y = y; 00235 } 00236 00237 bool SCA_MouseSensor::isValid(SCA_MouseSensor::KX_MOUSESENSORMODE m) 00238 { 00239 return ((m > KX_MOUSESENSORMODE_NODEF) && (m < KX_MOUSESENSORMODE_MAX)); 00240 } 00241 00242 #ifdef WITH_PYTHON 00243 00244 /* ------------------------------------------------------------------------- */ 00245 /* Python functions */ 00246 /* ------------------------------------------------------------------------- */ 00247 00248 KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus, 00249 "getButtonStatus(button)\n" 00250 "\tGet the given button's status (KX_INPUT_NONE, KX_INPUT_NONE, KX_INPUT_JUST_ACTIVATED, KX_INPUT_ACTIVE, KX_INPUT_JUST_RELEASED).\n") 00251 { 00252 if (PyLong_Check(value)) 00253 { 00254 int button = PyLong_AsSsize_t(value); 00255 00256 if ((button < SCA_IInputDevice::KX_LEFTMOUSE) 00257 || (button > SCA_IInputDevice::KX_RIGHTMOUSE)){ 00258 PyErr_SetString(PyExc_ValueError, "sensor.getButtonStatus(int): Mouse Sensor, invalid button specified!"); 00259 return NULL; 00260 } 00261 00262 SCA_IInputDevice* mousedev = ((SCA_MouseManager *)m_eventmgr)->GetInputDevice(); 00263 const SCA_InputEvent& event = mousedev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) button); 00264 return PyLong_FromSsize_t(event.m_status); 00265 } 00266 00267 Py_RETURN_NONE; 00268 } 00269 00270 /* ------------------------------------------------------------------------- */ 00271 /* Python Integration Hooks */ 00272 /* ------------------------------------------------------------------------- */ 00273 00274 PyTypeObject SCA_MouseSensor::Type = { 00275 PyVarObject_HEAD_INIT(NULL, 0) 00276 "SCA_MouseSensor", 00277 sizeof(PyObjectPlus_Proxy), 00278 0, 00279 py_base_dealloc, 00280 0, 00281 0, 00282 0, 00283 0, 00284 py_base_repr, 00285 0,0,0,0,0,0,0,0,0, 00286 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, 00287 0,0,0,0,0,0,0, 00288 Methods, 00289 0, 00290 0, 00291 &SCA_ISensor::Type, 00292 0,0,0,0,0,0, 00293 py_base_new 00294 }; 00295 00296 PyMethodDef SCA_MouseSensor::Methods[] = { 00297 KX_PYMETHODTABLE_O(SCA_MouseSensor, getButtonStatus), 00298 {NULL,NULL} //Sentinel 00299 }; 00300 00301 int SCA_MouseSensor::UpdateHotkeyPy(void *self, const PyAttributeDef*) 00302 { 00303 UpdateHotkey(self); 00304 // return value is used in py_setattro(), 00305 // 0=attribute checked ok (see Attributes array definition) 00306 return 0; 00307 } 00308 00309 PyAttributeDef SCA_MouseSensor::Attributes[] = { 00310 KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",KX_MOUSESENSORMODE_NODEF,KX_MOUSESENSORMODE_MAX-1,true,SCA_MouseSensor,m_mousemode,UpdateHotkeyPy), 00311 KX_PYATTRIBUTE_SHORT_LIST_RO("position",SCA_MouseSensor,m_x,2), 00312 { NULL } //Sentinel 00313 }; 00314 00315 #endif // WITH_PYTHON 00316 00317 /* eof */