Made a new TextBox class
This commit is contained in:
@@ -45,6 +45,10 @@ SDL_Texture* renderTextTexture(SDL_Renderer* renderer, TTF_Font* font, std::stri
|
|||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//TextLine
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
TextLine::TextLine() {
|
TextLine::TextLine() {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
@@ -68,3 +72,39 @@ void TextLine::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string str,
|
|||||||
void TextLine::ClearText() {
|
void TextLine::ClearText() {
|
||||||
SDL_DestroyTexture(texture);
|
SDL_DestroyTexture(texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//TextBox
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
TextBox::TextBox() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
TextBox::~TextBox() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::DrawTo(SDL_Renderer* renderer, int posX, int posY, int pointSize) {
|
||||||
|
for (std::list<TextLine>::iterator it = lineList.begin(); it != lineList.end(); it++) {
|
||||||
|
it->DrawTo(renderer, posX, posY);
|
||||||
|
posY -= pointSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::PushLine(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) {
|
||||||
|
lineList.emplace_front(renderer, font, str, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::PopLine(int num) {
|
||||||
|
//prevent underflow
|
||||||
|
num < lineList.size() ? num : lineList.size();
|
||||||
|
|
||||||
|
for (int i = 0; i < num; ++i) {
|
||||||
|
lineList.pop_back();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::ClearLines() {
|
||||||
|
lineList.clear();
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,14 +25,19 @@
|
|||||||
#include "SDL2/SDL_ttf.h"
|
#include "SDL2/SDL_ttf.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
constexpr SDL_Color COLOR_WHITE = {255, 255, 255, 255};
|
constexpr SDL_Color COLOR_WHITE = {255, 255, 255, 255};
|
||||||
|
|
||||||
SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
|
SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
class TextLine {
|
class TextLine {
|
||||||
public:
|
public:
|
||||||
TextLine();
|
TextLine();
|
||||||
|
TextLine(SDL_Renderer* r, TTF_Font* f, std::string s, SDL_Color c)
|
||||||
|
{ SetText(r, f, s, c); }
|
||||||
~TextLine();
|
~TextLine();
|
||||||
|
|
||||||
void DrawTo(SDL_Renderer*, int posX, int posY);
|
void DrawTo(SDL_Renderer*, int posX, int posY);
|
||||||
@@ -43,3 +48,20 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
SDL_Texture* texture = nullptr;
|
SDL_Texture* texture = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
class TextBox {
|
||||||
|
public:
|
||||||
|
TextBox();
|
||||||
|
~TextBox();
|
||||||
|
|
||||||
|
void DrawTo(SDL_Renderer*, int posX, int posY, int pointSize);
|
||||||
|
|
||||||
|
void PushLine(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
|
||||||
|
void PopLine(int num = 1);
|
||||||
|
void ClearLines();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::list<TextLine> lineList;
|
||||||
|
};
|
||||||
@@ -63,7 +63,10 @@ ExampleScene::ExampleScene(lua_State* L) {
|
|||||||
// {140, 62, 54, 255}
|
// {140, 62, 54, 255}
|
||||||
|
|
||||||
//DEBUG: testing textLine
|
//DEBUG: testing textLine
|
||||||
textLine.SetText(GetRenderer(), font, "Button Text", {255, 255, 255, 255});
|
textBox.PushLine(GetRenderer(), font, "first line", {255, 255, 255, 255});
|
||||||
|
textBox.PushLine(GetRenderer(), font, "second line", {255, 255, 255, 255});
|
||||||
|
textBox.PushLine(GetRenderer(), font, "third line", {255, 255, 255, 255});
|
||||||
|
textBox.PushLine(GetRenderer(), font, "FOURTH LINE!!", {255, 0, 0, 0});
|
||||||
}
|
}
|
||||||
|
|
||||||
ExampleScene::~ExampleScene() {
|
ExampleScene::~ExampleScene() {
|
||||||
@@ -94,7 +97,7 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) {
|
|||||||
|
|
||||||
//DEBUG: testing UI
|
//DEBUG: testing UI
|
||||||
button.DrawTo(renderer);
|
button.DrawTo(renderer);
|
||||||
textLine.DrawTo(renderer, 0, 550);
|
textBox.DrawTo(renderer, 0, 550, 12);
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -196,6 +199,10 @@ void ExampleScene::KeyDown(SDL_KeyboardEvent const& event) {
|
|||||||
case SDLK_SPACE:
|
case SDLK_SPACE:
|
||||||
camera.scale = 1.0;
|
camera.scale = 1.0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case SDLK_RETURN:
|
||||||
|
textBox.PushLine(GetRenderer(), font, "You pressed enter.", {128, 128, 128, 255});
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,5 +67,5 @@ private:
|
|||||||
TTF_Font* font = nullptr;
|
TTF_Font* font = nullptr;
|
||||||
Image buttonBG;
|
Image buttonBG;
|
||||||
Button button;
|
Button button;
|
||||||
TextLine textLine;
|
TextBox textBox;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user