Skip to content

Commit 552f286

Browse files
committed
add draw.setcursor() to change the mouse cursor
the function expects a table replicating the Cursor C struct: { offset = { x = INT, y = INT }, clr = INT[32], set = INT[32] } If the function is called with no parameter or nil it will reset the cursor to its default.
1 parent afebaba commit 552f286

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

a.h

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <draw.h>
44
#include <keyboard.h>
55
#include <event.h>
6+
#include <cursor.h>
67
#include <plumb.h>
78
#include <lua.h>
89
#include <lauxlib.h>

draw.c

+50
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,55 @@ lreadimage(lua_State *L)
580580
return 1;
581581
}
582582

583+
static int
584+
lsetcursor(lua_State *L)
585+
{
586+
Cursor c;
587+
int n, i;
588+
589+
if(lua_isnoneornil(L, 1)){
590+
esetcursor(nil);
591+
return 0;
592+
}
593+
lua_pushstring(L, "offset");
594+
lua_gettable(L, 1);
595+
lua_pushstring(L, "x");
596+
lua_gettable(L, -2);
597+
c.offset.x = luaL_checkinteger(L, -1);
598+
lua_pushstring(L, "y");
599+
lua_gettable(L, -3);
600+
c.offset.y = luaL_checkinteger(L, -1);
601+
lua_pop(L, 3); /* remove table|x|y */
602+
lua_pushstring(L, "clr");
603+
lua_gettable(L, 1);
604+
n = luaL_len(L, 2);
605+
if(n != 32){
606+
lua_pushfstring(L, "clr table has len %d but should be 32", n);
607+
return lua_error(L);
608+
}
609+
for(i = 1; i <= n; i++){
610+
lua_geti(L, 2, i);
611+
c.clr[i-1] = (uchar)luaL_checkinteger(L, 3); /* FIXME: overflow !!! */
612+
lua_pop(L, 1);
613+
}
614+
lua_pop(L, 1); /* remove clr table */
615+
lua_pushstring(L, "set");
616+
lua_gettable(L, 1);
617+
n = luaL_len(L, 2);
618+
if(n != 32){
619+
lua_pushfstring(L, "set table has len %d but should be 32", n);
620+
return lua_error(L);
621+
}
622+
for(i = 1; i <= n; i++){
623+
lua_geti(L, 2, i);
624+
c.set[i-1] = (uchar)luaL_checkinteger(L, 3); /* FIXME: overflow !!! */
625+
lua_pop(L, 1);
626+
}
627+
lua_pop(L, 1); /* remove set table */
628+
esetcursor(&c);
629+
return 0;
630+
}
631+
583632
static const struct luaL_Reg libdraw [] = {
584633
{ "init", linitdraw },
585634
{ "draw", ldraw },
@@ -612,6 +661,7 @@ static const struct luaL_Reg libdraw [] = {
612661
{ "allocimage", lallocimage },
613662
{ "allocimagemix", lallocimagemix },
614663
{ "readimage", lreadimage },
664+
{ "setcursor", lsetcursor },
615665
{ NULL, NULL }
616666
};
617667

0 commit comments

Comments
 (0)