aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui.c')
-rw-r--r--src/ui.c48
1 files changed, 40 insertions, 8 deletions
diff --git a/src/ui.c b/src/ui.c
index b8451e5..4035b44 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -18,7 +18,7 @@ void resizeInteractionChat(InteractionChat* chat, const Settings* settings)
void initInteractionChat(InteractionChat* chat, const Settings* settings)
{
- memset(&chat->text, 0, INTERACTION_CHAT_MAX * sizeof(char));
+ clearInteractionChat(chat);
chat->visible = false;
chat->entityId = ENTITY_NONE;
@@ -37,17 +37,37 @@ void hideInteractionChat(InteractionChat* chat)
void setInteractionChat(InteractionChat* chat, const char* text)
{
- strncpy(chat->text, text, INTERACTION_CHAT_MAX * sizeof(char) - 1);
+ strncpy(chat->text, text, INTERACTION_CHAT_MAX - 1);
+ chat->textSize = getStringLength(text, INTERACTION_CHAT_MAX);
+ chat->displayUpTo = 0;
+ chat->lastCharacterUpdate = GetTime();
}
void writeToInteractionChat(InteractionChat* chat, const char* text)
{
- strncat(chat->text, text, INTERACTION_CHAT_MAX * sizeof(char) - 1);
+ strncat(chat->text, text, INTERACTION_CHAT_MAX - 1);
+ chat->textSize += getStringLength(text, INTERACTION_CHAT_MAX);
+ chat->textSize %= INTERACTION_CHAT_MAX;
+ chat->lastCharacterUpdate = GetTime();
}
void clearInteractionChat(InteractionChat* chat)
{
- memset(&chat->text, 0, INTERACTION_CHAT_MAX * sizeof(char));
+ memset(&chat->text, 0, INTERACTION_CHAT_MAX);
+ memset(&chat->displayedText, 0, INTERACTION_CHAT_MAX);
+ chat->textSize = 0;
+ chat->displayUpTo = 0;
+}
+
+bool isInteractionChatAnimationDone(InteractionChat* chat)
+{
+ return chat->displayUpTo >= chat->textSize;
+}
+
+void endInteractionChatAnimation(InteractionChat* chat)
+{
+ chat->displayUpTo = chat->textSize;
+ strncpy(chat->displayedText, chat->text, chat->textSize);
}
void updateInteractionChat(InteractionChat* chat, Game* game)
@@ -72,6 +92,19 @@ void updateInteractionChat(InteractionChat* chat, Game* game)
DrawRectangleLinesEx(chat->rect, lineThickness, BLACK);
+ // Do cool animation.
+ double currentTime = GetTime();
+
+ if (chat->displayUpTo < chat->textSize &&
+ currentTime - chat->lastCharacterUpdate >=
+ game->settings.interactionChatAnimationSpeed)
+ {
+ chat->lastCharacterUpdate = currentTime;
+ chat->displayedText[chat->displayUpTo] = chat->text[chat->displayUpTo];
+ ++chat->displayUpTo;
+ }
+
+ // Draw text.
if (chat->entityId != ENTITY_NONE)
{
DrawText(TextFormat("%s says:", getEntityName(chat->entityId)),
@@ -81,7 +114,7 @@ void updateInteractionChat(InteractionChat* chat, Game* game)
WHITE);
}
- DrawText(chat->text,
+ DrawText(chat->displayedText,
chat->rect.x + border,
chat->rect.y + (border * 2.0) + fontSize,
fontSize,
@@ -127,8 +160,7 @@ void setInteractionMenu(InteractionMenu* menu,
for (int index = 0; index < itemCount; ++index)
{
- strncpy(menu->items[index], items[index],
- INTERACTION_LABEL_MAX * sizeof(char) - 1);
+ strncpy(menu->items[index], items[index], INTERACTION_LABEL_MAX - 1);
}
}
@@ -136,7 +168,7 @@ void resetInteractionMenu(InteractionMenu* menu)
{
for (int index = 0; index < INTERACTION_MENU_MAX; ++index)
{
- memset(menu->items[index], 0, INTERACTION_LABEL_MAX * sizeof(char));
+ memset(menu->items[index], 0, INTERACTION_LABEL_MAX);
}
menu->itemCount = 0;