Blender  V2.59
SCA_PythonMouse.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: SCA_PythonMouse.cpp 35169 2011-02-25 13:32:11Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributor(s): none yet.
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00030 #include "SCA_PythonMouse.h"
00031 #include "SCA_IInputDevice.h"
00032 #include "RAS_ICanvas.h"
00033 
00034 /* ------------------------------------------------------------------------- */
00035 /* Native functions                                                          */
00036 /* ------------------------------------------------------------------------- */
00037 
00038 SCA_PythonMouse::SCA_PythonMouse(SCA_IInputDevice* mouse, RAS_ICanvas* canvas)
00039 : PyObjectPlus(),
00040 m_mouse(mouse),
00041 m_canvas(canvas)
00042 {
00043 #ifdef WITH_PYTHON
00044         m_event_dict = PyDict_New();
00045 #endif
00046 }
00047 
00048 SCA_PythonMouse::~SCA_PythonMouse()
00049 {
00050 #ifdef WITH_PYTHON
00051         PyDict_Clear(m_event_dict);
00052         Py_DECREF(m_event_dict);
00053 #endif
00054 }
00055 
00056 #ifdef WITH_PYTHON
00057 
00058 /* ------------------------------------------------------------------------- */
00059 /* Python functions                                                          */
00060 /* ------------------------------------------------------------------------- */
00061 
00062 /* Integration hooks ------------------------------------------------------- */
00063 PyTypeObject SCA_PythonMouse::Type = {
00064         PyVarObject_HEAD_INIT(NULL, 0)
00065         "SCA_PythonMouse",
00066         sizeof(PyObjectPlus_Proxy),
00067         0,
00068         py_base_dealloc,
00069         0,
00070         0,
00071         0,
00072         0,
00073         py_base_repr,
00074         0,0,0,0,0,0,0,0,0,
00075         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00076         0,0,0,0,0,0,0,
00077         Methods,
00078         0,
00079         0,
00080         &PyObjectPlus::Type,
00081         0,0,0,0,0,0,
00082         py_base_new
00083 };
00084 
00085 PyMethodDef SCA_PythonMouse::Methods[] = {
00086         {NULL,NULL} //Sentinel
00087 };
00088 
00089 PyAttributeDef SCA_PythonMouse::Attributes[] = {
00090         KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_PythonMouse, pyattr_get_events),
00091         KX_PYATTRIBUTE_RW_FUNCTION("position", SCA_PythonMouse, pyattr_get_position, pyattr_set_position),
00092         KX_PYATTRIBUTE_RW_FUNCTION("visible", SCA_PythonMouse, pyattr_get_visible, pyattr_set_visible),
00093         { NULL }        //Sentinel
00094 };      
00095 
00096 PyObject* SCA_PythonMouse::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00097 {
00098         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
00099         
00100         for (int i=SCA_IInputDevice::KX_BEGINMOUSE; i<=SCA_IInputDevice::KX_ENDMOUSE; i++)
00101         {
00102                 const SCA_InputEvent & inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
00103                 
00104                 PyDict_SetItem(self->m_event_dict, PyLong_FromSsize_t(i), PyLong_FromSsize_t(inevent.m_status));
00105         }
00106         Py_INCREF(self->m_event_dict);
00107         return self->m_event_dict;
00108 }
00109 
00110 
00111 PyObject* SCA_PythonMouse::pyattr_get_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00112 {
00113         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
00114         const SCA_InputEvent & xevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
00115         const SCA_InputEvent & yevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
00116 
00117         float x_coord, y_coord;
00118 
00119         x_coord = self->m_canvas->GetMouseNormalizedX(xevent.m_eventval);
00120         y_coord = self->m_canvas->GetMouseNormalizedY(yevent.m_eventval);
00121 
00122         PyObject* ret = PyTuple_New(2);
00123 
00124         PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(x_coord));
00125         PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(y_coord));
00126 
00127         return ret;
00128 }
00129 
00130 int SCA_PythonMouse::pyattr_set_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
00131 {
00132         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
00133         int x, y;
00134         float pyx, pyy;
00135         if (!PyArg_ParseTuple(value, "ff:position", &pyx, &pyy))
00136                 return PY_SET_ATTR_FAIL;
00137 
00138         x = (int)(pyx*self->m_canvas->GetWidth());
00139         y = (int)(pyy*self->m_canvas->GetHeight());
00140 
00141         self->m_canvas->SetMousePosition(x, y);
00142 
00143         return PY_SET_ATTR_SUCCESS;
00144 }
00145 
00146 PyObject* SCA_PythonMouse::pyattr_get_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00147 {
00148         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
00149 
00150         int visible;
00151 
00152         if (self->m_canvas->GetMouseState() == RAS_ICanvas::MOUSE_INVISIBLE)
00153                 visible = 0;
00154         else
00155                 visible = 1;
00156 
00157         return PyBool_FromLong(visible);
00158 }
00159 
00160 int SCA_PythonMouse::pyattr_set_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
00161 {
00162         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
00163 
00164         int visible = PyObject_IsTrue(value);
00165 
00166         if (visible == -1)
00167         {
00168                 PyErr_SetString(PyExc_AttributeError, "SCA_PythonMouse.visible = bool: SCA_PythonMouse, expected True or False");
00169                 return PY_SET_ATTR_FAIL;
00170         }
00171 
00172         if (visible)
00173                 self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
00174         else
00175                 self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
00176 
00177         return PY_SET_ATTR_SUCCESS;
00178 }
00179 
00180 #endif