3
#include "gcompris/gcompris.h"
4
#include "py-gcompris-board.h"
5
#include "py-mod-anim.h"
7
#define THROW_INACTIVE_ANIMATION \
9
PyErr_SetString(PyExc_RuntimeError, "Tried to access an inactive " \
14
static int Animation_init(py_GcomprisAnimation *self, PyObject*, PyObject*);
15
static void Animation_free(py_GcomprisAnimation *self);
17
static int AnimCanvas_init(py_GcomprisAnimCanvas*, PyObject*, PyObject*);
18
static void AnimCanvas_free(py_GcomprisAnimCanvas*);
19
static PyObject *AnimCanvas_getattr(py_GcomprisAnimCanvas*, char*);
21
/* AnimCanvas methods */
22
static PyObject *py_gcompris_animcanvas_setstate(PyObject*, PyObject*);
23
static PyObject *py_gcompris_animcanvas_swapanim(PyObject*, PyObject*);
24
static PyObject *py_gcompris_animcanvas_destroy(PyObject*, PyObject*);
26
static PyMethodDef AnimCanvasMethods[] = {
27
{"setState", py_gcompris_animcanvas_setstate, METH_VARARGS,
28
"gcompris_animcanvas_setstate"},
29
{"swapAnimation", py_gcompris_animcanvas_swapanim, METH_VARARGS,
30
"gcompris_animcanvas_swapanim"},
31
{"destroy", py_gcompris_animcanvas_destroy, METH_VARARGS,
32
"gcompris_animcanvas_destroy"},
36
static PyMethodDef AnimationMethods[] = {
40
static PyTypeObject py_GcomprisAnimationType = {
42
PyObject_HEAD_INIT(NULL)
44
PyObject_HEAD_INIT(&PyType_Type)
47
"pyGcomprisAnimation", /* tp_name */
48
sizeof(py_GcomprisAnimation), /* tp_basicsize */
50
(destructor)Animation_free, /* tp_dealloc */
57
0, /* tp_as_sequence */
58
0, /* tp_as_mapping */
65
Py_TPFLAGS_DEFAULT, /* tp_flags */
66
"Animation objects", /* tp_doc */
69
0, /* tp_richcompare */
70
0, /* tp_weaklistoffset */
73
AnimationMethods, /* tp_methods */
80
0, /* tp_dictoffset */
81
(initproc)Animation_init, /* tp_init */
86
static PyTypeObject py_GcomprisAnimCanvasType = {
88
PyObject_HEAD_INIT(NULL)
90
PyObject_HEAD_INIT(&PyType_Type)
93
"pyGcomprisAnimCanvas", /* tp_name */
94
sizeof(py_GcomprisAnimCanvas), /* tp_basicsize */
96
(destructor)AnimCanvas_free, /* tp_dealloc */
98
(getattrfunc)AnimCanvas_getattr, /* tp_getattr */
102
0, /* tp_as_number */
103
0, /* tp_as_sequence */
104
0, /* tp_as_mapping */
110
0, /* tp_as_buffer */
111
Py_TPFLAGS_DEFAULT, /* tp_flags */
112
"Animated canvas objects", /* tp_doc */
115
0, /* tp_richcompare */
116
0, /* tp_weaklistoffset */
119
AnimCanvasMethods, /* tp_methods */
124
0, /* tp_descr_get */
125
0, /* tp_descr_set */
126
0, /* tp_dictoffset */
127
(initproc)AnimCanvas_init, /* tp_init */
132
static PyMethodDef PythonGcomprisAnimModule[] = {
133
{NULL, NULL, 0, NULL}
136
/*============================================================================*/
137
/* GcomprisAnimation functions */
138
/*============================================================================*/
140
Animation_init(py_GcomprisAnimation *self, PyObject *args, PyObject *key)
142
static char *kwlist[] =
149
if(!PyArg_ParseTupleAndKeywords(args, key, "|s", kwlist,
152
PyErr_SetString(PyExc_RuntimeError, "Invalid arguments to Animation()");
158
self->a = gc_anim_load(file);
163
PyErr_SetString(PyExc_RuntimeError, "Failed to load Animation");
169
static void Animation_free(py_GcomprisAnimation *self)
171
g_warning("*** Garbage collecting Animation ***\n");
173
gc_anim_free(self->a);
177
/*============================================================================*/
178
/* Animation Methods */
179
/*============================================================================*/
182
AnimCanvas_init(py_GcomprisAnimCanvas *self, PyObject *args, PyObject *key)
184
GcomprisAnimCanvasItem *item;
185
GcomprisAnimation *anim;
186
GnomeCanvasGroup *parent;
187
PyObject *py_p, *py_a;
189
if(!PyArg_ParseTuple(args, "OO:AnimCanvas_init", &py_a, &py_p)) {
190
PyErr_SetString(PyExc_RuntimeError, "Invalid arguments to AnimCanvas()");
193
if(!PyObject_TypeCheck(py_a, &py_GcomprisAnimationType) ||
194
!PyObject_TypeCheck(py_p,pygobject_lookup_class(GNOME_TYPE_CANVAS_GROUP))){
196
PyErr_SetString(PyExc_TypeError, "AnimCanvas() needs an Animation");
200
parent = (GnomeCanvasGroup*) pygobject_get(py_p);
201
anim = ( (py_GcomprisAnimation*)py_a )->a;
202
item = (GcomprisAnimCanvasItem*) gc_anim_activate(parent, anim);
206
Py_INCREF(self->anim);
211
AnimCanvas_free(py_GcomprisAnimCanvas *self)
213
g_warning("*** garbage collecting AnimCanvas ***\n");
216
g_warning("You should really call destroy() on an AnimCanvas "
217
"instead of relying on the refcounter\n");
218
gc_anim_deactivate(self->item);
220
Py_DECREF(self->anim);
225
static PyObject *AnimCanvas_getattr(py_GcomprisAnimCanvas *self, char *name)
227
if(!strcmp(name, "gnomecanvas"))
228
return (PyObject*) pygobject_new( (GObject*) self->item->canvas );
229
else if(!strcmp(name, "num_states"))
230
return Py_BuildValue("i", self->item->anim->numstates);
232
return Py_FindMethod(AnimCanvasMethods, (PyObject *)self, name);
236
py_gcompris_animcanvas_setstate(PyObject *self, PyObject *args)
239
GcomprisAnimCanvasItem *item = ( (py_GcomprisAnimCanvas*)self )->item;
241
if(!item) THROW_INACTIVE_ANIMATION;
243
if(!PyArg_ParseTuple(args, "i:gcompris_animcanvas_setstate", &state))
246
gc_anim_set_state( item, state );
253
py_gcompris_animcanvas_swapanim(PyObject *self, PyObject *args)
255
py_GcomprisAnimCanvas *s = (py_GcomprisAnimCanvas*)self;
256
py_GcomprisAnimation *new_anim;
257
py_GcomprisAnimation *old_anim = (py_GcomprisAnimation*)s->anim;
258
GcomprisAnimCanvasItem *item = s->item;
260
if(!item) THROW_INACTIVE_ANIMATION;
262
if(!PyArg_ParseTuple(args, "O:AnimCanvas_swapAnim", (PyObject**)&new_anim))
265
gc_anim_swap(item, new_anim->a);
267
s->anim = (PyObject*)new_anim;
275
py_gcompris_animcanvas_destroy(PyObject *self, PyObject *args)
277
py_GcomprisAnimCanvas *s = (py_GcomprisAnimCanvas*)self;
279
if(!s->item) THROW_INACTIVE_ANIMATION;
281
gc_anim_deactivate(s->item);
290
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
291
#define PyMODINIT_FUNC void
294
python_gcompris_anim_module_init(void)
298
py_GcomprisAnimationType.tp_new = PyType_GenericNew;
299
py_GcomprisAnimationType.ob_type = &PyType_Type;
300
py_GcomprisAnimCanvasType.tp_new = PyType_GenericNew;
301
py_GcomprisAnimCanvasType.ob_type = &PyType_Type;
302
if (PyType_Ready(&py_GcomprisAnimationType) < 0)
304
if (PyType_Ready(&py_GcomprisAnimCanvasType) < 0)
307
m = Py_InitModule("_gcompris_anim", PythonGcomprisAnimModule);
309
Py_INCREF(&py_GcomprisAnimationType);
310
Py_INCREF(&py_GcomprisAnimCanvasType);
311
PyModule_AddObject(m, "Animation", (PyObject *)&py_GcomprisAnimationType);
312
PyModule_AddObject(m, "CanvasItem", (PyObject *)&py_GcomprisAnimCanvasType);