blobwarsAttrition/src/system/widgets.c

516 lines
10 KiB
C
Raw Normal View History

2018-01-21 12:26:02 +01:00
/*
Copyright (C) 2018 Parallel Realities
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"
2018-02-14 09:32:09 +01:00
static void loadWidgetGroup(char *filename);
static void loadWidgets(void);
2018-02-14 20:12:29 +01:00
static void createWidgetOptions(Widget *w, char *options);
static void selectWidget(int dir);
2018-02-15 22:38:26 +01:00
static void updateWidgetValue(int dir);
2018-02-20 20:24:17 +01:00
static void handleInputWidget(void);
2018-01-24 09:14:11 +01:00
2018-02-14 20:12:29 +01:00
static Widget widgets[MAX_WIDGETS];
2018-01-24 09:14:11 +01:00
static Widget *selectedWidget;
2018-02-14 20:12:29 +01:00
static int widgetIndex;
static int numWidgets;
2018-02-20 12:29:50 +01:00
static Atlas *left;
static Atlas *right;
static Texture *atlasTexture;
2018-02-25 13:12:31 +01:00
static SDL_Rect frame;
2018-01-24 09:14:11 +01:00
void initWidgets(void)
{
2018-02-14 20:12:29 +01:00
memset(widgets, 0, sizeof(Widget) * MAX_WIDGETS);
numWidgets = 0;
2018-02-14 09:32:09 +01:00
2018-02-15 23:56:22 +01:00
selectedWidget = NULL;
2018-02-14 09:32:09 +01:00
loadWidgets();
2018-02-20 12:29:50 +01:00
atlasTexture = getTexture("gfx/atlas/atlas.png");
left = getImageFromAtlas("gfx/ui/left.png");
right = getImageFromAtlas("gfx/ui/right.png");
2018-01-24 09:14:11 +01:00
}
2018-02-14 20:12:29 +01:00
void doWidgets(void)
2018-01-24 09:14:11 +01:00
{
2018-02-20 20:24:17 +01:00
if (!app.awaitingWidgetInput)
2018-01-24 09:14:11 +01:00
{
2018-02-20 20:24:17 +01:00
if (isControl(CONTROL_UP) || app.keyboard[SDL_SCANCODE_UP])
{
selectWidget(-1);
2018-02-14 20:12:29 +01:00
2018-02-20 20:24:17 +01:00
app.keyboard[SDL_SCANCODE_UP] = 0;
clearControl(CONTROL_UP);
}
2018-01-24 09:14:11 +01:00
2018-02-20 20:24:17 +01:00
if (isControl(CONTROL_DOWN) || app.keyboard[SDL_SCANCODE_DOWN])
{
selectWidget(1);
2018-02-14 20:12:29 +01:00
2018-02-20 20:24:17 +01:00
app.keyboard[SDL_SCANCODE_DOWN] = 0;
clearControl(CONTROL_DOWN);
}
2018-02-14 20:12:29 +01:00
2018-02-20 20:24:17 +01:00
if (isControl(CONTROL_LEFT) || app.keyboard[SDL_SCANCODE_LEFT])
{
updateWidgetValue(-1);
}
2018-02-14 20:12:29 +01:00
2018-02-20 20:24:17 +01:00
if (isControl(CONTROL_RIGHT) || app.keyboard[SDL_SCANCODE_RIGHT])
{
updateWidgetValue(1);
}
2018-02-15 22:38:26 +01:00
2018-02-20 20:24:17 +01:00
if (isControl(CONTROL_FIRE) || app.keyboard[SDL_SCANCODE_RETURN] || app.keyboard[SDL_SCANCODE_SPACE])
2018-02-20 18:50:26 +01:00
{
2018-02-20 20:24:17 +01:00
if (selectedWidget->type != WT_INPUT)
{
selectedWidget->action();
}
else if (!isControl(CONTROL_FIRE))
{
app.awaitingWidgetInput = 1;
app.lastKeyPressed = 0;
app.lastButtonPressed = -1;
}
app.keyboard[SDL_SCANCODE_SPACE] = app.keyboard[SDL_SCANCODE_RETURN] = 0;
clearControl(CONTROL_FIRE);
2018-02-20 18:50:26 +01:00
}
2018-02-20 20:24:17 +01:00
}
else
{
handleInputWidget();
2018-02-15 22:38:26 +01:00
}
}
static void updateWidgetValue(int dir)
{
if (selectedWidget->type == WT_SLIDER)
{
2018-02-20 20:24:17 +01:00
selectedWidget->value[0] = limit(selectedWidget->value[0] + dir, selectedWidget->minValue, selectedWidget->maxValue);
2018-02-15 22:38:26 +01:00
selectedWidget->action();
}
else if (selectedWidget->type == WT_SPINNER)
{
2018-02-20 20:24:17 +01:00
selectedWidget->value[0] = limit(selectedWidget->value[0] + dir, 0, selectedWidget->numOptions - 1);
2018-02-15 22:38:26 +01:00
selectedWidget->action();
2018-02-20 12:29:50 +01:00
app.keyboard[SDL_SCANCODE_LEFT] = app.keyboard[SDL_SCANCODE_RIGHT] = 0;
clearControl(CONTROL_LEFT);
clearControl(CONTROL_RIGHT);
playSound(SND_MENU_SELECT, 0);
2018-02-14 20:12:29 +01:00
}
2018-01-24 09:14:11 +01:00
}
2018-02-20 20:24:17 +01:00
static void handleInputWidget(void)
{
if (app.keyboard[SDL_SCANCODE_ESCAPE])
{
app.awaitingWidgetInput = 0;
}
else if (app.keyboard[SDL_SCANCODE_BACKSPACE])
{
selectedWidget->value[0] = 0;
selectedWidget->value[1] = -1;
app.awaitingWidgetInput = 0;
}
else if (app.lastKeyPressed != 0 || app.lastButtonPressed != -1)
{
if (app.lastKeyPressed != 0)
{
selectedWidget->value[0] = app.lastKeyPressed;
}
if (app.lastButtonPressed != -1)
{
selectedWidget->value[1] = app.lastButtonPressed;
}
app.awaitingWidgetInput = 0;
}
}
2018-02-14 20:12:29 +01:00
void drawWidgets(void)
2018-01-24 09:14:11 +01:00
{
2018-02-20 13:17:06 +01:00
int i, j, x, tw, th, outline;
2018-01-24 09:14:11 +01:00
Widget *w;
2018-02-14 20:12:29 +01:00
for (i = 0 ; i < numWidgets ; i++)
2018-01-24 09:14:11 +01:00
{
2018-02-14 20:12:29 +01:00
w = &widgets[i];
if (w->visible)
2018-01-24 09:14:11 +01:00
{
2018-02-20 12:29:50 +01:00
if (w != selectedWidget)
{
drawRect(w->x, w->y, w->w, w->h, 0, 0, 0, 255);
drawOutlineRect(w->x, w->y, w->w, w->h, 0, 128, 0, 255);
}
else
{
drawRect(w->x, w->y, w->w, w->h, 0, 128, 0, 255);
drawOutlineRect(w->x, w->y, w->w, w->h, 0, 255, 0, 255);
}
drawText(w->x + w->w / 2, w->y + 2, 24, TA_CENTER, colors.white, w->label);
2018-02-20 13:17:06 +01:00
outline = (w == selectedWidget) ? 255 : 192;
2018-02-14 20:12:29 +01:00
switch (w->type)
{
2018-02-15 22:38:26 +01:00
case WT_SLIDER:
2018-02-20 20:24:17 +01:00
drawRect(w->x + w->w + 25, w->y, 500 * (w->value[0] * 1.0 / w->maxValue), 40, 0, 128, 0, 255);
2018-02-20 13:17:06 +01:00
drawOutlineRect(w->x + w->w + 25, w->y, 500, 40, 0, outline, 0, 255);
2018-02-14 20:12:29 +01:00
break;
case WT_SPINNER:
2018-02-20 13:17:06 +01:00
x = w->x + w->w + 25;
2018-02-20 12:29:50 +01:00
for (j = 0 ; j < w->numOptions ; j++)
{
2018-02-20 13:17:06 +01:00
textSize(w->options[j], 24, &tw, &th);
tw += 25;
2018-02-20 20:24:17 +01:00
if (j == w->value[0])
2018-02-20 12:29:50 +01:00
{
2018-02-20 13:17:06 +01:00
drawRect(x, w->y, tw, w->h, 0, 128, 0, 255);
drawOutlineRect(x, w->y, tw, w->h, 0, outline, 0, 255);
2018-02-20 12:29:50 +01:00
}
2018-02-20 13:17:06 +01:00
drawText(x + tw / 2, w->y + 2, 24, TA_CENTER, colors.white, w->options[j]);
x += tw + 25;
2018-02-20 12:29:50 +01:00
}
2018-02-14 20:12:29 +01:00
break;
2018-01-24 09:14:11 +01:00
2018-02-14 20:12:29 +01:00
case WT_INPUT:
2018-02-20 18:50:26 +01:00
x = w->x + w->w + 25;
drawRect(x, w->y, 200, w->h, 0, 0, 0, 255);
drawOutlineRect(x, w->y, 200, w->h, 0, outline, 0, 255);
2018-03-19 09:08:29 +01:00
2018-02-20 20:24:17 +01:00
if (app.awaitingWidgetInput && w == selectedWidget)
{
drawText(x + 100, w->y + 2, 24, TA_CENTER, colors.white, "...");
}
else if (w->value[0] != -1 && w->value[1] != -1)
{
drawText(x + 100, w->y + 2, 24, TA_CENTER, colors.white, "%s or Btn %d", SDL_GetScancodeName(w->value[0]), w->value[1]);
}
else if (w->value[0] != -1)
{
drawText(x + 100, w->y + 2, 24, TA_CENTER, colors.white, "%s", SDL_GetScancodeName(w->value[0]));
}
else if (w->value[1] != -1)
{
drawText(x + 100, w->y + 2, 24, TA_CENTER, colors.white, "Btn %d", w->value[1]);
}
2018-02-14 20:12:29 +01:00
break;
}
2018-03-19 09:08:29 +01:00
if (w->disabled)
{
drawRect(w->x, w->y, w->w, w->h, 0, 0, 0, 160);
}
2018-02-14 20:12:29 +01:00
}
2018-01-24 09:14:11 +01:00
}
}
2018-02-25 13:12:31 +01:00
void drawWidgetFrame(void)
{
drawRect(frame.x, frame.y, frame.w, frame.h, 0, 0, 0, 192);
drawOutlineRect(frame.x, frame.y, frame.w, frame.h, 255, 255, 255, 255);
}
2018-02-14 20:12:29 +01:00
static void selectWidget(int dir)
2018-01-24 09:14:11 +01:00
{
2018-03-19 09:08:29 +01:00
int oldWidgetIndex, valid;
oldWidgetIndex = widgetIndex;
valid = 0;
2018-02-15 23:56:22 +01:00
2018-02-14 20:12:29 +01:00
do
2018-01-24 09:14:11 +01:00
{
2018-02-14 20:12:29 +01:00
widgetIndex += dir;
2018-01-24 09:14:11 +01:00
2018-02-14 20:12:29 +01:00
if (widgetIndex < 0)
{
widgetIndex = numWidgets - 1;
}
if (widgetIndex >= numWidgets)
{
widgetIndex = 0;
}
selectedWidget = &widgets[widgetIndex];
2018-03-19 09:08:29 +01:00
valid = selectedWidget->visible && !selectedWidget->disabled;
2018-02-14 20:12:29 +01:00
2018-03-19 09:08:29 +01:00
} while (!valid);
2018-02-15 23:56:22 +01:00
if (oldWidgetIndex != widgetIndex)
{
playSound(SND_MENU_NAV, 0);
}
2018-01-24 09:14:11 +01:00
}
2018-02-14 20:12:29 +01:00
Widget *getWidget(char *name, char *group)
2018-01-24 09:14:11 +01:00
{
2018-02-14 20:12:29 +01:00
int i;
2018-01-24 09:14:11 +01:00
Widget *w;
2018-02-14 20:12:29 +01:00
for (i = 0 ; i < numWidgets ; i++)
2018-01-24 09:14:11 +01:00
{
2018-02-14 20:12:29 +01:00
w = &widgets[i];
if (strcmp(w->name, name) == 0 && strcmp(w->group, group) == 0)
2018-01-24 09:14:11 +01:00
{
return w;
}
}
2018-02-14 20:12:29 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "No such widget '%s', '%s'", name, group);
exit(1);
2018-01-24 09:14:11 +01:00
return NULL;
}
2018-03-19 09:08:29 +01:00
void setSelectedWidget(char *name, char *group)
{
2018-03-19 23:51:37 +01:00
Widget *w;
int i;
for (i = 0 ; i < numWidgets ; i++)
{
w = &widgets[i];
if (strcmp(w->name, name) == 0 && strcmp(w->group, group) == 0)
{
widgetIndex = i;
selectedWidget = w;
return;
}
}
}
Widget *getSelectedWidget(void)
{
return selectedWidget;
2018-03-19 09:08:29 +01:00
}
Widget *selectWidgetAt(int x, int y)
{
Widget *w;
int i;
for (i = 0 ; i < numWidgets ; i++)
{
w = &widgets[i];
if (w->visible && collision(w->x, w->y, w->w, w->h, x, y, 1, 1))
{
if (w != selectedWidget)
{
playSound(SND_MENU_NAV, 0);
}
widgetIndex = i;
selectedWidget = w;
return w;
}
}
selectedWidget = NULL;
return NULL;
}
2018-02-02 20:10:12 +01:00
void hideAllWidgets(void)
2018-01-24 09:14:11 +01:00
{
2018-02-14 20:12:29 +01:00
int i;
2018-01-24 09:14:11 +01:00
2018-02-14 20:12:29 +01:00
for (i = 0 ; i < numWidgets ; i++)
2018-01-24 09:14:11 +01:00
{
2018-02-14 20:12:29 +01:00
widgets[i].visible = 0;
2018-01-24 09:14:11 +01:00
}
selectedWidget = NULL;
2018-02-25 13:12:31 +01:00
frame.x = frame.y = frame.w = frame.h = 0;
2018-01-24 09:14:11 +01:00
}
void showWidgetGroup(char *group)
{
2018-02-14 20:12:29 +01:00
int i;
2018-01-24 09:14:11 +01:00
Widget *w;
2018-02-02 20:10:12 +01:00
hideAllWidgets();
2018-01-24 09:14:11 +01:00
2018-02-25 13:12:31 +01:00
frame.x = frame.y = SCREEN_WIDTH;
frame.w = frame.h = 0;
2018-02-14 20:12:29 +01:00
for (i = 0 ; i < numWidgets ; i++)
2018-01-24 09:14:11 +01:00
{
2018-02-14 20:12:29 +01:00
w = &widgets[i];
2018-01-24 09:14:11 +01:00
if (strcmp(w->group, group) == 0)
{
if (selectedWidget == NULL)
{
selectedWidget = w;
2018-02-15 23:56:22 +01:00
widgetIndex = i;
2018-01-24 09:14:11 +01:00
}
w->visible = 1;
2018-02-25 13:12:31 +01:00
frame.x = MIN(w->x - 25, frame.x);
frame.y = MIN(w->y - 25, frame.y);
frame.w = MAX(w->w + 50, frame.w);
frame.h = MAX(w->y + w->h + 25, frame.h);
2018-01-24 09:14:11 +01:00
}
}
2018-02-25 13:12:31 +01:00
frame.h -= frame.y;
2018-01-24 09:14:11 +01:00
}
2018-02-14 09:32:09 +01:00
static void loadWidgets(void)
{
char **filenames;
char path[MAX_FILENAME_LENGTH];
int count, i;
filenames = getFileList("data/widgets", &count);
for (i = 0 ; i < count ; i++)
{
sprintf(path, "data/widgets/%s", filenames[i]);
loadWidgetGroup(path);
free(filenames[i]);
}
free(filenames);
}
static void loadWidgetGroup(char *filename)
2018-01-24 09:14:11 +01:00
{
2018-02-14 09:32:09 +01:00
cJSON *root, *node;
char *text;
Widget *w;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(filename);
root = cJSON_Parse(text);
for (node = root->child ; node != NULL ; node = node->next)
{
2018-03-19 09:08:29 +01:00
if (numWidgets >= MAX_WIDGETS)
2018-02-14 20:12:29 +01:00
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Out of widget space.");
exit(1);
}
w = &widgets[numWidgets];
2018-02-14 09:32:09 +01:00
STRNCPY(w->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH);
STRNCPY(w->group, cJSON_GetObjectItem(node, "group")->valuestring, MAX_NAME_LENGTH);
2018-04-01 18:41:45 +02:00
STRNCPY(w->label, _(cJSON_GetObjectItem(node, "label")->valuestring), MAX_NAME_LENGTH);
2018-02-14 09:32:09 +01:00
w->x = cJSON_GetObjectItem(node, "x")->valueint;
w->y = cJSON_GetObjectItem(node, "y")->valueint;
2018-02-14 23:32:03 +01:00
w->w = cJSON_GetObjectItem(node, "w")->valueint;
w->h = cJSON_GetObjectItem(node, "h")->valueint;
2018-02-14 09:32:09 +01:00
w->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring);
2018-02-15 19:04:37 +01:00
if (w->x == -1)
{
w->x = (SCREEN_WIDTH - w->w) / 2;
}
2018-02-14 09:32:09 +01:00
switch (w->type)
{
case WT_SPINNER:
2018-02-14 20:12:29 +01:00
createWidgetOptions(w, cJSON_GetObjectItem(node, "options")->valuestring);
2018-02-14 09:32:09 +01:00
break;
2018-02-20 12:29:50 +01:00
case WT_SLIDER:
w->minValue = cJSON_GetObjectItem(node, "minValue")->valueint;
w->maxValue = cJSON_GetObjectItem(node, "maxValue")->valueint;
break;
2018-02-20 20:24:17 +01:00
case WT_INPUT:
break;
2018-02-14 09:32:09 +01:00
2018-02-14 20:12:29 +01:00
default:
2018-02-14 09:32:09 +01:00
break;
}
2018-03-19 09:08:29 +01:00
numWidgets++;
2018-02-14 09:32:09 +01:00
}
cJSON_Delete(root);
2018-01-24 09:14:11 +01:00
2018-02-14 09:32:09 +01:00
free(text);
2018-01-24 09:14:11 +01:00
}
2018-02-14 20:12:29 +01:00
static void createWidgetOptions(Widget *w, char *options)
{
int i;
char *option;
w->numOptions = 1;
for (i = 0 ; i < strlen(options) ; i++)
{
if (options[i] == '|')
{
w->numOptions++;
}
}
w->options = malloc(w->numOptions * sizeof(char*));
i = 0;
2018-04-02 09:53:24 +02:00
option = strtok(options, "|");
2018-02-14 20:12:29 +01:00
while (option)
{
2018-04-02 09:53:24 +02:00
w->options[i] = malloc(strlen(_(option)) + 1);
strcpy(w->options[i], _(option));
2018-02-14 20:12:29 +01:00
2018-04-02 09:53:24 +02:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "widget.option[%d] = %s", i, w->options[i]);
2018-02-14 20:12:29 +01:00
2018-04-02 09:53:24 +02:00
option = strtok(NULL, "|");
2018-02-14 20:12:29 +01:00
i++;
}
}
2018-01-24 09:14:11 +01:00
void destroy(void)
2018-01-21 12:26:02 +01:00
{
}