tbftss/src/system/widgets.c

609 lines
13 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
2019-01-01 17:14:11 +01:00
Copyright (C) 2015-2019 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);
static void updateSelectWidgets(void);
2015-10-20 13:51:49 +02:00
static Widget head;
static Widget *tail;
static Widget *selectedWidget;
2019-01-16 09:30:01 +01:00
static Widget *hoverWidget;
2018-12-06 09:37:19 +01:00
static AtlasImage *optionsLeft;
static AtlasImage *optionsRight;
2015-10-20 13:51:49 +02:00
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;
2018-12-06 09:37:19 +01:00
optionsLeft = getAtlasImage("gfx/widgets/optionsLeft.png");
optionsRight = getAtlasImage("gfx/widgets/optionsRight.png");
loadWidgets();
app.awaitingWidgetInput = drawingWidgets = 0;
2015-10-20 13:51:49 +02:00
}
void doWidgets(void)
{
if (drawingWidgets)
{
updateSelectWidgets();
2022-08-22 20:40:53 +02:00
2015-11-23 15:52:11 +01:00
handleMouse();
handleKeyboard();
2019-11-07 09:13:57 +01:00
if (app.awaitingWidgetInput)
{
handleControlWidgets();
}
2015-10-20 13:51:49 +02:00
}
2022-08-22 20:40:53 +02:00
#if !defined(__amigaos4__)
2019-01-16 09:30:01 +01:00
if (hoverWidget != selectedWidget)
{
selectedWidget = NULL;
}
2022-08-22 20:40:53 +02:00
#endif
2015-10-20 13:51:49 +02:00
drawingWidgets = 0;
}
static void updateSelectWidgets(void)
{
2022-08-22 20:40:53 +02:00
#if !defined(__amigaos4__)
Widget *w;
for (w = head.next; w != NULL ; w = w->next)
{
if (w->type == WT_SELECT_BUTTON && w->parent)
{
w->visible = 1;
2019-11-07 09:13:57 +01:00
if (w->value == -1 && w->parent->value == 0)
{
w->visible = 0;
}
2019-11-07 09:13:57 +01:00
if (w->value == 1 && w->parent->value == w->parent->numOptions - 1)
{
w->visible = 0;
}
}
}
2022-08-22 20:40:53 +02:00
#endif
}
2015-10-20 13:51:49 +02:00
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;
2019-11-07 09:13:57 +01:00
2019-01-16 09:30:01 +01:00
hoverWidget = NULL;
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
{
2018-12-12 09:32:32 +01:00
mouseOver = (w->type != WT_SELECT && w->enabled && collision(w->rect.x, w->rect.y, w->rect.w, w->rect.h, app.uiMouse.x, app.uiMouse.y, 1, 1));
2019-01-16 09:30:01 +01:00
if (mouseOver)
{
2019-01-16 09:30:01 +01:00
hoverWidget = w;
2019-11-07 09:13:57 +01:00
2019-01-16 09:30:01 +01:00
if (selectedWidget != w)
{
2019-01-16 09:30:01 +01:00
if (w->type == WT_BUTTON || w->type == WT_IMG_BUTTON || w->type == WT_CONTROL_CONFIG)
{
playSound(SND_GUI_CLICK);
}
2019-01-16 09:30:01 +01:00
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
{
2019-01-16 09:30:01 +01:00
setAtlasColor(255, 255, 255, 255);
2019-11-07 09:13:57 +01:00
2019-01-16 09:30:01 +01:00
if (selectedWidget == w)
{
setAtlasColor(128, 192, 255, 255);
}
2019-11-07 09:13:57 +01: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
{
2018-12-11 09:24:25 +01:00
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 255);
2015-11-26 09:16:29 +01:00
SDL_RenderFillRect(app.renderer, &w->rect);
2015-11-26 09:16:29 +01:00
if (w == selectedWidget)
{
2018-12-11 09:24:25 +01:00
SDL_SetRenderDrawColor(app.renderer, 64, 128, 200, 255);
2015-11-26 09:16:29 +01:00
SDL_RenderFillRect(app.renderer, &w->rect);
2018-12-11 09:24:25 +01:00
SDL_SetRenderDrawColor(app.renderer, 128, 192, 255, 255);
2015-11-26 09:16:29 +01:00
SDL_RenderDrawRect(app.renderer, &w->rect);
}
else
{
2018-12-11 09:24:25 +01:00
SDL_SetRenderDrawColor(app.renderer, 64, 64, 64, 255);
2015-11-26 09:16:29 +01:00
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->value]);
2015-10-20 13:51:49 +02:00
break;
2019-11-07 09:13:57 +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);
2019-11-07 09:13:57 +01:00
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
{
int oldOption = w->value;
w->value += dir;
w->value = MIN(MAX(0, w->value), w->numOptions - 1);
w->onChange(w->options[w->value]);
if (oldOption != w->value)
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->value = i;
2015-10-20 13:51:49 +02:00
return;
}
}
}
}
static void handleMouse(void)
2015-10-20 13:51:49 +02:00
{
Widget *old;
2018-12-12 09:32:32 +01:00
if (selectedWidget && collision(selectedWidget->rect.x, selectedWidget->rect.y, selectedWidget->rect.w, selectedWidget->rect.h, app.uiMouse.x, app.uiMouse.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;
2019-11-07 09:13:57 +01:00
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);
2019-11-07 09:13:57 +01:00
app.awaitingWidgetInput = 0;
}
else if (app.lastKeyPressed == SDL_SCANCODE_ESCAPE)
{
playSound(SND_GUI_CLOSE);
app.awaitingWidgetInput = 0;
}
else
{
2022-08-22 20:40:53 +02:00
if (app.lastKeyPressed != -1 && selectedWidget->name)
{
updateControlKey(selectedWidget->name);
2019-11-07 09:13:57 +01:00
app.awaitingWidgetInput = 0;
}
2019-11-07 09:13:57 +01:00
if (app.lastButtonPressed != -1)
{
updateControlButton(selectedWidget->name);
2019-11-07 09:13:57 +01:00
app.awaitingWidgetInput = 0;
}
}
2019-11-07 09:13:57 +01:00
if (!app.awaitingWidgetInput)
{
clearInput();
}
2019-11-07 09:13:57 +01:00
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);
2019-11-07 09:13:57 +01:00
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)
{
2018-12-14 09:00:16 +01:00
w->rect.x = UI_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:
2018-12-06 09:37:19 +01:00
w->texture = getAtlasImage(cJSON_GetObjectItem(node, "texture")->valuestring);
w->rect.w = w->texture->rect.w;
w->rect.h = w->texture->rect.h;
break;
case WT_SELECT:
2018-05-06 13:38:48 +02:00
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;
2019-11-07 09:13:57 +01:00
case WT_CONTROL_CONFIG:
w->rect.w = cJSON_GetObjectItem(node, "w")->valueint;
w->rect.h = cJSON_GetObjectItem(node, "h")->valueint;
break;
2019-11-07 09:13:57 +01:00
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);
}
2019-11-07 09:13:57 +01:00
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)
{
2018-07-07 15:47:04 +02:00
option = _(option);
2019-11-07 09:13:57 +01:00
2015-10-20 13:51:49 +02:00
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;
}
}
2018-05-06 19:50:11 +02:00
void autoSizeWidgetButtons(char *group, int recenter)
{
int width, height, maxWidth;
Widget *w;
2019-11-07 09:13:57 +01:00
2018-05-06 19:50:11 +02:00
maxWidth = 0;
2019-11-07 09:13:57 +01:00
2018-05-06 19:50:11 +02:00
for (w = head.next; w != NULL ; w = w->next)
{
if (strcmp(w->group, group) == 0 && w->type == WT_BUTTON)
{
2018-12-06 09:37:19 +01:00
calcTextDimensions(w->text, 20, &width, &height);
2019-11-07 09:13:57 +01:00
2018-05-06 19:50:11 +02:00
maxWidth = MAX(MAX(w->rect.w, width), maxWidth);
}
}
2019-11-07 09:13:57 +01:00
2018-05-06 19:50:11 +02:00
for (w = head.next; w != NULL ; w = w->next)
{
if (strcmp(w->group, group) == 0 && w->type == WT_BUTTON)
{
w->rect.w = maxWidth + 20;
2019-11-07 09:13:57 +01:00
2018-05-06 19:50:11 +02:00
if (recenter)
{
2018-12-14 09:00:16 +01:00
w->rect.x = (UI_WIDTH / 2) - (w->rect.w / 2);
2018-05-06 19:50:11 +02:00
}
}
}
}
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]);
}
2019-11-07 09:13:57 +01:00
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;
}
2022-08-22 20:40:53 +02:00