Basic WordWrap for UIText feature

This commit is contained in:
2022-02-04 17:08:01 -05:00
parent 72a941810d
commit b5639e4f41
3 changed files with 6 additions and 12 deletions

View File

@ -20,8 +20,7 @@
#include <string>
#include <iostream>
#include <tuple>
// Should include <iostream> and <tuple> to make an array of tuples with two SDL_Rect type vars to feed to the draw function.
#include <cmath>
class UITextComponent : public Component
{
@ -29,7 +28,6 @@ private:
TransformComponent *transform;
SDL_Texture *texture;
SDL_Rect srcRect, destRect;
// char letter;
std::string text;
int letterWidth, letterHeight;
int scale = 1;
@ -46,8 +44,6 @@ public:
letterWidth = letterW;
letterHeight = letterH;
scale = letterScale;
// destRect.w = letterW*letterScale;
// destRect.h = letterH*letterScale;
}
~UITextComponent()
@ -63,7 +59,6 @@ public:
void init() override
{
transform = &entity->getComponent<TransformComponent>();
}
void update() override
@ -74,7 +69,6 @@ public:
void draw() override
{
// This should be updated to iterate through an array of each letter's srcRect and destRect
for (int l = 0; l < text.length(); l++)
{
std::tuple<SDL_Rect, SDL_Rect> lttr = getLetterTexture(text[l],l);
@ -85,13 +79,13 @@ public:
std::tuple<SDL_Rect, SDL_Rect> getLetterTexture(char currentLetter, int i)
{
std::tuple<SDL_Rect, SDL_Rect> letterTuple;
int charsPerLine = std::floor(transform->width/letterWidth);
srcRect.x = ((currentLetter-ASCII_START_IDX) % ASCII_ROW_COUNT)*letterWidth;
srcRect.y = ((currentLetter-ASCII_START_IDX)/ASCII_ROW_COUNT)*letterHeight;
srcRect.w = letterWidth;
srcRect.h = letterHeight;
destRect.x = static_cast<int>(transform->position.x)*scale+i*letterWidth*scale;
destRect.y = static_cast<int>(transform->position.y)*scale;
destRect.x = static_cast<int>(transform->position.x)*scale+(i%charsPerLine)*letterWidth*scale;
destRect.y = static_cast<int>(transform->position.y)*scale+std::floor(i/charsPerLine)*letterHeight*scale*1.5;
destRect.w = letterWidth*scale;
destRect.h = letterHeight*scale;
letterTuple = std::make_tuple(srcRect,destRect);