22#include "land_outfits.h"
24#include "nlua_colour.h"
26#include "nlua_outfit.h"
29#include "tk/toolkit_priv.h"
32#define TK_CUSTOMDONE "__customDone"
33typedef struct custom_functions_s {
44static int cust_update(
double dt,
void* data );
45static void cust_render(
double x,
double y,
double w,
double h,
void* data );
46static int cust_event(
unsigned int wid, SDL_Event *event,
void* data );
47static int cust_key( SDL_Keycode key, SDL_Keymod mod,
int pressed,
int isrepeat,
custom_functions_t *cf );
49static int cust_mouse(
int type,
int button,
double x,
double y,
custom_functions_t *cf );
50static int cust_event_window( SDL_WindowEventID event, Sint32 w, Sint32 h,
custom_functions_t *cf );
55static int tkL_msg( lua_State *L );
145 const char *wdwname, *wgtname;
147 int bx, by, x, y, w, h;
148 wdwname = luaL_checkstring( L, 1 );
149 wgtname = luaL_optstring( L, 2, NULL );
154 if (wgtname == NULL) {
165 lua_pushinteger( L, x );
166 lua_pushinteger( L, y );
167 lua_pushinteger( L, w );
168 lua_pushinteger( L, h );
188 const char *title = luaL_checkstring(L,1);
189 const char *str = luaL_checkstring(L,2);
191 if (lua_gettop(L) > 2) {
193 const char *img = luaL_checkstring(L,3);
196 width = (lua_gettop(L) < 4) ? -1 : luaL_checkinteger(L,4);
197 height = (lua_gettop(L) < 5) ? -1 : luaL_checkinteger(L,5);
217 const char *title = luaL_checkstring(L,1);
218 const char *str = luaL_checkstring(L,2);
221 lua_pushboolean(L,ret);
238 const char *title, *str;
242 title = luaL_checkstring(L,1);
243 min = luaL_checkint(L,2);
244 max = luaL_checkint(L,3);
245 str = luaL_checkstring(L,4);
249 lua_pushstring(L, ret);
272 const char *title, *str;
276 opts = lua_gettop(L) - 2;
277 title = luaL_checkstring(L,1);
278 str = luaL_checkstring(L,2);
281 for (
int i=0; i<opts; i++)
282 luaL_checkstring(L, i+3);
286 for (
int i=0; i<opts; i++)
294 for (
int i=0; i<opts && ret==-1; i++) {
295 if (strcmp(result, luaL_checkstring(L,i+3)) == 0)
300 lua_pushnumber(L,ret);
301 lua_pushstring(L,result);
324 const char *title, *str;
329 opts = lua_gettop(L) - 2;
330 title = luaL_checkstring(L,1);
331 str = luaL_checkstring(L,2);
334 for (
int i=0; i<opts; i++)
335 luaL_checkstring(L, i+3);
338 choices = malloc(
sizeof(
char*) * opts );
339 for (
int i=0; i<opts; i++)
340 choices[i] = strdup( luaL_checkstring(L, i+3) );
349 lua_pushnumber(L, ret+1);
350 lua_pushstring(L, choices[ret]);
373 name = luaL_checkstring(L,1);
375 if (!lua_istable(L,2))
376 NLUA_INVALID_PARAMETER(L,2);
381 while (lua_next(L, -2) != 0) {
387 if (SCREEN_W < LAND_WIDTH || SCREEN_H < LAND_HEIGHT) {
392 w = LAND_WIDTH + 0.5 * (SCREEN_W - LAND_WIDTH);
393 h = LAND_HEIGHT + 0.5 * (SCREEN_H - LAND_HEIGHT);
395 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);
426#define GETFUNC( address, pos ) \
428 lua_pushvalue( L, (pos) ); \
429 luaL_checktype(L, -1, LUA_TFUNCTION); \
430 (address) = luaL_ref(L, LUA_REGISTRYINDEX); \
432 GETFUNC( cf->update, 4 );
433 GETFUNC( cf->draw, 5 );
434 GETFUNC( cf->keyboard, 6 );
435 GETFUNC( cf->mouse, 7 );
436 GETFUNC( cf->resize, 8 );
437 GETFUNC( cf->textinput, 9 );
439 nodynamic = lua_toboolean(L,10);
442 lua_pushboolean(L, 0);
443 lua_setglobal(L, TK_CUSTOMDONE );
446 dialogue_custom( caption, w, h, cust_update, cust_render, cust_event, cf, 1, !nodynamic );
450 luaL_unref(L, LUA_REGISTRYINDEX, cf->update);
451 luaL_unref(L, LUA_REGISTRYINDEX, cf->draw);
452 luaL_unref(L, LUA_REGISTRYINDEX, cf->keyboard);
453 luaL_unref(L, LUA_REGISTRYINDEX, cf->mouse);
454 luaL_unref(L, LUA_REGISTRYINDEX, cf->resize);
455 luaL_unref(L, LUA_REGISTRYINDEX, cf->textinput);
468 const char *s = luaL_checkstring(L,1);
471 return NLUA_ERROR(L, _(
"custom dialogue not open"));
484 int enable = lua_toboolean(L,1);
487 return NLUA_ERROR(L, _(
"custom dialogue not open"));
504 return NLUA_ERROR(L, _(
"custom dialogue not open"));
505 w = luaL_checkinteger(L,1);
506 h = luaL_checkinteger(L,2);
523 return NLUA_ERROR(L, _(
"custom dialogue not open"));
525 lua_pushinteger(L,w);
526 lua_pushinteger(L,h);
538 return NLUA_ERROR(L, _(
"custom dialogue not open"));
539 lua_pushboolean(L, 1);
540 lua_setglobal(L, TK_CUSTOMDONE );
544static int cust_pcall( lua_State *L,
int nargs,
int nresults )
549 errf = lua_gettop(L) - nargs;
556 ret = lua_pcall( L, nargs, nresults, errf );
564static int cust_update(
double dt,
void* data )
570 lua_State *L = cf->
L;
571 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->update);
572 lua_pushnumber(L, dt);
573 if (cust_pcall( L, 1, 0 )) {
575 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
580 lua_getglobal(L, TK_CUSTOMDONE );
581 ret = lua_toboolean(L, -1);
585static void cust_render(
double x,
double y,
double w,
double h,
void* data )
590 lua_State *L = cf->
L;
591 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->draw);
592 lua_pushnumber(L, x);
593 lua_pushnumber(L, y);
594 lua_pushnumber(L, w);
595 lua_pushnumber(L, h);
596 if (cust_pcall( L, 4, 0 )) {
598 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
602static int cust_event(
unsigned int wid, SDL_Event *event,
void* data )
611 switch (event->type) {
612 case SDL_MOUSEBUTTONDOWN:
615 return cust_mouse( 1, event->button.button, event->button.x+x, event->button.y+y, cf );
616 case SDL_MOUSEBUTTONUP:
619 return cust_mouse( 2, event->button.button, event->button.x+x, event->button.y+y, cf );
620 case SDL_MOUSEMOTION:
623 return cust_mouse( 3, -1, event->button.x+x, event->button.y+y, cf );
625 return cust_mouse( 4, -1, event->wheel.x, event->wheel.y, cf );
628 return cust_key( event->key.keysym.sym, event->key.keysym.mod, 1, event->key.repeat, cf );
630 return cust_key( event->key.keysym.sym, event->key.keysym.mod, 0, event->key.repeat, cf );
633 return cust_text( event->text.text, cf );
635 case SDL_WINDOWEVENT:
636 return cust_event_window( event->window.event, event->window.data1, event->window.data2, cf );
644static int cust_key( SDL_Keycode key, SDL_Keymod mod,
int pressed,
int isrepeat,
custom_functions_t *cf )
647 lua_State *L = cf->
L;
648 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->keyboard);
649 lua_pushboolean(L, pressed);
650 lua_pushstring(L, SDL_GetKeyName(key));
652 lua_pushboolean(L, isrepeat);
653 if (cust_pcall( L, 4, 1 )) {
655 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
659 b = lua_toboolean(L, -1);
666 lua_State *L = cf->
L;
667 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->textinput);
668 lua_pushstring(L, str);
669 if (cust_pcall( L, 1, 1 )) {
671 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
675 b = lua_toboolean(L, -1);
679static int cust_mouse(
int type,
int button,
double x,
double y,
custom_functions_t *cf )
682 lua_State *L = cf->
L;
683 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->mouse);
684 lua_pushnumber(L, x);
685 lua_pushnumber(L, y);
686 lua_pushnumber(L, type);
689 case SDL_BUTTON_LEFT: button=1;
break;
690 case SDL_BUTTON_RIGHT: button=2;
break;
691 default: button=3;
break;
693 lua_pushnumber(L, button);
696 if (cust_pcall( L, nargs, 1 )) {
698 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
702 b = lua_toboolean(L, -1);
706static int cust_event_window( SDL_WindowEventID event, Sint32 w, Sint32 h,
custom_functions_t *cf )
709 lua_State *L = cf->
L;
711 if (event == SDL_WINDOWEVENT_RESIZED)
714 lua_rawgeti(L, LUA_REGISTRYINDEX, cf->resize);
715 lua_pushinteger(L, w );
716 lua_pushinteger(L, h );
717 if (cust_pcall( L, 2, 1 )) {
719 WARN(_(
"Custom dialogue internal error: %s"), lua_tostring(L,-1));
723 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.
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, int dynamic)
Opens a custom dialogue window.
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_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, int blackmarket)
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_refresh(lua_State *L)
Forces the toolkit to rerender the screen.
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.