tbftss/src/system/widgets.c

524 lines
12 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
2016-02-21 16:50:27 +01:00
Copyright (C) 2015-2016 Parallel Realities
2015-10-20 13:51:49 +02:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "widgets.h"
static void loadWidgets(void);
2015-10-20 13:51:49 +02:00
static void loadWidgetSet(char *filename);
2015-11-23 15:52:11 +01:00
static void handleMouse(void);
static void handleKeyboard(void);
2015-10-20 13:51:49 +02:00
static void createOptions(Widget *w, char *options);
2015-11-26 09:16:29 +01:00
static void changeSelectedValue(Widget *w, int dir);
static void createSelectButtons(Widget *w);
static void handleControlWidgets(void);
2015-10-20 13:51:49 +02:00
static Widget head;
static Widget *tail;
static Widget *selectedWidget;
static SDL_Texture *optionsLeft;
static SDL_Texture *optionsRight;
static int drawingWidgets;
void initWidgets(void)
{
memset(&head, 0, sizeof(Widget));
2015-10-20 13:51:49 +02:00
tail = &head;
2015-10-20 13:51:49 +02:00
selectedWidget = NULL;
2015-11-26 09:16:29 +01:00
optionsLeft = getTexture("gfx/widgets/optionsLeft.png");
optionsRight = getTexture("gfx/widgets/optionsRight.png");
loadWidgets();
app.awaitingWidgetInput = drawingWidgets = 0;
2015-10-20 13:51:49 +02:00
}
void doWidgets(void)
{
if (drawingWidgets)
{
2015-11-23 15:52:11 +01:00
handleMouse();
handleKeyboard();
if (app.awaitingWidgetInput)
{
handleControlWidgets();
}
2015-10-20 13:51:49 +02:00
}
2015-10-20 13:51:49 +02:00
drawingWidgets = 0;
}
Widget *getWidget(const char *name, const char *group)
{
Widget *w;
for (w = head.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_WARN, "No such widget ('%s', '%s')", name, group);
return NULL;
}
void selectWidget(const char *name, const char *group)
{
selectedWidget = getWidget(name, group);
2015-10-20 13:51:49 +02:00
}
void drawWidgets(const char *group)
{
2015-11-23 15:52:11 +01:00
int mouseOver;
2015-10-20 13:51:49 +02:00
Widget *w;
2015-10-20 13:51:49 +02:00
drawingWidgets = 1;
mouseOver = 0;
2015-10-20 13:51:49 +02:00
for (w = head.next; w != NULL ; w = w->next)
{
2016-02-23 23:20:07 +01:00
if ((app.modalDialog.type == MD_NONE || (app.modalDialog.type != MD_NONE && w->isModal)) && w->visible && strcmp(w->group, group) == 0)
2015-10-20 13:51:49 +02:00
{
if (!mouseOver && !app.awaitingWidgetInput)
2015-11-23 15:52:11 +01:00
{
mouseOver = (w->type != WT_SELECT && w->enabled && collision(w->rect.x, w->rect.y, w->rect.w, w->rect.h, app.mouse.x, app.mouse.y, 1, 1));
if (mouseOver && selectedWidget != w)
{
if (w->type == WT_BUTTON || w->type == WT_CONTROL_CONFIG)
{
playSound(SND_GUI_CLICK);
}
selectedWidget = w;
}
2015-11-23 15:52:11 +01:00
}
2015-11-26 09:16:29 +01:00
if (w->texture)
2015-10-20 13:51:49 +02:00
{
2015-11-26 09:16:29 +01:00
blit(w->texture , w->rect.x, w->rect.y, 0);
2015-10-20 13:51:49 +02:00
}
else
{
2015-11-26 09:16:29 +01:00
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(app.renderer, &w->rect);
2015-11-26 09:16:29 +01:00
if (w == selectedWidget)
{
SDL_SetRenderDrawColor(app.renderer, 64, 128, 200, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(app.renderer, &w->rect);
SDL_SetRenderDrawColor(app.renderer, 128, 192, 255, SDL_ALPHA_OPAQUE);
SDL_RenderDrawRect(app.renderer, &w->rect);
}
else
{
SDL_SetRenderDrawColor(app.renderer, 64, 64, 64, SDL_ALPHA_OPAQUE);
SDL_RenderDrawRect(app.renderer, &w->rect);
}
2015-10-20 13:51:49 +02:00
}
2015-10-20 13:51:49 +02:00
switch (w->type)
{
case WT_BUTTON:
SDL_RenderDrawRect(app.renderer, &w->rect);
drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, w->text);
break;
2015-10-20 13:51:49 +02:00
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]);
break;
2016-03-04 23:11:13 +01:00
2016-03-05 09:42:35 +01:00
case WT_CONTROL_CONFIG:
2016-03-04 23:11:13 +01:00
SDL_RenderDrawRect(app.renderer, &w->rect);
if (!app.awaitingWidgetInput || (app.awaitingWidgetInput && w != selectedWidget))
2016-03-05 09:42:35 +01:00
{
if (strlen(w->options[0]) && strlen(w->options[1]))
{
drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "%s or %s", w->options[0], w->options[1]);
}
else if (strlen(w->options[0]))
{
drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "%s", w->options[0]);
}
else if (strlen(w->options[1]))
{
drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "%s", w->options[1]);
}
else
{
drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "");
}
2016-03-05 09:42:35 +01:00
}
else
{
drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "...");
2016-03-05 09:42:35 +01:00
}
2016-03-04 23:11:13 +01:00
break;
2015-10-20 13:51:49 +02:00
}
if (!w->enabled)
{
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 192);
SDL_RenderFillRect(app.renderer, &w->rect);
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_NONE);
}
}
}
}
2015-11-26 09:16:29 +01:00
static void changeSelectedValue(Widget *w, int dir)
2015-10-20 13:51:49 +02:00
{
2015-11-26 09:16:29 +01:00
int oldOption = w->currentOption;
2015-11-26 09:16:29 +01:00
w->currentOption += dir;
2015-11-26 09:16:29 +01:00
w->currentOption = MIN(MAX(0, w->currentOption), w->numOptions - 1);
2015-11-26 09:16:29 +01:00
w->onChange(w->options[w->currentOption]);
2015-11-26 09:16:29 +01:00
if (oldOption != w->currentOption)
2015-10-20 13:51:49 +02:00
{
playSound(SND_GUI_CLICK);
}
}
void setWidgetOption(const char *name, const char *group, const char *value)
{
int i;
Widget *w = getWidget(name, group);
2015-10-20 13:51:49 +02:00
if (w)
{
for (i = 0 ; i < w->numOptions ; i++)
{
if (strcmp(w->options[i], value) == 0)
{
w->currentOption = i;
return;
}
}
}
}
static void handleMouse(void)
2015-10-20 13:51:49 +02:00
{
Widget *old;
if (selectedWidget && collision(selectedWidget->rect.x, selectedWidget->rect.y, selectedWidget->rect.w, selectedWidget->rect.h, app.mouse.x, app.mouse.y, 1, 1))
2015-10-20 13:51:49 +02:00
{
if (app.mouse.button[SDL_BUTTON_LEFT])
2015-10-20 13:51:49 +02:00
{
switch (selectedWidget->type)
{
case WT_BUTTON:
2015-11-26 09:16:29 +01:00
case WT_IMG_BUTTON:
if (selectedWidget->action)
{
playSound(SND_GUI_SELECT);
old = selectedWidget;
selectedWidget->action();
if (old == selectedWidget)
{
selectedWidget = NULL;
}
app.mouse.button[SDL_BUTTON_LEFT] = 0;
}
break;
2015-11-26 09:16:29 +01:00
case WT_SELECT_BUTTON:
changeSelectedValue(selectedWidget->parent, selectedWidget->value);
app.mouse.button[SDL_BUTTON_LEFT] = 0;
break;
case WT_CONTROL_CONFIG:
if (!app.awaitingWidgetInput)
{
app.awaitingWidgetInput = 1;
app.lastKeyPressed = app.lastButtonPressed = -1;
playSound(SND_GUI_SELECT);
}
app.mouse.button[SDL_BUTTON_LEFT] = 0;
break;
}
2015-10-20 13:51:49 +02:00
}
2015-11-23 15:52:11 +01:00
}
}
static void handleKeyboard(void)
{
Widget *old;
if (selectedWidget != NULL)
{
if (selectedWidget->type == WT_BUTTON)
{
if (app.keyboard[SDL_SCANCODE_SPACE] ||app.keyboard[SDL_SCANCODE_RETURN])
{
playSound(SND_GUI_SELECT);
old = selectedWidget;
selectedWidget->action();
if (old == selectedWidget)
{
selectedWidget = NULL;
}
app.keyboard[SDL_SCANCODE_SPACE] = app.keyboard[SDL_SCANCODE_RETURN] = 0;
}
}
}
}
static void handleControlWidgets(void)
{
if (app.lastKeyPressed == SDL_SCANCODE_BACKSPACE)
{
clearControlConfig(selectedWidget->name);
app.awaitingWidgetInput = 0;
}
else if (app.lastKeyPressed == SDL_SCANCODE_ESCAPE)
{
playSound(SND_GUI_CLOSE);
app.awaitingWidgetInput = 0;
}
else
{
if (app.lastKeyPressed != -1)
{
updateControlKey(selectedWidget->name);
app.awaitingWidgetInput = 0;
}
if (app.lastButtonPressed != -1)
{
updateControlButton(selectedWidget->name);
app.awaitingWidgetInput = 0;
}
}
if (!app.awaitingWidgetInput)
{
clearInput();
}
app.lastKeyPressed = app.lastButtonPressed = -1;
}
static void loadWidgets()
2015-10-20 13:51:49 +02:00
{
char **filenames;
char path[MAX_FILENAME_LENGTH];
int count, i;
filenames = getFileList("data/widgets", &count);
for (i = 0 ; i < count ; i++)
2015-10-20 13:51:49 +02:00
{
sprintf(path, "data/widgets/%s", filenames[i]);
loadWidgetSet(path);
free(filenames[i]);
2015-10-20 13:51:49 +02:00
}
free(filenames);
2015-10-20 13:51:49 +02:00
}
static void loadWidgetSet(char *filename)
{
cJSON *root, *node;
char *text;
Widget *w;
2015-10-20 13:51:49 +02:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(filename);
2015-10-20 13:51:49 +02:00
root = cJSON_Parse(text);
if (root)
2015-10-20 13:51:49 +02:00
{
for (node = root->child ; node != NULL ; node = node->next)
2015-10-20 13:51:49 +02:00
{
w = malloc(sizeof(Widget));
memset(w, 0, sizeof(Widget));
w->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring);
STRNCPY(w->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH);
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->enabled = 1;
w->visible = 1;
if (w->rect.x == -1)
{
w->rect.x = SCREEN_WIDTH / 2;
}
switch (w->type)
{
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;
case WT_CONTROL_CONFIG:
w->rect.w = cJSON_GetObjectItem(node, "w")->valueint;
w->rect.h = cJSON_GetObjectItem(node, "h")->valueint;
break;
default:
printf("Widget type %d not handled\n", w->type);
exit(1);
break;
}
tail->next = w;
tail = w;
2015-10-20 13:51:49 +02:00
}
cJSON_Delete(root);
2015-10-20 13:51:49 +02:00
}
2016-03-12 19:22:48 +01:00
else
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Failed to load '%s'", filename);
}
2015-10-20 13:51:49 +02:00
free(text);
}
static void createOptions(Widget *w, char *options)
{
int i;
char *option;
2015-10-20 13:51:49 +02:00
w->numOptions = 1;
2015-10-20 13:51:49 +02:00
for (i = 0 ; i < strlen(options) ; i++)
{
if (options[i] == ';')
{
w->numOptions++;
}
}
2015-10-20 13:51:49 +02:00
w->options = malloc(w->numOptions * sizeof(char*));
2015-10-20 13:51:49 +02:00
i = 0;
option = strtok(options, ";");
while (option)
{
w->options[i] = malloc(strlen(option) + 1);
strcpy(w->options[i], option);
2015-10-20 13:51:49 +02:00
option = strtok(NULL, ";");
2015-10-20 13:51:49 +02:00
i++;
}
}
2015-11-26 09:16:29 +01:00
static void createSelectButtons(Widget *w)
{
int i;
Widget *btn;
2015-11-26 09:16:29 +01:00
for (i = 0 ; i < 2 ; i++)
{
btn = malloc(sizeof(Widget));
memcpy(btn, w, sizeof(Widget));
strcpy(btn->name, "");
2015-11-26 09:16:29 +01:00
btn->type = WT_SELECT_BUTTON;
btn->parent = w;
2015-11-26 09:16:29 +01:00
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;
}
2015-11-26 09:16:29 +01:00
tail->next = btn;
tail = btn;
}
}
2015-10-20 13:51:49 +02:00
void destroyWidgets(void)
{
int i;
Widget *w = head.next;
Widget *next;
while (w)
{
for (i = 0 ; i < w->numOptions ; i++)
{
free(w->options[i]);
}
2016-03-12 19:22:48 +01:00
free(w->options);
2015-10-20 13:51:49 +02:00
next = w->next;
free(w);
w = next;
}
head.next = NULL;
}