[Add] Allow to move or get window positions after creation.

[Change] Simplified window position creation.
[Add] More comments for window creation.
This commit is contained in:
Allanis 2013-07-23 20:42:17 +01:00
parent b680520738
commit cf1bf0d647
2 changed files with 67 additions and 38 deletions

View File

@ -120,10 +120,12 @@ static glColour* toolkit_colDark = &cGrey30;
/* Extern. */ /* Extern. */
extern void main_loop(void); /* lephisto.c */ extern void main_loop(void); /* lephisto.c */
/* Static. */ /* Static. */
/* Widgets. */
static Widget* window_newWidget(Window* w); static Widget* window_newWidget(Window* w);
static void widget_cleanup(Widget* widget); static void widget_cleanup(Widget* widget);
static Window* window_wget(const unsigned int wid); static Window* window_wget(const unsigned int wid);
static Widget* window_getwgt(const unsigned int wid, char* name); static Widget* window_getwgt(const unsigned int wid, char* name);
static void toolkit_setPos(Window* wdw, Widget* wgt, int x, int y);
/* Input. */ /* Input. */
static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key); static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key);
static void toolkit_mouseEvent(SDL_Event* event); static void toolkit_mouseEvent(SDL_Event* event);
@ -160,6 +162,14 @@ static void dialogue_inputClose(char* str);
static int loop_done; static int loop_done;
static int toolkit_loop(void); static int toolkit_loop(void);
/* Set the internal widget position. */
static void toolkit_setPos(Window* wdw, Widget* wgt, int x, int y) {
if(x < 0) wgt->x = wdw->w - wgt->w + x;
else wgt->x = (double) x;
if(y < 0) wgt->y = wdw->h - wgt->h + y;
else wgt->y = (double) y;
}
/* Add a button that when pressed will trigger call, passing it's name as the */ /* Add a button that when pressed will trigger call, passing it's name as the */
/* only parameter. */ /* only parameter. */
void window_addButton(const unsigned int wid, const int x, const int y, void window_addButton(const unsigned int wid, const int x, const int y,
@ -169,20 +179,19 @@ void window_addButton(const unsigned int wid, const int x, const int y,
Window* wdw = window_wget(wid); Window* wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw); Widget* wgt = window_newWidget(wdw);
/* Specific. */ /* Generic. */
wgt->type = WIDGET_BUTTON; wgt->type = WIDGET_BUTTON;
wgt->name = strdup(name); wgt->name = strdup(name);
/* Specific. */
wgt->dat.btn.display = strdup(display); wgt->dat.btn.display = strdup(display);
wgt->dat.btn.disabled = 0; /* Initially enabled. */ wgt->dat.btn.disabled = 0; /* Initially enabled. */
wgt->dat.btn.fptr = call;
/* Set the properties. */ /* Position/Size. */
wgt->w = (double) w; wgt->w = (double) w;
wgt->h = (double) h; wgt->h = (double) h;
if(x < 0) wgt->x = wdw->w - wgt->w + x; toolkit_setPos(wdw, wgt, x, y);
else wgt->x = (double)x;
if(y < 0) wgt->y = wdw->h - wgt->h + y;
else wgt->y = (double)y;
wgt->dat.btn.fptr = call;
if(wdw->focus == -1) /* Init the focus. */ if(wdw->focus == -1) /* Init the focus. */
toolkit_nextFocus(); toolkit_nextFocus();
@ -196,23 +205,25 @@ void window_addText(const unsigned int wid, const int x, const int y,
Window* wdw = window_wget(wid); Window* wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw); Widget* wgt = window_newWidget(wdw);
/* Generic. */
wgt->type = WIDGET_TEXT; wgt->type = WIDGET_TEXT;
wgt->name = strdup(name); /* Display the widgets name. */ wgt->name = strdup(name);
/* Set the properties. */ /* Specific. */
wgt->w = (double) w; if(font == NULL) wgt->dat.txt.font = &gl_defFont;
wgt->h = (double) h; else wgt->dat.txt.font = font;
if(font == NULL) wgt->dat.txt.font = &gl_defFont; if(font == NULL) wgt->dat.txt.font = &gl_defFont;
else wgt->dat.txt.font = font; else wgt->dat.txt.font = font;
if(x < 0) wgt->x = wdw->w - wgt->w + x;
else wgt->x = (double)x;
if(y < 0) wgt->y = wdw->h + y - h;
else wgt->y = (double) y;
if(colour == NULL) wgt->dat.txt.colour = &cBlack; if(colour == NULL) wgt->dat.txt.colour = &cBlack;
else wgt->dat.txt.colour = colour; else wgt->dat.txt.colour = colour;
wgt->dat.txt.centered = centered; wgt->dat.txt.centered = centered;
if(string) wgt->dat.txt.text = strdup(string); if(string) wgt->dat.txt.text = strdup(string);
else wgt->dat.txt.text = NULL; else wgt->dat.txt.text = NULL;
/* Position/Size. */
wgt->w = (double)w;
wgt->h = (double)h;
toolkit_setPos(wdw, wgt, x, y);
} }
/* Add a graphic to the window. */ /* Add a graphic to the window. */
@ -222,20 +233,19 @@ void window_addImage(const unsigned int wid, const int x, const int y,
Window* wdw = window_wget(wid); Window* wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw); Widget* wgt = window_newWidget(wdw);
/* Generic. */
wgt->type = WIDGET_IMAGE; wgt->type = WIDGET_IMAGE;
wgt->name = strdup(name); wgt->name = strdup(name);
/* Set the properties. */ /* Specific. */
wgt->dat.img.image = image; wgt->dat.img.image = image;
wgt->dat.img.border = border; wgt->dat.img.border = border;
wgt->dat.img.colour = NULL; /* Normal colour. */ wgt->dat.img.colour = NULL; /* Normal colour. */
/* Position/Size. */
wgt->w = (image == NULL) ? 0 : wgt->dat.img.image->sw; wgt->w = (image == NULL) ? 0 : wgt->dat.img.image->sw;
wgt->h = (image == NULL) ? 0 : wgt->dat.img.image->sh; wgt->h = (image == NULL) ? 0 : wgt->dat.img.image->sh;
if(x < 0) wgt->x = wdw->w - wgt->w + x; toolkit_setPos(wdw, wgt, x, y);
else wgt->x = (double)x;
if(y < 0) wgt->y = wdw->h - wgt->h + y;
else wgt->y = (double)y;
} }
void window_addList(const unsigned int wid, const int x, const int y, void window_addList(const unsigned int wid, const int x, const int y,
@ -245,21 +255,21 @@ void window_addList(const unsigned int wid, const int x, const int y,
Window *wdw = window_wget(wid); Window *wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw); Widget* wgt = window_newWidget(wdw);
/* Generic. */
wgt->type = WIDGET_LIST; wgt->type = WIDGET_LIST;
wgt->name = strdup(name); wgt->name = strdup(name);
/* Specific. */
wgt->dat.lst.options = items; wgt->dat.lst.options = items;
wgt->dat.lst.noptions = nitems; wgt->dat.lst.noptions = nitems;
wgt->dat.lst.selected = defitem; /* -1 would be none. */ wgt->dat.lst.selected = defitem; /* -1 would be none. */
wgt->dat.lst.pos = 0; wgt->dat.lst.pos = 0;
wgt->dat.lst.fptr = call; wgt->dat.lst.fptr = call;
/* Position/Size. */
wgt->w = (double) w; wgt->w = (double) w;
wgt->h = (double) h - ((h % (gl_defFont.h+2)) + 2); wgt->h = (double) h - ((h % (gl_defFont.h+2)) + 2);
if(x < 0) wgt->x = wdw->w - wgt->w + x; toolkit_setPos(wdw, wgt, x, y);
else wgt->x = (double) x;
if(y < 0) wgt->y = wdw->h - wgt->h + y;
else wgt->y = (double) y;
if(wdw->focus == -1) if(wdw->focus == -1)
/* Initialize the focus. */ /* Initialize the focus. */
@ -272,18 +282,18 @@ void window_addRect(const unsigned int wid, const int x, const int y,
Window* wdw = window_wget(wid); Window* wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw); Widget* wgt = window_newWidget(wdw);
/* Generic. */
wgt->type = WIDGET_RECT; wgt->type = WIDGET_RECT;
wgt->name = strdup(name); wgt->name = strdup(name);
/* Specific. */
wgt->dat.rct.colour = colour; wgt->dat.rct.colour = colour;
wgt->dat.rct.border = border; wgt->dat.rct.border = border;
/* Position/Size. */
wgt->w = (double)w; wgt->w = (double)w;
wgt->h = (double)h; wgt->h = (double)h;
if(x < 0) wgt->x = wdw->w - wgt->w + x; toolkit_setPos(wdw, wgt, x, y);
else wgt->x = (double)x;
if(y < 0) wgt->y = wdw->h - wgt->h + y;
else wgt->y = (double)y;
} }
void window_addCust(const unsigned int wid, const int x, const int y, void window_addCust(const unsigned int wid, const int x, const int y,
@ -306,10 +316,7 @@ void window_addCust(const unsigned int wid, const int x, const int y,
/* Position/size. */ /* Position/size. */
wgt->w = (double)w; wgt->w = (double)w;
wgt->h = (double)h; wgt->h = (double)h;
if(x < 0) wgt->x = wdw->w - wgt->w + x; toolkit_setPos(wdw, wgt, x, y);
else wgt->x = (double) x;
if(y < 0) wgt->y = wdw->h - wgt->h + y;
else wgt->y = (double) y;
} }
/* Add an input widget. */ /* Add an input widget. */
@ -336,10 +343,7 @@ void window_addInput(const unsigned int wid,
/* Position/Size. */ /* Position/Size. */
wgt->w = (double)w; wgt->w = (double)w;
wgt->h = (double)h; wgt->h = (double)h;
if(x < 0) wgt->x = wdw->w - wgt->w + x; toolkit_setPos(wdw, wgt, x, y);
else wgt->x = (double)x;
if(y < 0) wgt->y = wdw->h - wgt->h + y;
else wgt->y = (double)y;
} }
/* Return pointer to newly allocated widget. */ /* Return pointer to newly allocated widget. */
@ -383,6 +387,25 @@ void window_modifyText(const unsigned int wid, char* name, char* newstring) {
wgt->dat.txt.text = (newstring) ? strdup(newstring) : NULL; wgt->dat.txt.text = (newstring) ? strdup(newstring) : NULL;
} }
/* Get a widgets position. */
void window_posWidget(const unsigned int wid,
char* name, int* x, int* y) {
Widget* wgt = window_getwgt(wid, name);
(*x) = wgt->x;
(*y) = wgt->y;
}
/* Move a widget. */
void window_moveWidget(const unsigned int wid,
char* name, int x, int y) {
Window* wdw = window_wget(wid);
Widget* wgt = window_getwgt(wid, name);
toolkit_setPos(wdw, wgt, x, y);
}
/* Disable a button. */ /* Disable a button. */
void window_disableButton(const unsigned int wid, char* name) { void window_disableButton(const unsigned int wid, char* name) {
Widget* wgt = window_getwgt(wid, name); Widget* wgt = window_getwgt(wid, name);

View File

@ -63,13 +63,19 @@ void window_enableButton(const unsigned int wid, char* name);
void window_modifyImage(const unsigned int wid, char* name, glTexture* image); void window_modifyImage(const unsigned int wid, char* name, glTexture* image);
void window_imgColour(const unsigned int wid, char* name, glColour* colour); void window_imgColour(const unsigned int wid, char* name, glColour* colour);
/* Get the window by name. */ /* Get. */
/* Generic. */
int window_exists(const char* wdwname); int window_exists(const char* wdwname);
unsigned int window_get(const char* wdwname); unsigned int window_get(const char* wdwname);
char* window_getInput(const unsigned int wid, char* name);
void window_posWidget(const unsigned int wid,
char* name, int* x, int* y);
void window_moveWidget(const unsigned int wid,
char* name, int x, int y);
/* Specific. */
char* toolkit_getList(const unsigned int wid, char* name); char* toolkit_getList(const unsigned int wid, char* name);
int toolkit_getListPos(const unsigned int wid, char* name); int toolkit_getListPos(const unsigned int wid, char* name);
glTexture* window_getImage(const unsigned int wid, char* name); glTexture* window_getImage(const unsigned int wid, char* name);
char* window_getInput(const unsigned int wid, char* name);
/* Destroy window. */ /* Destroy window. */
void window_destroy(const unsigned int wid); void window_destroy(const unsigned int wid);