option to build the BGE without python, uses existing python check (cmake and scons)
[blender.git] / source / gameengine / GameLogic / SCA_MouseSensor.cpp
1 /**
2  * Sensor for mouse input
3  *
4  *
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): José I. Romero (cleanup and fixes)
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include "SCA_MouseSensor.h"
34 #include "SCA_EventManager.h"
35 #include "SCA_MouseManager.h"
36 #include "SCA_LogicManager.h"
37 #include "SCA_IInputDevice.h"
38 #include "ConstExpr.h"
39 #include <iostream>
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 /* ------------------------------------------------------------------------- */
46 /* Native functions                                                          */
47 /* ------------------------------------------------------------------------- */
48
49 SCA_MouseSensor::SCA_MouseSensor(SCA_MouseManager* eventmgr, 
50                                                                  int startx,int starty,
51                                                                  short int mousemode,
52                                                                  SCA_IObject* gameobj)
53     : SCA_ISensor(gameobj,eventmgr),
54         m_x(startx),
55         m_y(starty)
56 {
57         m_mousemode   = mousemode;
58         m_triggermode = true;
59
60         UpdateHotkey(this);
61         Init();
62 }
63
64 void SCA_MouseSensor::Init()
65 {
66         m_val = (m_invert)?1:0; /* stores the latest attribute */
67         m_reset = true;
68 }
69
70 SCA_MouseSensor::~SCA_MouseSensor() 
71 {
72     /* Nothing to be done here. */
73 }
74
75 void SCA_MouseSensor::UpdateHotkey(void *self)
76 {
77         // gosh, this function is so damn stupid
78         // its here because of a design mistake in the mouse sensor, it should only
79         // have 3 trigger modes (button, wheel, move), and let the user set the 
80         // hotkey separately, like the other sensors. but instead it has a mode for 
81         // each friggin key and i have to update the hotkey based on it... genius!
82         SCA_MouseSensor* sensor = reinterpret_cast<SCA_MouseSensor*>(self);
83
84         switch (sensor->m_mousemode) {
85         case KX_MOUSESENSORMODE_LEFTBUTTON:
86                 sensor->m_hotkey = SCA_IInputDevice::KX_LEFTMOUSE;
87                 break;
88         case KX_MOUSESENSORMODE_MIDDLEBUTTON:
89                 sensor->m_hotkey = SCA_IInputDevice::KX_MIDDLEMOUSE;
90                 break;
91         case KX_MOUSESENSORMODE_RIGHTBUTTON:
92                 sensor->m_hotkey = SCA_IInputDevice::KX_RIGHTMOUSE;
93                 break;
94         case KX_MOUSESENSORMODE_WHEELUP:
95                 sensor->m_hotkey = SCA_IInputDevice::KX_WHEELUPMOUSE;
96                 break;
97         case KX_MOUSESENSORMODE_WHEELDOWN:
98                 sensor->m_hotkey = SCA_IInputDevice::KX_WHEELDOWNMOUSE;
99                 break;
100         default:
101                 ; /* ignore, no hotkey */
102         }
103 }
104
105 CValue* SCA_MouseSensor::GetReplica()
106 {
107         SCA_MouseSensor* replica = new SCA_MouseSensor(*this);
108         // this will copy properties and so on...
109         replica->ProcessReplica();
110         replica->Init();
111
112         return replica;
113 }
114
115
116
117 bool SCA_MouseSensor::IsPositiveTrigger()
118 {
119         bool result = (m_val != 0);
120         if (m_invert)
121                 result = !result;
122                 
123         return result;
124 }
125
126
127
128 short int SCA_MouseSensor::GetModeKey()
129
130         return m_mousemode;
131 }
132
133
134
135 SCA_IInputDevice::KX_EnumInputs SCA_MouseSensor::GetHotKey()
136
137         return m_hotkey;
138 }
139
140
141
142 bool SCA_MouseSensor::Evaluate()
143 {
144         bool result = false;
145         bool reset = m_reset && m_level;
146         SCA_IInputDevice* mousedev = ((SCA_MouseManager *)m_eventmgr)->GetInputDevice();
147
148         m_reset = false;
149         switch (m_mousemode) {
150         case KX_MOUSESENSORMODE_LEFTBUTTON:
151         case KX_MOUSESENSORMODE_MIDDLEBUTTON:
152         case KX_MOUSESENSORMODE_RIGHTBUTTON:
153         case KX_MOUSESENSORMODE_WHEELUP:
154         case KX_MOUSESENSORMODE_WHEELDOWN:
155                 {
156                         const SCA_InputEvent& mevent = mousedev->GetEventValue(m_hotkey);
157                         switch (mevent.m_status){       
158                         case SCA_InputEvent::KX_JUSTACTIVATED:
159                                 m_val = 1;
160                                 result = true;
161                                 break;
162                         case SCA_InputEvent::KX_JUSTRELEASED:
163                                 m_val = 0;
164                                 result = true;
165                                 break;
166                         case SCA_InputEvent::KX_ACTIVE:
167                                 if (m_val == 0)
168                                 {
169                                         m_val = 1;
170                                         if (m_level)
171                                                 result = true;
172                                 }
173                                 break;
174                         default:
175                                 if (m_val == 1)
176                                 {
177                                         m_val = 0;
178                                         result = true;
179                                 }
180                                 break;
181                         }
182                         break;
183                 }
184         case KX_MOUSESENSORMODE_MOVEMENT:
185                 {
186                         const SCA_InputEvent& eventX = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
187                         const SCA_InputEvent& eventY = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
188
189                         if (eventX.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
190                                 eventY.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
191                                 eventX.m_status == SCA_InputEvent::KX_ACTIVE ||
192                                 eventY.m_status == SCA_InputEvent::KX_ACTIVE)   
193                         {
194                                 m_val = 1;
195                                 result = true;
196                         } 
197                         else if (eventX.m_status == SCA_InputEvent::KX_JUSTRELEASED ||
198                                         eventY.m_status == SCA_InputEvent::KX_JUSTRELEASED )
199                         {
200                                 m_val = 0;
201                                 result = true;
202                         } 
203                         else //KX_NO_IMPUTSTATUS
204                         { 
205                                 if (m_val == 1)
206                                 {
207                                         m_val = 0;
208                                         result = true;
209                                 }
210                         }
211                         
212                         break;
213                 }
214         default:
215                 ; /* error */
216         }
217
218         if (reset)
219                 // force an event
220                 result = true;
221         return result;
222 }
223
224 void SCA_MouseSensor::setX(short x)
225 {
226         m_x = x;
227 }
228
229 void SCA_MouseSensor::setY(short y)
230 {
231         m_y = y;
232 }
233
234 bool SCA_MouseSensor::isValid(SCA_MouseSensor::KX_MOUSESENSORMODE m)
235 {
236         return ((m > KX_MOUSESENSORMODE_NODEF) && (m < KX_MOUSESENSORMODE_MAX));
237 }
238
239 #ifndef DISABLE_PYTHON
240
241 /* ------------------------------------------------------------------------- */
242 /* Python functions                                                          */
243 /* ------------------------------------------------------------------------- */
244
245 KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus,
246 "getButtonStatus(button)\n"
247 "\tGet the given button's status (KX_INPUT_NONE, KX_INPUT_NONE, KX_INPUT_JUST_ACTIVATED, KX_INPUT_ACTIVE, KX_INPUT_JUST_RELEASED).\n")
248 {
249         if (PyLong_Check(value))
250         {
251                 int button = PyLong_AsSsize_t(value);
252                 
253                 if ((button < SCA_IInputDevice::KX_LEFTMOUSE)
254                         || (button > SCA_IInputDevice::KX_RIGHTMOUSE)){
255                         PyErr_SetString(PyExc_ValueError, "sensor.getButtonStatus(int): Mouse Sensor, invalid button specified!");
256                         return NULL;
257                 }
258                 
259                 SCA_IInputDevice* mousedev = ((SCA_MouseManager *)m_eventmgr)->GetInputDevice();
260                 const SCA_InputEvent& event = mousedev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) button);
261                 return PyLong_FromSsize_t(event.m_status);
262         }
263         
264         Py_RETURN_NONE;
265 }
266
267 /* ------------------------------------------------------------------------- */
268 /* Python Integration Hooks                                                  */
269 /* ------------------------------------------------------------------------- */
270
271 PyTypeObject SCA_MouseSensor::Type = {
272         PyVarObject_HEAD_INIT(NULL, 0)
273         "SCA_MouseSensor",
274         sizeof(PyObjectPlus_Proxy),
275         0,
276         py_base_dealloc,
277         0,
278         0,
279         0,
280         0,
281         py_base_repr,
282         0,0,0,0,0,0,0,0,0,
283         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
284         0,0,0,0,0,0,0,
285         Methods,
286         0,
287         0,
288         &SCA_ISensor::Type,
289         0,0,0,0,0,0,
290         py_base_new
291 };
292
293 PyMethodDef SCA_MouseSensor::Methods[] = {
294         KX_PYMETHODTABLE_O(SCA_MouseSensor, getButtonStatus),
295         {NULL,NULL} //Sentinel
296 };
297
298 int SCA_MouseSensor::UpdateHotkeyPy(void *self, const PyAttributeDef*)
299 {
300         UpdateHotkey(self);
301         // return value is used in py_setattro(),
302         // 0=attribute checked ok (see Attributes array definition)
303         return 0;
304 }
305
306 PyAttributeDef SCA_MouseSensor::Attributes[] = {
307         KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",KX_MOUSESENSORMODE_NODEF,KX_MOUSESENSORMODE_MAX-1,true,SCA_MouseSensor,m_mousemode,UpdateHotkeyPy),
308         KX_PYATTRIBUTE_SHORT_LIST_RO("position",SCA_MouseSensor,m_x,2),
309         { NULL }        //Sentinel
310 };
311
312 #endif // DISABLE_PYTHON
313
314 /* eof */