Blender V2.61 - r43446

SCA_PythonMouse.cpp

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