Browse Source

Added keybind system from Codesearch GUI

master
Macoy Madson 9 months ago
parent
commit
13a2727b14
  1. 332
      src/SDL.cake

332
src/SDL.cake

@ -6,7 +6,7 @@
(export-and-evaluate
(c-import "SDL.h" "SDL_syswm.h"))
(c-import "stdio.h")
(c-import "<stdio.h>" &with-decls "<stddef.h>")
;;
;; Core/windowing
@ -212,6 +212,336 @@
(at key current-key-states)
(not (at key last-frame-key-states)))))
(defun sdl-scancode-to-user-string (code (unsigned int) &return (* (const char)))
;; TODO: Would be faster in switch, possibly
(cond
((= code SDL_SCANCODE_UNKNOWN) (return "Unknown"))
((= code SDL_SCANCODE_A) (return "A"))
((= code SDL_SCANCODE_B) (return "B"))
((= code SDL_SCANCODE_C) (return "C"))
((= code SDL_SCANCODE_D) (return "D"))
((= code SDL_SCANCODE_E) (return "E"))
((= code SDL_SCANCODE_F) (return "F"))
((= code SDL_SCANCODE_G) (return "G"))
((= code SDL_SCANCODE_H) (return "H"))
((= code SDL_SCANCODE_I) (return "I"))
((= code SDL_SCANCODE_J) (return "J"))
((= code SDL_SCANCODE_K) (return "K"))
((= code SDL_SCANCODE_L) (return "L"))
((= code SDL_SCANCODE_M) (return "M"))
((= code SDL_SCANCODE_N) (return "N"))
((= code SDL_SCANCODE_O) (return "O"))
((= code SDL_SCANCODE_P) (return "P"))
((= code SDL_SCANCODE_Q) (return "Q"))
((= code SDL_SCANCODE_R) (return "R"))
((= code SDL_SCANCODE_S) (return "S"))
((= code SDL_SCANCODE_T) (return "T"))
((= code SDL_SCANCODE_U) (return "U"))
((= code SDL_SCANCODE_V) (return "V"))
((= code SDL_SCANCODE_W) (return "W"))
((= code SDL_SCANCODE_X) (return "X"))
((= code SDL_SCANCODE_Y) (return "Y"))
((= code SDL_SCANCODE_Z) (return "Z"))
((= code SDL_SCANCODE_1) (return "1"))
((= code SDL_SCANCODE_2) (return "2"))
((= code SDL_SCANCODE_3) (return "3"))
((= code SDL_SCANCODE_4) (return "4"))
((= code SDL_SCANCODE_5) (return "5"))
((= code SDL_SCANCODE_6) (return "6"))
((= code SDL_SCANCODE_7) (return "7"))
((= code SDL_SCANCODE_8) (return "8"))
((= code SDL_SCANCODE_9) (return "9"))
((= code SDL_SCANCODE_0) (return "0"))
((= code SDL_SCANCODE_RETURN) (return "RETURN"))
((= code SDL_SCANCODE_ESCAPE) (return "ESCAPE"))
((= code SDL_SCANCODE_BACKSPACE) (return "BACKSPACE"))
((= code SDL_SCANCODE_TAB) (return "TAB"))
((= code SDL_SCANCODE_SPACE) (return "SPACE"))
((= code SDL_SCANCODE_MINUS) (return "MINUS"))
((= code SDL_SCANCODE_EQUALS) (return "EQUALS"))
((= code SDL_SCANCODE_LEFTBRACKET) (return "LEFTBRACKET"))
((= code SDL_SCANCODE_RIGHTBRACKET) (return "RIGHTBRACKET"))
((= code SDL_SCANCODE_BACKSLASH) (return "BACKSLASH"))
((= code SDL_SCANCODE_NONUSHASH) (return "NONUSHASH"))
((= code SDL_SCANCODE_SEMICOLON) (return "SEMICOLON"))
((= code SDL_SCANCODE_APOSTROPHE) (return "APOSTROPHE"))
((= code SDL_SCANCODE_GRAVE) (return "GRAVE"))
((= code SDL_SCANCODE_COMMA) (return "COMMA"))
((= code SDL_SCANCODE_PERIOD) (return "PERIOD"))
((= code SDL_SCANCODE_SLASH) (return "SLASH"))
((= code SDL_SCANCODE_CAPSLOCK) (return "CAPSLOCK"))
((= code SDL_SCANCODE_F1) (return "F1"))
((= code SDL_SCANCODE_F2) (return "F2"))
((= code SDL_SCANCODE_F3) (return "F3"))
((= code SDL_SCANCODE_F4) (return "F4"))
((= code SDL_SCANCODE_F5) (return "F5"))
((= code SDL_SCANCODE_F6) (return "F6"))
((= code SDL_SCANCODE_F7) (return "F7"))
((= code SDL_SCANCODE_F8) (return "F8"))
((= code SDL_SCANCODE_F9) (return "F9"))
((= code SDL_SCANCODE_F10) (return "F10"))
((= code SDL_SCANCODE_F11) (return "F11"))
((= code SDL_SCANCODE_F12) (return "F12"))
((= code SDL_SCANCODE_PRINTSCREEN) (return "PRINTSCREEN"))
((= code SDL_SCANCODE_SCROLLLOCK) (return "SCROLLLOCK"))
((= code SDL_SCANCODE_PAUSE) (return "PAUSE"))
((= code SDL_SCANCODE_INSERT) (return "INSERT"))
((= code SDL_SCANCODE_HOME) (return "HOME"))
((= code SDL_SCANCODE_PAGEUP) (return "PAGEUP"))
((= code SDL_SCANCODE_DELETE) (return "DELETE"))
((= code SDL_SCANCODE_END) (return "END"))
((= code SDL_SCANCODE_PAGEDOWN) (return "PAGEDOWN"))
((= code SDL_SCANCODE_RIGHT) (return "RIGHT"))
((= code SDL_SCANCODE_LEFT) (return "LEFT"))
((= code SDL_SCANCODE_DOWN) (return "DOWN"))
((= code SDL_SCANCODE_UP) (return "UP"))
((= code SDL_SCANCODE_NUMLOCKCLEAR) (return "NUMLOCKCLEAR"))
((= code SDL_SCANCODE_KP_DIVIDE) (return "Keypad DIVIDE"))
((= code SDL_SCANCODE_KP_MULTIPLY) (return "Keypad MULTIPLY"))
((= code SDL_SCANCODE_KP_MINUS) (return "Keypad MINUS"))
((= code SDL_SCANCODE_KP_PLUS) (return "Keypad PLUS"))
((= code SDL_SCANCODE_KP_ENTER) (return "Keypad ENTER"))
((= code SDL_SCANCODE_KP_1) (return "Keypad 1"))
((= code SDL_SCANCODE_KP_2) (return "Keypad 2"))
((= code SDL_SCANCODE_KP_3) (return "Keypad 3"))
((= code SDL_SCANCODE_KP_4) (return "Keypad 4"))
((= code SDL_SCANCODE_KP_5) (return "Keypad 5"))
((= code SDL_SCANCODE_KP_6) (return "Keypad 6"))
((= code SDL_SCANCODE_KP_7) (return "Keypad 7"))
((= code SDL_SCANCODE_KP_8) (return "Keypad 8"))
((= code SDL_SCANCODE_KP_9) (return "Keypad 9"))
((= code SDL_SCANCODE_KP_0) (return "Keypad 0"))
((= code SDL_SCANCODE_KP_PERIOD) (return "Keypad PERIOD"))
((= code SDL_SCANCODE_NONUSBACKSLASH) (return "NONUSBACKSLASH"))
((= code SDL_SCANCODE_APPLICATION) (return "APPLICATION"))
((= code SDL_SCANCODE_POWER) (return "POWER"))
((= code SDL_SCANCODE_KP_EQUALS) (return "Keypad EQUALS"))
((= code SDL_SCANCODE_F13) (return "F13"))
((= code SDL_SCANCODE_F14) (return "F14"))
((= code SDL_SCANCODE_F15) (return "F15"))
((= code SDL_SCANCODE_F16) (return "F16"))
((= code SDL_SCANCODE_F17) (return "F17"))
((= code SDL_SCANCODE_F18) (return "F18"))
((= code SDL_SCANCODE_F19) (return "F19"))
((= code SDL_SCANCODE_F20) (return "F20"))
((= code SDL_SCANCODE_F21) (return "F21"))
((= code SDL_SCANCODE_F22) (return "F22"))
((= code SDL_SCANCODE_F23) (return "F23"))
((= code SDL_SCANCODE_F24) (return "F24"))
((= code SDL_SCANCODE_EXECUTE) (return "EXECUTE"))
((= code SDL_SCANCODE_HELP) (return "HELP"))
((= code SDL_SCANCODE_MENU) (return "MENU"))
((= code SDL_SCANCODE_SELECT) (return "SELECT"))
((= code SDL_SCANCODE_STOP) (return "STOP"))
((= code SDL_SCANCODE_AGAIN) (return "AGAIN"))
((= code SDL_SCANCODE_UNDO) (return "UNDO"))
((= code SDL_SCANCODE_CUT) (return "CUT"))
((= code SDL_SCANCODE_COPY) (return "COPY"))
((= code SDL_SCANCODE_PASTE) (return "PASTE"))
((= code SDL_SCANCODE_FIND) (return "FIND"))
((= code SDL_SCANCODE_MUTE) (return "MUTE"))
((= code SDL_SCANCODE_VOLUMEUP) (return "VOLUMEUP"))
((= code SDL_SCANCODE_VOLUMEDOWN) (return "VOLUMEDOWN"))
((= code SDL_SCANCODE_KP_COMMA) (return "Keypad COMMA"))
((= code SDL_SCANCODE_KP_EQUALSAS400) (return "Keypad EQUALSAS400"))
((= code SDL_SCANCODE_INTERNATIONAL1) (return "INTERNATIONAL1"))
((= code SDL_SCANCODE_INTERNATIONAL2) (return "INTERNATIONAL2"))
((= code SDL_SCANCODE_INTERNATIONAL3) (return "INTERNATIONAL3"))
((= code SDL_SCANCODE_INTERNATIONAL4) (return "INTERNATIONAL4"))
((= code SDL_SCANCODE_INTERNATIONAL5) (return "INTERNATIONAL5"))
((= code SDL_SCANCODE_INTERNATIONAL6) (return "INTERNATIONAL6"))
((= code SDL_SCANCODE_INTERNATIONAL7) (return "INTERNATIONAL7"))
((= code SDL_SCANCODE_INTERNATIONAL8) (return "INTERNATIONAL8"))
((= code SDL_SCANCODE_INTERNATIONAL9) (return "INTERNATIONAL9"))
((= code SDL_SCANCODE_LANG1) (return "LANG1"))
((= code SDL_SCANCODE_LANG2) (return "LANG2"))
((= code SDL_SCANCODE_LANG3) (return "LANG3"))
((= code SDL_SCANCODE_LANG4) (return "LANG4"))
((= code SDL_SCANCODE_LANG5) (return "LANG5"))
((= code SDL_SCANCODE_LANG6) (return "LANG6"))
((= code SDL_SCANCODE_LANG7) (return "LANG7"))
((= code SDL_SCANCODE_LANG8) (return "LANG8"))
((= code SDL_SCANCODE_LANG9) (return "LANG9"))
((= code SDL_SCANCODE_ALTERASE) (return "ALTERASE"))
((= code SDL_SCANCODE_SYSREQ) (return "SYSREQ"))
((= code SDL_SCANCODE_CANCEL) (return "CANCEL"))
((= code SDL_SCANCODE_CLEAR) (return "CLEAR"))
((= code SDL_SCANCODE_PRIOR) (return "PRIOR"))
((= code SDL_SCANCODE_RETURN2) (return "RETURN2"))
((= code SDL_SCANCODE_SEPARATOR) (return "SEPARATOR"))
((= code SDL_SCANCODE_OUT) (return "OUT"))
((= code SDL_SCANCODE_OPER) (return "OPER"))
((= code SDL_SCANCODE_CLEARAGAIN) (return "CLEARAGAIN"))
((= code SDL_SCANCODE_CRSEL) (return "CRSEL"))
((= code SDL_SCANCODE_EXSEL) (return "EXSEL"))
((= code SDL_SCANCODE_KP_00) (return "Keypad 00"))
((= code SDL_SCANCODE_KP_000) (return "Keypad 000"))
((= code SDL_SCANCODE_THOUSANDSSEPARATOR) (return "THOUSANDSSEPARATOR"))
((= code SDL_SCANCODE_DECIMALSEPARATOR) (return "DECIMALSEPARATOR"))
((= code SDL_SCANCODE_CURRENCYUNIT) (return "CURRENCYUNIT"))
((= code SDL_SCANCODE_CURRENCYSUBUNIT) (return "CURRENCYSUBUNIT"))
((= code SDL_SCANCODE_KP_LEFTPAREN) (return "Keypad LEFTPAREN"))
((= code SDL_SCANCODE_KP_RIGHTPAREN) (return "Keypad RIGHTPAREN"))
((= code SDL_SCANCODE_KP_LEFTBRACE) (return "Keypad LEFTBRACE"))
((= code SDL_SCANCODE_KP_RIGHTBRACE) (return "Keypad RIGHTBRACE"))
((= code SDL_SCANCODE_KP_TAB) (return "Keypad TAB"))
((= code SDL_SCANCODE_KP_BACKSPACE) (return "Keypad BACKSPACE"))
((= code SDL_SCANCODE_KP_A) (return "Keypad A"))
((= code SDL_SCANCODE_KP_B) (return "Keypad B"))
((= code SDL_SCANCODE_KP_C) (return "Keypad C"))
((= code SDL_SCANCODE_KP_D) (return "Keypad D"))
((= code SDL_SCANCODE_KP_E) (return "Keypad E"))
((= code SDL_SCANCODE_KP_F) (return "Keypad F"))
((= code SDL_SCANCODE_KP_XOR) (return "Keypad XOR"))
((= code SDL_SCANCODE_KP_POWER) (return "Keypad POWER"))
((= code SDL_SCANCODE_KP_PERCENT) (return "Keypad PERCENT"))
((= code SDL_SCANCODE_KP_LESS) (return "Keypad LESS"))
((= code SDL_SCANCODE_KP_GREATER) (return "Keypad GREATER"))
((= code SDL_SCANCODE_KP_AMPERSAND) (return "Keypad AMPERSAND"))
((= code SDL_SCANCODE_KP_DBLAMPERSAND) (return "Keypad DBLAMPERSAND"))
((= code SDL_SCANCODE_KP_VERTICALBAR) (return "Keypad VERTICALBAR"))
((= code SDL_SCANCODE_KP_DBLVERTICALBAR) (return "Keypad DBLVERTICALBAR"))
((= code SDL_SCANCODE_KP_COLON) (return "Keypad COLON"))
((= code SDL_SCANCODE_KP_HASH) (return "Keypad HASH"))
((= code SDL_SCANCODE_KP_SPACE) (return "Keypad SPACE"))
((= code SDL_SCANCODE_KP_AT) (return "Keypad AT"))
((= code SDL_SCANCODE_KP_EXCLAM) (return "Keypad EXCLAM"))
((= code SDL_SCANCODE_KP_MEMSTORE) (return "Keypad MEMSTORE"))
((= code SDL_SCANCODE_KP_MEMRECALL) (return "Keypad MEMRECALL"))
((= code SDL_SCANCODE_KP_MEMCLEAR) (return "Keypad MEMCLEAR"))
((= code SDL_SCANCODE_KP_MEMADD) (return "Keypad MEMADD"))
((= code SDL_SCANCODE_KP_MEMSUBTRACT) (return "Keypad MEMSUBTRACT"))
((= code SDL_SCANCODE_KP_MEMMULTIPLY) (return "Keypad MEMMULTIPLY"))
((= code SDL_SCANCODE_KP_MEMDIVIDE) (return "Keypad MEMDIVIDE"))
((= code SDL_SCANCODE_KP_PLUSMINUS) (return "Keypad PLUSMINUS"))
((= code SDL_SCANCODE_KP_CLEAR) (return "Keypad CLEAR"))
((= code SDL_SCANCODE_KP_CLEARENTRY) (return "Keypad CLEARENTRY"))
((= code SDL_SCANCODE_KP_BINARY) (return "Keypad BINARY"))
((= code SDL_SCANCODE_KP_OCTAL) (return "Keypad OCTAL"))
((= code SDL_SCANCODE_KP_DECIMAL) (return "Keypad DECIMAL"))
((= code SDL_SCANCODE_KP_HEXADECIMAL) (return "Keypad HEXADECIMAL"))
((= code SDL_SCANCODE_LCTRL) (return "LCTRL"))
((= code SDL_SCANCODE_LSHIFT) (return "LSHIFT"))
((= code SDL_SCANCODE_LALT) (return "LALT"))
((= code SDL_SCANCODE_LGUI) (return "LGUI"))
((= code SDL_SCANCODE_RCTRL) (return "RCTRL"))
((= code SDL_SCANCODE_RSHIFT) (return "RSHIFT"))
((= code SDL_SCANCODE_RALT) (return "RALT"))
((= code SDL_SCANCODE_RGUI) (return "RGUI"))
((= code SDL_SCANCODE_MODE) (return "MODE"))
((= code SDL_SCANCODE_AUDIONEXT) (return "AUDIONEXT"))
((= code SDL_SCANCODE_AUDIOPREV) (return "AUDIOPREV"))
((= code SDL_SCANCODE_AUDIOSTOP) (return "AUDIOSTOP"))
((= code SDL_SCANCODE_AUDIOPLAY) (return "AUDIOPLAY"))
((= code SDL_SCANCODE_AUDIOMUTE) (return "AUDIOMUTE"))
((= code SDL_SCANCODE_MEDIASELECT) (return "MEDIASELECT"))
((= code SDL_SCANCODE_WWW) (return "WWW"))
((= code SDL_SCANCODE_MAIL) (return "MAIL"))
((= code SDL_SCANCODE_CALCULATOR) (return "CALCULATOR"))
((= code SDL_SCANCODE_COMPUTER) (return "COMPUTER"))
((= code SDL_SCANCODE_AC_SEARCH) (return "AC_SEARCH"))
((= code SDL_SCANCODE_AC_HOME) (return "AC_HOME"))
((= code SDL_SCANCODE_AC_BACK) (return "AC_BACK"))
((= code SDL_SCANCODE_AC_FORWARD) (return "AC_FORWARD"))
((= code SDL_SCANCODE_AC_STOP) (return "AC_STOP"))
((= code SDL_SCANCODE_AC_REFRESH) (return "AC_REFRESH"))
((= code SDL_SCANCODE_AC_BOOKMARKS) (return "AC_BOOKMARKS"))
((= code SDL_SCANCODE_BRIGHTNESSDOWN) (return "BRIGHTNESSDOWN"))
((= code SDL_SCANCODE_BRIGHTNESSUP) (return "BRIGHTNESSUP"))
((= code SDL_SCANCODE_DISPLAYSWITCH) (return "DISPLAYSWITCH"))
((= code SDL_SCANCODE_KBDILLUMTOGGLE) (return "KBDILLUMTOGGLE"))
((= code SDL_SCANCODE_KBDILLUMDOWN) (return "KBDILLUMDOWN"))
((= code SDL_SCANCODE_KBDILLUMUP) (return "KBDILLUMUP"))
((= code SDL_SCANCODE_EJECT) (return "EJECT"))
((= code SDL_SCANCODE_SLEEP) (return "SLEEP"))
((= code SDL_SCANCODE_APP1) (return "APP1"))
((= code SDL_SCANCODE_APP2) (return "APP2"))
((= code SDL_SCANCODE_AUDIOREWIND) (return "AUDIOREWIND"))
((= code SDL_SCANCODE_AUDIOFASTFORWARD) (return "AUDIOFASTFORWARD"))
(true
(return "Unknown"))))
;; TODO Cakelisp CHelpers support flags
(var-global keybind-modifier-flags-none (unsigned char) 0)
(var-global keybind-modifier-flags-ctrl (unsigned char) (bit-<< 1 0))
(var-global keybind-modifier-flags-shift (unsigned char) (bit-<< 1 1))
(var-global keybind-modifier-flags-alt (unsigned char) (bit-<< 1 2))
(defstruct keybind-key
key (unsigned int)
modifier-flags (unsigned char))
(defstruct keybind
bound-keys (* keybind-key)
num-binds (unsigned char))
(defstruct sdl-key-states
last-frame-states (* (const (unsigned char)))
this-frame-states (* (const (unsigned char))))
(defun keybind-tapped (keybinds (* keybind) key-states (* sdl-key-states) &return bool)
(each-in-range (path keybinds > num-binds) i
(var bind (* keybind-key) (addr (at i (path keybinds > bound-keys))))
(when (sdl-is-key-tapped-this-frame
(path bind > key)
(path key-states > this-frame-states)
(path key-states > last-frame-states))
(cond
((bit-and (path bind > modifier-flags) keybind-modifier-flags-ctrl)
(return (or (at SDL_SCANCODE_LCTRL (path key-states > this-frame-states))
(at SDL_SCANCODE_RCTRL (path key-states > this-frame-states)))))
((bit-and (path bind > modifier-flags) keybind-modifier-flags-shift)
(return (or (at SDL_SCANCODE_LSHIFT (path key-states > this-frame-states))
(at SDL_SCANCODE_RSHIFT (path key-states > this-frame-states)))))
((bit-and (path bind > modifier-flags) keybind-modifier-flags-alt)
(return (or (at SDL_SCANCODE_LALT (path key-states > this-frame-states))
(at SDL_SCANCODE_RALT (path key-states > this-frame-states)))))
(true
(return true)))))
(return false))
(defun-local append-to-string-buffer (buffer-out (* char) buffer-size size_t write-head (* (* char))
append-str (* (const char)))
(var str-length size_t (strlen append-str))
(when (> str-length (- buffer-size (- (deref write-head) buffer-out)))
(fprintf stderr "error: Buffer was not large enough to fit string! Buffer size: %d\n"
(type-cast buffer-size int))
(return))
(strcpy (deref write-head) append-str)
(set (deref write-head) (+ (deref write-head) str-length)))
(defun keybind-get-description (buffer-out (* char) buffer-size size_t keybinds (* keybind))
(set (at 0 buffer-out) 0)
(unless (path keybinds > num-binds)
(return))
(var write-head (* char) buffer-out)
(each-in-range (path keybinds > num-binds) i
(var bind (* keybind-key) (addr (at i (path keybinds > bound-keys))))
(defstruct flag-to-modifier flag (unsigned char) text (* (const char)))
(var flag-to-modifiers ([] (const flag-to-modifier))
(array (array keybind-modifier-flags-ctrl "Ctrl+")
(array keybind-modifier-flags-shift "Shift+")
(array keybind-modifier-flags-alt "Alt+")))
(each-in-array flag-to-modifiers modifier-index
(when (bit-and (path bind > modifier-flags)
(field (at modifier-index flag-to-modifiers) flag))
(append-to-string-buffer buffer-out buffer-size (addr write-head)
(field (at modifier-index flag-to-modifiers) text))))
(append-to-string-buffer buffer-out buffer-size (addr write-head)
(sdl-scancode-to-user-string (path bind > key)))
(when (< i (- (path keybinds > num-binds) 1))
(append-to-string-buffer buffer-out buffer-size (addr write-head)
", "))))
(defmacro define-keybind (name symbol &rest binds array)
(var buttons-name Token (deref name))
(call-on append (field buttons-name contents) "-buttons")
(tokenize-push output
(var (token-splice-addr buttons-name) ([] keybind-key)
(array (token-splice-rest binds tokens)))
(var (token-splice name) keybind (array (token-splice-addr buttons-name)
(array-size (token-splice-addr buttons-name)))))
(return true))
;;
;; Files
;;

Loading…
Cancel
Save