/** * 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 = 3; 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) { int32_t i; // "Next text box" if(vnTextBoxHasScrolled(box) && box->linesMax > 0) { if(!inputIsPressed(&engine->input, INPUT_ACCEPT)) return; box->lineCurrent += box->linesMax; return; } 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; float yOffset; if(box->text == NULL) return; // Determine where we're rendering the indices up to. charCount = box->textScroll; if(charCount == 0) return; // Clamp to lines if necessary. if(box->linesMax > 0) { if(box->lineCurrent >= box->textInfo.lineCount) return; 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 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 - yOffset,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) { int32_t currentChar; currentChar = box->textScroll; // 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; }