diff --git a/src/defs.h b/src/defs.h index cb702f0..eb8f172 100644 --- a/src/defs.h +++ b/src/defs.h @@ -79,6 +79,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define JUMP_POWER 1 #define MAX_KEY_TYPES 12 +#define MAX_WIDGET_OPTIONS 8 + #define ET_ENEMY 0 #define ET_KEY 1 #define ET_MIA 2 @@ -218,3 +220,10 @@ enum CH_WEAPON, CH_MAX }; + +enum +{ + WT_BUTTON, + WT_SPINNER, + WT_PLAIN_BUTTON +}; diff --git a/src/structs.h b/src/structs.h index 28cdbb3..4157c75 100644 --- a/src/structs.h +++ b/src/structs.h @@ -29,6 +29,7 @@ typedef struct Particle Particle; typedef struct Sprite Sprite; typedef struct Tuple Tuple; typedef struct HubMission HubMission; +typedef struct Widget Widget; typedef struct { int debug; @@ -295,3 +296,20 @@ typedef struct { Objective objectiveHead, *objectiveTail; Trigger triggerHead, *triggerTail; } World; + +struct Widget { + int type; + int x; + int y; + int w; + int h; + int visible; + int enabled; + char name[MAX_NAME_LENGTH]; + char group[MAX_NAME_LENGTH]; + char label[MAX_NAME_LENGTH]; + char options[MAX_WIDGET_OPTIONS][MAX_NAME_LENGTH]; + int value; + int clicked; + Widget *next; +}; diff --git a/src/widgets/widgets.c b/src/widgets/widgets.c index 1246809..e036978 100644 --- a/src/widgets/widgets.c +++ b/src/widgets/widgets.c @@ -20,6 +20,130 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "widgets.h" -void showWidgetGroup(char *groupName) +Widget *getWidgetAt(int x, int y); + +static Widget widgetHead; +static Widget *widgetTail; +static Widget *selectedWidget; + +void initWidgets(void) +{ + memset(&widgetHead, 0, sizeof(Widget)); + widgetTail = &widgetHead; +} + +Widget *getWidget(char *name, char *group) +{ + Widget *w; + + for (w = widgetHead.next ; w != NULL ; w = w->next) + { + if (strcmp(w->name, name) == 0 && strcmp(w->group, group) == 0) + { + return w; + } + } + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "No such widget '%s', '%s'", name, group); + exit(1); + + return NULL; +} + +void handleWidgetClick(int x, int y) +{ + Widget *w; + + w = getWidgetAt(x, y); + + if (w != NULL) + { + if (w->type == WT_SPINNER) + { + w->value++; + + w->value %= MAX_WIDGET_OPTIONS; + } + + w->clicked = 1; + } +} + +void activeSelected(void) +{ + if (selectedWidget->type == WT_SPINNER) + { + selectedWidget->value++; + + selectedWidget->value %= MAX_WIDGET_OPTIONS; + } + + selectedWidget->clicked = 1; +} + +Widget *getWidgetAt(int x, int y) +{ + Widget *w; + + for (w = widgetHead.next ; w != NULL ; w = w->next) + { + if (w->visible && w->enabled && collision(x, y, 1, 1, w->x - (w->w / 2), w->y, w->w, w->h)) + { + return w; + } + } + + return NULL; +} + +void hideAll(void) +{ + Widget *w; + + for (w = widgetHead.next ; w != NULL ; w = w->next) + { + w->visible = 0; + } + + selectedWidget = NULL; +} + +void showWidgetGroup(char *group) +{ + Widget *w; + + hideAll(); + + for (w = widgetHead.next ; w != NULL ; w = w->next) + { + if (strcmp(w->group, group) == 0) + { + if (selectedWidget == NULL) + { + selectedWidget = w; + } + + w->visible = 1; + } + } +} + +int wasWidgetClicked(Widget *w) +{ + int wasClicked; + + wasClicked = w->clicked; + + w->clicked = 0; + + return wasClicked; +} + +void loadWidgets(char *filename) +{ + +} + +void destroy(void) { } diff --git a/src/widgets/widgets.h b/src/widgets/widgets.h index 8ad0dc9..ad8a06b 100644 --- a/src/widgets/widgets.h +++ b/src/widgets/widgets.h @@ -19,3 +19,5 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "../common.h" + +extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);