naev 0.10.4
Data Structures | Functions | Variables
dialogue.c File Reference

Is a high-level API around toolkit.c for easy window creation. More...

#include "dialogue.h"
#include "input.h"
#include "log.h"
#include "conf.h"
#include "menu.h"
#include "ndata.h"
#include "nstring.h"
#include "opengl.h"
#include "pause.h"
#include "toolkit.h"

Go to the source code of this file.

Data Structures

struct  dialogue_update_t
 
struct  dialogue_custom_data_s
 
struct  InputDialogue
 Used to store information for input dialogues. More...
 

Functions

static int dialogue_custom_event (unsigned int wid, SDL_Event *event)
 
void main_loop (int update)
 Split main loop from main() for secondary loop hack in toolkit.c. More...
 
static void dialogue_close (unsigned int wid, const char *str)
 Generic window close. More...
 
static void dialogue_cancel (unsigned int wid, const char *str)
 Generic window cancel. More...
 
static glFontdialogue_getSize (const char *title, const char *msg, int *width, int *height)
 Gets the size needed for the dialogue. More...
 
static void dialogue_YesNoClose (unsigned int wid, const char *str)
 Closes a yesno dialogue. More...
 
static void dialogue_inputClose (unsigned int wid, const char *str)
 Closes an input dialogue. More...
 
static void dialogue_choiceClose (unsigned int wid, const char *str)
 Closes a choice dialogue. More...
 
static void dialogue_listClose (unsigned int wid, const char *str)
 
static void dialogue_listCancel (unsigned int wid, const char *str)
 
static int toolkit_loop (int *loop_done, dialogue_update_t *du)
 Creates a secondary loop until loop_done is set to 1 or the toolkit closes. More...
 
static void select_call_wrapper (unsigned int wid, const char *wgtname)
 used to pass appropriate information to the method that handles updating the extra information area in the dialogue listpanel. More...
 
int dialogue_isOpen (void)
 Checks to see if a dialogue is open. More...
 
void dialogue_alert (const char *fmt,...)
 Displays an alert popup with only an ok button and a message. More...
 
void dialogue_msg (const char *caption, const char *fmt,...)
 Opens a dialogue window with an ok button and a message. More...
 
void dialogue_msgImg (const char *caption, const char *img, const char *fmt,...)
 Opens a dialogue window with an ok button, a message and an image. More...
 
void dialogue_msgRaw (const char *caption, const char *msg)
 Opens a dialogue window with an ok button and a fixed message. More...
 
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. More...
 
int dialogue_YesNo (const char *caption, const char *fmt,...)
 Runs a dialogue with both yes and no options. More...
 
int dialogue_YesNoRaw (const char *caption, const char *msg)
 Runs a dialogue with both yes and no options. More...
 
char * dialogue_input (const char *title, int min, int max, const char *fmt,...)
 Creates a dialogue that allows the player to write a message. More...
 
char * dialogue_inputRaw (const char *title, int min, int max, const char *msg)
 Creates a dialogue that allows the player to write a message. More...
 
int dialogue_list (const char *title, char **items, int nitems, const char *fmt,...)
 Creates a list dialogue with OK and Cancel button with a fixed message. More...
 
int dialogue_listRaw (const char *title, char **items, int nitems, const char *msg)
 Creates a list dialogue with OK and Cancel button. More...
 
int dialogue_listPanel (const char *title, char **items, int nitems, int extrawidth, int minheight, void(*add_widgets)(unsigned int wid, int x, int y, int w, int h), void(*select_call)(unsigned int wid, const char *wgtname, int x, int y, int w, int h), const char *fmt,...)
 Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra area for the list to react to item selected events. More...
 
int dialogue_listPanelRaw (const char *title, char **items, int nitems, int extrawidth, int minheight, void(*add_widgets)(unsigned int wid, int x, int y, int w, int h), void(*select_call)(unsigned int wid, const char *wgtname, int x, int y, int w, int h), const char *msg)
 Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra area for the list to react to item selected events. More...
 
void dialogue_makeChoice (const char *caption, const char *msg, int opts)
 Create the choice dialog. Need to add choices with below method. More...
 
void dialogue_addChoice (const char *caption, const char *msg, const char *opt)
 Add a choice to the dialog. More...
 
