~ubuntu-branches/ubuntu/trusty/gcompris/trusty

« back to all changes in this revision

Viewing changes to src/boards/py-mod-anim.c

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2006-12-15 23:08:17 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20061215230817-exr5ks1hd73s3tlk
Tags: 8.2.2-1
* New upstream bugfix release, fixes among other things the support for
  the version of gnucap shipped in etch.
* Add missing dependency on python-gtk2 (Closes: #396523).
* Removed reference to non-existent sound file from memory.c (upstream
  fix - impacts 8.2 as well).  
* Now suggests gnuchess, gnucap, and tuxpaint.
* Updated extended description for the main package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <Python.h>
 
2
#include <pygobject.h>
 
3
#include "gcompris/gcompris.h"
 
4
#include "py-gcompris-board.h"
 
5
#include "py-mod-anim.h"
 
6
 
 
7
#define THROW_INACTIVE_ANIMATION                                        \
 
8
{                                                                       \
 
9
    PyErr_SetString(PyExc_RuntimeError, "Tried to access an inactive "  \
 
10
                                        "AnimCanvas");                  \
 
11
    return NULL;                                                        \
 
12
}
 
13
 
 
14
static int Animation_init(py_GcomprisAnimation *self, PyObject*, PyObject*);
 
15
static void Animation_free(py_GcomprisAnimation *self);
 
16
 
 
17
static int AnimCanvas_init(py_GcomprisAnimCanvas*, PyObject*, PyObject*);
 
18
static void AnimCanvas_free(py_GcomprisAnimCanvas*);
 
19
static PyObject *AnimCanvas_getattr(py_GcomprisAnimCanvas*, char*);
 
20
 
 
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*);
 
25
 
 
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"},
 
33
  {NULL, NULL, 0, NULL}
 
34
};
 
35
 
 
36
static PyMethodDef AnimationMethods[] = {
 
37
  {NULL, NULL, 0, NULL}
 
38
};
 
39
 
 
40
static PyTypeObject py_GcomprisAnimationType = {
 
41
#if defined(WIN32)
 
42
  PyObject_HEAD_INIT(NULL)
 
43
#else /* ! WIN32 */
 
44
  PyObject_HEAD_INIT(&PyType_Type)
 
45
#endif
 
46
  0,                            /* ob_size */
 
47
  "pyGcomprisAnimation",        /* tp_name */
 
48
  sizeof(py_GcomprisAnimation), /* tp_basicsize */
 
49
  0,                            /* tp_itemsize */
 
50
  (destructor)Animation_free,   /* tp_dealloc */
 
51
  0,                            /* tp_print */
 
52
  0,                            /* tp_getattr */
 
53
  0,                            /* tp_setattr */
 
54
  0,                            /* tp_compare */
 
55
  0,                            /* tp_repr */
 
56
  0,                            /* tp_as_number */
 
57
  0,                            /* tp_as_sequence */
 
58
  0,                            /* tp_as_mapping */
 
59
  0,                            /* tp_hash */
 
60
  0,                            /* tp_call */
 
61
  0,                            /* tp_str */
 
62
  0,                            /* tp_getattro */
 
63
  0,                            /* tp_setattro */
 
64
  0,                            /* tp_as_buffer */
 
65
  Py_TPFLAGS_DEFAULT,           /* tp_flags */
 
66
  "Animation objects",          /* tp_doc */
 
67
  0,                            /* tp_traverse */
 
68
  0,                            /* tp_clear */
 
69
  0,                            /* tp_richcompare */
 
70
  0,                            /* tp_weaklistoffset */
 
71
  0,                            /* tp_iter */
 
72
  0,                            /* tp_iternext */
 
73
  AnimationMethods,             /* tp_methods */
 
74
  0,                            /* tp_members */
 
75
  0,                            /* tp_getset */
 
76
  0,                            /* tp_base */
 
77
  0,                            /* tp_dict */
 
78
  0,                            /* tp_descr_get */
 
79
  0,                            /* tp_descr_set */
 
80
  0,                            /* tp_dictoffset */
 
81
  (initproc)Animation_init,     /* tp_init */
 
82
  0,                            /* tp_alloc */
 
83
  0,                            /* tp_new */
 
84
};
 
