Merge branch '#710'

This commit is contained in:
Vadim Grigoruk 2018-08-25 01:13:51 +03:00
commit 26551ca5ec
4 changed files with 270 additions and 182 deletions

View File

@ -1,82 +1,233 @@
[tic_key_unknown] = SDLK_UNKNOWN, [0] = tic_key_unknown,
[tic_key_a] = SDLK_a, [1] = tic_key_unknown,
[tic_key_b] = SDLK_b, [2] = tic_key_unknown,
[tic_key_c] = SDLK_c, [3] = tic_key_unknown,
[tic_key_d] = SDLK_d, [SDL_SCANCODE_A] = tic_key_a,
[tic_key_e] = SDLK_e, [SDL_SCANCODE_B] = tic_key_b,
[tic_key_f] = SDLK_f, [SDL_SCANCODE_C] = tic_key_c,
[tic_key_g] = SDLK_g, [SDL_SCANCODE_D] = tic_key_d,
[tic_key_h] = SDLK_h, [SDL_SCANCODE_E] = tic_key_e,
[tic_key_i] = SDLK_i, [SDL_SCANCODE_F] = tic_key_f,
[tic_key_j] = SDLK_j, [SDL_SCANCODE_G] = tic_key_g,
[tic_key_k] = SDLK_k, [SDL_SCANCODE_H] = tic_key_h,
[tic_key_l] = SDLK_l, [SDL_SCANCODE_I] = tic_key_i,
[tic_key_m] = SDLK_m, [SDL_SCANCODE_J] = tic_key_j,
[tic_key_n] = SDLK_n, [SDL_SCANCODE_K] = tic_key_k,
[tic_key_o] = SDLK_o, [SDL_SCANCODE_L] = tic_key_l,
[tic_key_p] = SDLK_p, [SDL_SCANCODE_M] = tic_key_m,
[tic_key_q] = SDLK_q, [SDL_SCANCODE_N] = tic_key_n,
[tic_key_r] = SDLK_r, [SDL_SCANCODE_O] = tic_key_o,
[tic_key_s] = SDLK_s, [SDL_SCANCODE_P] = tic_key_p,
[tic_key_t] = SDLK_t, [SDL_SCANCODE_Q] = tic_key_q,
[tic_key_u] = SDLK_u, [SDL_SCANCODE_R] = tic_key_r,
[tic_key_v] = SDLK_v, [SDL_SCANCODE_S] = tic_key_s,
[tic_key_w] = SDLK_w, [SDL_SCANCODE_T] = tic_key_t,
[tic_key_x] = SDLK_x, [SDL_SCANCODE_U] = tic_key_u,
[tic_key_y] = SDLK_y, [SDL_SCANCODE_V] = tic_key_v,
[tic_key_z] = SDLK_z, [SDL_SCANCODE_W] = tic_key_w,
[SDL_SCANCODE_X] = tic_key_x,
[tic_key_0] = SDLK_0, [SDL_SCANCODE_Y] = tic_key_y,
[tic_key_1] = SDLK_1, [SDL_SCANCODE_Z] = tic_key_z,
[tic_key_2] = SDLK_2, [SDL_SCANCODE_1] = tic_key_1,
[tic_key_3] = SDLK_3, [SDL_SCANCODE_2] = tic_key_2,
[tic_key_4] = SDLK_4, [SDL_SCANCODE_3] = tic_key_3,
[tic_key_5] = SDLK_5, [SDL_SCANCODE_4] = tic_key_4,
[tic_key_6] = SDLK_6, [SDL_SCANCODE_5] = tic_key_5,
[tic_key_7] = SDLK_7, [SDL_SCANCODE_6] = tic_key_6,
[tic_key_8] = SDLK_8, [SDL_SCANCODE_7] = tic_key_7,
[tic_key_9] = SDLK_9, [SDL_SCANCODE_8] = tic_key_8,
[SDL_SCANCODE_9] = tic_key_9,
[tic_key_minus] = SDLK_MINUS, [SDL_SCANCODE_0] = tic_key_0,
[tic_key_equals] = SDLK_EQUALS, [SDL_SCANCODE_RETURN] = tic_key_return,
[tic_key_leftbracket] = SDLK_LEFTBRACKET, [SDL_SCANCODE_ESCAPE] = tic_key_escape,
[tic_key_rightbracket] = SDLK_RIGHTBRACKET, [SDL_SCANCODE_AC_BACK] = tic_key_escape,
[tic_key_backslash] = SDLK_BACKSLASH, [SDL_SCANCODE_BACKSPACE] = tic_key_backspace,
[tic_key_semicolon] = SDLK_SEMICOLON, [SDL_SCANCODE_TAB] = tic_key_tab,
[tic_key_apostrophe] = SDLK_QUOTE, [SDL_SCANCODE_SPACE] = tic_key_space,
[tic_key_grave] = SDLK_BACKQUOTE, [SDL_SCANCODE_MINUS] = tic_key_minus,
[tic_key_comma] = SDLK_COMMA, [SDL_SCANCODE_EQUALS] = tic_key_equals,
[tic_key_period] = SDLK_PERIOD, [SDL_SCANCODE_LEFTBRACKET] = tic_key_leftbracket,
[tic_key_slash] = SDLK_SLASH, [SDL_SCANCODE_RIGHTBRACKET] = tic_key_rightbracket,
[tic_key_space] = SDLK_SPACE, [SDL_SCANCODE_BACKSLASH] = tic_key_backslash,
[tic_key_tab] = SDLK_TAB, [50] = tic_key_unknown,
[tic_key_return] = SDLK_RETURN, [SDL_SCANCODE_SEMICOLON] = tic_key_semicolon,
[tic_key_backspace] = SDLK_BACKSPACE, [SDL_SCANCODE_APOSTROPHE] = tic_key_apostrophe,
[tic_key_delete] = SDLK_DELETE, [SDL_SCANCODE_GRAVE] = tic_key_grave,
[tic_key_insert] = SDLK_INSERT, [SDL_SCANCODE_COMMA] = tic_key_comma,
[tic_key_pageup] = SDLK_PAGEUP, [SDL_SCANCODE_PERIOD] = tic_key_period,
[tic_key_pagedown] = SDLK_PAGEDOWN, [SDL_SCANCODE_SLASH] = tic_key_slash,
[tic_key_home] = SDLK_HOME, [SDL_SCANCODE_CAPSLOCK] = tic_key_capslock,
[tic_key_end] = SDLK_END, [SDL_SCANCODE_F1] = tic_key_f1,
[tic_key_up] = SDLK_UP, [SDL_SCANCODE_F2] = tic_key_f2,
[tic_key_down] = SDLK_DOWN, [SDL_SCANCODE_F3] = tic_key_f3,
[tic_key_left] = SDLK_LEFT, [SDL_SCANCODE_F4] = tic_key_f4,
[tic_key_right] = SDLK_RIGHT, [SDL_SCANCODE_F5] = tic_key_f5,
[tic_key_capslock] = SDLK_CAPSLOCK, [SDL_SCANCODE_F6] = tic_key_f6,
[tic_key_ctrl] = SDLK_LCTRL, [SDL_SCANCODE_F7] = tic_key_f7,
[tic_key_shift] = SDLK_LSHIFT, [SDL_SCANCODE_F8] = tic_key_f8,
[tic_key_alt] = SDLK_LALT, [SDL_SCANCODE_F9] = tic_key_f9,
[tic_key_escape] = SDLK_ESCAPE, [SDL_SCANCODE_F10] = tic_key_f10,
[SDL_SCANCODE_F11] = tic_key_f11,
[tic_key_f1] = SDLK_F1, [SDL_SCANCODE_F12] = tic_key_f12,
[tic_key_f2] = SDLK_F2, [70] = tic_key_unknown,
[tic_key_f3] = SDLK_F3, [71] = tic_key_unknown,
[tic_key_f4] = SDLK_F4, [72] = tic_key_unknown,
[tic_key_f5] = SDLK_F5, [SDL_SCANCODE_INSERT] = tic_key_insert,
[tic_key_f6] = SDLK_F6, [SDL_SCANCODE_HOME] = tic_key_home,
[tic_key_f7] = SDLK_F7, [SDL_SCANCODE_PAGEUP] = tic_key_pageup,
[tic_key_f8] = SDLK_F8, [SDL_SCANCODE_DELETE] = tic_key_delete,
[tic_key_f9] = SDLK_F9, [SDL_SCANCODE_END] = tic_key_end,
[tic_key_f10] = SDLK_F10, [SDL_SCANCODE_PAGEDOWN] = tic_key_pagedown,
[tic_key_f11] = SDLK_F11, [SDL_SCANCODE_RIGHT] = tic_key_right,
[tic_key_f12] = SDLK_F12, [SDL_SCANCODE_LEFT] = tic_key_left,
[SDL_SCANCODE_DOWN] = tic_key_down,
[SDL_SCANCODE_UP] = tic_key_up,
[83] = tic_key_unknown,
[84] = tic_key_unknown,
[85] = tic_key_unknown,
[86] = tic_key_unknown,
[87] = tic_key_unknown,
[88] = tic_key_unknown,
[89] = tic_key_unknown,
[90] = tic_key_unknown,
[91] = tic_key_unknown,
[92] = tic_key_unknown,
[93] = tic_key_unknown,
[94] = tic_key_unknown,
[95] = tic_key_unknown,
[96] = tic_key_unknown,
[97] = tic_key_unknown,
[98] = tic_key_unknown,
[99] = tic_key_unknown,
[100] = tic_key_unknown,
[101] = tic_key_unknown,
[102] = tic_key_unknown,
[103] = tic_key_unknown,
[104] = tic_key_unknown,
[105] = tic_key_unknown,
[106] = tic_key_unknown,
[107] = tic_key_unknown,
[108] = tic_key_unknown,
[109] = tic_key_unknown,
[110] = tic_key_unknown,
[111] = tic_key_unknown,
[112] = tic_key_unknown,
[113] = tic_key_unknown,
[114] = tic_key_unknown,
[115] = tic_key_unknown,
[116] = tic_key_unknown,
[117] = tic_key_unknown,
[118] = tic_key_unknown,
[119] = tic_key_unknown,
[120] = tic_key_unknown,
[121] = tic_key_unknown,
[122] = tic_key_unknown,
[123] = tic_key_unknown,
[124] = tic_key_unknown,
[125] = tic_key_unknown,
[126] = tic_key_unknown,
[127] = tic_key_unknown,
[128] = tic_key_unknown,
[129] = tic_key_unknown,
[130] = tic_key_unknown,
[131] = tic_key_unknown,
[132] = tic_key_unknown,
[133] = tic_key_unknown,
[134] = tic_key_unknown,
[135] = tic_key_unknown,
[136] = tic_key_unknown,
[137] = tic_key_unknown,
[138] = tic_key_unknown,
[139] = tic_key_unknown,
[140] = tic_key_unknown,
[141] = tic_key_unknown,
[142] = tic_key_unknown,
[143] = tic_key_unknown,
[144] = tic_key_unknown,
[145] = tic_key_unknown,
[146] = tic_key_unknown,
[147] = tic_key_unknown,
[148] = tic_key_unknown,
[149] = tic_key_unknown,
[150] = tic_key_unknown,
[151] = tic_key_unknown,
[152] = tic_key_unknown,
[153] = tic_key_unknown,
[154] = tic_key_unknown,
[155] = tic_key_unknown,
[156] = tic_key_unknown,
[157] = tic_key_unknown,
[158] = tic_key_unknown,
[159] = tic_key_unknown,
[160] = tic_key_unknown,
[161] = tic_key_unknown,
[162] = tic_key_unknown,
[163] = tic_key_unknown,
[164] = tic_key_unknown,
[165] = tic_key_unknown,
[166] = tic_key_unknown,
[167] = tic_key_unknown,
[168] = tic_key_unknown,
[169] = tic_key_unknown,
[170] = tic_key_unknown,
[171] = tic_key_unknown,
[172] = tic_key_unknown,
[173] = tic_key_unknown,
[174] = tic_key_unknown,
[175] = tic_key_unknown,
[176] = tic_key_unknown,
[177] = tic_key_unknown,
[178] = tic_key_unknown,
[179] = tic_key_unknown,
[180] = tic_key_unknown,
[181] = tic_key_unknown,
[182] = tic_key_unknown,
[183] = tic_key_unknown,
[184] = tic_key_unknown,
[185] = tic_key_unknown,
[186] = tic_key_unknown,
[187] = tic_key_unknown,
[188] = tic_key_unknown,
[189] = tic_key_unknown,
[190] = tic_key_unknown,
[191] = tic_key_unknown,
[192] = tic_key_unknown,
[193] = tic_key_unknown,
[194] = tic_key_unknown,
[195] = tic_key_unknown,
[196] = tic_key_unknown,
[197] = tic_key_unknown,
[198] = tic_key_unknown,
[199] = tic_key_unknown,
[200] = tic_key_unknown,
[201] = tic_key_unknown,
[202] = tic_key_unknown,
[203] = tic_key_unknown,
[204] = tic_key_unknown,
[205] = tic_key_unknown,
[206] = tic_key_unknown,
[207] = tic_key_unknown,
[208] = tic_key_unknown,
[209] = tic_key_unknown,
[210] = tic_key_unknown,
[211] = tic_key_unknown,
[212] = tic_key_unknown,
[213] = tic_key_unknown,
[214] = tic_key_unknown,
[215] = tic_key_unknown,
[216] = tic_key_unknown,
[217] = tic_key_unknown,
[218] = tic_key_unknown,
[219] = tic_key_unknown,
[220] = tic_key_unknown,
[221] = tic_key_unknown,
[222] = tic_key_unknown,
[223] = tic_key_unknown,
[SDL_SCANCODE_LCTRL] = tic_key_ctrl,
[SDL_SCANCODE_LSHIFT] = tic_key_shift,
[SDL_SCANCODE_LALT] = tic_key_alt,
[SDL_SCANCODE_LGUI] = tic_key_ctrl,
[SDL_SCANCODE_RCTRL] = tic_key_ctrl,
[SDL_SCANCODE_RSHIFT] = tic_key_shift,
[SDL_SCANCODE_RALT] = tic_key_alt,
[SDL_SCANCODE_RGUI] = tic_key_ctrl,

