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,
"soundVolume" : 10,
"controls" : {
"keys": {
"keys" : {
"CONTROL_FIRE" : 0,
"CONTROL_ACCELERATE" : 0,
"CONTROL_BOOST" : 26,
"CONTROL_ECM" : 7,
"CONTROL_BRAKE" : 22,
@ -13,17 +15,21 @@
"CONTROL_MISSILE" : 44,
"CONTROL_GUNS" : 29,
"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_ECM" : -1,
"CONTROL_BRAKE" : -1,
"CONTROL_TARGET" : -1,
"CONTROL_MISSILE" : -1,
"CONTROL_GUNS" : -1,
"CONTROL_RADAR" : -1
"CONTROL_MISSILE" : 2,
"CONTROL_GUNS" : 4,
"CONTROL_RADAR" : 5,
"CONTROL_PREV_FIGHTER" : 4,
"CONTROL_NEXT_FIGHTER" : 5
}
}
}

View File

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

View File

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

View File

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

View File

@ -423,6 +423,8 @@ typedef struct {
SDL_Window *window;
Delegate delegate;
ModalDialog modalDialog;
int lastKeyPressed;
int lastButtonPressed;
int keyControls[CONTROL_MAX];
int mouseControls[CONTROL_MAX];
} App;
@ -447,19 +449,19 @@ struct Bucket
Bucket *next;
};
typedef struct HashTable
typedef struct
{
Bucket **bucket;
int *bucketCount;
} HashTable;
typedef struct MOHeader
typedef struct
{
int32_t magicNumber, version, stringCount;
int32_t originalOffset, translationOffset;
} MOHeader;
typedef struct MOEntry
typedef struct
{
int32_t length, offset;
} 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 Widget *controlWidget[CONTROL_MAX];
static const char *widgetNames[] = {"fire", "accelerate", "boost", "ecm", "brake", "target", "missile", "guns", "radar", "nextFighter", "prevFighter"};
void initControls(void)
{
@ -42,7 +41,7 @@ void initControls(void)
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]->options = malloc(2 * sizeof(char*));
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)
{
return app.keyboard[app.keyControls[type]];
@ -62,22 +82,29 @@ void clearControl(int type)
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;
for (i = 0 ; i < CONTROL_MAX ; i++)
{
if (app.mouseControls[i] != 0)
{
sprintf(controlWidget[i]->options[0], "%s", SDL_GetScancodeName(app.keyControls[i]));
}
i = lookup(name);
if (app.mouseControls[i] != -1)
{
sprintf(controlWidget[i]->options[1], "Btn %d", app.mouseControls[i]);
}
}
app.keyControls[i] = app.mouseControls[i] = -1;
initControlsDisplay();
}
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 Widget *getWidget(const char *name, const char *group);
extern void drawWidgets(char *groupName);
extern char *getLookupName(char *prefix, long num);
extern long lookup(char *name);
extern App app;
extern Colors colors;

View File

@ -31,11 +31,29 @@ void initInput(void)
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)
{
if (event->button >= 0 && event->button < MAX_MOUSE_BUTTONS)
{
app.mouse.button[event->button] = 1;
app.lastButtonPressed = event->button;
}
}
@ -55,11 +73,13 @@ void doMouseWheel(SDL_MouseWheelEvent *event)
if (event->y == -1)
{
app.mouse.button[SDL_BUTTON_X1] = 1;
app.lastButtonPressed = SDL_BUTTON_X1;
}
if (event->y == 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 changeSelectedValue(Widget *w, int dir);
static void createSelectButtons(Widget *w);
static void handleControlWidgets(void);
static Widget head;
static Widget *tail;
@ -34,6 +35,7 @@ static Widget *selectedWidget;
static SDL_Texture *optionsLeft;
static SDL_Texture *optionsRight;
static int drawingWidgets;
static int waitingForInput;
void initWidgets(void)
{
@ -48,7 +50,7 @@ void initWidgets(void)
loadWidgets();
drawingWidgets = 0;
waitingForInput = drawingWidgets = 0;
}
void doWidgets(void)
@ -58,6 +60,11 @@ void doWidgets(void)
handleMouse();
handleKeyboard();
if (waitingForInput)
{
handleControlWidgets();
}
}
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 (!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));
@ -149,21 +156,29 @@ void drawWidgets(const char *group)
case WT_CONTROL_CONFIG:
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]);
}
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]);
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, "");
}
}
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;
}
@ -242,6 +257,15 @@ static void handleMouse(void)
changeSelectedValue(selectedWidget->parent, selectedWidget->value);
app.mouse.button[SDL_BUTTON_LEFT] = 0;
break;
case WT_CONTROL_CONFIG:
if (!waitingForInput)
{
waitingForInput = 1;
app.lastKeyPressed = app.lastButtonPressed = -1;
}
app.mouse.button[SDL_BUTTON_LEFT] = 0;
break;
}
}
}
@ -251,24 +275,64 @@ static void handleKeyboard(void)
{
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);
old = selectedWidget;
selectedWidget->action();
if (old == selectedWidget)
if (app.keyboard[SDL_SCANCODE_SPACE] ||app.keyboard[SDL_SCANCODE_RETURN])
{
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()
{
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 char **getFileList(char *dir, int *count);
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 Colors colors;