Allow keys and mouse buttons to be selected in options.

This commit is contained in:
Steve 2016-03-05 12:35:19 +00:00
parent acd53f6b97
commit 6dfb51e8b0
10 changed files with 189 additions and 68 deletions

View File

@ -5,7 +5,9 @@
"musicVolume" : 8, "musicVolume" : 8,
"soundVolume" : 10, "soundVolume" : 10,
"controls" : { "controls" : {
"keys": { "keys" : {
"CONTROL_FIRE" : 0,
"CONTROL_ACCELERATE" : 0,
"CONTROL_BOOST" : 26, "CONTROL_BOOST" : 26,
"CONTROL_ECM" : 7, "CONTROL_ECM" : 7,
"CONTROL_BRAKE" : 22, "CONTROL_BRAKE" : 22,
@ -13,17 +15,21 @@
"CONTROL_MISSILE" : 44, "CONTROL_MISSILE" : 44,
"CONTROL_GUNS" : 29, "CONTROL_GUNS" : 29,
"CONTROL_RADAR" : 27, "CONTROL_RADAR" : 27,
"CONTROL_NEXT_FIGHTER": 7, "CONTROL_PREV_FIGHTER" : 4,
"CONTROL_PREV_FIGHTER": 4 "CONTROL_NEXT_FIGHTER" : 7
}, },
"mouse" : { "mouse" : {
"CONTROL_FIRE" : 1,
"CONTROL_ACCELERATE" : 3,
"CONTROL_BOOST" : -1, "CONTROL_BOOST" : -1,
"CONTROL_ECM" : -1, "CONTROL_ECM" : -1,
"CONTROL_BRAKE" : -1, "CONTROL_BRAKE" : -1,
"CONTROL_TARGET" : -1, "CONTROL_TARGET" : -1,
"CONTROL_MISSILE" : -1, "CONTROL_MISSILE" : 2,
"CONTROL_GUNS" : -1, "CONTROL_GUNS" : 4,
"CONTROL_RADAR" : -1 "CONTROL_RADAR" : 5,
"CONTROL_PREV_FIGHTER" : 4,
"CONTROL_NEXT_FIGHTER" : 5
} }
} }
} }

View File

