get rid of some warnings,
[blender.git] / source / gameengine / GameLogic / SCA_PythonMouse.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): none yet.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include "SCA_PythonMouse.h"
26 #include "SCA_IInputDevice.h"
27 #include "RAS_ICanvas.h"
28
29 /* ------------------------------------------------------------------------- */
30 /* Native functions                                                          */
31 /* ------------------------------------------------------------------------- */
32
33 SCA_PythonMouse::SCA_PythonMouse(SCA_IInputDevice* mouse, RAS_ICanvas* canvas)
34 : PyObjectPlus(),
35 m_mouse(mouse),
36 m_canvas(canvas)
37 {
38 }
39
40 SCA_PythonMouse::~SCA_PythonMouse()
41 {
42         /* intentionally empty */
43 }
44
45 #ifndef DISABLE_PYTHON
46
47 /* ------------------------------------------------------------------------- */
48 /* Python functions                                                          */
49 /* ------------------------------------------------------------------------- */
50
51 /* Integration hooks ------------------------------------------------------- */
52 PyTypeObject SCA_PythonMouse::Type = {
53         PyVarObject_HEAD_INIT(NULL, 0)
54         "SCA_PythonMouse",
55         sizeof(PyObjectPlus_Proxy),
56         0,
57         py_base_dealloc,
58         0,
59         0,
60         0,
61         0,
62         py_base_repr,
63         0,0,0,0,0,0,0,0,0,
64         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
65         0,0,0,0,0,0,0,
66         Methods,
67         0,
68         0,
69         &PyObjectPlus::Type,
70         0,0,0,0,0,0,
71         py_base_new
72 };
73
74 PyMethodDef SCA_PythonMouse::Methods[] = {
75 //      KX_PYMETHODTABLE(SCA_PythonMouse, show),
76         {NULL,NULL} //Sentinel
77 };
78
79 PyAttributeDef SCA_PythonMouse::Attributes[] = {
80         KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_PythonMouse, pyattr_get_events),
81         KX_PYATTRIBUTE_RW_FUNCTION("position", SCA_PythonMouse, pyattr_get_position, pyattr_set_position),
82         KX_PYATTRIBUTE_RW_FUNCTION("visible", SCA_PythonMouse, pyattr_get_visible, pyattr_set_visible),
83         { NULL }        //Sentinel
84 };      
85
86 PyObject* SCA_PythonMouse::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
87 {
88         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
89
90         PyObject* resultlist = PyList_New(0);
91
92         for (int i=SCA_IInputDevice::KX_BEGINMOUSE; i<=SCA_IInputDevice::KX_ENDMOUSE; i++)
93         {
94                 const SCA_InputEvent & inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
95                 
96                 
97                 if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
98                 {
99                         PyObject* keypair = PyTuple_New(2);
100                         PyTuple_SET_ITEM(keypair, 0, PyLong_FromSsize_t(i));
101                         PyTuple_SET_ITEM(keypair, 1, PyLong_FromSsize_t(inevent.m_status));
102                         PyList_Append(resultlist, keypair);
103                 }
104         }
105
106         return resultlist;
107 }
108
109 PyObject* SCA_PythonMouse::pyattr_get_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
110 {
111         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
112         const SCA_InputEvent & xevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
113         const SCA_InputEvent & yevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
114
115         float x_coord, y_coord;
116
117         x_coord = self->m_canvas->GetMouseNormalizedX(xevent.m_eventval);
118         y_coord = self->m_canvas->GetMouseNormalizedY(yevent.m_eventval);
119
120         PyObject* ret = PyTuple_New(2);
121
122         PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(x_coord));
123         PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(y_coord));
124
125         return ret;
126 }
127
128 int SCA_PythonMouse::pyattr_set_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
129 {
130         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
131         int x, y;
132         float pyx, pyy;
133         if (!PyArg_ParseTuple(value, "ff:position", &pyx, &pyy))
134                 return PY_SET_ATTR_FAIL;
135
136         x = (int)(pyx*self->m_canvas->GetWidth());
137         y = (int)(pyy*self->m_canvas->GetHeight());
138
139         self->m_canvas->SetMousePosition(x, y);
140
141         return PY_SET_ATTR_SUCCESS;
142 }
143
144 PyObject* SCA_PythonMouse::pyattr_get_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
145 {
146         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
147
148         int visible;
149
150         if (self->m_canvas->GetMouseState() == RAS_ICanvas::MOUSE_INVISIBLE)
151                 visible = 0;
152         else
153                 visible = 1;
154
155         return PyBool_FromLong(visible);
156 }
157
158 int SCA_PythonMouse::pyattr_set_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
159 {
160         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
161
162         int visible = PyObject_IsTrue(value);
163
164         if (visible == -1)
165         {
166                 PyErr_SetString(PyExc_AttributeError, "SCA_PythonMouse.visible = bool: SCA_PythonMouse, expected True or False");
167                 return PY_SET_ATTR_FAIL;
168         }
169
170         if (visible)
171                 self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
172         else
173                 self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
174
175         return PY_SET_ATTR_SUCCESS;
176 }
177
178 #endif