From 027907d5a46caee36616ea1808d81cf9a445ad79 Mon Sep 17 00:00:00 2001
From: Dominic Masters <dominic@domsplace.com>
Date: Sun, 18 Jul 2021 14:09:44 -0700
Subject: [PATCH] Working on line clamping in vn text box

---
 include/dawn/vn/gui/vntextbox.h |  3 +-
 src/display/gui/font.c          | 13 +++++++
 src/display/gui/font.h          | 13 ++++++-
 src/vn/gui/vntextbox.c          | 60 +++++++++++++++++----------------
 4 files changed, 58 insertions(+), 31 deletions(-)

diff --git a/include/dawn/vn/gui/vntextbox.h b/include/dawn/vn/gui/vntextbox.h
index a1ad36db..51d1d020 100644
--- a/include/dawn/vn/gui/vntextbox.h
+++ b/include/dawn/vn/gui/vntextbox.h
@@ -28,7 +28,8 @@ typedef struct {
   float x, y;
 
   /** Animation of the textbox */
-  timeline_t animTimeline;
+  float textScroll;
+  // timeline_t animTimeline;
   // timelineaction_t animActions;
 
   /** Information about the current rendered text */
diff --git a/src/display/gui/font.c b/src/display/gui/font.c
index 7ab785ad..8146c5de 100644
--- a/src/display/gui/font.c
+++ b/src/display/gui/font.c
@@ -163,4 +163,17 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
     info->width = mathMax(info->width, quad->x1);
     info->height = mathMax(info->height, quad->y1);
   }
+}
+
+int32_t fontGetLineCharCount(fonttextinfo_t *info,int32_t start,int32_t count) {
+  int32_t charCount, i, m;
+  
+  m = mathMin(start+count, info->lineCount);
+
+  charCount = 0;
+  for(i = start; i < m; i++) {
+    charCount += info->lines[i].length;
+  }
+
+  return charCount;
 }
\ No newline at end of file
diff --git a/src/display/gui/font.h b/src/display/gui/font.h
index db8574a0..65c8d950 100644
--- a/src/display/gui/font.h
+++ b/src/display/gui/font.h
@@ -56,4 +56,15 @@ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info);
  */
 void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
   float maxWidth
-);
\ No newline at end of file
+);
+
+/**
+ * Returns the number of real characters on the lines specified. Useful when
+ * attempting to clamp to a set of lines.
+ * 
+ * @param info Info to get the character counts from.
+ * @param start Starting line index.
+ * @param count Count of lines, this method will clamp to info's line length.
+ * @returns The count of characters in those lines summed.
+ */
+int32_t fontGetLineCharCount(fonttextinfo_t *info, int32_t start,int32_t count);
\ No newline at end of file
diff --git a/src/vn/gui/vntextbox.c b/src/vn/gui/vntextbox.c
index df2b4629..a9ba6791 100644
--- a/src/vn/gui/vntextbox.c
+++ b/src/vn/gui/vntextbox.c
@@ -13,7 +13,7 @@ void vnTextBoxInit(vntextbox_t *box, font_t *font) {
   box->text = NULL;
   box->x = 0, box->y = 0;
 
-  box->linesMax = 1;
+  box->linesMax = 3;
   box->lineCurrent = 0;
 }
 
@@ -42,49 +42,43 @@ void vnTextBoxRebuffer(vntextbox_t *box) {
 }
 
 void vnTextBoxUpdate(vntextbox_t *box, engine_t *engine) {
+  int32_t i;
+
   if(vnTextBoxHasScrolled(box)) {
+    printf(".");
     if(!inputIsPressed(&engine->input, INPUT_ACCEPT)) return;
+
     // "Next text box"
     printf("Next");
-    box->lineCurrent++;
+    i = 1;
+    box->lineCurrent += i;
     return;
   }
 
-  bool speedup = inputIsDown(&engine->input, INPUT_ACCEPT);
-  timelineUpdate(&box->animTimeline, engine->time.delta * (speedup ? 1 : 2));
+  i = inputIsDown(&engine->input, INPUT_ACCEPT) ? 2 : 1;
+  box->textScroll += engine->time.delta  * VN_TEXTBOX_SCROLL_SPEED * i;
+  // timelineUpdate(&box->animTimeline, engine->time.delta);
 }
 
 void vnTextBoxRender(vntextbox_t *box, shader_t *shader) {
-  int32_t charStart, charCount, i;
+  int32_t charStart, charCount;
+  float yOffset;
   if(box->text == NULL) return;
 
   // Determine where we're rendering the indices up to.
-  charCount = box->animTimeline.current * VN_TEXTBOX_SCROLL_SPEED;
+  // charCount = box->animTimeline.current * VN_TEXTBOX_SCROLL_SPEED;
+  charCount = box->textScroll;
   if(charCount == 0) return;
 
   // Clamp to lines if necessary.
   if(box->linesMax > 0) {
     if(box->lineCurrent >= box->textInfo.lineCount) return;
-  
-    // From lineCurrent to lineCurrent+lineMax (or max lines in text).
-    i = box->lineCurrent;
-    charStart = 0;// Misusing variable for now.
-    while(i < mathMin(box->lineCurrent+box->linesMax, box->textInfo.lineCount)){
-      charStart += box->textInfo.lines[i].length;// Sum char counts for lines.
-      i++;
-    }
-    i = box->textInfo.lines[box->lineCurrent].start;//i=the starting indice now
-
-    if(charCount >= charStart) {
-      int32_t brhva;
-      brhva = 32;
-    }
-
-    // Select either the scroll position or the total chars for the lowest.
-    charCount = mathMin(charCount - i, charStart);
-    charStart = box->lineCurrent;
+    charStart = box->textInfo.lines[box->lineCurrent].start;
+    charCount -= charStart;
+    yOffset = FONT_LINE_HEIGHT * box->lineCurrent;
   } else {
     charStart = 0;
+    yOffset = 0;
   }
 
   // Convert characters into indices, don't render if we can't
@@ -99,7 +93,7 @@ void vnTextBoxRender(vntextbox_t *box, shader_t *shader) {
   if(charCount < 1) return;
 
   // Render the debug box.
-  shaderUsePosition(shader, box->x,box->y,0, 0,0,0);
+  shaderUsePosition(shader, box->x,box->y - yOffset,0, 0,0,0);
   shaderUseTexture(shader, &box->testTexture);
   primitiveDraw(&box->testPrimitive, 0, -1);
   
@@ -116,9 +110,17 @@ void vnTextBoxDispose(vntextbox_t *box) {
 }
 
 bool vnTextBoxHasScrolled(vntextbox_t *box) {
-  if(box->text == NULL) return true;
+  int32_t currentChar;
+  currentChar = box->textScroll;
 
-  return (box->animTimeline.current * (
-    VN_TEXTBOX_SCROLL_SPEED * QUAD_INDICE_COUNT
-  )) >= box->primitive.indiceCount;
+  // Are we clamping lines?
+  if(box->linesMax > 0) {
+    return (
+      currentChar - box->textInfo.lines[box->lineCurrent].start// "Line" space
+    ) >= fontGetLineCharCount(
+      &box->textInfo, box->lineCurrent, box->linesMax
+    );
+  }
+
+  return currentChar > box->textInfo.realLength;
 }
\ No newline at end of file