char * dialogue_runChoice (void)
 Run the dialog and return the clicked string. More...
 
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. More...
 
int dialogue_customFullscreen (int enable)
 Converts a custom dialogue to fullscreen. More...
 
int dialogue_customResize (int width, int height)
 Resizes a custom dialogue. More...
 

Variables

static int dialogue_open
 
static int dlgid = 0
 
static InputDialogue input_dialogue
 
static int dialogue_listSelected = -1
 
static unsigned int choice_wid = 0
 
static char * choice_result
 
static int choice_nopts
 

Detailed Description

Is a high-level API around toolkit.c for easy window creation.

Only one dialogue may be open at once or behaviour is unspecified.

All these dialogues use what I call the secondary main loop hack. Basically they spawn another main loop identical to the primary whose only difference is that it breaks on loop_done. Therefore this loop hijacks the main loop until it's over, making these functions seem to be blocking without really being blocking.

See also
toolkit.c

Definition in file dialogue.c.

Function Documentation

◆ dialogue_addChoice()

void dialogue_addChoice ( const char *  caption,
const char *  msg,
const char *  opt 
)

Add a choice to the dialog.

Parameters
captionCaption to use for the dialogue (for sizing).
msgMessage to display (for sizing).
*optThe value of the option.

Definition at line 770 of file dialogue.c.

◆ dialogue_alert()

void dialogue_alert ( const char *  fmt,
  ... 
)

Displays an alert popup with only an ok button and a message.

Parameters
fmtPrintf style message to display.

Definition at line 132 of file dialogue.c.

◆ dialogue_cancel()

static void dialogue_cancel ( unsigned int  wid,
const char *  str 
)
static

Generic window cancel.

Definition at line 118 of file dialogue.c.

◆ dialogue_choiceClose()

static void dialogue_choiceClose ( unsigned int  wid,
const char *  str 
)
static

Closes a choice dialogue.

Parameters
widWindow being closed.
strStored to choice_result.

Definition at line 812 of file dialogue.c.

◆ dialogue_close()

static void dialogue_close ( unsigned int  wid,
const char *  str 
)
static

Generic window close.

Definition at line 106 of file dialogue.c.

◆ dialogue_custom()

void dialogue_custom ( const char *  caption,
int  width,
int  height,
int(*)(double dt, void *data)  update,
void(*)(double x, double y, double w, double h, void *data)  render,
int(*)(unsigned int wid, SDL_Event *event, void *data)  event,
void *  data,
int  autofree 
)

Opens a custom dialogue window.

Parameters
captionWindow title.
widthWidth of the widget.
heightHeight of the widget.
updateCustom render callback.
renderCustom render callback.
eventCustom event callback.
dataCustom data.
autofreeShould data be freed when the window is destroyed?

Definition at line 863 of file dialogue.c.

◆ dialogue_custom_event()

static int dialogue_custom_event ( unsigned int  wid,
SDL_Event *  event 
)
static

Definition at line 828 of file dialogue.c.

◆ dialogue_customFullscreen()

int dialogue_customFullscreen ( int  enable)

Converts a custom dialogue to fullscreen.

Parameters
enableWhether or not to enable it.
Returns
0 on success.

Definition at line 927 of file dialogue.c.

◆ dialogue_customResize()

int dialogue_customResize ( int  width,
int  height 
)

Resizes a custom dialogue.

Parameters
widthWidth to set to.
heightHeight to set to.
Returns
0 on success.

Definition at line 964 of file dialogue.c.

◆ dialogue_getSize()

static glFont * dialogue_getSize ( const char *  title,
const char *  msg,
int *  width,
int *  height 
)
static

Gets the size needed for the dialogue.

Parameters
titleTitle of the dialogue.
msgMessage of the dialogue.
[out]widthGets the width needed.
[out]heightGets the height needed.
Returns
The font that matches the size.

Definition at line 168 of file dialogue.c.

◆ dialogue_input()

char * dialogue_input ( const char *  title,
int  min,
int  max,
const char *  fmt,
  ... 
)

Creates a dialogue that allows the player to write a message.

You must free the result if it's not null.

Parameters
titleTitle of the dialogue window.
minMinimum length of the message (must be non-zero).
maxMaximum length of the message (must be non-zero).
fmtPrintf style message to display on the dialogue.
Returns
The message the player typed or NULL if it was cancelled.

