85 lines
1.8 KiB
C
85 lines
1.8 KiB
C
/**
|
|
* Copyright (c) 2024 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "textbox.h"
|
|
#include "assert/assert.h"
|
|
#include "input.h"
|
|
#include "game/time.h"
|
|
#include "locale/language.h"
|
|
|
|
textbox_t TEXTBOX;
|
|
|
|
void textboxInit() {
|
|
memset(&TEXTBOX, 0, sizeof(textbox_t));
|
|
}
|
|
|
|
void textboxSetText(
|
|
const char_t *title,
|
|
const char_t *text
|
|
) {
|
|
size_t len;
|
|
const char_t *temp;
|
|
assertNotNull(text, "Text cannot be NULL.");
|
|
|
|
// Copy translation
|
|
temp = languageGetPointer(text);
|
|
len = strlen(temp);
|
|
assertTrue(len < TEXTBOX_TEXT_MAX, "Text is too long.");
|
|
strcpy(TEXTBOX.text, temp);
|
|
TEXTBOX.textLength = len;
|
|
|
|
// Setup title
|
|
if(title) {
|
|
temp = languageGetPointer(title);
|
|
len = strlen(temp);
|
|
assertTrue(len < TEXTBOX_TITLE_MAX, "Title is too long.");
|
|
strcpy(TEXTBOX.title, temp);
|
|
} else {
|
|
TEXTBOX.title[0] = '\0';
|
|
}
|
|
|
|
// Prepare for scrolling
|
|
TEXTBOX.nextChar = (1.0f / TEXTBOX_CHARS_PER_SECOND);
|
|
TEXTBOX.textIndex = 0;
|
|
TEXTBOX.open = true;
|
|
}
|
|
|
|
bool_t textboxIsOpen() {
|
|
return TEXTBOX.open;
|
|
}
|
|
|
|
void textboxUpdate() {
|
|
if(!textboxIsOpen()) return;
|
|
|
|
// Have we finished scrolling?
|
|
if(TEXTBOX.textIndex >= TEXTBOX.textLength) {
|
|
// Wait for input
|
|
if(!inputWasPressed(INPUT_BIND_ACCEPT)) return;
|
|
TEXTBOX.open = false;
|
|
return;
|
|
}
|
|
|
|
// Update scrolling
|
|
TEXTBOX.nextChar -= TIME.delta;
|
|
if(TEXTBOX.nextChar > 0.0f) return;
|
|
|
|
switch(TEXTBOX.text[TEXTBOX.textIndex]) {
|
|
// Exaggerate the pause for these characters
|
|
case '\n':
|
|
case '.':
|
|
case ',':
|
|
case ';':
|
|
case ':':
|
|
TEXTBOX.nextChar = TEXTBOX_CHARS_PAUSE_DELAY;
|
|
break;
|
|
|
|
default:
|
|
TEXTBOX.nextChar = (1.0f / TEXTBOX_CHARS_PER_SECOND);
|
|
break;
|
|
}
|
|
TEXTBOX.textIndex++;
|
|
} |