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