Definition at line 436 of file dialogue.c.

◆ dialogue_inputClose()

static void dialogue_inputClose ( unsigned int  wid,
const char *  str 
)
static

Closes an input dialogue.

Parameters
widUnused.
strUnused.

Definition at line 532 of file dialogue.c.

◆ dialogue_inputRaw()

char * dialogue_inputRaw ( const char *  title,
int  min,
int  max,
const char *  msg 
)

Creates a dialogue that allows the player to write a message.

You must free the result if it's not null.

Parameters
titleTitle of the dialogue window.
minMinimum length of the message (must be non-zero).
maxMaximum length of the message (must be non-zero).
msgMessage to be displayed.
Returns
The message the player typed or NULL if it was cancelled.

Definition at line 465 of file dialogue.c.

◆ dialogue_isOpen()

int dialogue_isOpen ( void  )

Checks to see if a dialogue is open.

Definition at line 98 of file dialogue.c.

◆ dialogue_list()

int dialogue_list ( const char *  title,
char **  items,
int  nitems,
const char *  fmt,
  ... 
)

Creates a list dialogue with OK and Cancel button with a fixed message.

Parameters
titleTitle of the dialogue.
itemsItems in the list (should be all malloced, automatically freed).
nitemsNumber of items.
fmtprintf formatted string with text to display.

Definition at line 576 of file dialogue.c.

◆ dialogue_listCancel()

static void dialogue_listCancel ( unsigned int  wid,
const char *  str 
)
static

Definition at line 543 of file dialogue.c.

◆ dialogue_listClose()

static void dialogue_listClose ( unsigned int  wid,
const char *  str 
)
static

Definition at line 548 of file dialogue.c.

◆ dialogue_listPanel()

int dialogue_listPanel ( const char *  title,
char **  items,
int  nitems,
int  extrawidth,
int  minheight,
void(*)(unsigned int wid, int x, int y, int w, int h)  add_widgets,
void(*)(unsigned int wid, const char *wgtname, int x, int y, int w, int h)  select_call,
const char *  fmt,
  ... 
)

Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra area for the list to react to item selected events.

Parameters
titleTitle of the dialogue.
itemsItems in the list (should be all malloced, automatically freed).
nitemsNumber of items.
extrawidthWidth of area to add for select_call callback.
minheightMinimum height for the window.
add_widgetsThis function is called with the new window as an argument allowing for initial population of the extra area.
select_callThis function is called when a new item in the list is selected, receiving the window's id and the selected widgets name as arguments.
fmtprintf formatted string with text to display.

Definition at line 621 of file dialogue.c.

◆ dialogue_listPanelRaw()

int dialogue_listPanelRaw ( const char *  title,
char **  items,
int  nitems,
int  extrawidth,
int  minheight,
void(*)(unsigned int wid, int x, int y, int w, int h)  add_widgets,
void(*)(unsigned int wid, const char *wgtname, int x, int y, int w, int h)  select_call,
const char *  msg 
)

Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra area for the list to react to item selected events.

Parameters
titleTitle of the dialogue.
itemsItems in the list (should be all malloced, automatically freed).
nitemsNumber of items.
extrawidthWidth of area to add for select_call callback.
minheightMinimum height for the window.
add_widgetsThis function is called with the new window as an argument allowing for initial population of the extra area.
select_call(optional) This function is called when a new item in the list is selected, receiving the window's id and the selected widgets name as arguments.
msgstring with text to display.

Definition at line 659 of file dialogue.c.

◆ dialogue_listRaw()

int dialogue_listRaw ( const char *  title,
char **  items,
int  nitems,
const char *  msg 
)

Creates a list dialogue with OK and Cancel button.

Parameters
titleTitle of the dialogue.
itemsItems in the list (should be all malloced, automatically freed).
nitemsNumber of items.
msgstring with text to display.

Definition at line 600 of file dialogue.c.

◆ dialogue_makeChoice()

void dialogue_makeChoice ( const char *  caption,
const char *  msg,
int  opts 
)

Create the choice dialog. Need to add choices with below method.

Parameters
captionCaption to use for the dialogue.
msgMessage to display.
optsThe number of options.

Definition at line 748 of file dialogue.c.

◆ dialogue_msg()

