|
@@ -3,81 +3,90 @@
|
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
#include <string.h>
|
|
|
|
|
|
|
|
-void TextBoxInit(TextBox *tb, int screenWidth, int screenHeight)
|
|
|
|
|
|
|
+void TextBoxInit(TextBox *tb, Rectangle rect, Font gameFont)
|
|
|
{
|
|
{
|
|
|
- tb->rect = (Rectangle){
|
|
|
|
|
- .x = 40,
|
|
|
|
|
- .y = screenHeight - 180,
|
|
|
|
|
- .width = screenWidth - 80,
|
|
|
|
|
- .height = 160};
|
|
|
|
|
|
|
+ memset(tb, 0, sizeof(TextBox));
|
|
|
|
|
+ tb->rect = rect;
|
|
|
|
|
+ tb->padding = 20.0f;
|
|
|
|
|
|
|
|
tb->bg_color = (Color){23, 28, 37, 220};
|
|
tb->bg_color = (Color){23, 28, 37, 220};
|
|
|
tb->border_color = (Color){25, 31, 40, 180};
|
|
tb->border_color = (Color){25, 31, 40, 180};
|
|
|
tb->border_thickness = 3;
|
|
tb->border_thickness = 3;
|
|
|
- // TODO: remake to use charset instead of 0-2048 range
|
|
|
|
|
- tb->font = LoadFontEx("assets/fonts/NotoSans-Regular.ttf", 32, NULL, 2048);
|
|
|
|
|
- SetTextureFilter(tb->font.texture, TEXTURE_FILTER_BILINEAR);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ tb->font = gameFont;
|
|
|
tb->font_size = 24;
|
|
tb->font_size = 24;
|
|
|
|
|
+ tb->font_spacing = 1.0f;
|
|
|
tb->text_color = WHITE;
|
|
tb->text_color = WHITE;
|
|
|
- tb->name_color = RED;
|
|
|
|
|
|
|
|
|
|
- tb->typing_speed = 25.0f;
|
|
|
|
|
|
|
+ tb->typing_speed = 45.0f;
|
|
|
tb->timer = 0.0f;
|
|
tb->timer = 0.0f;
|
|
|
|
|
+ tb->text = NULL;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- memset(tb->name, 0, sizeof(tb->name));
|
|
|
|
|
|
|
+void TextBoxCleanup(TextBox *tb)
|
|
|
|
|
+{
|
|
|
|
|
+ if (tb->text) free(tb->text);
|
|
|
|
|
+ if (tb->wrapped_text) free(tb->wrapped_text);
|
|
|
tb->text = NULL;
|
|
tb->text = NULL;
|
|
|
- tb->max_text_length = 1024;
|
|
|
|
|
|
|
+ tb->wrapped_text = NULL;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void TextBoxReflow(TextBox *tb)
|
|
|
|
|
+{
|
|
|
|
|
+ if (tb->wrapped_text) {
|
|
|
|
|
+ free(tb->wrapped_text);
|
|
|
|
|
+ tb->wrapped_text = NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (tb->text) {
|
|
|
|
|
+ float max_text_width = tb->rect.width - (tb->padding * 2);
|
|
|
|
|
+ tb->wrapped_text = WrapText(tb->font, tb->text, tb->font_size,
|
|
|
|
|
+ tb->font_spacing, max_text_width);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void TextBoxSetText(TextBox *tb, const char *name, const char *text)
|
|
|
|
|
|
|
+void TextBoxUpdate(TextBox *tb, float dt)
|
|
|
{
|
|
{
|
|
|
- if (name)
|
|
|
|
|
- strncpy(tb->name, name, 63);
|
|
|
|
|
- else
|
|
|
|
|
- tb->name[0] = '\0';
|
|
|
|
|
|
|
+ tb->timer += dt;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+void TextBoxSetText(TextBox *tb, const char *text)
|
|
|
|
|
+{
|
|
|
if (tb->text)
|
|
if (tb->text)
|
|
|
free(tb->text);
|
|
free(tb->text);
|
|
|
- tb->text = my_strdup(text ? text : "");
|
|
|
|
|
|
|
+ if (text) {
|
|
|
|
|
+ size_t len = strlen(text) + 1;
|
|
|
|
|
+ tb->text = malloc(len);
|
|
|
|
|
+ if (tb->text) {
|
|
|
|
|
+ memcpy(tb->text, text, len);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else
|
|
|
|
|
+ tb->text = NULL;
|
|
|
|
|
+ tb->timer = 0.0f;
|
|
|
|
|
+ TextBoxReflow(tb);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void TextBoxDraw(TextBox *tb)
|
|
|
|
|
|
|
+void TextBoxDraw(const TextBox *tb)
|
|
|
{
|
|
{
|
|
|
DrawRectangleRec(tb->rect, tb->bg_color);
|
|
DrawRectangleRec(tb->rect, tb->bg_color);
|
|
|
- DrawRectangleLinesEx(tb->rect, tb->border_thickness, tb->border_color);
|
|
|
|
|
-
|
|
|
|
|
- if (tb->name[0] != '\0')
|
|
|
|
|
|
|
+ if (tb->border_thickness > 0)
|
|
|
|
|
+ DrawRectangleLinesEx(tb->rect, tb->border_thickness, tb->border_color);
|
|
|
|
|
+ if (tb->wrapped_text && tb->wrapped_text[0] != '\0')
|
|
|
{
|
|
{
|
|
|
- Vector2 nameSize = MeasureTextEx(tb->font, tb->name,
|
|
|
|
|
- tb->font_size + 2, 1);
|
|
|
|
|
- Rectangle nameRect = {
|
|
|
|
|
- tb->rect.x + 20,
|
|
|
|
|
- tb->rect.y - 30,
|
|
|
|
|
- nameSize.x + 24,
|
|
|
|
|
- 32};
|
|
|
|
|
|
|
+ int max_chars = (tb->typing_speed > 0)
|
|
|
|
|
+ ? (int)(tb->timer * tb->typing_speed)
|
|
|
|
|
+ : (int)strlen(tb->wrapped_text);
|
|
|
|
|
+ const char *visible_text = TextSubtext(tb->wrapped_text, 0, max_chars);
|
|
|
|
|
|
|
|
- DrawRectangleRec(nameRect, tb->bg_color);
|
|
|
|
|
- DrawRectangleLinesEx(nameRect, 3, tb->border_color);
|
|
|
|
|
|
|
+ Vector2 text_pos = {
|
|
|
|
|
+ tb->rect.x + tb->padding,
|
|
|
|
|
+ tb->rect.y + tb->padding
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
- DrawTextEx(tb->font, tb->name,
|
|
|
|
|
- (Vector2){nameRect.x + 12, nameRect.y + 3},
|
|
|
|
|
- tb->font_size + 2, 1, tb->name_color);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ DrawTextEx(tb->font, visible_text, text_pos,
|
|
|
|
|
+ tb->font_size, tb->font_spacing, tb->text_color);
|
|
|
|
|
|
|
|
- if (tb->text)
|
|
|
|
|
- {
|
|
|
|
|
- DrawTextEx(tb->font, TextSubtext(tb->text, 0, tb->timer * tb->typing_speed),
|
|
|
|
|
- (Vector2){tb->rect.x + 25, tb->rect.y + 25},
|
|
|
|
|
- tb->font_size, 1.2f, tb->text_color);
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void TextBoxCleanup(TextBox *tb)
|
|
|
|
|
-{
|
|
|
|
|
- UnloadFont(tb->font);
|
|
|
|
|
- if (tb->text)
|
|
|
|
|
- {
|
|
|
|
|
- free(tb->text);
|
|
|
|
|
- tb->text = NULL;
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|