Updates to widget-mouse interactions.

This commit is contained in:
Steve 2015-11-26 08:16:29 +00:00
parent 5aa3daf342
commit cd0463c92f
7 changed files with 124 additions and 74 deletions

View File

@ -1,4 +1,26 @@
[
{
"name" : "prev",
"group" : "stats",
"type" : "WT_IMG_BUTTON",
"text" : "",
"x" : 540,
"y" : 110,
"w" : 150,
"h": 34,
"texture" : "gfx/widgets/optionsLeft.png"
},
{
"name" : "next",
"group" : "stats",
"type" : "WT_IMG_BUTTON",
"text" : "",
"x" : 720,
"y" : 110,
"w" : 150,
"h": 34,
"texture" : "gfx/widgets/optionsRight.png"
},
{
"name" : "ok",
"group" : "stats",

View File

@ -222,7 +222,9 @@ enum
enum
{
WT_BUTTON,
WT_SELECT
WT_IMG_BUTTON,
WT_SELECT,
WT_SELECT_BUTTON
};
enum

View File

@ -20,6 +20,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "stats.h"
static void prevPage(void);
static void nextPage(void);
static char *statDescription[] = {
"Missions Started",
"Missons Completed",
@ -49,43 +52,19 @@ static char *statDescription[] = {
static int page;
static int maxPages;
static SDL_Texture *pagePrev;
static SDL_Texture *pageNext;
static SDL_Rect left, right;
static Widget *prev;
static Widget *next;
void initStatsDisplay(void)
{
page = 0;
maxPages = (STAT_MAX / MAX_STAT_ITEMS);
pagePrev = getTexture("gfx/widgets/optionsLeft.png");
pageNext = getTexture("gfx/widgets/optionsRight.png");
prev = getWidget("prev", "stats");
prev->action = prevPage;
left.x = (SCREEN_WIDTH / 2) - 100;
left.y = 120;
SDL_QueryTexture(pagePrev, NULL, NULL, &left.w, &left.h);
right.x = (SCREEN_WIDTH / 2) + 100;
right.y = 120;
SDL_QueryTexture(pageNext, NULL, NULL, &right.w, &right.h);
}
void doStats(void)
{
if (app.mouse.button[SDL_BUTTON_LEFT])
{
if (collision(app.mouse.x - (app.mouse.w / 2), app.mouse.y - (app.mouse.h / 2), app.mouse.w, app.mouse.h, left.x - (left.w / 2), left.y - (left.h / 2), left.w, left.h))
{
page = MAX(0, page - 1);
app.mouse.button[SDL_BUTTON_LEFT] = 0;
}
if (collision(app.mouse.x - (app.mouse.w / 2), app.mouse.y - (app.mouse.h / 2), app.mouse.w, app.mouse.h, right.x - (right.w / 2), right.y - (right.h / 2), right.w, right.h))
{
page = MIN(page + 1, maxPages);
app.mouse.button[SDL_BUTTON_LEFT] = 0;
}
}
next = getWidget("next", "stats");
next->action = nextPage;
}
void drawStats(void)
@ -113,16 +92,6 @@ void drawStats(void)
drawText(SCREEN_WIDTH / 2, 110, 16, TA_CENTER, colors.lightGrey, "Page %d / %d", page + 1, maxPages + 1);
if (page > 0)
{
blit(pagePrev, left.x, left.y, 1);
}
if (page < maxPages)
{
blit(pageNext, right.x, right.y, 1);
}
y = 170;
startIndex = (page * MAX_STAT_ITEMS);
@ -149,3 +118,13 @@ void drawStats(void)
drawWidgets("stats");
}
static void nextPage(void)
{
page = MIN(page + 1, maxPages);
}
static void prevPage(void)
{
page = MAX(0, page - 1);
}

View File

@ -30,6 +30,7 @@ extern void drawText(int x, int y, int size, int align, SDL_Color c, const char
extern void blit(SDL_Texture *texture, int x, int y, int centered);
extern SDL_Texture *getTexture(char *filename);
extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
extern Widget *getWidget(const char *name, const char *group);
extern App app;
extern Colors colors;

View File

@ -279,8 +279,10 @@ struct Widget {
int visible;
int enabled;
SDL_Rect rect;
SDL_Texture *texture;
void (*action)(void);
void (*onChange)(char *value);
Widget *parent;
Widget *next;
};

View File

@ -63,6 +63,7 @@ void initLookups(void)
addLookup("WT_BUTTON", WT_BUTTON);
addLookup("WT_SELECT", WT_SELECT);
addLookup("WT_IMG_BUTTON", WT_IMG_BUTTON);
addLookup("SIDE_ALLIES", SIDE_ALLIES);
addLookup("SIDE_PIRATE", SIDE_PIRATE);

View File

@ -24,7 +24,8 @@ static void loadWidgets(char *filename);
static void loadWidgetSet(char *filename);
static void handleMouse(void);
static void createOptions(Widget *w, char *options);
static void changeSelectedValue(int dir);
static void changeSelectedValue(Widget *w, int dir);
static void createSelectButtons(Widget *w);
static Widget head;
static Widget *tail;
@ -41,12 +42,12 @@ void initWidgets(void)
selectedWidget = NULL;
optionsLeft = getTexture("gfx/widgets/optionsLeft.png");
optionsRight = getTexture("gfx/widgets/optionsRight.png");
loadWidgets("data/widgets/list.json");
drawingWidgets = 0;
optionsLeft = getTexture("gfx/widgets/optionsLeft.png");
optionsRight = getTexture("gfx/widgets/optionsRight.png");
}
void doWidgets(void)
@ -104,6 +105,12 @@ void drawWidgets(const char *group)
}
}
if (w->texture)
{
blit(w->texture , w->rect.x, w->rect.y, 0);
}
else
{
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(app.renderer, &w->rect);
@ -119,6 +126,7 @@ void drawWidgets(const char *group)
SDL_SetRenderDrawColor(app.renderer, 64, 64, 64, SDL_ALPHA_OPAQUE);
SDL_RenderDrawRect(app.renderer, &w->rect);
}
}
switch (w->type)
{
@ -130,8 +138,6 @@ void drawWidgets(const char *group)
case WT_SELECT:
drawText(w->rect.x + 10, w->rect.y + 2, 20, TA_LEFT, colors.white, w->text);
drawText(w->rect.x + w->rect.w - 10, w->rect.y + 2, 20, TA_RIGHT, colors.white, w->options[w->currentOption]);
blit(optionsLeft, w->rect.x - 24, w->rect.y + 16, 1);
blit(optionsRight, w->rect.x + w->rect.w + 24, w->rect.y + 16, 1);
break;
}
@ -156,17 +162,17 @@ void drawConfirmMessage(char *message)
drawWidgets("okCancel");
}
static void changeSelectedValue(int dir)
static void changeSelectedValue(Widget *w, int dir)
{
int oldOption = selectedWidget->currentOption;
int oldOption = w->currentOption;
selectedWidget->currentOption += dir;
w->currentOption += dir;
selectedWidget->currentOption = MIN(MAX(0, selectedWidget->currentOption), selectedWidget->numOptions - 1);
w->currentOption = MIN(MAX(0, w->currentOption), w->numOptions - 1);
selectedWidget->onChange(selectedWidget->options[selectedWidget->currentOption]);
w->onChange(w->options[w->currentOption]);
if (oldOption != selectedWidget->currentOption)
if (oldOption != w->currentOption)
{
playSound(SND_GUI_CLICK);
}
@ -199,6 +205,7 @@ static void handleMouse(void)
switch (selectedWidget->type)
{
case WT_BUTTON:
case WT_IMG_BUTTON:
if (selectedWidget->action)
{
playSound(SND_GUI_SELECT);
@ -207,18 +214,12 @@ static void handleMouse(void)
}
break;
case WT_SELECT:
changeSelectedValue(-1);
case WT_SELECT_BUTTON:
changeSelectedValue(selectedWidget->parent, selectedWidget->value);
app.mouse.button[SDL_BUTTON_LEFT] = 0;
break;
}
}
if (app.mouse.button[SDL_BUTTON_RIGHT] && selectedWidget->type == WT_SELECT)
{
changeSelectedValue(1);
app.mouse.button[SDL_BUTTON_RIGHT] = 0;
}
}
}
@ -260,8 +261,6 @@ static void loadWidgetSet(char *filename)
STRNCPY(w->group, cJSON_GetObjectItem(node, "group")->valuestring, MAX_NAME_LENGTH);
w->rect.x = cJSON_GetObjectItem(node, "x")->valueint;
w->rect.y = cJSON_GetObjectItem(node, "y")->valueint;
w->rect.w = cJSON_GetObjectItem(node, "w")->valueint;
w->rect.h = cJSON_GetObjectItem(node, "h")->valueint;
w->enabled = 1;
w->visible = 1;
@ -274,14 +273,24 @@ static void loadWidgetSet(char *filename)
{
case WT_BUTTON:
STRNCPY(w->text, cJSON_GetObjectItem(node, "text")->valuestring, MAX_NAME_LENGTH);
w->rect.w = cJSON_GetObjectItem(node, "w")->valueint;
w->rect.h = cJSON_GetObjectItem(node, "h")->valueint;
w->rect.x -= w->rect.w / 2;
w->rect.y -= (w->rect.h / 2) + 8;
break;
case WT_IMG_BUTTON:
w->texture = getTexture(cJSON_GetObjectItem(node, "texture")->valuestring);
SDL_QueryTexture(w->texture, NULL, NULL, &w->rect.w, &w->rect.h);
break;
case WT_SELECT:
STRNCPY(w->text, cJSON_GetObjectItem(node, "text")->valuestring, MAX_NAME_LENGTH);
w->rect.w = cJSON_GetObjectItem(node, "w")->valueint;
w->rect.h = cJSON_GetObjectItem(node, "h")->valueint;
w->rect.x -= w->rect.w / 2;
w->rect.y -= (w->rect.h / 2) + 8;
createSelectButtons(w);
createOptions(w, cJSON_GetObjectItem(node, "options")->valuestring);
break;
}
@ -324,6 +333,40 @@ static void createOptions(Widget *w, char *options)
}
}
static void createSelectButtons(Widget *w)
{
int i;
Widget *btn;
for (i = 0 ; i < 2 ; i++)
{
btn = malloc(sizeof(Widget));
memcpy(btn, w, sizeof(Widget));
strcpy(btn->name, "");
btn->type = WT_SELECT_BUTTON;
btn->parent = w;
if (i == 0)
{
btn->value = -1;
btn->rect.x -= 32;
btn->rect.y += 4;
btn->texture = optionsLeft;
}
else
{
btn->value = 1;
btn->rect.x += btn->rect.w + 8;
btn->rect.y += 4;
btn->texture = optionsRight;
}
tail->next = btn;
tail = btn;
}
}
void destroyWidgets(void)
{
int i;