#pragma once
#include <stdlib.h>
#include "raylib.h"
typedef enum {
CONTROLS_UP,
CONTROLS_UP_ALT,
CONTROLS_DOWN,
CONTROLS_DOWN_ALT,
CONTROLS_LEFT,
CONTROLS_LEFT_ALT,
CONTROLS_RIGHT,
CONTROLS_RIGHT_ALT,
CONTROLS_ACTION_ALT,
CONTROLS_SPECIAL_ALT,
CONTROLS_UTILITY,
CONTROLS_ENTER,
CONTROLS_KCOUNT
} KeyboardControlType;
typedef enum {
CONTROLS_ACTION,
CONTROLS_SPECIAL,
CONTROLS_MCOUNT
} MouseControlType;
#define KEYBOARD_CONTROL_COUNT CONTROLS_KCOUNT
#define MOUSE_CONTROL_COUNT CONTROLS_MCOUNT
typedef struct {
KeyboardKey keys[KEYBOARD_CONTROL_COUNT];
} KeyboardControls;
typedef struct {
KeyboardKey buttons[MOUSE_CONTROL_COUNT];
} MouseControls;
typedef struct {
KeyboardControls keyboard;
MouseControls mouse;
} Controls;
void controls_set_key(Controls* controls, KeyboardControlType action, KeyboardKey key);
void controls_set_mouse_button(Controls* controls, MouseControlType action, MouseButton button);
Controls* controls_init();
#ifdef CONTROLS_IMPLEMENTATION
Controls* controls_init(){
Controls* c = malloc(sizeof(Controls));
if (!c) {
exit(-1);
}
c->keyboard.keys[CONTROLS_UP] = KEY_W;
c->keyboard.keys[CONTROLS_UP_ALT] = KEY_UP;
c->keyboard.keys[CONTROLS_DOWN] = KEY_S;
c->keyboard.keys[CONTROLS_DOWN_ALT] = KEY_DOWN;
c->keyboard.keys[CONTROLS_LEFT] = KEY_A;
c->keyboard.keys[CONTROLS_LEFT_ALT] = KEY_LEFT;
c->keyboard.keys[CONTROLS_RIGHT] = KEY_D;
c->keyboard.keys[CONTROLS_RIGHT_ALT] = KEY_RIGHT;
c->keyboard.keys[CONTROLS_ACTION_ALT] = KEY_Z;
c->keyboard.keys[CONTROLS_SPECIAL_ALT] = KEY_X;
c->keyboard.keys[CONTROLS_ENTER] = KEY_ENTER;
c->mouse.buttons[CONTROLS_ACTION] = MOUSE_BUTTON_LEFT;
c->mouse.buttons[CONTROLS_SPECIAL] = MOUSE_BUTTON_RIGHT;
return c;
}
void controls_set_key(Controls* controls, KeyboardControlType action, KeyboardKey key){
for (int k = 0; k < CONTROLS_KCOUNT; k++) {
if (controls->keyboard.keys[k] == key) {
controls->keyboard.keys[k] = KEY_NULL;
break;
}
}
controls->keyboard.keys[action] = key;
}
void controls_set_mouse_button(Controls* controls, MouseControlType action, MouseButton button){
for (int m = 0; m < CONTROLS_MCOUNT; m++) {
if (controls->mouse.buttons[m] == button) {
controls->mouse.buttons[m] = controls->mouse.buttons[action];
}
controls->mouse.buttons[action] = button;
}
}
#endif