Skip to content

Commit

Permalink
Fix editor text input (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Oct 24, 2015
1 parent 469ddae commit 96bbe9d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 55 deletions.
14 changes: 14 additions & 0 deletions src/cdogs/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ void EventPoll(EventHandlers *handlers, Uint32 ticks)
case SDL_KEYUP:
KeyOnKeyUp(&handlers->keyboard, e.key.keysym);
break;
case SDL_TEXTINPUT:
strcpy(handlers->keyboard.Typed, e.text.text);
break;
case SDL_MOUSEBUTTONDOWN:
MouseOnButtonDown(&handlers->mouse, e.button.button);
break;
Expand Down Expand Up @@ -442,6 +445,17 @@ SDL_Scancode GetKey(EventHandlers *handlers)
return k;
}

SDL_Scancode EventWaitKeyOrText(EventHandlers *handlers)
{
SDL_Scancode k = SDL_SCANCODE_UNKNOWN;
do
{
EventPoll(handlers, SDL_GetTicks());
k = KeyGetPressed(&handlers->keyboard);
} while (k == SDL_SCANCODE_UNKNOWN && handlers->keyboard.Typed[0] == '\0');
return k;
}

static GameLoopResult WaitResult(
GameLoopWaitForAnyKeyOrButtonData *data, const bool result);
GameLoopResult GameLoopWaitForAnyKeyOrButtonFunc(void *data)
Expand Down
2 changes: 2 additions & 0 deletions src/cdogs/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ int GetGameCmd(
EventHandlers *handlers,
const PlayerData *playerData, const Vec2i playerPos);
SDL_Scancode GetKey(EventHandlers *handlers);
// Wait until there is a key press or text input
SDL_Scancode EventWaitKeyOrText(EventHandlers *handlers);
typedef struct
{
bool IsOK;
Expand Down
16 changes: 1 addition & 15 deletions src/cdogs/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void KeyPrePoll(keyboard_t *keyboard)
keyboard->currentKeys,
sizeof keyboard->previousKeys);
keyboard->modState = SDL_GetModState();
keyboard->Typed[0] = '\0';
}

void KeyOnKeyDown(keyboard_t *keyboard, const SDL_Keysym s)
Expand Down Expand Up @@ -195,21 +196,6 @@ SDL_Scancode KeyGetPressed(const keyboard_t *k)
return 0;
}

SDL_Keycode KeyGetTyped(const keyboard_t *k)
{
for (int i = 0; i < SDL_NUM_SCANCODES; i++)
{
const SDL_Keycode keycode = k->currentKeys[i].keycode;
if (KeyIsPressed(k, i) &&
keycode >= (SDL_Keycode)' ' &&
keycode <= (SDL_Keycode)'z')
{
return keycode;
}
}
return 0;
}

SDL_Scancode KeyGet(const InputKeys *keys, const key_code_e keyCode)
{
switch (keyCode)
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ typedef struct
KeyPress currentKeys[SDL_NUM_SCANCODES];
KeyPress pressedKeys[SDL_NUM_SCANCODES];
SDL_Keymod modState;
char Typed[32];
Uint32 ticks;
Uint32 repeatedTicks;
bool isFirstRepeat;
Expand All @@ -87,6 +88,5 @@ bool KeyIsDown(const keyboard_t *k, const int key);
bool KeyIsPressed(const keyboard_t *k, const int key);
bool KeyIsReleased(const keyboard_t *k, const int key);
SDL_Scancode KeyGetPressed(const keyboard_t *k);
SDL_Keycode KeyGetTyped(const keyboard_t *k);

SDL_Scancode KeyGet(const InputKeys *keys, const key_code_e keyCode);
82 changes: 43 additions & 39 deletions src/cdogsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ static void ReloadUI(void)
sTooltipObj = NULL;
}

static void GetTextInput(char *buf);