View File

@ -237,6 +237,10 @@ static struct
char getKeyboardText() char getKeyboardText()
{ {
char text = getSystem()->getKeyboardText();
if(text) return text;
tic_mem* tic = impl.studio.tic; tic_mem* tic = impl.studio.tic;
static const char Symbols[] = " abcdefghijklmnopqrstuvwxyz0123456789-=[]\\;'`,./ "; static const char Symbols[] = " abcdefghijklmnopqrstuvwxyz0123456789-=[]\\;'`,./ ";

View File

@ -65,8 +65,7 @@ static struct
GPU_Image* down; GPU_Image* down;
} texture; } texture;
u8 text; char text;
} keyboard; } keyboard;
u32 touchCounter; u32 touchCounter;
@ -396,107 +395,41 @@ static void processMouse()
} }
} }
static void appendKeycode(tic_keycode code) // TODO: ugly hack, but I didn't find a better solution
// will try to fix it later
static char getKeyboardText()
{ {
tic80_input* input = &platform.studio->tic->ram.input; char text = platform.keyboard.text;
platform.keyboard.text = 0;
enum{BufSize = COUNT_OF(input->keyboard.keys)}; return text;
for(s32 i = 0; i < BufSize; i++)
{
if(input->keyboard.keys[i] == tic_key_unknown)
{
input->keyboard.keys[i] = code;
return;
}
else if(input->keyboard.keys[i] == code) return;
}
} }
static void processKeyboard() static void processKeyboard()
{ {
tic80_input* input = &platform.studio->tic->ram.input; static const u8 KeyboardCodes[] =
input->keyboard.data = 0;
SDL_Keymod mod = SDL_GetModState();
if(mod & KMOD_RALT)
{
static const struct {tic_keycode code; tic_keycode shift;} TextCodes[] =
{
['-'] = {tic_key_minus, tic_key_unknown},
['='] = {tic_key_equals, tic_key_unknown},
['['] = {tic_key_leftbracket, tic_key_unknown},
[']'] = {tic_key_rightbracket, tic_key_unknown},
['\\'] = {tic_key_backslash, tic_key_unknown},
[';'] = {tic_key_semicolon, tic_key_unknown},
['\''] = {tic_key_apostrophe, tic_key_unknown},
['`'] = {tic_key_grave, tic_key_unknown},
[','] = {tic_key_comma, tic_key_unknown},
['.'] = {tic_key_period, tic_key_unknown},
['/'] = {tic_key_slash, tic_key_unknown},
[')'] = {tic_key_0, tic_key_shift},
['!'] = {tic_key_1, tic_key_shift},
['@'] = {tic_key_2, tic_key_shift},
['#'] = {tic_key_3, tic_key_shift},
['$'] = {tic_key_4, tic_key_shift},
['%'] = {tic_key_5, tic_key_shift},
['^'] = {tic_key_6, tic_key_shift},
['&'] = {tic_key_7, tic_key_shift},
['*'] = {tic_key_8, tic_key_shift},
['('] = {tic_key_9, tic_key_shift},
['_'] = {tic_key_minus, tic_key_shift},
['+'] = {tic_key_equals, tic_key_shift},
['{'] = {tic_key_leftbracket, tic_key_shift},
['}'] = {tic_key_rightbracket, tic_key_shift},
['|'] = {tic_key_backslash, tic_key_shift},
[':'] = {tic_key_semicolon, tic_key_shift},
['"'] = {tic_key_apostrophe, tic_key_shift},
['~'] = {tic_key_grave, tic_key_shift},
['<'] = {tic_key_comma, tic_key_shift},
['>'] = {tic_key_period, tic_key_shift},
['?'] = {tic_key_slash, tic_key_shift},
};
if(platform.keyboard.text && platform.keyboard.text < COUNT_OF(TextCodes))
{
appendKeycode(TextCodes[platform.keyboard.text].code);
appendKeycode(TextCodes[platform.keyboard.text].shift);
platform.keyboard.text = 0;
}
return;
}
const u8* keyboard = SDL_GetKeyboardState(NULL);
if(mod & KMOD_SHIFT) appendKeycode(tic_key_shift);
if(mod & (KMOD_CTRL | KMOD_GUI)) appendKeycode(tic_key_ctrl);
if(mod & KMOD_CAPS) appendKeycode(tic_key_capslock);
static const SDL_Keycode KeyboardCodes[tic_keys_count] =
{ {
#include "keycodes.inl" #include "keycodes.inl"
}; };
for(s32 i = 0; i < SDL_NUM_SCANCODES; i++) tic80_input* input = &platform.studio->tic->ram.input;
{ input->keyboard.data = 0;
if(keyboard[i])
{
SDL_Keycode keycode = i == SDL_SCANCODE_AC_BACK
? SDLK_ESCAPE
: SDL_GetKeyFromScancode(i);
for(s32 k = 0; k < COUNT_OF(KeyboardCodes); k++) enum{BufSize = COUNT_OF(input->keyboard.keys)};
{
if(KeyboardCodes[k] == keycode) s32 c = 0;
{ {
appendKeycode(k); SDL_Keymod mod = SDL_GetModState();
break; if(mod & KMOD_SHIFT) input->keyboard.keys[c++] = tic_key_shift;
} if(mod & (KMOD_CTRL | KMOD_GUI)) input->keyboard.keys[c++] = tic_key_ctrl;
} if(mod & KMOD_ALT) input->keyboard.keys[c++] = tic_key_alt;
} if(mod & KMOD_CAPS) input->keyboard.keys[c++] = tic_key_capslock;
} }
const u8* keyboard = SDL_GetKeyboardState(NULL);
for(s32 i = 0; i < COUNT_OF(KeyboardCodes) && c < COUNT_OF(input->keyboard.keys); i++)
if(keyboard[i] && KeyboardCodes[i] > tic_key_unknown)
input->keyboard.keys[c++] = KeyboardCodes[i];
} }
#if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__) #if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__)
@ -864,15 +797,12 @@ static void pollEvent()
break; break;
case SDL_TEXTINPUT: case SDL_TEXTINPUT:
{ {
const char* symbol = event.text.text; const char* text = event.text.text;
if(strlen(symbol) == 1)
platform.keyboard.text = *symbol; if(strlen(text) == 1)
platform.keyboard.text = *text;
} }
break; break;
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_RALT)
platform.keyboard.text = 0;
break;
case SDL_QUIT: case SDL_QUIT:
platform.studio->exit(); platform.studio->exit();
break; break;
@ -1248,7 +1178,7 @@ static void preseed()
static char* prepareShader(const char* code) static char* prepareShader(const char* code)
{ {
GPU_Renderer* renderer = GPU_GetCurrentRenderer(); GPU_Renderer* renderer = GPU_GetCurrentRenderer();
const char* header = ""; const char* header = "";
if(renderer->shader_language == GPU_LANGUAGE_GLSL) if(renderer->shader_language == GPU_LANGUAGE_GLSL)
{ {
@ -1366,6 +1296,7 @@ static System systemInterface =
.preseed = preseed, .preseed = preseed,
.poll = pollEvent, .poll = pollEvent,
.updateConfig = updateConfig, .updateConfig = updateConfig,
.getKeyboardText = getKeyboardText,
}; };
static void gpuTick() static void gpuTick()

View File

@ -29,6 +29,8 @@ typedef struct
void (*updateConfig)(); void (*updateConfig)();
char (*getKeyboardText)();
} System; } System;
typedef struct typedef struct