85
 
 
86
static PyTypeObject py_GcomprisAnimCanvasType = {
 
87
#if defined(WIN32)
 
88
  PyObject_HEAD_INIT(NULL)
 
89
#else /* ! WIN32 */
 
90
  PyObject_HEAD_INIT(&PyType_Type)
 
91
#endif
 
92
  0,                                /* ob_size */
 
93
  "pyGcomprisAnimCanvas",           /* tp_name */
 
94
  sizeof(py_GcomprisAnimCanvas),    /* tp_basicsize */
 
95
  0,                                /* tp_itemsize */
 
96
  (destructor)AnimCanvas_free,      /* tp_dealloc */
 
97
  0,                                /* tp_print */
 
98
  (getattrfunc)AnimCanvas_getattr,  /* tp_getattr */
 
99
  0,                            /* tp_setattr */
 
100
  0,                            /* tp_compare */
 
101
  0,                            /* tp_repr */
 
102
  0,                            /* tp_as_number */
 
103
  0,                            /* tp_as_sequence */
 
104
  0,                            /* tp_as_mapping */
 
105
  0,                            /* tp_hash */
 
106
  0,                            /* tp_call */
 
107
  0,                            /* tp_str */
 
108
  0,                            /* tp_getattro */
 
109
  0,                            /* tp_setattro */
 
110
  0,                            /* tp_as_buffer */
 
111
  Py_TPFLAGS_DEFAULT,           /* tp_flags */
 
112
  "Animated canvas objects",    /* tp_doc */
 
113
  0,                            /* tp_traverse */
 
114
  0,                            /* tp_clear */
 
115
  0,                            /* tp_richcompare */
 
116
  0,                            /* tp_weaklistoffset */
 
117
  0,                            /* tp_iter */
 
118
  0,                            /* tp_iternext */
 
119
  AnimCanvasMethods,            /* tp_methods */
 
120
  0,                            /* tp_members */
 
121
  0,                            /* tp_getset */
 
122
  0,                            /* tp_base */
 
123
  0,                            /* tp_dict */
 
124
  0,                            /* tp_descr_get */
 
125
  0,                            /* tp_descr_set */
 
126
  0,                            /* tp_dictoffset */
 
127
  (initproc)AnimCanvas_init,    /* tp_init */
 
128
  0,                            /* tp_alloc */
 
129
  0,                            /* tp_new */
 
130
};
 
131
 
 
132
static PyMethodDef PythonGcomprisAnimModule[] = {
 
133
  {NULL, NULL, 0, NULL}
 
134
};
 
135
 
 
136
/*============================================================================*/
 
137
/*                      GcomprisAnimation functions                           */
 
138
/*============================================================================*/
 
139
static int
 
140
Animation_init(py_GcomprisAnimation *self, PyObject *args, PyObject *key)
 
141
{
 
142
  static char *kwlist[] =
 
143
  {
 
144
    "filename", NULL
 
145
  };
 
146
 
 
147
  char *file=NULL;
 
148
 
 
149
  if(!PyArg_ParseTupleAndKeywords(args, key, "|s", kwlist,
 
150
                                   &file))
 
151
    {
 
152
      PyErr_SetString(PyExc_RuntimeError, "Invalid arguments to Animation()");
 
153
      return -1;
 
154
    }
 
155
 
 
156
  if(file)
 
157
    {
 
158
      self->a = gc_anim_load(file);
 
159
    }
 
160
 
 
161
  if(!self->a)
 
162
    {
 
163
      PyErr_SetString(PyExc_RuntimeError, "Failed to load Animation");
 
164
      return -1;
 
165
    }
 
166
  return 0;
 
167
}
 
168
 
 
169
static void Animation_free(py_GcomprisAnimation *self)
 
170
{
 
171
  g_warning("*** Garbage collecting Animation ***\n");
 
172
  if( self->a)
 
173
      gc_anim_free(self->a);
 
174
  PyObject_DEL(self);
 
175
}
 
176
 
 
177
/*============================================================================*/
 
178
/*                            Animation Methods                               */
 
179
/*============================================================================*/
 
180
 
 
181
static int
 
182
AnimCanvas_init(py_GcomprisAnimCanvas *self, PyObject *args, PyObject *key)
 
