tbftss/src/system/modalDialog.c

144 lines
3.4 KiB
C
Raw Normal View History

2016-02-23 08:19:31 +01:00
/*
2018-04-29 11:01:09 +02:00
Copyright (C) 2015-2018 Parallel Realities
2016-02-23 08:19:31 +01: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 "modalDialog.h"
static int enableTimer;
2016-02-23 08:19:31 +01:00
static Widget *ok;
static Widget *okCancelOK;
static Widget *okCancelCancel;
2016-02-23 23:20:07 +01:00
static char textBuffer[MAX_DESCRIPTION_LENGTH];
2016-02-23 08:19:31 +01:00
void initModalDialog(void)
{
ok = getWidget("ok", "ok");
2016-02-23 23:20:07 +01:00
ok->action = NULL;
ok->isModal = 1;
2016-02-23 08:19:31 +01:00
okCancelOK = getWidget("ok", "okCancel");
2016-02-23 23:20:07 +01:00
okCancelOK->action = NULL;
okCancelOK->isModal = 1;
2016-02-23 08:19:31 +01:00
okCancelCancel = getWidget("cancel", "okCancel");
2016-02-23 23:20:07 +01:00
okCancelCancel->action = NULL;
okCancelCancel->isModal = 1;
enableTimer = 0;
2016-02-23 23:20:07 +01:00
}
void showOKDialog(void (*callback)(void), const char *format, ...)
{
va_list args;
memset(&textBuffer, '\0', sizeof(textBuffer));
va_start(args, format);
vsprintf(textBuffer, format, args);
va_end(args);
2016-02-23 23:20:07 +01:00
STRNCPY(app.modalDialog.message, textBuffer, MAX_DESCRIPTION_LENGTH);
2016-02-23 23:20:07 +01:00
app.modalDialog.type = MD_OK;
2016-02-23 23:20:07 +01:00
ok->action = callback;
enableTimer = FPS;
2016-03-04 23:11:13 +01:00
ok->enabled = okCancelOK->enabled = okCancelCancel->enabled = 0;
2016-02-23 23:20:07 +01:00
}
void showOKCancelDialog(void (*okCallback)(void), void (*cancelCallback)(void), const char *format, ...)
{
va_list args;
memset(&textBuffer, '\0', sizeof(textBuffer));
va_start(args, format);
vsprintf(textBuffer, format, args);
va_end(args);
2016-02-23 23:20:07 +01:00
STRNCPY(app.modalDialog.message, textBuffer, MAX_DESCRIPTION_LENGTH);
2016-02-23 23:20:07 +01:00
app.modalDialog.type = MD_OK_CANCEL;
2016-02-23 23:20:07 +01:00
okCancelOK->action = okCallback;
okCancelCancel->action = cancelCallback;
enableTimer = FPS / 4;
2016-03-04 23:11:13 +01:00
ok->enabled = okCancelOK->enabled = okCancelCancel->enabled = 0;
2016-02-23 08:19:31 +01:00
}
void doModalDialog(void)
{
doWidgets();
2016-02-23 08:19:31 +01:00
clearInput();
enableTimer = MAX(enableTimer - 1, 0);
if (!enableTimer)
{
2016-03-04 23:11:13 +01:00
ok->enabled = okCancelOK->enabled = okCancelCancel->enabled = 1;
}
2016-02-23 08:19:31 +01:00
}
void drawModalDialog(void)
{
SDL_Rect r;
2018-12-13 08:59:31 +01:00
2018-12-06 09:37:19 +01:00
app.textWidth = 700;
2016-02-23 08:19:31 +01:00
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 96);
SDL_RenderFillRect(app.renderer, NULL);
2018-12-17 09:30:47 +01:00
SDL_SetRenderTarget(app.renderer, app.uiBuffer);
2016-02-23 08:19:31 +01:00
r.w = 800;
r.h = getWrappedTextHeight(app.modalDialog.message, 24) + 100;
2018-12-13 08:59:31 +01:00
r.x = (UI_WIDTH / 2) - (r.w / 2);
r.y = (UI_HEIGHT / 2) - (r.h / 2);
2016-02-23 08:19:31 +01:00
ok->rect.y = okCancelOK->rect.y = okCancelCancel->rect.y = r.y + r.h - 50;
2016-02-23 08:19:31 +01:00
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 225);
SDL_RenderFillRect(app.renderer, &r);
2016-02-23 08:19:31 +01:00
SDL_SetRenderDrawColor(app.renderer, 255, 255, 255, 200);
SDL_RenderDrawRect(app.renderer, &r);
2016-02-23 08:19:31 +01:00
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_NONE);
2018-12-13 08:59:31 +01:00
drawText(UI_WIDTH / 2, r.y + 10, 24, TA_CENTER, colors.white, app.modalDialog.message);
2016-02-23 08:19:31 +01:00
switch (app.modalDialog.type)
{
case MD_OK:
drawWidgets("ok");
break;
2016-02-23 08:19:31 +01:00
case MD_OK_CANCEL:
drawWidgets("okCancel");
break;
}
2018-12-06 09:37:19 +01:00
app.textWidth = 0;
2018-12-13 08:59:31 +01:00
SDL_SetRenderTarget(app.renderer, app.backBuffer);
2016-02-23 08:19:31 +01:00
}