Basic WordWrap for UIText feature

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

Binary file not shown.

View File

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

View File

@ -151,7 +151,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
my9Slice = new UINineSlice("textBox"); my9Slice = new UINineSlice("textBox");
my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0); my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0);
uiInfo.addComponent<TransformComponent>(160,120,64,32,1); uiInfo.addComponent<TransformComponent>(10,10,180,56,1);
uiInfo.addComponent<UITextComponent>("font", "UI Text works again!", 8, 12, 1); uiInfo.addComponent<UITextComponent>("font", "UI Text works again!", 8, 12, 1);
uiInfo.addGroup(groupUI_Layer3); uiInfo.addGroup(groupUI_Layer3);
// debug UI text // debug UI text
@ -258,7 +258,7 @@ void Game::update()
SDL_Rect playerCol = player.getComponent<ColliderComponent>().collider; SDL_Rect playerCol = player.getComponent<ColliderComponent>().collider;
Vector2D playerPos = player.getComponent<TransformComponent>().position; Vector2D playerPos = player.getComponent<TransformComponent>().position;
uiInfo.getComponent<UITextComponent>().updateString("New Text Works!"); uiInfo.getComponent<UITextComponent>().updateString("Updating UIText works well, even with basic word wrap for new lines.");
// if (gsm->currentState == GameStateManager::ST_INIT) // if (gsm->currentState == GameStateManager::ST_INIT)
// { // {