keyboard/mouse handling

This commit is contained in:
Lieuwe 2011-03-05 20:08:41 +01:00
parent 20b6a64228
commit d6d661c434
3 changed files with 68 additions and 11 deletions

File diff suppressed because one or more lines are too long

View File

@ -1817,14 +1817,27 @@ emb_draw_fillrect(PyObject *self, PyObject *args)
return Py_BuildValue("i",-1); return Py_BuildValue("i",-1);
} }
//int textwidth(char *s) //int textwidth(char *s)
emb_string_get_width(PyObject *self, PyObject *args) emb_get_width(PyObject *self, PyObject *args)
{ {
char *txt; char *txt;
if(!PyArg_ParseTuple(args, "s:",&txt)) if(!PyArg_ParseTuple(args, "s:get_width",&txt))
return NULL; return NULL;
return Py_BuildValue("i",textwidth(txt)); return Py_BuildValue("i",textwidth(txt));
} }
//SDL_GetMouseState(&x, &y)
emb_get_mouse(PyObject *self, PyObject *args)
{
int x,y,mask,b1,b2,b3;
if(!PyArg_ParseTuple(args, ":get_mouse"))
return NULL;
mask=SDL_GetMouseState(&x, &y);
b1=mask&SDL_BUTTON(1);
b2=mask&SDL_BUTTON(2);
b3=mask&SDL_BUTTON(3);
return Py_BuildValue("(ii(iii))",x,y,b1,b2,b3);
}
static PyMethodDef EmbMethods[] = { //WARNING! don't forget to register your function here! static PyMethodDef EmbMethods[] = { //WARNING! don't forget to register your function here!
{"create", emb_create, METH_VARARGS|METH_KEYWORDS, "create a particle."}, {"create", emb_create, METH_VARARGS|METH_KEYWORDS, "create a particle."},
{"log", emb_log, METH_VARARGS, "logs an error string to the console."}, {"log", emb_log, METH_VARARGS, "logs an error string to the console."},
@ -1854,7 +1867,8 @@ static PyMethodDef EmbMethods[] = { //WARNING! don't forget to register your fun
{"draw_text", emb_draw_text, METH_VARARGS, "draw some text."}, {"draw_text", emb_draw_text, METH_VARARGS, "draw some text."},
{"draw_rect", emb_draw_rect, METH_VARARGS, "draw a rect."}, {"draw_rect", emb_draw_rect, METH_VARARGS, "draw a rect."},
{"draw_fillrect", emb_draw_fillrect, METH_VARARGS, "draw a rect."}, {"draw_fillrect", emb_draw_fillrect, METH_VARARGS, "draw a rect."},
{"string_get_width", emb_string_get_width, METH_VARARGS, "get string width."}, {"get_width", emb_get_width, METH_VARARGS, "get string width."},
{"get_mouse", emb_get_mouse, METH_VARARGS, "get mouse status."},
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };
@ -1891,7 +1905,7 @@ int main(int argc, char *argv[])
SDL_AudioSpec fmt; SDL_AudioSpec fmt;
int username_flash = 0, username_flash_t = 1; int username_flash = 0, username_flash_t = 1;
GSPEED = 1; GSPEED = 1;
PyObject *pname,*pmodule,*pfunc,*pvalue,*pargs,*pstep; PyObject *pname,*pmodule,*pfunc,*pvalue,*pargs,*pstep,*pkey;
/* Set 16-bit stereo audio at 22Khz */ /* Set 16-bit stereo audio at 22Khz */
fmt.freq = 22050; fmt.freq = 22050;
@ -1927,14 +1941,23 @@ int main(int argc, char *argv[])
} }
pstep=PyObject_GetAttrString(pmodule,"step");//get the handler function pstep=PyObject_GetAttrString(pmodule,"step");//get the handler function
if(pstep && PyCallable_Check(pfunc))//check if it's really a function if(pstep && PyCallable_Check(pstep))//check if it's really a function
{ {
printf("step function found.\n"); printf("step function found.\n");
} }
else else
{ {
printf("unable to find step function. ignoring.\n"); printf("unable to find step function. ignoring.\n");
//return -1; }
pkey=PyObject_GetAttrString(pmodule,"keypress");//get the handler function
if(pstep && PyCallable_Check(pkey))//check if it's really a function
{
printf("key function found.\n");
}
else
{
printf("unable to find key function. ignoring.\n");
} }
} }
else else
@ -2207,6 +2230,19 @@ int main(int argc, char *argv[])
do_s_check = (do_s_check+1) & 15; do_s_check = (do_s_check+1) & 15;
} }
if(pkey!=NULL && sdl_key!=NULL)
{
pargs=Py_BuildValue("(c)",sdl_key);
pvalue = PyObject_CallObject(pkey, pargs);
Py_DECREF(pargs);
pargs=NULL;
if(pvalue==NULL)
strcpy(console_error,"failed to execute key code.");
//Py_DECREF(pvalue);
//puts("a");
pvalue=NULL;
}
if (sdl_key=='q' || sdl_key==SDLK_ESCAPE) if (sdl_key=='q' || sdl_key==SDLK_ESCAPE)
{ {
if (confirm_ui(vid_buf, "You are about to quit", "Are you sure you want to quit?", "Quit")) if (confirm_ui(vid_buf, "You are about to quit", "Are you sure you want to quit?", "Quit"))

View File

@ -75,7 +75,17 @@ def _handle(txt):
except Exception as ex: except Exception as ex:
error(ex) error(ex)
#optional key handler:
"""def keypress(key):
try:
_key(key)
except Exception as ex:
error(ex)"""
def _key(key):
print "key %s"%key
#optional step handler: #optional step handler:
"""
def step(): def step():
try: try:
a=handle.glob a=handle.glob
@ -84,7 +94,7 @@ def step():
try: try:
_step() _step()
except Exception as ex: except Exception as ex:
error(ex) error(ex)"""
def _step(): def _step():
try: try:
@ -94,9 +104,20 @@ def _step():
step.i=0 step.i=0
step.txt="!FUCK YEAH!" step.txt="!FUCK YEAH!"
#toggle_pause() #toggle_pause()
mouse=tpt.get_mouse()
step.txt=repr(mouse)
xx=50+abs(25-(step.i%50)) xx=50+abs(25-(step.i%50))
w=tpt.string_get_width(step.txt)+16 w=tpt.get_width(step.txt)+16
tpt.draw_fillrect(xx-8,92,w,32,0,0,0,128) r=0
g=0
b=0
if(mouse[0]>xx-8 and mouse[0]<xx+w-8 and mouse[1]>92 and mouse[1]<92+32):
r=255
if(mouse[2][0]):
g=255
if(mouse[2][2]):
b=255
tpt.draw_fillrect(xx-8,92,w,32,r,g,b,128)
tpt.draw_rect(xx-8,92,w,32,255,255,255) tpt.draw_rect(xx-8,92,w,32,255,255,255)
tpt.draw_text(xx,100,step.txt,255,255,255) tpt.draw_text(xx,100,step.txt,255,255,255)
step.i+=1 step.i+=1