/** * Copyright (c) 2021 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #include "vntextbox.h" void vnTextBoxInit(vntextbox_t *box, font_t *font) { box->font = font; box->widthMax = 400; box->text = NULL; box->x = 0, box->y = 0; box->linesMax = 1; box->lineCurrent = 0; } void vnTextBoxRebuffer(vntextbox_t *box) { if(box->primitive.indiceCount > 0) { vnTextBoxDispose(box); } if(box->text == NULL) return; // Rebuffer the text. fontTextClamp(box->font, &box->textInfo, box->text, box->widthMax); fontTextInit(box->font, &box->primitive, &box->textInfo); quadInit(&box->testPrimitive, 0, 0, 0, 0.3, 0.3, box->textInfo.width, box->textInfo.height, 0.6, 0.6 ); pixel_t pixels[25]; for(uint8_t i = 0; i < 25; i++) { pixels[i].r = pixels[i].a = 0xFF; pixels[i].g = pixels[i].b = 0x00; } textureInit(&box->testTexture, 5, 5, pixels); } void vnTextBoxUpdate(vntextbox_t *box, engine_t *engine) { if(vnTextBoxHasScrolled(box)) { if(!inputIsPressed(&engine->input, INPUT_ACCEPT)) return; // "Next text box" printf("Next"); box->lineCurrent++; return; } bool speedup = inputIsDown(&engine->input, INPUT_ACCEPT); timelineUpdate(&box->animTimeline, engine->time.delta * (speedup ? 1 : 2)); } void vnTextBoxRender(vntextbox_t *box, shader_t *shader) { int32_t charStart, charCount, i; if(box->text == NULL) return; // Determine where we're rendering the indices up to. charCount = box->animTimeline.current * VN_TEXTBOX_SCROLL_SPEED; 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; } else { charStart = 0; } // Convert characters into indices, don't render if we can't charStart *= QUAD_INDICE_COUNT; if(charStart >= box->primitive.indiceCount) return; // Clamp charCount = mathMin( charCount * QUAD_INDICE_COUNT, box->primitive.indiceCount - charStart ); if(charCount < 1) return; // Render the debug box. shaderUsePosition(shader, box->x,box->y,0, 0,0,0); shaderUseTexture(shader, &box->testTexture); primitiveDraw(&box->testPrimitive, 0, -1); // Render the Text Box shaderUseTexture(shader, &box->font->texture); primitiveDraw(&box->primitive, charStart, charCount); } void vnTextBoxDispose(vntextbox_t *box) { primitiveDispose(&box->primitive); primitiveDispose(&box->testPrimitive); textureDispose(&box->testTexture); box->primitive.indiceCount = 0; } bool vnTextBoxHasScrolled(vntextbox_t *box) { if(box->text == NULL) return true; return (box->animTimeline.current * ( VN_TEXTBOX_SCROLL_SPEED * QUAD_INDICE_COUNT )) >= box->primitive.indiceCount; }