[Change] Major toolkit change to simplify things a lot, makes it more "object orientated"

This commit is contained in:
Allanis 2014-01-11 23:16:08 +00:00
parent 7893d5e0ec
commit b4c29d1157
5 changed files with 63 additions and 47 deletions

View File

@ -37,9 +37,9 @@ extern int diff_load(xmlNodePtr parent);
extern void menu_main_close(void);
/* Static. */
static int save_data(xmlTextWriterPtr writer);
static void load_menu_close(char* str);
static void load_menu_load(char* str);
static void load_menu_delete(char* str);
static void load_menu_close(unsigned int wdw, char* str);
static void load_menu_load(unsigned int wdw, char* str);
static void load_menu_delete(unsigned wdw, char* str);
static int load_game(char* file);
/* Save all the game data. */
@ -151,13 +151,13 @@ void load_game_menu(void) {
window_setAccept(wid, load_menu_load);
}
static void load_menu_close(char* str) {
static void load_menu_close(unsigned int wdw, char* str) {
(void)str;
window_destroy(window_get("Load Game"));
window_destroy(wdw);
}
static void load_menu_load(char* str) {
static void load_menu_load(unsigned int wdw, char* str) {
(void)str;
char* save, path[PATH_MAX];
int wid;
@ -171,11 +171,11 @@ static void load_menu_load(char* str) {
snprintf(path, PATH_MAX, "%ssaves/%s.ls", lfile_basePath(), save);
load_game(path);
load_menu_close(NULL);
load_menu_close(wdw, NULL);
menu_main_close();
}
static void load_menu_delete(char* str) {
static void load_menu_delete(unsigned int wdw, char* str) {
(void)str;
char* save, path[PATH_MAX];
int wid;
@ -194,7 +194,7 @@ static void load_menu_delete(char* str) {
unlink(path);
/* Need to reload the menu. */
load_menu_close(NULL);
load_menu_close(wdw, NULL);
load_game_menu();
}

View File

@ -26,7 +26,6 @@ static Ship* ship_stack = NULL;
static int ship_nstack = 0;
static Ship* ship_parse(xmlNodePtr parent);
static void ship_view_close(char* btn);
/* Get a ship based on it's name. */
Ship* ship_get(const char* name) {
@ -393,7 +392,8 @@ void ships_free(void) {
}
/* Used to visualize the ships status. */
void ship_view(char* shipname) {
void ship_view(unsigned int unused, char* shipname) {
(void) unused;
Ship* s;
char buf[1024];
unsigned int wid;
@ -453,10 +453,6 @@ void ship_view(char* shipname) {
snprintf(buf, 37, "close%s", shipname);
window_addButton(wid, -20, 20,
BUTTON_WIDTH, BUTTON_HEIGHT,
buf, "Close", ship_view_close);
}
static void ship_view_close(char* btn) {
window_destroy(window_get(btn+5)); /* "closefoo -> Foo" */
buf, "Close", window_close);
}

View File

@ -88,5 +88,5 @@ int ships_load(void);
void ships_free(void);
/* Toolkit. */
void ship_view(char* shipname);
void ship_view(unsigned int unused, char* shipname);

View File

@ -47,7 +47,7 @@ typedef struct Widget_ {
union {
/* Widget button. */
struct {
void(*fptr) (char*); /* Active callback. */
void(*fptr) (unsigned int, char*); /* Active callback. */
char* display; /* Stored text. */
int disabled;
} btn;
@ -70,7 +70,7 @@ typedef struct Widget_ {
int noptions; /* total number of options. */
int selected; /* Currently selected option. */
int pos; /* Current topmost option (in view). */
void (*fptr) (char*); /* Modify callback. */
void (*fptr) (unsigned int, char*); /* Modify callback. */
} lst;
/* Widget rect. */
struct {
@ -81,7 +81,7 @@ typedef struct Widget_ {
struct {
int border;
void(*render) (double bx, double by, double bw, double bh);
void(*mouse) (SDL_Event* event, double bx, double by);
void(*mouse) (unsigned int wid, SDL_Event* event, double bx, double by);
} cst;
struct {
/* Widget input. */
@ -98,7 +98,7 @@ typedef struct Widget_ {
double pos; /**< Current y position. */
int iw; /**< Image width to use. */
int ih; /**< Image height to use. */
void(*fptr)(char*); /**< Modify callback - triggered on selection. */
void(*fptr)(unsigned int, char*); /**< Modify callback - triggered on selection. */
} iar; /**< WIDGET_IMAGEARRAY */
} dat;
} Widget;
@ -110,9 +110,9 @@ typedef struct Window_ {
int hidden; /* Is it hidden? */
int focus; /* Which window is focused. */
void(*accept_fptr)(char*); /**< Triggered by hitting 'enter' with no widget
void(*accept_fptr)(unsigned int, char*); /**< Triggered by hitting 'enter' with no widget
that catches the keypress. */
void(*cancel_fptr)(char*); /**< Triggered by hitting 'escape' with no
void(*cancel_fptr)(unsigned int, char*); /**< Triggered by hitting 'escape' with no
widget that catches the keypress. */
double x,y; /* Position. */
@ -188,7 +188,7 @@ static void toolkit_setPos(Window* wdw, Widget* wgt, int x, int y) {
/* only parameter. */
void window_addButton(const unsigned int wid, const int x, const int y,
const int w, const int h, char* name,
char* display, void (*call)(char*)) {
char* display, void (*call)(unsigned int, char*)) {
Window* wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw);
@ -266,7 +266,7 @@ void window_addImage(const unsigned int wid, const int x, const int y,
void window_addList(const unsigned int wid, const int x, const int y,
const int w, const int h, char* name, char** items, int nitems,
int defitem, void(*call) (char*)) {
int defitem, void(*call) (unsigned int, char*)) {
Window *wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw);
@ -315,7 +315,7 @@ void window_addRect(const unsigned int wid, const int x, const int y,
void window_addCust(const unsigned int wid, const int x, const int y,
const int w, const int h, char* name, const int border,
void(*render) (double x, double y, double w, double h),
void(*mouse) (SDL_Event* event, double x, double y)) {
void(*mouse) (unsigned int wid, SDL_Event* event, double x, double y)) {
Window* wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw);
@ -382,7 +382,7 @@ void window_addImageArray(const unsigned int wid,
const int w, const int h, /* Size. */
char* name, const int iw, const int ih, /* Name and image sizes. */
glTexture** tex, char** caption, int nelem, /* Elements. */
void(*call) (char*)) {
void(*call) (unsigned int, char*)) {
Window* wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw);
@ -532,7 +532,6 @@ unsigned int window_get(const char* wdwname) {
for(i = 0; i < nwindows; i++)
if(strcmp(windows[i].name, wdwname)==0)
return windows[i].id;
DEBUG("Window '%s' not found in windows stack", wdwname);
return 0;
}
@ -596,7 +595,7 @@ unsigned int window_create(char* name, const int x, const int y,
}
/**
* @fn void window_setAccept(const unsigned int wid, void(*accept)(char*))
* @fn void window_setAccept(const unsigned int wid, void(*accept)(unsigned int, char*))
*
* @brief Set the default accept function of the window.
*
@ -606,7 +605,7 @@ unsigned int window_create(char* name, const int x, const int y,
* @param accept Function to trigger when window is "accepted". Parameter
* passed is window name.
*/
void window_setAccept(const unsigned int wid, void(*accept)(char*)) {
void window_setAccept(const unsigned int wid, void(*accept)(unsigned int, char*)) {
Window* wdw;
wdw = window_wget(wid);
@ -614,7 +613,7 @@ void window_setAccept(const unsigned int wid, void(*accept)(char*)) {
}
/**
* @fn void window_setCancel(const unsigned int wid, void(*cancel)(char*))
* @fn void window_setCancel(const unsigned int wid, void(*cancel)(unsigned int, char*))
*
* @brief Set the default cancel function of the window.
*
@ -624,7 +623,7 @@ void window_setAccept(const unsigned int wid, void(*accept)(char*)) {
* @param cancel Function to trigger when window is "cancelled".
* Parameter passed is the window name.
*/
void window_setCancel(const unsigned int wid, void(*cancel)(char*)) {
void window_setCancel(const unsigned int wid, void(*cancel)(unsigned int, char*)) {
Window* wdw;
wdw = window_wget(wid);
@ -668,6 +667,19 @@ static void widget_cleanup(Widget* widget) {
}
}
/**
* @fn void window_close(unsigned int wid, char* str)
*
* @brief Helper function to automatically close the window calling it.
* @param wid Window to close.
* @param str Unused.
*/
void window_close(unsigned int wid, char* str) {
(void) str;
window_destroy(wid);
}
/* Destroy a window. */
void window_destroy(const unsigned int wid) {
int i, j;
@ -1406,7 +1418,7 @@ static void toolkit_mouseEvent(SDL_Event* event) {
(y > wgt->y) && (y < (wgt->y + wgt->h))) {
/* Custom widgets take it from here. */
if((wgt->type == WIDGET_CUST) && wgt->dat.cst.mouse)
(*wgt->dat.cst.mouse)(event, x-wgt->x, y-wgt->y);
(*wgt->dat.cst.mouse)(w->id, event, x-wgt->x, y-wgt->y);
else
switch(event->type) {
case SDL_MOUSEMOTION:
@ -1463,16 +1475,17 @@ static void toolkit_mouseEvent(SDL_Event* event) {
break;
}
}
/* Otherwise custom widgets can get stuck on mousedown. */
else if((wgt->type == WIDGET_CUST) &&
(event->type == SDL_MOUSEBUTTONUP) && wgt->dat.cst.mouse)
(*wgt->dat.cst.mouse) (event, x-wgt->x, y-wgt->y);
(*wgt->dat.cst.mouse) (w->id, event, x-wgt->x, y-wgt->y);
else if(!mouse_down)
wgt->status = WIDGET_STATUS_NORMAL;
}
/* We trigger this at the end in case it destroys the window that is calling
* this function. Otherwise ugly segfaults appear. */
if(wgt_func) (*wgt_func->dat.btn.fptr)(wgt_func->name);
if(wgt_func) (*wgt_func->dat.btn.fptr)(w->id, wgt_func->name);
}
/* Handle the key events. */
@ -1529,7 +1542,7 @@ static int toolkit_keyEvent(SDL_Event* event) {
case SDLK_ESCAPE:
if(event->type == SDL_KEYDOWN)
if(wdw->cancel_fptr != NULL) {
(*wdw->cancel_fptr)(wdw->name);
(*wdw->cancel_fptr)(wdw->id, wdw->name);
return 1;
}
return 0;
@ -1631,13 +1644,13 @@ static void toolkit_triggerFocus(void) {
switch(wgt->type) {
case WIDGET_BUTTON:
if(wgt->dat.btn.fptr)(*wgt->dat.btn.fptr)(wgt->name);
if(wgt->dat.btn.fptr)(*wgt->dat.btn.fptr)(wdw->id, wgt->name);
else DEBUG("Toolkit: Button '%s' of Window '%s'"
"Doesn't have a function trigger",
wgt->name, wdw->name);
break;
default:
if(wdw->accept_fptr)(*wdw->accept_fptr)(wgt->name);
if(wdw->accept_fptr)(*wdw->accept_fptr)(wdw->id, wgt->name);
break;
}
}
@ -1647,15 +1660,18 @@ static void toolkit_listScroll(Widget* wgt, int direction) {
double w, h;
int xelem, yelem;
double hmax;
Window* wdw;
if(wgt == NULL) return;
wdw = &windows[nwindows-1]; /* Get active window. */
switch(wgt->type) {
case WIDGET_LIST:
wgt->dat.lst.selected -= direction;
wgt->dat.lst.selected = MAX(0, wgt->dat.lst.selected);
wgt->dat.lst.selected = MIN(wgt->dat.lst.selected, wgt->dat.lst.noptions-1);
if(wgt->dat.lst.fptr) (*wgt->dat.lst.fptr)(wgt->name);
if(wgt->dat.lst.fptr) (*wgt->dat.lst.fptr)(wdw->id, wgt->name);
break;
case WIDGET_IMAGEARRAY:
@ -1678,7 +1694,7 @@ static void toolkit_listScroll(Widget* wgt, int direction) {
/* Boundry check. */
wgt->dat.iar.pos = MAX(wgt->dat.iar.pos, 0.);
wgt->dat.iar.pos = MIN(wgt->dat.iar.pos, hmax);
if(wgt->dat.iar.fptr)(*wgt->dat.iar.fptr)(wgt->name);
if(wgt->dat.iar.fptr)(*wgt->dat.iar.fptr)(wdw->id, wgt->name);
break;
default:
@ -1758,6 +1774,9 @@ static void toolkit_imgarrFocus(Widget* iar, double bx, double by) {
double x, y, w, h, ycurs, xcurs;
double scroll_pos, hmax;
int xelem, xspace, yelem;
Window* wdw;
wdw = &windows[nwindows-1]; /* Get active window. */
/* Positions. */
x = bx + iar->x;
@ -1788,7 +1807,7 @@ static void toolkit_imgarrFocus(Widget* iar, double bx, double by) {
(by > ycurs) && (by < ycurs+h-4.)) {
iar->dat.iar.selected = j*xelem+i;
if(iar->dat.iar.fptr != NULL)
(*iar->dat.iar.fptr)(iar->name);
(*iar->dat.iar.fptr)(wdw->id, iar->name);
return;
}
xcurs += xspace + w;

View File

@ -11,7 +11,7 @@ unsigned int window_create(char* name, const int x, const int y,
void window_addButton(const unsigned int wid, const int x, const int y,
const int w, const int h, char* name, char* display,
void(*call)(char*));
void(*call)(unsigned int, char*));
void window_addText(const unsigned int wid, const int x, const int y,
const int w, const int h, const int centered, char* name,
@ -24,7 +24,7 @@ void window_addList(const unsigned int wid,
const int x, const int y, /* Position. */
const int w, const int h, /* Size. */
char* name, char** items, int nitems, int defitem,
void(*call)(char*));
void(*call)(unsigned int, char*));
void window_addRect(const unsigned int wid,
const int x, const int y, /* Position. */
@ -36,7 +36,7 @@ void window_addCust(const unsigned int wid,
const int w, const int h, /* Size. */
char* name, const int border,
void(*render) (double x, double y, double w, double h),
void(*mouse) (SDL_Event* event, double x, double y));
void(*mouse) (unsigned int wid, SDL_Event* event, double x, double y));
void window_addInput(const unsigned int wid,
const int x, const int y,
@ -48,11 +48,11 @@ void window_addImageArray(const unsigned int wid,
const int w, const int h, /* Size. */
char* name, const int iw, const int ih, /* Name and images sizes. */
glTexture** tex, char** caption, int nelem, /* Elements. */
void(*call)(char*));
void(*call)(unsigned int, char*));
/* Modification. */
void window_setAccept(const unsigned int wid, void(*fptr)(char*));
void window_setCancel(const unsigned int wid, void(*cancel)(char*));
void window_setAccept(const unsigned int wid, void(*fptr)(unsigned int, char*));
void window_setCancel(const unsigned int wid, void(*cancel)(unsigned int, char*));
/* Text. */
void window_modifyText(const unsigned int wid, char* name, char* newstring);
/* Button. */
@ -77,6 +77,7 @@ int toolkit_getListPos(const unsigned int wid, char* name);
glTexture* window_getImage(const unsigned int wid, char* name);
/* Destroy window. */
void window_close(unsigned int wid, char* str);
void window_destroy(const unsigned int wid);
void window_destroyWidget(unsigned wid, const char* wgtname);