Still working on my scrolling text and bug fixing.
This commit is contained in:
124
src/vn/gui/vntextbox.c
Normal file
124
src/vn/gui/vntextbox.c
Normal file
@ -0,0 +1,124 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
61
src/vn/gui/vntextbox.h
Normal file
61
src/vn/gui/vntextbox.h
Normal file
@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
#include "../../display/primitive.h"
|
||||
#include "../../display/shader.h"
|
||||
#include "../../display/animation/timeline.h"
|
||||
#include "../../display/gui/font.h"
|
||||
#include "../../display/primitives/quad.h"
|
||||
#include "../../input/input.h"
|
||||
|
||||
/**
|
||||
* Initializes the Visual Novel Text Box to its initial state.
|
||||
*
|
||||
* @param box The box to initialize.
|
||||
* @param font Font for the text box to use by default.
|
||||
*/
|
||||
void vnTextBoxInit(vntextbox_t *box, font_t *font);
|
||||
|
||||
/**
|
||||
* Method required to be called when changing anything within the textbox. This
|
||||
* will recalculate the text widths, fonts, etc.
|
||||
* @param box Box to recalculate for.
|
||||
*/
|
||||
void vnTextBoxRebuffer(vntextbox_t *box);
|
||||
|
||||
/**
|
||||
* Updates the Visual Novel Text Box. This includes handling logic for both,
|
||||
* the animation and the input controlling for the textbox.
|
||||
* @param box Box to update.
|
||||
* @param engine Engine that the box is being updated by.
|
||||
*/
|
||||
void vnTextBoxUpdate(vntextbox_t *box, engine_t *engine);
|
||||
|
||||
/**
|
||||
* Renders the Visual Novel Text Box. No logic occurs, just renders. Animations
|
||||
* will not be ticked, use update to do this.
|
||||
* @param box Box to render.
|
||||
* @param shader Shader to render to.
|
||||
*/
|
||||
void vnTextBoxRender(vntextbox_t *box, shader_t *shader);
|
||||
|
||||
/**
|
||||
* Disposes a previously created Visual Novel Text Box.
|
||||
* @param box Text Box to dispose.
|
||||
*/
|
||||
void vnTextBoxDispose(vntextbox_t *box);
|
||||
|
||||
/**
|
||||
* Checks the current buffer position of a vn text box and returns true if it has finished
|
||||
* scrolling.
|
||||
*
|
||||
* @param box Box to check.
|
||||
* @return True if the text box has finished scrolling.
|
||||
*/
|
||||
bool vnTextBoxHasScrolled(vntextbox_t *box);
|
Reference in New Issue
Block a user