Initial revision
[blender.git] / source / gameengine / Ketsji / KX_VertexProxy.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #include "KX_VertexProxy.h"
33
34 #include "RAS_TexVert.h"
35
36
37 PyTypeObject KX_VertexProxy::Type = {
38         PyObject_HEAD_INIT(&PyType_Type)
39         0,
40         "KX_VertexProxy",
41         sizeof(KX_VertexProxy),
42         0,
43         PyDestructor,
44         0,
45         __getattr,
46         __setattr,
47         0, //&MyPyCompare,
48         __repr,
49         0, //&cvalue_as_number,
50         0,
51         0,
52         0,
53         0
54 };
55
56 PyParentObject KX_VertexProxy::Parents[] = {
57         &KX_VertexProxy::Type,
58         &SCA_IObject::Type,
59         &CValue::Type,
60         NULL
61 };
62
63 PyMethodDef KX_VertexProxy::Methods[] = {
64 {"getXYZ", (PyCFunction)KX_VertexProxy::sPyGetXYZ,METH_VARARGS},
65 {"setXYZ", (PyCFunction)KX_VertexProxy::sPySetXYZ,METH_VARARGS},
66 {"getUV", (PyCFunction)KX_VertexProxy::sPyGetUV,METH_VARARGS},
67 {"setUV", (PyCFunction)KX_VertexProxy::sPySetUV,METH_VARARGS},
68 {"getRGBA", (PyCFunction)KX_VertexProxy::sPyGetRGBA,METH_VARARGS},
69 {"setRGBA", (PyCFunction)KX_VertexProxy::sPySetRGBA,METH_VARARGS},
70 {"getNormal", (PyCFunction)KX_VertexProxy::sPyGetNormal,METH_VARARGS},
71 {"setNormal", (PyCFunction)KX_VertexProxy::sPySetNormal,METH_VARARGS},
72   {NULL,NULL} //Sentinel
73 };
74
75 PyObject*
76 KX_VertexProxy::_getattr(char* attr)
77 {
78   _getattr_up(SCA_IObject);
79 }
80
81
82
83 KX_VertexProxy::KX_VertexProxy(RAS_TexVert* vertex)
84 :m_vertex(vertex)
85 {
86         
87 }
88
89 KX_VertexProxy::~KX_VertexProxy()
90 {
91         
92 }
93
94
95
96 // stuff for cvalue related things
97 CValue*         KX_VertexProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
98 CValue*         KX_VertexProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}        
99 STR_String      sVertexName="vertex";
100 const STR_String &      KX_VertexProxy::GetText() {return sVertexName;};
101 float           KX_VertexProxy::GetNumber() { return -1;}
102 STR_String      KX_VertexProxy::GetName() { return sVertexName;}
103 void            KX_VertexProxy::SetName(STR_String name) { };
104 CValue*         KX_VertexProxy::GetReplica() { return NULL;}
105 void            KX_VertexProxy::ReplicaSetName(STR_String name) {};
106
107
108 // stuff for python integration
109         
110 PyObject* KX_VertexProxy::PyGetXYZ(PyObject* self, 
111                                PyObject* args, 
112                                PyObject* kwds)
113 {
114         
115         MT_Point3 pos = m_vertex->getLocalXYZ();
116         
117         PyObject* resultlist = PyList_New(3);
118         int index;
119         for (index=0;index<3;index++)
120         {
121                 PyList_SetItem(resultlist,index,PyFloat_FromDouble(pos[index]));
122         }
123
124         return resultlist;
125
126 }
127
128 PyObject* KX_VertexProxy::PySetXYZ(PyObject* self, 
129                                PyObject* args, 
130                                PyObject* kwds)
131 {
132
133         MT_Point3 pos = ConvertPythonVectorArg(args);
134         m_vertex->SetXYZ(pos);
135
136
137         Py_Return;
138 }
139
140 PyObject* KX_VertexProxy::PyGetNormal(PyObject* self, 
141                                PyObject* args, 
142                                PyObject* kwds)
143 {
144         
145         const short* shortnormal = m_vertex->getNormal();
146         MT_Vector3 normal(shortnormal[0],shortnormal[1],shortnormal[2]);
147         normal.normalize();
148         
149         PyObject* resultlist = PyList_New(3);
150         int index;
151         for (index=0;index<3;index++)
152         {
153                 PyList_SetItem(resultlist,index,PyFloat_FromDouble(normal[index]));
154         }
155
156         return resultlist;
157
158 }
159
160 PyObject* KX_VertexProxy::PySetNormal(PyObject* self, 
161                                PyObject* args, 
162                                PyObject* kwds)
163 {
164         MT_Point3 normal = ConvertPythonVectorArg(args);
165         m_vertex->SetNormal(normal);
166         Py_Return;
167 }
168
169
170 PyObject* KX_VertexProxy::PyGetRGBA(PyObject* self,
171                                PyObject* args, 
172                                PyObject* kwds)
173 {
174         int rgba = m_vertex->getRGBA();
175         return PyInt_FromLong(rgba);
176 }
177
178 PyObject* KX_VertexProxy::PySetRGBA(PyObject* self, 
179                                PyObject* args, 
180                                PyObject* kwds)
181 {
182         int rgba;
183         if (PyArg_ParseTuple(args,"i",&rgba))
184         {
185                 m_vertex->SetRGBA(rgba);
186         }
187         Py_Return;
188 }
189
190
191 PyObject* KX_VertexProxy::PyGetUV(PyObject* self, 
192                                PyObject* args, 
193                                PyObject* kwds)
194 {
195         MT_Vector2 uv = m_vertex->getUV1();
196         PyObject* resultlist = PyList_New(2);
197         int index;
198         for (index=0;index<2;index++)
199         {
200                 PyList_SetItem(resultlist,index,PyFloat_FromDouble(uv[index]));
201         }
202
203         return resultlist;
204
205 }
206
207 PyObject* KX_VertexProxy::PySetUV(PyObject* self, 
208                                PyObject* args, 
209                                PyObject* kwds)
210 {
211         MT_Point3 uv = ConvertPythonVectorArg(args);
212         m_vertex->SetUV(MT_Point2(uv[0],uv[1]));
213         Py_Return;
214 }
215
216
217