static bool TryOpen(const char *filename);
static void Open(void)
{
Expand Down Expand Up @@ -514,8 +516,8 @@ static void Open(void)
BlitFlip(&gGraphicsDevice);

bool doOpen = false;
int c = GetKey(&gEventHandlers);
switch (c)
const SDL_Scancode sc = EventWaitKeyOrText(&gEventHandlers);
switch (sc)
{
case SDL_SCANCODE_RETURN:
case SDL_SCANCODE_KP_ENTER:
Expand All @@ -536,22 +538,10 @@ static void Open(void)
break;

default:
if (strlen(filename) == sizeof(filename) - 1)
{
break;
}
c = KeyGetTyped(&gEventHandlers.keyboard);
if (c && c != '*' &&
(strlen(filename) > 1 || c != '-') &&
c != ':' && c != '<' && c != '>' && c != '?' &&
c != '|')
{
size_t si = strlen(filename);
filename[si + 1] = 0;
filename[si] = (char)c;
}
// Do nothing
break;
}
GetTextInput(filename);
if (doOpen)
{
ClearScreen(&gGraphicsDevice);
Expand Down Expand Up @@ -618,8 +608,8 @@ static void Save(void)
pos = FontCh('<', pos);
BlitFlip(&gGraphicsDevice);

int c = GetKey(&gEventHandlers);
switch (c)
const SDL_Scancode sc = EventWaitKeyOrText(&gEventHandlers);
switch (sc)
{
case SDL_SCANCODE_RETURN:
case SDL_SCANCODE_KP_ENTER:
Expand All @@ -640,21 +630,10 @@ static void Save(void)
break;

default:
if (strlen(filename) == sizeof(filename) - 1)
{
break;
}
c = KeyGetTyped(&gEventHandlers.keyboard);
if (c && c != '*' &&
(strlen(filename) > 1 || c != '-') &&
c != ':' && c != '<' && c != '>' && c != '?' &&
c != '|')
{
size_t si = strlen(filename);
filename[si + 1] = 0;
filename[si] = (char)c;
}
// Do nothing
break;
}
GetTextInput(filename);
SDL_Delay(10);
}
if (doSave)
Expand All @@ -672,6 +651,28 @@ static void Save(void)
}
}

// Collect keyboard text input into buffer
// Only support ASCII and limited characters
static void GetTextInput(char *buf)
{
// Get the filename typed, ASCII only
char *c = gEventHandlers.keyboard.Typed;
while (c && strlen(buf) < CDOGS_PATH_MAX - 1 && *c >= ' ' && *c <= '~')
{
// Prohibit some characters bad for filenames and/or not in font
if (*c != '*' &&
(strlen(buf) > 1 || *c != '-') &&
*c != ':' && *c != '<' && *c != '>' && *c != '?' &&
*c != '|')
{
const size_t si = strlen(buf);
buf[si + 1] = 0;
buf[si] = *c;
}
c++;
}
}

static void HelpScreen(void)
{
Vec2i pos = Vec2iNew(20, 20);
Expand Down Expand Up @@ -1125,15 +1126,16 @@ static HandleInputResult HandleInput(
break;

default:
{
SDL_Keycode kc = KeyGetTyped(&gEventHandlers.keyboard);
if (kc)
{
fileChanged |= UIObjectAddChar(sObjs, (char)kc);
}
}
// Do nothing
break;
}
// Get text input, ASCII only
char *c = gEventHandlers.keyboard.Typed;
while (c && *c >= ' ' && *c <= '~')
{
fileChanged |= UIObjectAddChar(sObjs, *c);
c++;
}
}
if (gEventHandlers.HasQuit)
{
Expand Down Expand Up @@ -1221,6 +1223,7 @@ static void EditCampaign(void)
Uint32 ticksNow = SDL_GetTicks();
sTicksElapsed = 0;
ticksAutosave = AUTOSAVE_INTERVAL_SECONDS * 1000;
SDL_StartTextInput();
for (;;)
{
Uint32 ticksThen = ticksNow;
Expand Down Expand Up @@ -1258,6 +1261,7 @@ static void EditCampaign(void)
debug(D_MAX, "End loop\n");
sTicksElapsed -= 1000 / (FPS_FRAMELIMIT * 2);
}
SDL_StopTextInput();
}


Expand Down

0 comments on commit 96bbe9d

Please sign in to comment.