@ -1,6 +1,6 @@
[ [
{ {
"name" : "fire", "name" : "CONTROL_FIRE",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -9,7 +9,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "accelerate", "name" : "CONTROL_ACCELERATE",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -18,7 +18,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "missile", "name" : "CONTROL_MISSILE",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -27,7 +27,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "boost", "name" : "CONTROL_BOOST",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -36,7 +36,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "brake", "name" : "CONTROL_BRAKE",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -45,7 +45,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "ecm", "name" : "CONTROL_ECM",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -54,7 +54,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "target", "name" : "CONTROL_TARGET",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -63,7 +63,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "radar", "name" : "CONTROL_RADAR",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -72,7 +72,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "guns", "name" : "CONTROL_GUNS",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -81,7 +81,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "prevFighter", "name" : "CONTROL_PREV_FIGHTER",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,
@ -90,7 +90,7 @@
"h": 34 "h": 34
}, },
{ {
"name" : "nextFighter", "name" : "CONTROL_NEXT_FIGHTER",
"group" : "controls", "group" : "controls",
"type" : "WT_CONTROL_CONFIG", "type" : "WT_CONTROL_CONFIG",
"x" : 0, "x" : 0,

View File

@ -79,17 +79,11 @@ int main(int argc, char *argv[])
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (event.key.keysym.scancode >= 0 && event.key.keysym.scancode < MAX_KEYBOARD_KEYS && event.key.repeat == 0) doKeyDown(&event.key);
{
app.keyboard[event.key.keysym.scancode] = 1;
}
break; break;
case SDL_KEYUP: case SDL_KEYUP:
if (event.key.keysym.scancode >= 0 && event.key.keysym.scancode < MAX_KEYBOARD_KEYS) doKeyUp(&event.key);
{
app.keyboard[event.key.keysym.scancode] = 0;
}
break; break;
case SDL_QUIT: case SDL_QUIT:

View File

@ -46,6 +46,8 @@ extern char *getSaveFilePath(char *filename);
extern void init18N(int argc, char *argv[]); extern void init18N(int argc, char *argv[]);
extern void initLookups(void); extern void initLookups(void);
extern void initGame(void); extern void initGame(void);
extern void doKeyDown(SDL_KeyboardEvent *event);
extern void doKeyUp(SDL_KeyboardEvent *event);
App app; App app;
Colors colors; Colors colors;

View File

@ -423,6 +423,8 @@ typedef struct {
SDL_Window *window; SDL_Window *window;
Delegate delegate; Delegate delegate;
ModalDialog modalDialog; ModalDialog modalDialog;
int lastKeyPressed;
int lastButtonPressed;
int keyControls[CONTROL_MAX]; int keyControls[CONTROL_MAX];
int mouseControls[CONTROL_MAX]; int mouseControls[CONTROL_MAX];
} App; } App;
@ -447,19 +449,19 @@ struct Bucket
Bucket *next; Bucket *next;
}; };
typedef struct HashTable typedef struct
{ {
Bucket **bucket; Bucket **bucket;
int *bucketCount; int *bucketCount;
} HashTable; } HashTable;
typedef struct MOHeader typedef struct
{ {
int32_t magicNumber, version, stringCount; int32_t magicNumber, version, stringCount;
int32_t originalOffset, translationOffset; int32_t originalOffset, translationOffset;
} MOHeader; } MOHeader;
typedef struct MOEntry typedef struct
{ {
int32_t length, offset; int32_t length, offset;
} MOEntry; } MOEntry;

View File

@ -22,7 +22,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static const char *controlName[CONTROL_MAX]; static const char *controlName[CONTROL_MAX];
static Widget *controlWidget[CONTROL_MAX]; static Widget *controlWidget[CONTROL_MAX];
static const char *widgetNames[] = {"fire", "accelerate", "boost", "ecm", "brake", "target", "missile", "guns", "radar", "nextFighter", "prevFighter"};
void initControls(void) void initControls(void)
{ {
@ -42,7 +41,7 @@ void initControls(void)
for (i = 0 ; i < CONTROL_MAX ; i++) for (i = 0 ; i < CONTROL_MAX ; i++)
{ {
controlWidget[i] = getWidget(widgetNames[i], "controls"); controlWidget[i] = getWidget(getLookupName("CONTROL_", i), "controls");
controlWidget[i]->numOptions = 2; controlWidget[i]->numOptions = 2;
controlWidget[i]->options = malloc(2 * sizeof(char*)); controlWidget[i]->options = malloc(2 * sizeof(char*));
controlWidget[i]->options[0] = malloc(sizeof(char) * MAX_NAME_LENGTH); controlWidget[i]->options[0] = malloc(sizeof(char) * MAX_NAME_LENGTH);
@ -52,6 +51,27 @@ void initControls(void)
} }
} }
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)
{
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]);
}
}
}
int isKeyControl(int type) int isKeyControl(int type)
{ {
return app.keyboard[app.keyControls[type]]; return app.keyboard[app.keyControls[type]];
@ -62,22 +82,29 @@ void clearControl(int type)
app.keyboard[app.keyControls[type]] = 0; app.keyboard[app.keyControls[type]] = 0;
} }
void initControlsDisplay(void) 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; int i;
for (i = 0 ; i < CONTROL_MAX ; i++) i = lookup(name);
{
if (app.mouseControls[i] != 0) app.keyControls[i] = app.mouseControls[i] = -1;
{
sprintf(controlWidget[i]->options[0], "%s", SDL_GetScancodeName(app.keyControls[i])); initControlsDisplay();
}
if (app.mouseControls[i] != -1)
{
sprintf(controlWidget[i]->options[1], "Btn %d", app.mouseControls[i]);
}
}
} }
void drawControls(void) void drawControls(void)

