Merged 15170:15635 from trunk (no conflicts or even merges)
[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): none yet.
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         switch (m_mousemode) {
63         case KX_MOUSESENSORMODE_LEFTBUTTON:
64                 m_hotkey = SCA_IInputDevice::KX_LEFTMOUSE;
65                 break;
66         case KX_MOUSESENSORMODE_MIDDLEBUTTON:
67                 m_hotkey = SCA_IInputDevice::KX_MIDDLEMOUSE;
68                 break;
69         case KX_MOUSESENSORMODE_RIGHTBUTTON:
70                 m_hotkey = SCA_IInputDevice::KX_RIGHTMOUSE;
71                 break;
72         case KX_MOUSESENSORMODE_WHEELUP:
73                 m_hotkey = SCA_IInputDevice::KX_WHEELUPMOUSE;
74                 break;
75         case KX_MOUSESENSORMODE_WHEELDOWN:
76                 m_hotkey = SCA_IInputDevice::KX_WHEELDOWNMOUSE;
77                 break;
78         default:
79                 ; /* ignore, no hotkey */
80         }
81         Init();
82 }
83
84 void SCA_MouseSensor::Init()
85 {
86         m_val = (m_invert)?1:0; /* stores the latest attribute */
87         m_reset = true;
88 }
89
90 SCA_MouseSensor::~SCA_MouseSensor() 
91 {
92     /* Nothing to be done here. */
93 }
94
95
96
97 CValue* SCA_MouseSensor::GetReplica()
98 {
99         SCA_MouseSensor* replica = new SCA_MouseSensor(*this);
100         // this will copy properties and so on...
101         CValue::AddDataToReplica(replica);
102         replica->Init();
103
104         return replica;
105 }
106
107
108
109 bool SCA_MouseSensor::IsPositiveTrigger()
110 {
111         bool result = (m_val != 0);
112         if (m_invert)
113                 result = !result;
114                 
115         return result;
116 }
117
118
119
120 short int SCA_MouseSensor::GetModeKey()
121
122         return m_mousemode;
123 }
124
125
126
127 SCA_IInputDevice::KX_EnumInputs SCA_MouseSensor::GetHotKey()
128
129         return m_hotkey;
130 }
131
132
133
134 bool SCA_MouseSensor::Evaluate(CValue* event)
135 {
136         bool result = false;
137         bool reset = m_reset && m_level;
138         SCA_IInputDevice* mousedev = m_pMouseMgr->GetInputDevice();
139
140
141
142 //      SCA_ILogicBrick::RegisterEvent(event);
143 //      if (m_mousemode == KX_MOUSESENSORMODE_MOVEMENT) cout << "\nChecking for movement...";
144 //CValue* val = event->GetProperty("val");
145
146         /* both MOUSEX and MOUSEY. Treat all of these as key-presses.            */
147         /* So, treat KX_MOUSESENSORMODE_POSITION as                              */
148         /* KX_MOUSESENSORMODE_POSITIONX || KX_MOUSESENSORMODE_POSITIONY          */
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                         if (event.m_status == SCA_InputEvent::KX_JUSTACTIVATED)
159                         {
160                                 m_val = 1;
161                                 result = true;
162                         } else
163                         {
164                                 if (event.m_status == SCA_InputEvent::KX_JUSTRELEASED)
165                                 {
166                                         m_val = 0;
167                                         result = true;
168                                 } else
169                                 {
170                                         if (event.m_status == SCA_InputEvent::KX_ACTIVE)
171                                         {
172                                                 if (m_val == 0)
173                                                 {
174                                                         m_val = 1;
175                                                         if (m_level)
176                                                         {
177                                                                 result = true;
178                                                         }
179                                                 }
180                                         } else
181                                         {
182                                                 if (m_val == 1)
183                                                 {
184                                                         m_val = 0;
185                                                         result = true;
186                                                 }
187                                         }
188                                 }
189                         }
190                         break;
191                 }
192         case KX_MOUSESENSORMODE_MOVEMENT:
193                 
194                 {
195                         const SCA_InputEvent& eventX = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
196                         const SCA_InputEvent& eventY = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
197
198                         if (eventX.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
199                                 eventY.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
200                                 eventX.m_status == SCA_InputEvent::KX_ACTIVE ||
201                                 eventY.m_status == SCA_InputEvent::KX_ACTIVE)
202                                 
203                         {
204                                 m_val = 1;
205                                 result = true;
206                         } else
207                         {
208                                 if (eventX.m_status == SCA_InputEvent::KX_JUSTRELEASED ||
209                                         eventY.m_status == SCA_InputEvent::KX_JUSTRELEASED )
210                                 {
211                                         m_val = 0;
212                                         result = true;
213                                 } else
214                                 {
215                                         if (m_val == 1)
216                                         {
217                                                 m_val = 0;
218                                                 result = true;
219                                         }
220                                 }
221                         }
222                         break;
223                 }
224         default:
225                 ; /* error */
226         }
227
228         if (reset)
229                 // force an event
230                 result = true;
231         return result;
232 }
233
234 void SCA_MouseSensor::setX(short x)
235 {
236         m_x = x;
237 }
238
239 void SCA_MouseSensor::setY(short y)
240 {
241         m_y = y;
242 }
243
244 bool SCA_MouseSensor::isValid(SCA_MouseSensor::KX_MOUSESENSORMODE m)
245 {
246         bool res = false;
247
248         res = ((m > KX_MOUSESENSORMODE_NODEF) && (m < KX_MOUSESENSORMODE_MAX));
249         
250         return res;
251 }
252
253 /* ------------------------------------------------------------------------- */
254 /* Python functions                                                          */
255 /* ------------------------------------------------------------------------- */
256
257 /* Integration hooks ------------------------------------------------------- */
258 PyTypeObject SCA_MouseSensor::Type = {
259         PyObject_HEAD_INIT(&PyType_Type)
260         0,
261         "SCA_MouseSensor",
262         sizeof(SCA_MouseSensor),
263         0,
264         PyDestructor,
265         0,
266         __getattr,
267         __setattr,
268         0, //&MyPyCompare,
269         __repr,
270         0, //&cvalue_as_number,
271         0,
272         0,
273         0,
274         0
275 };
276
277 PyParentObject SCA_MouseSensor::Parents[] = {
278         &SCA_MouseSensor::Type,
279         &SCA_ISensor::Type,
280         &SCA_ILogicBrick::Type,
281         &CValue::Type,
282         NULL
283 };
284
285 PyMethodDef SCA_MouseSensor::Methods[] = {
286         {"getXPosition", (PyCFunction) SCA_MouseSensor::sPyGetXPosition, METH_VARARGS, GetXPosition_doc},
287         {"getYPosition", (PyCFunction) SCA_MouseSensor::sPyGetYPosition, METH_VARARGS, GetYPosition_doc},
288         {NULL,NULL} //Sentinel
289 };
290
291 PyObject* SCA_MouseSensor::_getattr(const STR_String& attr) {
292         _getattr_up(SCA_ISensor);
293 }
294
295 /* get x position ---------------------------------------------------------- */
296 char SCA_MouseSensor::GetXPosition_doc[] = 
297 "getXPosition\n"
298 "\tReturns the x-coordinate of the mouse sensor, in frame coordinates.\n"
299 "\tThe lower-left corner is the origin. The coordinate is given in\n"
300 "\tpixels\n";
301 PyObject* SCA_MouseSensor::PyGetXPosition(PyObject* self, 
302                                                                                  PyObject* args, 
303                                                                                  PyObject* kwds) {
304         return PyInt_FromLong(m_x);
305 }
306
307 /* get y position ---------------------------------------------------------- */
308 char SCA_MouseSensor::GetYPosition_doc[] = 
309 "getYPosition\n"
310 "\tReturns the y-coordinate of the mouse sensor, in frame coordinates.\n"
311 "\tThe lower-left corner is the origin. The coordinate is given in\n"
312 "\tpixels\n";
313 PyObject* SCA_MouseSensor::PyGetYPosition(PyObject* self, 
314                                                                                  PyObject* args, 
315                                                                                  PyObject* kwds) {
316         return PyInt_FromLong(m_y);
317 }
318
319 /* eof */