183
{
 
184
  GcomprisAnimCanvasItem *item;
 
185
  GcomprisAnimation *anim;
 
186
  GnomeCanvasGroup *parent;
 
187
  PyObject *py_p, *py_a;
 
188
 
 
189
  if(!PyArg_ParseTuple(args, "OO:AnimCanvas_init", &py_a, &py_p)) {
 
190
      PyErr_SetString(PyExc_RuntimeError, "Invalid arguments to AnimCanvas()");
 
191
      return -1;
 
192
  }
 
193
  if(!PyObject_TypeCheck(py_a, &py_GcomprisAnimationType) ||
 
194
     !PyObject_TypeCheck(py_p,pygobject_lookup_class(GNOME_TYPE_CANVAS_GROUP))){
 
195
 
 
196
      PyErr_SetString(PyExc_TypeError, "AnimCanvas() needs an Animation");
 
197
      return -1;
 
198
  }
 
199
 
 
200
  parent = (GnomeCanvasGroup*) pygobject_get(py_p);
 
201
  anim = ( (py_GcomprisAnimation*)py_a )->a;
 
202
  item = (GcomprisAnimCanvasItem*) gc_anim_activate(parent, anim);
 
203
  self->item = item;
 
204
  self->anim = py_a;
 
205
 
 
206
  Py_INCREF(self->anim);
 
207
  return 0;
 
208
}
 
209
 
 
210
static void
 
211
AnimCanvas_free(py_GcomprisAnimCanvas *self)
 
212
{
 
213
  g_warning("*** garbage collecting AnimCanvas ***\n");
 
214
  if(self->item)
 
215
    {
 
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);
 
219
      self->item = NULL;
 
220
      Py_DECREF(self->anim);
 
221
    }
 
222
  PyObject_DEL(self);
 
223
}
 
224
 
 
225
static PyObject *AnimCanvas_getattr(py_GcomprisAnimCanvas *self, char *name)
 
226
{
 
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);
 
231
 
 
232
  return Py_FindMethod(AnimCanvasMethods, (PyObject *)self, name);
 
233
}
 
234
 
 
235
static PyObject*
 
236
py_gcompris_animcanvas_setstate(PyObject *self, PyObject *args)
 
237
{
 
238
  int state;
 
239
  GcomprisAnimCanvasItem *item = ( (py_GcomprisAnimCanvas*)self )->item;
 
240
 
 
241
  if(!item) THROW_INACTIVE_ANIMATION;
 
242
 
 
243
  if(!PyArg_ParseTuple(args, "i:gcompris_animcanvas_setstate", &state))
 
244
    return NULL;
 
245
 
 
246
  gc_anim_set_state( item, state );
 
247
 
 
248
  Py_INCREF(Py_None);
 
249
  return Py_None;
 
250
}
 
251
 
 
252
static PyObject*
 
253
py_gcompris_animcanvas_swapanim(PyObject *self, PyObject *args)
 
254
{
 
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;
 
259
 
 
260
  if(!item) THROW_INACTIVE_ANIMATION;
 
261
 
 
262
  if(!PyArg_ParseTuple(args, "O:AnimCanvas_swapAnim", (PyObject**)&new_anim))
 
263
    return NULL;
 
264
 
 
265
  gc_anim_swap(item, new_anim->a);
 
266
  Py_INCREF(new_anim);
 
267
  s->anim = (PyObject*)new_anim;
 
268
  Py_DECREF(old_anim);
 
269
 
 
270
  Py_INCREF(Py_None);
 
271
  return Py_None;
 
272
}
 
273
 
 
274
static PyObject*
 
275
py_gcompris_animcanvas_destroy(PyObject *self, PyObject *args)
 
276
{
 
277
  py_GcomprisAnimCanvas *s = (py_GcomprisAnimCanvas*)self;
 
278
 
 
279
  if(!s->item) THROW_INACTIVE_ANIMATION;
 
280
 
 
281
  gc_anim_deactivate(s->item);
 
282
  Py_DECREF(s->anim);
 
283
  s->item = NULL;
 
284
  s->anim = NULL;
 
285
 
 
286
  Py_INCREF(Py_None);
 
287
  return Py_None;
 
288
}
 
289
 
 
290
#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
 
291
#define PyMODINIT_FUNC void
 
292
#endif
 
293
PyMODINIT_FUNC
 
294
python_gcompris_anim_module_init(void)
 
295
{
 
296
  PyObject* m;
 
297
 
 
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)
 
303
      return;
 
304
  if (PyType_Ready(&py_GcomprisAnimCanvasType) < 0)
 
305
      return;
 
306
 
 
307
  m = Py_InitModule("_gcompris_anim", PythonGcomprisAnimModule);
 
308
 
 
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);
 
313
}
 
314