void dialogue_msg ( const char *  caption,
const char *  fmt,
  ... 
)

Opens a dialogue window with an ok button and a message.

Parameters
captionWindow title.
fmtPrintf style message to display.

Definition at line 218 of file dialogue.c.

◆ dialogue_msgImg()

void dialogue_msgImg ( const char *  caption,
const char *  img,
const char *  fmt,
  ... 
)

Opens a dialogue window with an ok button, a message and an image.

Parameters
captionWindow title.
imgPath of the image file to display.
fmtPrintf style message to display.

Definition at line 240 of file dialogue.c.

◆ dialogue_msgImgRaw()

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.

Parameters
captionWindow title.
msgMessage to display.
imgPath of the image file to display.
widthWidth of the image. Negative uses image width.
heightHeight of the image. Negative uses image height.

Definition at line 290 of file dialogue.c.

◆ dialogue_msgRaw()

void dialogue_msgRaw ( const char *  caption,
const char *  msg 
)

Opens a dialogue window with an ok button and a fixed message.

Parameters
captionWindow title.
msgMessage to display.

Definition at line 261 of file dialogue.c.

◆ dialogue_runChoice()

char * dialogue_runChoice ( void  )

Run the dialog and return the clicked string.

Note
You must free the return value.
Returns
The string chosen.

Definition at line 792 of file dialogue.c.

◆ dialogue_YesNo()

int dialogue_YesNo ( const char *  caption,
const char *  fmt,
  ... 
)

Runs a dialogue with both yes and no options.

Parameters
captionCaption to use for the dialogue.
fmtPrintf style message.
Returns
1 if yes is clicked or 0 if no is clicked.

Definition at line 344 of file dialogue.c.

◆ dialogue_YesNoClose()

static void dialogue_YesNoClose ( unsigned int  wid,
const char *  str 
)
static

Closes a yesno dialogue.

Parameters
widWindow being closed.
strUnused.

Definition at line 404 of file dialogue.c.

◆ dialogue_YesNoRaw()

int dialogue_YesNoRaw ( const char *  caption,
const char *  msg 
)

Runs a dialogue with both yes and no options.

Parameters
captionCaption to use for the dialogue.
msgMessage to display.
Returns
1 if yes is clicked or 0 if no is clicked.

Definition at line 366 of file dialogue.c.

◆ main_loop()

void main_loop ( int  update)

Split main loop from main() for secondary loop hack in toolkit.c.

Definition at line 720 of file naev.c.

◆ select_call_wrapper()

static void select_call_wrapper ( unsigned int  wid,
const char *  wgtname 
)
static

used to pass appropriate information to the method that handles updating the extra information area in the dialogue listpanel.

Parameters
widWindow id
wgtnamename of the widget that raised the event.

Definition at line 561 of file dialogue.c.

◆ toolkit_loop()

static int toolkit_loop ( int *  loop_done,
dialogue_update_t du 
)
static

Creates a secondary loop until loop_done is set to 1 or the toolkit closes.

Almost identical to the main loop in naev.c.

Todo:
Fix this, we need proper threading as the music Lua and dialogue running Lua may be run in parallel and this will make everyone cry. So basically we have a race condition due to the "threading" effect this creates. Solved most of it by removing globals in the Lua event/mission code, but this doesn't mean it's solved. It just means it's extremely unlikely.
Returns
0 on success.

Definition at line 991 of file dialogue.c.

Variable Documentation

◆ choice_nopts

int choice_nopts
static

Counter variable.

Definition at line 740 of file dialogue.c.

◆ choice_result

char* choice_result
static

Pointer to the choice result.

Definition at line 739 of file dialogue.c.

◆ choice_wid

unsigned int choice_wid = 0
static

Stores the choice window id.

Definition at line 738 of file dialogue.c.

◆ dialogue_listSelected

int dialogue_listSelected = -1
static

Definition at line 542 of file dialogue.c.

◆ dialogue_open

int dialogue_open
static

Number of dialogues open.

Definition at line 38 of file dialogue.c.

◆ dlgid

int dlgid = 0
static

ID generator.

Definition at line 39 of file dialogue.c.

◆ input_dialogue

InputDialogue input_dialogue
static

Stores the input window id and callback.

Definition at line 424 of file dialogue.c.