First batch of GE API cleanup.
[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                                                                  PyTypeObject* T)
54     : SCA_ISensor(gameobj,eventmgr, T),
55         m_pMouseMgr(eventmgr),
56         m_x(startx),
57         m_y(starty)
58 {
59         m_mousemode   = mousemode;
60         m_triggermode = true;
61
62         UpdateHotkey();
63         Init();
64 }
65
66 void SCA_MouseSensor::Init()
67 {
68         m_val = (m_invert)?1:0; /* stores the latest attribute */
69         m_reset = true;
70 }
71
72 SCA_MouseSensor::~SCA_MouseSensor() 
73 {
74     /* Nothing to be done here. */
75 }
76
77 void SCA_MouseSensor::UpdateHotkey()
78 {
79         // gosh, this function is so damn stupid
80         // its here because of a design mistake in the mouse sensor, it should only
81         // have 3 trigger modes (button, wheel, move), and let the user set the 
82         // hotkey separately, like the other sensors. but instead it has a mode for 
83         // each friggin key and i have to update the hotkey based on it... genius!
84         
85         switch (m_mousemode) {
86         case KX_MOUSESENSORMODE_LEFTBUTTON:
87                 m_hotkey = SCA_IInputDevice::KX_LEFTMOUSE;
88                 break;
89         case KX_MOUSESENSORMODE_MIDDLEBUTTON:
90                 m_hotkey = SCA_IInputDevice::KX_MIDDLEMOUSE;
91                 break;
92         case KX_MOUSESENSORMODE_RIGHTBUTTON:
93                 m_hotkey = SCA_IInputDevice::KX_RIGHTMOUSE;
94                 break;
95         case KX_MOUSESENSORMODE_WHEELUP:
96                 m_hotkey = SCA_IInputDevice::KX_WHEELUPMOUSE;
97                 break;
98         case KX_MOUSESENSORMODE_WHEELDOWN:
99                 m_hotkey = SCA_IInputDevice::KX_WHEELDOWNMOUSE;
100                 break;
101         default:
102                 ; /* ignore, no hotkey */
103         }
104 }
105
106 CValue* SCA_MouseSensor::GetReplica()
107 {
108         SCA_MouseSensor* replica = new SCA_MouseSensor(*this);
109         // this will copy properties and so on...
110         CValue::AddDataToReplica(replica);
111         replica->Init();
112
113         return replica;
114 }
115
116
117
118 bool SCA_MouseSensor::IsPositiveTrigger()
119 {
120         bool result = (m_val != 0);
121         if (m_invert)
122                 result = !result;
123                 
124         return result;
125 }
126
127
128
129 short int SCA_MouseSensor::GetModeKey()
130
131         return m_mousemode;
132 }
133
134
135
136 SCA_IInputDevice::KX_EnumInputs SCA_MouseSensor::GetHotKey()
137
138         return m_hotkey;
139 }
140
141
142
143 bool SCA_MouseSensor::Evaluate(CValue* event)
144 {
145         bool result = false;
146         bool reset = m_reset && m_level;
147         SCA_IInputDevice* mousedev = m_pMouseMgr->GetInputDevice();
148
149         m_reset = false;
150         switch (m_mousemode) {
151         case KX_MOUSESENSORMODE_LEFTBUTTON:
152         case KX_MOUSESENSORMODE_MIDDLEBUTTON:
153         case KX_MOUSESENSORMODE_RIGHTBUTTON:
154         case KX_MOUSESENSORMODE_WHEELUP:
155         case KX_MOUSESENSORMODE_WHEELDOWN:
156                 {
157                         const SCA_InputEvent& event = mousedev->GetEventValue(m_hotkey);
158                         switch (event.m_status){        
159                         case SCA_InputEvent::KX_JUSTACTIVATED:
160                                 m_val = 1;
161                                 result = true;
162                                 break;
163                         case SCA_InputEvent::KX_JUSTRELEASED:
164                                 m_val = 0;
165                                 result = true;
166                                 break;
167                         case SCA_InputEvent::KX_ACTIVE:
168                                 if (m_val == 0)
169                                 {
170                                         m_val = 1;
171                                         if (m_level)
172                                                 result = true;
173                                 }
174                                 break;
175                         default:
176                                 if (m_val == 1)
177                                 {
178                                         m_val = 0;
179                                         result = true;
180                                 }
181                                 break;
182                         }
183                         break;
184                 }
185         case KX_MOUSESENSORMODE_MOVEMENT:
186                 {
187                         const SCA_InputEvent& eventX = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
188                         const SCA_InputEvent& eventY = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
189
190                         if (eventX.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
191                                 eventY.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
192                                 eventX.m_status == SCA_InputEvent::KX_ACTIVE ||
193                                 eventY.m_status == SCA_InputEvent::KX_ACTIVE)   
194                         {
195                                 m_val = 1;
196                                 result = true;
197                         } 
198                         else if (eventX.m_status == SCA_InputEvent::KX_JUSTRELEASED ||
199                                         eventY.m_status == SCA_InputEvent::KX_JUSTRELEASED )
200                         {
201                                 m_val = 0;
202                                 result = true;
203                         } 
204                         else //KX_NO_IMPUTSTATUS
205                         { 
206                                 if (m_val == 1)
207                                 {
208                                         m_val = 0;
209                                         result = true;
210                                 }
211                         }
212                         
213                         break;
214                 }
215         default:
216                 ; /* error */
217         }
218
219         if (reset)
220                 // force an event
221                 result = true;
222         return result;
223 }
224
225 void SCA_MouseSensor::setX(short x)
226 {
227         m_x = x;
228 }
229
230 void SCA_MouseSensor::setY(short y)
231 {
232         m_y = y;
233 }
234
235 bool SCA_MouseSensor::isValid(SCA_MouseSensor::KX_MOUSESENSORMODE m)
236 {
237         return ((m > KX_MOUSESENSORMODE_NODEF) && (m < KX_MOUSESENSORMODE_MAX));
238 }
239
240 /* ------------------------------------------------------------------------- */
241 /* Python functions                                                          */
242 /* ------------------------------------------------------------------------- */
243
244 //Deprecated functions ------>
245 /* get x position ---------------------------------------------------------- */
246 const char SCA_MouseSensor::GetXPosition_doc[] = 
247 "getXPosition\n"
248 "\tReturns the x-coordinate of the mouse sensor, in frame coordinates.\n"
249 "\tThe lower-left corner is the origin. The coordinate is given in\n"
250 "\tpixels\n";
251 PyObject* SCA_MouseSensor::PyGetXPosition(PyObject* self, 
252                                                                                  PyObject* args, 
253                                                                                  PyObject* kwds) {
254         ShowDeprecationWarning("getXPosition()", "the position property");
255         return PyInt_FromLong(m_x);
256 }
257
258 /* get y position ---------------------------------------------------------- */
259 const char SCA_MouseSensor::GetYPosition_doc[] = 
260 "getYPosition\n"
261 "\tReturns the y-coordinate of the mouse sensor, in frame coordinates.\n"
262 "\tThe lower-left corner is the origin. The coordinate is given in\n"
263 "\tpixels\n";
264 PyObject* SCA_MouseSensor::PyGetYPosition(PyObject* self, 
265                                                                                  PyObject* args, 
266                                                                                  PyObject* kwds) {
267         ShowDeprecationWarning("getYPosition()", "the position property");
268         return PyInt_FromLong(m_y);
269 }
270 //<----- Deprecated
271
272 KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus,
273 "getButtonStatus(button)\n"
274 "\tGet the given button's status (KX_NO_INPUTSTATUS, KX_JUSTACTIVATED, KX_ACTIVE or KX_JUSTRELEASED).\n")
275 {
276         if (PyInt_Check(value))
277         {
278                 int button = PyInt_AsLong(value);
279                 
280                 if ((button < SCA_IInputDevice::KX_LEFTMOUSE)
281                         || (button > SCA_IInputDevice::KX_MIDDLEMOUSE)){
282                         PyErr_SetString(PyExc_ValueError, "invalid button specified!");
283                         return NULL;
284                 }
285                 
286                 SCA_IInputDevice* mousedev = m_pMouseMgr->GetInputDevice();
287                 const SCA_InputEvent& event = mousedev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) button);
288                 return PyInt_FromLong(event.m_status);
289         }
290         
291         Py_Return;
292 }
293
294 /* ------------------------------------------------------------------------- */
295 /* Python Integration Hooks                                                  */
296 /* ------------------------------------------------------------------------- */
297
298 PyTypeObject SCA_MouseSensor::Type = {
299         PyObject_HEAD_INIT(&PyType_Type)
300         0,
301         "SCA_MouseSensor",
302         sizeof(SCA_MouseSensor),
303         0,
304         PyDestructor,
305         0,
306         __getattr,
307         __setattr,
308         0, //&MyPyCompare,
309         __repr,
310         0, //&cvalue_as_number,
311         0,
312         0,
313         0,
314         0
315 };
316
317 PyParentObject SCA_MouseSensor::Parents[] = {
318         &SCA_MouseSensor::Type,
319         &SCA_ISensor::Type,
320         &SCA_ILogicBrick::Type,
321         &CValue::Type,
322         NULL
323 };
324
325 PyMethodDef SCA_MouseSensor::Methods[] = {
326         //Deprecated functions ------>
327         {"getXPosition", (PyCFunction) SCA_MouseSensor::sPyGetXPosition, METH_VARARGS, (PY_METHODCHAR)GetXPosition_doc},
328         {"getYPosition", (PyCFunction) SCA_MouseSensor::sPyGetYPosition, METH_VARARGS, (PY_METHODCHAR)GetYPosition_doc},
329         //<----- Deprecated
330         KX_PYMETHODTABLE_O(SCA_MouseSensor, getButtonStatus),
331         {NULL,NULL} //Sentinel
332 };
333
334 PyObject* SCA_MouseSensor::_getattr(const STR_String& attr) {
335         if (attr == "position")
336                 return Py_BuildValue("[ii]", m_x, m_y);
337         
338         if (attr == "mode")
339                 return PyInt_FromLong(m_mousemode);
340         
341         _getattr_up(SCA_ISensor);
342 }
343
344 int SCA_MouseSensor::_setattr(const STR_String& attr, PyObject *value)
345 {
346         if (PyInt_Check(value))
347         {
348                 int val = PyInt_AsLong(value);
349
350                 if (attr == "mode")
351                 {
352                         if ((val < KX_MOUSESENSORMODE_NODEF) 
353                                 || (val > KX_MOUSESENSORMODE_MAX)){     
354                                 
355                                 PyErr_SetString(PyExc_ValueError, "invalid mode specified!");
356                                 return NULL;
357                         }
358                         
359                         m_mousemode = val;
360                         UpdateHotkey();
361                         return 0;
362                 }
363         }
364         else 
365         {
366                 if (attr == "position")
367                 {
368                         PyErr_SetString(PyExc_AttributeError, "read-only property!");
369                         return NULL;            
370                 }
371         }
372         
373         return SCA_ISensor::_setattr(attr, value);
374 }
375
376 /* eof */