View File

@ -24,6 +24,8 @@ extern void drawText(int x, int y, int size, int align, SDL_Color c, const char
extern char *getTranslatedString(char *string); extern char *getTranslatedString(char *string);
extern Widget *getWidget(const char *name, const char *group); extern Widget *getWidget(const char *name, const char *group);
extern void drawWidgets(char *groupName); extern void drawWidgets(char *groupName);
extern char *getLookupName(char *prefix, long num);
extern long lookup(char *name);
extern App app; extern App app;
extern Colors colors; extern Colors colors;

View File

@ -31,11 +31,29 @@ void initInput(void)
SDL_QueryTexture(mousePointer, NULL, NULL, &app.mouse.w, &app.mouse.h); SDL_QueryTexture(mousePointer, NULL, NULL, &app.mouse.w, &app.mouse.h);
} }
void doKeyDown(SDL_KeyboardEvent *event)
{
if (event->keysym.scancode >= 0 && event->keysym.scancode < MAX_KEYBOARD_KEYS && event->repeat == 0)
{
app.keyboard[event->keysym.scancode] = 1;
app.lastKeyPressed = event->keysym.scancode;
}
}
void doKeyUp(SDL_KeyboardEvent *event)
{
if (event->keysym.scancode >= 0 && event->keysym.scancode < MAX_KEYBOARD_KEYS)
{
app.keyboard[event->keysym.scancode] = 0;
}
}
void doMouseDown(SDL_MouseButtonEvent *event) void doMouseDown(SDL_MouseButtonEvent *event)
{ {
if (event->button >= 0 && event->button < MAX_MOUSE_BUTTONS) if (event->button >= 0 && event->button < MAX_MOUSE_BUTTONS)
{ {
app.mouse.button[event->button] = 1; app.mouse.button[event->button] = 1;
app.lastButtonPressed = event->button;
} }
} }
@ -55,11 +73,13 @@ void doMouseWheel(SDL_MouseWheelEvent *event)
if (event->y == -1) if (event->y == -1)
{ {
app.mouse.button[SDL_BUTTON_X1] = 1; app.mouse.button[SDL_BUTTON_X1] = 1;
app.lastButtonPressed = SDL_BUTTON_X1;
} }
if (event->y == 1) if (event->y == 1)
{ {
app.mouse.button[SDL_BUTTON_X2] = 1; app.mouse.button[SDL_BUTTON_X2] = 1;
app.lastButtonPressed = SDL_BUTTON_X2;
} }
} }

View File

