change distance calculation
[numtypysics.git] / PythonInput.cpp
blobf76bb7aeae2f43844db221c487e11bb4800cf479
2 /*
3 * Python Input Module for NumptyPhysics
4 * Copyright (c) 2009 Thomas Perl <thpinfo.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
18 #include "PythonInput.h"
19 #include "Multitouch.h"
21 #ifdef HAVE_PYTHON
23 #include <Python.h>
24 #include "SDL/SDL.h"
25 #include <SDL/SDL_mixer.h>
27 PyObject* PythonInput::create_module()
29 static PyMethodDef NumptyMethods[] = {
30 {"post_event", post_event, METH_O, "Post a new mouse event"},
31 {NULL, NULL, 0, NULL} /* Sentinel */
34 PyObject* module;
35 module = Py_InitModule("numptyphysics", NumptyMethods);
37 PyModule_AddIntConstant(module, "START_STROKE", SDL_NP_START_STROKE);
38 PyModule_AddIntConstant(module, "APPEND_STROKE", SDL_NP_APPEND_STROKE);
39 PyModule_AddIntConstant(module, "FINISH_STROKE", SDL_NP_FINISH_STROKE);
40 PyModule_AddIntConstant(module, "START_ROPE", SDL_NP_START_ROPE);
41 PyModule_AddIntConstant(module, "APPEND_ROPE", SDL_NP_APPEND_ROPE);
42 PyModule_AddIntConstant(module, "FINISH_ROPE", SDL_NP_FINISH_ROPE);
43 PyModule_AddIntConstant(module, "PREVIEW_CURSOR", SDL_NP_PREVIEW_CURSOR);
44 PyModule_AddIntConstant(module, "CANCEL_DRAW", SDL_NP_CANCEL_DRAW);
45 PyModule_AddIntConstant(module, "START_DRAG", SDL_NP_START_DRAG);
46 PyModule_AddIntConstant(module, "DRAG", SDL_NP_DRAG);
47 PyModule_AddIntConstant(module, "END_DRAG", SDL_NP_END_DRAG);
48 PyModule_AddIntConstant(module, "PAN", SDL_NP_PAN);
49 PyModule_AddIntConstant(module, "ZOOM", SDL_NP_ZOOM);
50 PyModule_AddIntConstant(module, "DELETE", SDL_NP_DELETE);
52 PyModule_AddIntConstant(module, "WIDTH", m_width);
53 PyModule_AddIntConstant(module, "HEIGHT", m_height);
54 PyModule_AddIntConstant(module, "MAX_CURSORS", MT_MAX_CURSORS);
56 return module;
59 PyObject* PythonInput::post_event(PyObject* self, PyObject* event)
61 static Mix_Chunk* sDraw = Mix_LoadWAV("draw.ogg");
62 static Mix_Chunk* sDrag = Mix_LoadWAV("drag.ogg");
63 static Mix_Chunk* sDelete = Mix_LoadWAV("delete.ogg");
65 int xpos = -1, ypos = -1;
66 int type = -1;
67 int cursor_id = -1;
68 PyObject *o = NULL;
69 SDL_Event e = {0};
71 if(sDrag == NULL || sDraw == NULL || sDelete == NULL) {
72 fprintf(stderr, "Error: %s\n", Mix_GetError());
73 exit(1);
76 assert(self == NULL);
78 /* Get X position */
79 o = PyObject_GetAttrString(event, "x");
80 if (PyInt_Check(o)) {
81 xpos = (int)PyInt_AsLong(o);
82 } else {
83 fprintf(stderr, "x not a number: ");
84 PyObject_Print(o, stderr, 0);
85 fprintf(stderr, "\n");
86 exit(1);
88 Py_DECREF(o);
90 /* Get Y position */
91 o = PyObject_GetAttrString(event, "y");
92 if (PyInt_Check(o)) {
93 ypos = (int)PyInt_AsLong(o);
94 } else {
95 fprintf(stderr, "y not a number: ");
96 PyObject_Print(o, stderr, 0);
97 fprintf(stderr, "\n");
98 exit(1);
100 Py_DECREF(o);
102 /* Get type */
103 o = PyObject_GetAttrString(event, "event_type");
104 if (PyInt_Check(o)) {
105 type = (int)PyInt_AsLong(o);
106 } else {
107 fprintf(stderr, "event_type is not an int: ");
108 PyObject_Print(o, stderr, 0);
109 fputc('\n', stderr);
110 exit(1);
112 Py_DECREF(o);
114 /* Get cursor_id */
115 o = PyObject_GetAttrString(event, "cursor_id");
116 if (PyInt_Check(o)) {
117 cursor_id = (int)PyInt_AsLong(o);
118 } else {
119 fprintf(stderr, "cursor_id is not an int: ");
120 PyObject_Print(o, stderr, 0);
121 fputc('\n', stderr);
122 exit(1);
124 Py_DECREF(o);
126 if (type == SDL_NP_START_STROKE) {
127 Mix_PlayChannel(-1, sDraw, 0);
128 queueStartStrokeEvent(cursor_id, xpos, ypos);
129 } else if (type == SDL_NP_FINISH_STROKE) {
130 queueFinishStrokeEvent(cursor_id, xpos, ypos);
131 } else if (type == SDL_NP_APPEND_STROKE) {
132 queueAppendStrokeEvent(cursor_id, xpos, ypos);
133 } else if (type == SDL_NP_START_DRAG) {
134 Mix_PlayChannel(-1, sDrag, 0);
135 queueStartDragEvent(cursor_id, xpos, ypos);
136 } else if (type == SDL_NP_END_DRAG) {
137 Mix_PlayChannel(-1, sDrag, 0);
138 queueEndDragEvent(cursor_id, xpos, ypos);
139 } else if (type == SDL_NP_DRAG) {
140 queueDragEvent(cursor_id, xpos, ypos);
141 } else if (type == SDL_NP_PREVIEW_CURSOR) {
142 queuePreviewCursorEvent(cursor_id, xpos, ypos);
143 } else if (type == SDL_NP_DELETE) {
144 Mix_PlayChannel(-1, sDelete, 0);
145 queueDeleteEvent(xpos, ypos);
146 } else if (type == SDL_NP_CANCEL_DRAW) {
147 queueCancelDrawEvent(cursor_id);
148 } else {
149 fprintf(stderr, "Warning: unknown event type %d.\n", type);
150 exit(1);
153 //return PyErr_Format(PyExc_TypeError, "This function needs a bot class to work");
154 Py_RETURN_NONE;
157 #endif /* HAVE_PYTHON */