Lumenarium/foldhaus_text_entry.cpp

114 lines
3.1 KiB
C++
Raw Normal View History

internal void
2019-09-21 20:19:02 +00:00
ResetTextInput (text_entry* Input)
{
Input->CursorPosition = 0;
Input->Buffer.Length = 0;
}
internal void
2019-09-21 20:19:02 +00:00
PipeSearchStringToDestination (text_entry* Input)
{
switch (Input->Destination.Type)
{
case TextTranslateTo_String:
{
CopyStringTo(Input->Buffer, Input->Destination.StringDest);
}break;
case TextTranslateTo_R32:
{
parse_result FloatParseResult = ParseFloat(Input->Buffer.Memory, Input->Buffer.Length);
*Input->Destination.FloatDest = FloatParseResult.FloatValue;
}break;
InvalidDefaultCase;
}
}
2019-09-21 20:19:02 +00:00
internal void
RemoveCharacterAtCursor (text_entry* TextEntry)
{
2019-09-21 20:19:02 +00:00
if (TextEntry->CursorPosition > 0)
{
2019-09-21 20:19:02 +00:00
RemoveCharAt(&TextEntry->Buffer,
TextEntry->CursorPosition - 1);
TextEntry->CursorPosition--;
}
}
internal void
2019-09-21 20:19:02 +00:00
SetTextInputDestinationToString (text_entry* TextInput, string* DestinationString)
{
ResetTextInput(TextInput);
TextInput->Destination.Type = TextTranslateTo_String;
TextInput->Destination.StringDest = DestinationString;
CopyStringTo(*DestinationString, &TextInput->Buffer);
}
internal void
2019-09-21 20:19:02 +00:00
SetTextInputDestinationToFloat (text_entry* TextInput, r32* DestinationFloat)
{
ResetTextInput(TextInput);
TextInput->Destination.Type = TextTranslateTo_R32;
TextInput->Destination.FloatDest = DestinationFloat;
PrintF(&TextInput->Buffer, "%f", *DestinationFloat);
if (*DestinationFloat == 0.0f)
{
TextInput->CursorPosition = 1;
}
}
internal void
2019-09-21 20:19:02 +00:00
AppendInputToEntryString (text_entry* EntryString, char* InputString, s32 InputStringLength)
{
if (InputStringLength > 0)
{
for (s32 i = 0; i < InputStringLength; i++)
{
InsertChar(&EntryString->Buffer, InputString[i], EntryString->CursorPosition);
EntryString->CursorPosition++;
}
}
PipeSearchStringToDestination(EntryString);
}
2019-09-21 20:19:02 +00:00
internal void
MoveCursorRight (text_entry* TextEntry)
{
2019-09-21 20:19:02 +00:00
TextEntry->CursorPosition = GSMin(TextEntry->Buffer.Length,
TextEntry->CursorPosition + 1);
}
2019-09-21 20:19:02 +00:00
internal void
MoveCursorLeft (text_entry* TextEntry)
{
2019-09-21 20:19:02 +00:00
TextEntry->CursorPosition = GSMax(0, TextEntry->CursorPosition - 1);
}
2019-09-21 20:19:02 +00:00
FOLDHAUS_INPUT_COMMAND_PROC(RemoveCharacterFromEntryString)
{
RemoveCharacterAtCursor(&State->ActiveTextEntry);
}
FOLDHAUS_INPUT_COMMAND_PROC(TextEntryMoveCursorRight)
{
MoveCursorRight(&State->ActiveTextEntry);
}
FOLDHAUS_INPUT_COMMAND_PROC(TextEntryMoveCursorLeft)
{
2019-09-21 20:19:02 +00:00
MoveCursorLeft(&State->ActiveTextEntry);
}
internal void
2019-09-21 20:19:02 +00:00
InitializeTextInputCommands (input_command_registry* Commands, memory_arena* PermanentStorage)
{
2019-09-21 20:19:02 +00:00
if (Commands->Size > 0)
{
2019-09-21 20:19:02 +00:00
RegisterKeyPressCommand(Commands, KeyCode_Backspace, false, KeyCode_Invalid, RemoveCharacterFromEntryString);
RegisterKeyPressCommand(Commands, KeyCode_LeftArrow, false, KeyCode_Invalid, TextEntryMoveCursorLeft);
RegisterKeyPressCommand(Commands, KeyCode_RightArrow, false, KeyCode_Invalid, TextEntryMoveCursorRight);
}
}