summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLieuwe <lieuwemo@gmail.com>2011-03-05 19:08:41 (GMT)
committer Lieuwe <lieuwemo@gmail.com>2011-03-05 19:08:41 (GMT)
commitd6d661c4343b135cff5153dc2e63d8d16fea2dcf (patch)
tree5d772cd1e93a305fdbf80dae4a52d13be8cb5abc /src
parent20b6a64228061a0b4689fcad439871e38ec88038 (diff)
downloadpowder-d6d661c4343b135cff5153dc2e63d8d16fea2dcf.zip
powder-d6d661c4343b135cff5153dc2e63d8d16fea2dcf.tar.gz
keyboard/mouse handling
Diffstat (limited to 'src')
-rw-r--r--src/main.c50
-rw-r--r--src/python/tpt_console.py27
2 files changed, 67 insertions, 10 deletions
diff --git a/src/main.c b/src/main.c
index 04ba232..decb44c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1817,14 +1817,27 @@ emb_draw_fillrect(PyObject *self, PyObject *args)
return Py_BuildValue("i",-1);
}
//int textwidth(char *s)
-emb_string_get_width(PyObject *self, PyObject *args)
+emb_get_width(PyObject *self, PyObject *args)
{
char *txt;
- if(!PyArg_ParseTuple(args, "s:",&txt))
+ if(!PyArg_ParseTuple(args, "s:get_width",&txt))
return NULL;
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!
{"create", emb_create, METH_VARARGS|METH_KEYWORDS, "create a particle."},
{"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_rect", emb_draw_rect, 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}
};
@@ -1891,7 +1905,7 @@ int main(int argc, char *argv[])
SDL_AudioSpec fmt;
int username_flash = 0, username_flash_t = 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 */
fmt.freq = 22050;
@@ -1927,14 +1941,23 @@ int main(int argc, char *argv[])
}
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");
}
else
{
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
@@ -2206,7 +2229,20 @@ int main(int argc, char *argv[])
}
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 (confirm_ui(vid_buf, "You are about to quit", "Are you sure you want to quit?", "Quit"))
diff --git a/src/python/tpt_console.py b/src/python/tpt_console.py
index eb70363..7adfeb8 100644
--- a/src/python/tpt_console.py
+++ b/src/python/tpt_console.py
@@ -75,7 +75,17 @@ def _handle(txt):
except Exception as 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:
+
+"""
def step():
try:
a=handle.glob
@@ -84,7 +94,7 @@ def step():
try:
_step()
except Exception as ex:
- error(ex)
+ error(ex)"""
def _step():
try:
@@ -94,9 +104,20 @@ def _step():
step.i=0
step.txt="!FUCK YEAH!"
#toggle_pause()
+ mouse=tpt.get_mouse()
+ step.txt=repr(mouse)
xx=50+abs(25-(step.i%50))
- w=tpt.string_get_width(step.txt)+16
- tpt.draw_fillrect(xx-8,92,w,32,0,0,0,128)
+ w=tpt.get_width(step.txt)+16
+ 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_text(xx,100,step.txt,255,255,255)
step.i+=1