static void ref_parent(lua_State *L, int index, int parent) { get_reftable(L, index); lua_pushvalue(L, parent); lua_rawseti(L, -2, 0); // set self to uservalue[0] (parent) lua_pop(L, 1); }
static int lgetmaterial(lua_State *L) { struct sprite *s = self(L); if (s->material) { get_reftable(L,1); lua_getfield(L, -1, "material"); return 1; } return 0; }
static int lgetusrdata(lua_State *L) { get_reftable(L, 1); lua_rawgetp(L, -1, &ud_key); if (lua_isnoneornil(L, -1)) { lua_newtable(L); lua_pushvalue(L, -1); //sprite, uservalue, nil, table, table lua_rawsetp(L, 2, &ud_key); } return 1; }
static int lsetprogram(lua_State *L) { struct sprite *s = self(L); if (lua_isnoneornil(L,2)) { s->t.program = PROGRAM_DEFAULT; } else { s->t.program = (int)luaL_checkinteger(L,2); } if (s->material) { s->material = NULL; get_reftable(L, 1); lua_pushnil(L); lua_setfield(L, -2, "material"); } return 0; }
static int lset_anchor_particle(lua_State *L) { struct sprite *s = self(L); if (s->type != TYPE_ANCHOR) return luaL_error(L, "need a anchor"); // ref the ps object and pic to anchor object get_reftable(L, 1); lua_pushvalue(L, 2); lua_rawseti(L, -2, 0); lua_pop(L, 1); s->data.anchor->ps = (struct particle_system*)lua_touserdata(L, 2); struct sprite *p = (struct sprite *)lua_touserdata(L, 3); if (p==NULL || p->type != TYPE_PICTURE) return luaL_error(L, "need a picture sprite"); s->data.anchor->pic = p->s.pic; return 0; }
static int lnewmaterial(lua_State *L) { struct sprite *s = self(L); int sz = sprite_material_size(s); if (sz == 0) return luaL_error(L, "The program has not material"); get_reftable(L, 1); lua_createtable(L, 0, 1); void * m = lua_newuserdata(L, sz); // sprite, uservalue, table, matertial s->material = (struct material*)m; material_init(m, sz, s->t.program); lua_setfield(L, -2, "__obj"); lua_pushvalue(L, -1); // sprite, uservalue, table, table lua_setfield(L, -3, "material"); lua_pushinteger(L, s->t.program); return 2; // return table, program }
static int lgetuserdata(lua_State *L){ struct particle_system *ps = (struct particle_system *)lua_touserdata(L, 1); get_reftable(L, 1); return 1; }
static int lsettext(lua_State *L) { struct sprite *s = self(L); if (s->type != TYPE_LABEL) { return luaL_error(L, "Only label can set rich text"); } if (lua_isnoneornil(L, 2)) { s->data.rich_text = NULL; get_reftable(L, 1); lua_pushnil(L); //sprite, nil, uservalue, nil lua_setfield(L, -2, "richtext"); return 0; } /* if (lua_isstring(L, 2)) { s->data.rich_text = (struct rich_text*)lua_newuserdata(L, sizeof(struct rich_text)); s->data.rich_text->text = lua_tostring(L, 2); s->data.rich_text->count = 0; s->data.rich_text->width = 0; s->data.rich_text->height = 0; s->data.rich_text->fields = NULL; lua_createtable(L, 2, 0); lua_pushvalue(L, 2); lua_rawseti(L, -2, 1); lua_pushvalue(L, 3); lua_rawseti(L, -2, 2); lua_setuservalue(L, 1); return 0; }*/ s->data.rich_text = NULL; if (!lua_istable(L, 2) || lua_rawlen(L, 2) != 4) { return luaL_error(L, "rich text must has a table with two items"); } lua_rawgeti(L, 2, 1); const char *txt = luaL_checkstring(L, -1); lua_pop(L, 1); lua_rawgeti(L, 2, 2); int cnt = lua_rawlen(L, -1); lua_pop(L, 1); struct rich_text *rich = (struct rich_text*)lua_newuserdata(L, sizeof(struct rich_text)); rich->text = txt; rich->count = cnt; lua_rawgeti(L, 2, 3); rich->width = luaL_checkinteger(L, -1); lua_pop(L, 1); lua_rawgeti(L, 2, 4); rich->height = luaL_checkinteger(L, -1); lua_pop(L, 1); int size = cnt * sizeof(struct label_field); rich->fields = (struct label_field*)lua_newuserdata(L, size); struct label_field *fields = rich->fields; int i; lua_rawgeti(L, 2, 2); for (i=0; i<cnt; i++) { lua_rawgeti(L, -1, i+1); if (!lua_istable(L,-1)) { return luaL_error(L, "rich text unit must be table"); } lua_rawgeti(L, -1, 1); //start ((struct label_field*)(fields+i))->start = luaL_checkinteger(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 2); //end ((struct label_field*)(fields+i))->end = luaL_checkinteger(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 3); //type uint32_t type = luaL_checkinteger(L, -1); ((struct label_field*)(fields+i))->type = type; lua_pop(L, 1); lua_rawgeti(L, -1, 4); //val if (type == RL_COLOR) { ((struct label_field*)(fields+i))->color = luaL_checkinteger(L, -1); } else { ((struct label_field*)(fields+i))->val = luaL_checkinteger(L, -1); } lua_pop(L, 1); //extend here lua_pop(L, 1); } lua_pop(L, 1); get_reftable(L, 1); lua_createtable(L,3,0); //sprite, table, userdata, userdata, uservalue, table //userdata of rich_text lua_pushvalue(L, 3); lua_rawseti(L, -2, 1); //userdata of fields lua_pushvalue(L, 4); lua_rawseti(L, -2, 2); //string lua_rawgeti(L, 2, 1); lua_rawseti(L, -2, 3); lua_setfield(L, -2, "richtext"); s->data.rich_text = rich; return 0; }