tbftss/src/system/controls.c

188 lines
4.7 KiB
C
Raw Normal View History

2016-03-05 09:42:35 +01:00
/*
Copyright (C) 2015-2016 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 "controls.h"
static const char *controlName[CONTROL_MAX];
static Widget *controlWidget[CONTROL_MAX];
void initControls(void)
{
int i;
controlName[CONTROL_FIRE] = _("Fire");
controlName[CONTROL_ACCELERATE] = _("Accelerate");
controlName[CONTROL_BOOST] = _("Boost");
controlName[CONTROL_ECM] = _("ECM");
controlName[CONTROL_BRAKE] = _("Brake");
controlName[CONTROL_TARGET] = _("Select Target");
controlName[CONTROL_MISSILE] = _("Fire Missile");
controlName[CONTROL_GUNS] = _("Cycle Guns");
controlName[CONTROL_RADAR] = _("Cycle Radar");
controlName[CONTROL_NEXT_FIGHTER] = _("Next Fighter");
controlName[CONTROL_PREV_FIGHTER] = _("Previous Fighter");
for (i = 0 ; i < CONTROL_MAX ; i++)
{
controlWidget[i] = getWidget(getLookupName("CONTROL_", i), "controls");
2016-03-05 09:42:35 +01:00
controlWidget[i]->numOptions = 2;
controlWidget[i]->options = malloc(2 * sizeof(char*));
controlWidget[i]->options[0] = malloc(sizeof(char) * MAX_NAME_LENGTH);
controlWidget[i]->options[1] = malloc(sizeof(char) * MAX_NAME_LENGTH);
strcpy(controlWidget[i]->options[0], "");
strcpy(controlWidget[i]->options[1], "");
}
}
void initControlsDisplay(void)
{
int i;
for (i = 0 ; i < CONTROL_MAX ; i++)
{
strcpy(controlWidget[i]->options[0], "");
strcpy(controlWidget[i]->options[1], "");
if (app.keyControls[i] != -1)
2016-03-05 09:42:35 +01:00
{
sprintf(controlWidget[i]->options[0], "%s", SDL_GetScancodeName(app.keyControls[i]));
}
if (app.mouseControls[i] != -1)
{
sprintf(controlWidget[i]->options[1], "Btn %d", app.mouseControls[i]);
}
}
}
2016-03-05 14:21:17 +01:00
int isControl(int type)
{
2016-03-05 14:21:17 +01:00
int key = app.keyControls[type];
int btn = app.mouseControls[type];
return ((key != -1 && app.keyboard[key]) || (btn != -1 && app.mouse.button[btn]));
}
2016-03-27 12:21:23 +02:00
int isAcceptControl(void)
{
return (app.keyboard[SDL_SCANCODE_SPACE] ||app.keyboard[SDL_SCANCODE_RETURN] || isControl(CONTROL_FIRE));
}
void clearControl(int type)
{
2016-03-05 14:21:17 +01:00
int key = app.keyControls[type];
int btn = app.mouseControls[type];
if (key != -1)
{
app.keyboard[key] = 0;
}
if (btn != -1)
{
app.mouse.button[btn] = 0;
}
}
2016-03-27 12:21:23 +02:00
void resetAcceptControls(void)
{
app.keyboard[SDL_SCANCODE_SPACE] = app.keyboard[SDL_SCANCODE_RETURN] = 0;
clearControl(CONTROL_FIRE);
}
void updateControlKey(char *name)
{
app.keyControls[lookup(name)] = app.lastKeyPressed;
initControlsDisplay();
}
void updateControlButton(char *name)
{
app.mouseControls[lookup(name)] = app.lastButtonPressed;
initControlsDisplay();
}
void clearControlConfig(char *name)
{
int i;
i = lookup(name);
app.keyControls[i] = app.mouseControls[i] = -1;
initControlsDisplay();
}
2016-03-05 09:42:35 +01:00
void drawControls(void)
{
int i;
SDL_Rect r;
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 128);
SDL_RenderFillRect(app.renderer, NULL);
SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_NONE);
r.w = 800;
r.h = 650;
r.x = (SCREEN_WIDTH / 2) - r.w / 2;
r.y = (SCREEN_HEIGHT / 2) - r.h / 2;
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 0);
SDL_RenderFillRect(app.renderer, &r);
SDL_SetRenderDrawColor(app.renderer, 200, 200, 200, 255);
SDL_RenderDrawRect(app.renderer, &r);
drawText(SCREEN_WIDTH / 2, 50, 28, TA_CENTER, colors.white, _("Controls"));
SDL_SetRenderDrawColor(app.renderer, 128, 128, 128, 255);
SDL_RenderDrawLine(app.renderer, r.x, r.y + 65, r.x + r.w, r.y + 65);
r.x += 25;
r.y = 125;
for (i = 0 ; i < CONTROL_MAX ; i++)
{
drawText(r.x, r.y + 2, 20, TA_LEFT, colors.white, controlName[i]);
2016-03-05 09:42:35 +01:00
controlWidget[i]->rect.x = r.x + 175;
controlWidget[i]->rect.y = r.y;
2016-03-05 14:21:17 +01:00
r.y += 65;
2016-03-05 09:42:35 +01:00
2016-03-05 14:21:17 +01:00
if (r.y > 500)
2016-03-05 09:42:35 +01:00
{
r.y = 125;
r.x += 400;
}
}
2016-03-05 14:21:17 +01:00
limitTextWidth(r.w - 100);
drawText(SCREEN_WIDTH / 2, 525, 16, TA_CENTER, colors.white, _("Click a control to change it, and then the key or mouse button you want to use."));
drawText((SCREEN_WIDTH / 2) - 50, 560, 16, TA_RIGHT, colors.white, _("[BACKSPACE] - Clear"));
drawText((SCREEN_WIDTH / 2) + 50, 560, 16, TA_LEFT, colors.white, _("[ESCAPE] - Cancel"));
limitTextWidth(0);
2016-03-05 09:42:35 +01:00
drawWidgets("controls");
}