102 lines
2.5 KiB
C++
102 lines
2.5 KiB
C++
/*
|
|
* UITextComponent.h
|
|
*
|
|
* Created on: Feb 22, 2020
|
|
* Author: ayoungblood
|
|
*/
|
|
|
|
#ifndef SRC_ECS_UITEXTCOMPONENT_H_
|
|
#define SRC_ECS_UITEXTCOMPONENT_H_
|
|
|
|
#define ASCII_START_IDX 32
|
|
#define ASCII_COUNT 96
|
|
#define ASCII_ROW_COUNT 16
|
|
|
|
#include "Components.h"
|
|
#include <SDL2/SDL.h>
|
|
#include "../assetmgr/TextureManager.h"
|
|
#include "../assetmgr/AssetManager.h"
|
|
#include <stdio.h>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <tuple>
|
|
#include <cmath>
|
|
|
|
class UITextComponent : public Component
|
|
{
|
|
private:
|
|
TransformComponent *transform;
|
|
SDL_Texture *texture;
|
|
SDL_Rect srcRect, destRect;
|
|
std::string text;
|
|
int letterWidth, letterHeight;
|
|
int scale = 1;
|
|
SDL_RendererFlip spriteFlip = SDL_FLIP_NONE;
|
|
std::tuple <SDL_Rect, SDL_Rect> letter;
|
|
|
|
public:
|
|
UITextComponent(std::string id, std::string textToPrint, int letterW, int letterH, int letterScale)
|
|
{
|
|
setTex(id);
|
|
text = textToPrint;
|
|
letterWidth = letterW;
|
|
letterHeight = letterH;
|
|
scale = letterScale;
|
|
}
|
|
|
|
~UITextComponent()
|
|
{
|
|
SDL_DestroyTexture(texture);
|
|
}
|
|
|
|
void setTex(std::string id)
|
|
{
|
|
texture = Game::assets->GetTexture(id);
|
|
}
|
|
|
|
void init() override
|
|
{
|
|
transform = &entity->getComponent<TransformComponent>();
|
|
}
|
|
|
|
void update() override
|
|
{
|
|
destRect.x = static_cast<int>(transform->position.x);
|
|
destRect.y = static_cast<int>(transform->position.y);
|
|
}
|
|
|
|
void draw() override
|
|
{
|
|
for (int l = 0; l < text.length(); l++)
|
|
{
|
|
std::tuple<SDL_Rect, SDL_Rect> lttr = getLetterTexture(text[l],l);
|
|
TextureManager::Draw(texture, std::get<0>(lttr), std::get<1>(lttr), spriteFlip);
|
|
}
|
|
}
|
|
|
|
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*scale));
|
|
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%charsPerLine)*letterWidth*scale;
|
|
destRect.y = static_cast<int>(transform->position.y)*scale+std::floor(i/charsPerLine)*letterHeight*scale*1.1;
|
|
destRect.w = letterWidth*scale;
|
|
destRect.h = letterHeight*scale;
|
|
letterTuple = std::make_tuple(srcRect,destRect);
|
|
return letterTuple;
|
|
}
|
|
|
|
void updateString(std::string newString)
|
|
{
|
|
text = newString;
|
|
}
|
|
|
|
};
|
|
|
|
#endif /* SRC_ECS_UITEXTCOMPONENT_H_ */
|
|
|