@ -27,6 +27,7 @@ static void handleKeyboard(void);
static void createOptions(Widget *w, char *options); static void createOptions(Widget *w, char *options);
static void changeSelectedValue(Widget *w, int dir); static void changeSelectedValue(Widget *w, int dir);
static void createSelectButtons(Widget *w); static void createSelectButtons(Widget *w);
static void handleControlWidgets(void);
static Widget head; static Widget head;
static Widget *tail; static Widget *tail;
@ -34,6 +35,7 @@ static Widget *selectedWidget;
static SDL_Texture *optionsLeft; static SDL_Texture *optionsLeft;
static SDL_Texture *optionsRight; static SDL_Texture *optionsRight;
static int drawingWidgets; static int drawingWidgets;
static int waitingForInput;
void initWidgets(void) void initWidgets(void)
{ {
@ -48,7 +50,7 @@ void initWidgets(void)
loadWidgets(); loadWidgets();
drawingWidgets = 0; waitingForInput = drawingWidgets = 0;
} }
void doWidgets(void) void doWidgets(void)
@ -58,6 +60,11 @@ void doWidgets(void)
handleMouse(); handleMouse();
handleKeyboard(); handleKeyboard();
if (waitingForInput)
{
handleControlWidgets();
}
} }
drawingWidgets = 0; drawingWidgets = 0;
@ -97,7 +104,7 @@ void drawWidgets(const char *group)
{ {
if ((app.modalDialog.type == MD_NONE || (app.modalDialog.type != MD_NONE && w->isModal)) && w->visible && strcmp(w->group, group) == 0) if ((app.modalDialog.type == MD_NONE || (app.modalDialog.type != MD_NONE && w->isModal)) && w->visible && strcmp(w->group, group) == 0)
{ {
if (!mouseOver) if (!mouseOver && !waitingForInput)
{ {
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)); 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));
@ -149,21 +156,29 @@ void drawWidgets(const char *group)
case WT_CONTROL_CONFIG: case WT_CONTROL_CONFIG:
SDL_RenderDrawRect(app.renderer, &w->rect); SDL_RenderDrawRect(app.renderer, &w->rect);
if (strlen(w->options[0]) && strlen(w->options[1]))
if (!waitingForInput || (waitingForInput && w != selectedWidget))
{ {
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]); if (strlen(w->options[0]) && strlen(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 or %s", w->options[0], w->options[1]);
{ }
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[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[0]);
{ }
drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "%s", w->options[1]); 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, "");
}
} }
else else
{ {
drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, ""); drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, "...");
} }
break; break;
} }
@ -242,6 +257,15 @@ static void handleMouse(void)
changeSelectedValue(selectedWidget->parent, selectedWidget->value); changeSelectedValue(selectedWidget->parent, selectedWidget->value);
app.mouse.button[SDL_BUTTON_LEFT] = 0; app.mouse.button[SDL_BUTTON_LEFT] = 0;
break; break;
case WT_CONTROL_CONFIG:
if (!waitingForInput)
{
waitingForInput = 1;
app.lastKeyPressed = app.lastButtonPressed = -1;
}
app.mouse.button[SDL_BUTTON_LEFT] = 0;
break;
} }
} }
} }
@ -250,25 +274,65 @@ static void handleMouse(void)
static void handleKeyboard(void) static void handleKeyboard(void)
{ {
Widget *old; Widget *old;
if (app.keyboard[SDL_SCANCODE_SPACE] ||app.keyboard[SDL_SCANCODE_RETURN]) if (selectedWidget != NULL)
{ {
if (selectedWidget != NULL && selectedWidget->type == WT_BUTTON) if (selectedWidget->type == WT_BUTTON)
{ {
playSound(SND_GUI_SELECT); if (app.keyboard[SDL_SCANCODE_SPACE] ||app.keyboard[SDL_SCANCODE_RETURN])
old = selectedWidget;
selectedWidget->action();
if (old == selectedWidget)
{ {
selectedWidget = NULL; playSound(SND_GUI_SELECT);
} old = selectedWidget;
selectedWidget->action();
app.keyboard[SDL_SCANCODE_SPACE] = app.keyboard[SDL_SCANCODE_RETURN] = 0; 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);
waitingForInput = 0;
}
else if (app.lastKeyPressed == SDL_SCANCODE_ESCAPE)
{
waitingForInput = 0;
}
else
{
if (app.lastKeyPressed != -1)
{
updateControlKey(selectedWidget->name);
waitingForInput = 0;
}
if (app.lastButtonPressed != -1)
{
updateControlButton(selectedWidget->name);
waitingForInput = 0;
}
}
if (!waitingForInput)
{
clearInput();
}
app.lastKeyPressed = app.lastButtonPressed = -1;
}
static void loadWidgets() static void loadWidgets()
{ {
char **filenames; char **filenames;

View File

@ -32,6 +32,10 @@ extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int
extern void playSound(int id); extern void playSound(int id);
extern char **getFileList(char *dir, int *count); extern char **getFileList(char *dir, int *count);
extern char *getTranslatedString(char *string); extern char *getTranslatedString(char *string);
extern void updateControlKey(char *name);
extern void updateControlButton(char *name);
extern void clearControlConfig(char *name);
extern void clearInput(void);
extern App app; extern App app;
extern Colors colors; extern Colors colors;