Made a new TextBox class

This commit is contained in:
2015-08-13 23:14:10 +10:00
parent d618023a91
commit 9e5a39f251
4 changed files with 72 additions and 3 deletions
+40
View File
@@ -45,6 +45,10 @@ SDL_Texture* renderTextTexture(SDL_Renderer* renderer, TTF_Font* font, std::stri
return texture;
}
//-------------------------
//TextLine
//-------------------------
TextLine::TextLine() {
//
}
@@ -68,3 +72,39 @@ void TextLine::SetText(SDL_Renderer* renderer, TTF_Font* font, std::string str,
void TextLine::ClearText() {
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();
}
+22
View File
@@ -25,14 +25,19 @@
#include "SDL2/SDL_ttf.h"
#include <string>
#include <list>
constexpr SDL_Color COLOR_WHITE = {255, 255, 255, 255};
SDL_Texture* renderTextTexture(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color);
//-------------------------
class TextLine {
public:
TextLine();
TextLine(SDL_Renderer* r, TTF_Font* f, std::string s, SDL_Color c)
{ SetText(r, f, s, c); }
~TextLine();
void DrawTo(SDL_Renderer*, int posX, int posY);
@@ -42,4 +47,21 @@ public:
protected:
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;
};
+9 -2
View File
@@ -63,7 +63,10 @@ ExampleScene::ExampleScene(lua_State* L) {
// {140, 62, 54, 255}
//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() {
@@ -94,7 +97,7 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) {
//DEBUG: testing UI
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:
camera.scale = 1.0;
break;
case SDLK_RETURN:
textBox.PushLine(GetRenderer(), font, "You pressed enter.", {128, 128, 128, 255});
break;
}
}
+1 -1
View File
@@ -67,5 +67,5 @@ private:
TTF_Font* font = nullptr;
Image buttonBG;
Button button;
TextLine textLine;
TextBox textBox;
};