This repository has been archived on 2024-11-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Dawn-GB/src/conversation/questionbox.c

87 lines
2.3 KiB
C

/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "questionbox.h"
uint8_t QUESTION_BOX_OPTION_COUNT;
uint8_t QUESTION_BOX_OPTION_CURRENT;
void questionBoxSetOptions(char *title, char **options, uint8_t count) {
uint8_t i, j;
char buffer[TEXTBOX_CHARS_MAX + 1];
char spaces[QUESTION_BOX_QUESTION_SPACES];
sprintf(buffer, title);
i = 0;
QUESTION_BOX_OPTION_CURRENT = 0;
QUESTION_BOX_OPTION_COUNT = count;
for(i = 0; i < QUESTION_BOX_QUESTION_SPACES; i++) {
spaces[i] = ' ';
}
// For each row...
for(i = 0; i < count; i++) {
if((i & 1) == 0) {
j = strlen(options[i]);
sprintf(buffer, "%s\n %s", buffer, options[i]);
} else {
j = QUESTION_BOX_QUESTION_SPACES - j;
spaces[j + 1] = '\0';
sprintf(buffer, "%s%s%s", buffer, spaces, options[i]);
spaces[j + 1] = ' ';
}
}
conversationTextboxSetText(buffer);
// Modify the textbox state
TEXTBOX_STATE |= TEXTBOX_STATE_IS_QUESTION;
// Repurpose old array and draw arrow
spaces[0] = spriteFontTileFromChar(QUESTION_BOX_CURSOR);
spriteBufferWindow(0x01, TEXTBOX_Y + 0x02, 1, 1, spaces);
}
inline void questionBoxUpdate() {
uint8_t tiles[1];
uint8_t i;
if((TEXTBOX_STATE & (TEXTBOX_STATE_VISIBLE|TEXTBOX_STATE_IS_QUESTION)) == 0) return;
// Detect input
if(INPUT_PRESSED & J_RIGHT) {
QUESTION_BOX_OPTION_CURRENT++;
} else if(INPUT_PRESSED & J_LEFT) {
QUESTION_BOX_OPTION_CURRENT--;
} else if(INPUT_PRESSED & J_UP) {
QUESTION_BOX_OPTION_CURRENT -= 2;
} else if(INPUT_PRESSED & J_DOWN) {
QUESTION_BOX_OPTION_CURRENT += 2;
} else {
return;
}
// Bound.
QUESTION_BOX_OPTION_CURRENT = QUESTION_BOX_OPTION_CURRENT % QUESTION_BOX_OPTION_COUNT;
// Decide where to render arrow
for(i = 0; i < QUESTION_BOX_OPTION_COUNT; i++) {
if(i == QUESTION_BOX_OPTION_CURRENT) {
tiles[0] = spriteFontTileFromChar(QUESTION_BOX_CURSOR);
} else {
tiles[0] = spriteFontTileFromChar(' ');
}
spriteBufferWindow(
0x01 + ((i % 0x02) * QUESTION_BOX_QUESTION_SPACES),
TEXTBOX_Y + 0x02 + (i / 0x02),
1, 1,
tiles
);
}
}