22#include "land_outfits.h"
24#include "nlua_colour.h"
26#include "nlua_outfit.h"
31#define TK_CUSTOMDONE "__customDone"
32typedef struct custom_functions_s {
43static int cust_update(
double dt,
void* data );
44static void cust_render(
double x,
double y,
double w,
double h,
void* data );
45static int cust_event(
unsigned int wid, SDL_Event *event,
void* data );
46static int cust_key( SDL_Keycode key, SDL_Keymod mod,
int pressed,
custom_functions_t *cf );
48static int cust_mouse(
int type,
int button,
double x,
double y,
custom_functions_t *cf );
49static int cust_event_window( SDL_WindowEventID event, Sint32 w, Sint32 h,
custom_functions_t *cf );
54static int tkL_msg( lua_State *L );
142 const char *wdwname, *wgtname;
144 int bx, by, x, y, w, h;
145 wdwname = luaL_checkstring( L, 1 );
146 wgtname = luaL_optstring( L, 2, NULL );
151 if (wgtname == NULL) {
162 lua_pushinteger( L, x );
163 lua_pushinteger( L, y );
164 lua_pushinteger( L, w );
165 lua_pushinteger( L, h );
184 const char *title, *str, *img;
188 title = luaL_checkstring(L,1);
189 str = luaL_checkstring(L,2);
191 if (lua_gettop(L) > 2) {
192 img = luaL_checkstring(L,3);
195 width = (lua_gettop(L) < 4) ? -1 : luaL_checkinteger(L,4);
196 height = (lua_gettop(L) < 5) ? -1 : luaL_checkinteger(L,5);
217 const char *title, *str;
219 title = luaL_checkstring(L,1);
220 str = luaL_checkstring(L,2);
223 lua_pushboolean(L,ret);
240 const char *title, *str;
244 title = luaL_checkstring(L,1);
245 min = luaL_checkint(L,2);
246 max = luaL_checkint(L,3);
247 str = luaL_checkstring(L,4);
251 lua_pushstring(L, ret);
274 const char *title, *str;
278 opts = lua_gettop(L) - 2;
279 title = luaL_checkstring(L,1);
280 str = luaL_checkstring(L,2);
283 for (
int i=0; i<opts; i++)
284 luaL_checkstring(L, i+3);
288 for (
int i=0; i<opts; i++)
296 for (
int i=0; i<opts && ret==-1; i++) {
297 if (strcmp(result, luaL_checkstring(L,i+3)) == 0)
302 lua_pushnumber(L,ret);
303 lua_pushstring(L,result);
326 const char *title, *str;
331 opts = lua_gettop(L) - 2;
332 title = luaL_checkstring(L,1);
333 str = luaL_checkstring(L,2);
336 for (
int i=0; i<opts; i++)
337 luaL_checkstring(L, i+3);
340 choices = malloc(
sizeof(
char*) * opts );
341 for (
int i=0; i<opts; i++)
342 choices[i] = strdup( luaL_checkstring(L, i+3) );
351 lua_pushnumber(L, ret+1);
352 lua_pushstring(L, choices[ret]);
374 name = luaL_checkstring(L,1);
376 if (!lua_istable(L,2))
377 NLUA_INVALID_PARAMETER(L);
382 while (lua_next(L, -2) != 0) {
388 if (SCREEN_W < LAND_WIDTH || SCREEN_H < LAND_HEIGHT) {
393 w = LAND_WIDTH + 0.5 * (SCREEN_W - LAND_WIDTH);
394 h = LAND_HEIGHT + 0.5 * (SCREEN_H - LAND_HEIGHT);
396 wid =
window_create(
"wdwMerchantOutfit", name, -1, -1, w, h );
418 int w = luaL_checkinteger(L, 2);
419 int h = luaL_checkinteger(L, 3);
420 const char *caption = luaL_checkstring(L, 1);
425#define GETFUNC( address, pos ) \
427 lua_pushvalue( L, (pos) ); \
428 luaL_checktype(L, -1, LUA_TFUNCTION); \
429 (address) = luaL_ref(L, LUA_REGISTRYINDEX); \
431 GETFUNC( cf->update, 4 );
432 GETFUNC( cf->draw, 5 );
433 GETFUNC( cf->keyboard, 6 );
434 GETFUNC( cf->mouse, 7 );
435 GETFUNC( cf->resize, 8 );
436 GETFUNC( cf->textinput, 9 );
440 lua_pushboolean(L, 0);
441 lua_setglobal(L, TK_CUSTOMDONE );
444 dialogue_custom( caption, w, h, cust_update, cust_render, cust_event, cf, 1 );
448 luaL_unref(L, LUA_REGISTRYINDEX, cf->update);
449 luaL_unref(L, LUA_REGISTRYINDEX, cf->draw);
450 luaL_unref(L, LUA_REGISTRYINDEX, cf->keyboard);
451 luaL_unref(L, LUA_REGISTRYINDEX, cf->mouse);
452 luaL_unref(L, LUA_REGISTRYINDEX, cf->resize);
453 luaL_unref(L, LUA_REGISTRYINDEX, cf->textinput);
466 const char *s = luaL_checkstring(L,1);
469 NLUA_ERROR(L, _(
"custom dialogue not open"));
482 int enable = lua_toboolean(L,1);
485 NLUA_ERROR(L, _(
"custom dialogue not open"));
502 NLUA_ERROR(L, _(
"custom dialogue not open"));
503 w = luaL_checkinteger(L,1);
504 h = luaL_checkinteger(L,2);
521 NLUA_ERROR(L, _(
"custom dialogue not open"));
523 lua_pushinteger(L,w);
524 lua_pushinteger(L,h);
536 NLUA_ERROR(L, _(
"custom dialogue not open"));
537 lua_pushboolean(L, 1);
538 lua_setglobal(L, TK_CUSTOMDONE );
542static int cust_pcall( lua_State *L,
int nargs,
int nresults )
547 errf = lua_gettop(L) - nargs;
554 ret = lua_pcall( L, nargs, nresults, errf );
562static int cust_update(
double dt,
void* data )
568 lua_State *L = cf->
L;
569 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->update);
570 lua_pushnumber(L, dt);
571 if (cust_pcall( L, 1, 0 )) {
573 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
578 lua_getglobal(L, TK_CUSTOMDONE );
579 ret = lua_toboolean(L, -1);
583static void cust_render(
double x,
double y,
double w,
double h,
void* data )
588 lua_State *L = cf->
L;
589 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->draw);
590 lua_pushnumber(L, x);
591 lua_pushnumber(L, y);
592 lua_pushnumber(L, w);
593 lua_pushnumber(L, h);
594 if (cust_pcall( L, 4, 0 )) {
596 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
600static int cust_event(
unsigned int wid, SDL_Event *event,
void* data )
609 switch (event->type) {
610 case SDL_MOUSEBUTTONDOWN:
613 return cust_mouse( 1, event->button.button, event->button.x+x, event->button.y+y, cf );
614 case SDL_MOUSEBUTTONUP:
617 return cust_mouse( 2, event->button.button, event->button.x+x, event->button.y+y, cf );
618 case SDL_MOUSEMOTION:
621 return cust_mouse( 3, -1, event->button.x+x, event->button.y+y, cf );
623 return cust_mouse( 4, -1, event->wheel.x, event->wheel.y, cf );
626 return cust_key( event->key.keysym.sym, event->key.keysym.mod, 1, cf );
628 return cust_key( event->key.keysym.sym, event->key.keysym.mod, 0, cf );
631 return cust_text( event->text.text, cf );
633 case SDL_WINDOWEVENT:
634 return cust_event_window( event->window.event, event->window.data1, event->window.data2, cf );
642static int cust_key( SDL_Keycode key, SDL_Keymod mod,
int pressed,
custom_functions_t *cf )
645 lua_State *L = cf->
L;
646 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->keyboard);
647 lua_pushboolean(L, pressed );
648 lua_pushstring(L, SDL_GetKeyName(key));
650 if (cust_pcall( L, 3, 1 )) {
652 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
656 b = lua_toboolean(L, -1);
663 lua_State *L = cf->
L;
664 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->textinput);
665 lua_pushstring(L, str);
666 if (cust_pcall( L, 1, 1 )) {
668 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
672 b = lua_toboolean(L, -1);
676static int cust_mouse(
int type,
int button,
double x,
double y,
custom_functions_t *cf )
679 lua_State *L = cf->
L;
680 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->mouse);
681 lua_pushnumber(L, x);
682 lua_pushnumber(L, y);
683 lua_pushnumber(L, type);
686 case SDL_BUTTON_LEFT: button=1;
break;
687 case SDL_BUTTON_RIGHT: button=2;
break;
688 default: button=3;
break;
690 lua_pushnumber(L, button);
693 if (cust_pcall( L, nargs, 1 )) {
695 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
699 b = lua_toboolean(L, -1);
703static int cust_event_window( SDL_WindowEventID event, Sint32 w, Sint32 h,
custom_functions_t *cf )
706 lua_State *L = cf->
L;
708 if (event == SDL_WINDOWEVENT_RESIZED)
711 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->resize);
712 lua_pushinteger(L, w );
713 lua_pushinteger(L, h );
714 if (cust_pcall( L, 2, 1 )) {
716 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
720 b = lua_toboolean(L, -1);
Provides macros to work with dynamic arrays.
#define array_create_size(basic_type, capacity)
Creates a new dynamic array of ‘basic_type’ with an initial capacity.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
char * dialogue_runChoice(void)
Run the dialog and return the clicked string.
char * dialogue_inputRaw(const char *title, int min, int max, const char *msg)
Creates a dialogue that allows the player to write a message.
int dialogue_listRaw(const char *title, char **items, int nitems, const char *msg)
Creates a list dialogue with OK and Cancel button.
int dialogue_YesNoRaw(const char *caption, const char *msg)
Runs a dialogue with both yes and no options.
void dialogue_custom(const char *caption, int width, int height, int(*update)(double dt, void *data), void(*render)(double x, double y, double w, double h, void *data), int(*event)(unsigned int wid, SDL_Event *event, void *data), void *data, int autofree)
Opens a custom dialogue window.
void dialogue_addChoice(const char *caption, const char *msg, const char *opt)
Add a choice to the dialog.
int dialogue_customResize(int width, int height)
Resizes a custom dialogue.
void dialogue_msgImgRaw(const char *caption, const char *msg, const char *img, int width, int height)
Opens a dialogue window with an ok button, a fixed message and an image.
void dialogue_makeChoice(const char *caption, const char *msg, int opts)
Create the choice dialog. Need to add choices with below method.
void dialogue_msgRaw(const char *caption, const char *msg)
Opens a dialogue window with an ok button and a fixed message.
int dialogue_customFullscreen(int enable)
Converts a custom dialogue to fullscreen.
void outfits_open(unsigned int wid, const Outfit **outfits)
Opens the outfit exchange center window.
Header file with generic functions and naev-specifics.
int nlua_errTrace(lua_State *L)
Gets a trace from Lua.
int nlua_loadCol(nlua_env env)
Loads the colour library.
int nlua_loadGFX(nlua_env env)
Loads the graphics library.
const Outfit * luaL_validoutfit(lua_State *L, int ind)
Makes sure the outfit is valid or raises a Lua error.
static int tkL_customSize(lua_State *L)
Gets the size of a custom widget.
static int tkL_customRename(lua_State *L)
Renames the custom widget window.
static int tkL_isOpen(lua_State *L)
Bindings for interacting with the Toolkit.
static const luaL_Reg tkL_methods[]
static int tkL_msg(lua_State *L)
Creates a window with an ok button, and optionally an image.
int nlua_loadTk(nlua_env env)
Loads the Toolkit Lua library.
static int tkL_input(lua_State *L)
Creates a window that allows player to write text input.
static int tkL_choice(lua_State *L)
Creates a window with a number of selectable options.
static int tkL_query(lua_State *L)
Gets the position and dimensions of either a window or a widget.
static int tkL_yesno(lua_State *L)
Displays a window with Yes and No buttons.
static int tkL_customFullscreen(lua_State *L)
Sets the custom widget fullscreen.
static int tkL_list(lua_State *L)
Creates a window with an embedded list of choices.
static int tkL_customDone(lua_State *L)
Ends the execution of a custom widget.
static int tkL_customResize(lua_State *L)
Sets the custom widget fullscreen.
static int tkL_merchantOutfit(lua_State *L)
Opens an outfit merchant window.
static int tkL_custom(lua_State *L)
Creates a custom widget window.
A ship outfit, depends radically on the type.