From 9e5a39f251b7aa3dcaefc287aa5db08d43bd4e68 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 13 Aug 2015 23:14:10 +1000 Subject: [PATCH] Made a new TextBox class --- src/common/text_box.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/common/text_box.hpp | 22 ++++++++++++++++++++++ src/example_scene.cpp | 11 +++++++++-- src/example_scene.hpp | 2 +- 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/common/text_box.cpp b/src/common/text_box.cpp index be2c111..91ecc3d 100644 --- a/src/common/text_box.cpp +++ b/src/common/text_box.cpp @@ -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::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(); +} diff --git a/src/common/text_box.hpp b/src/common/text_box.hpp index 56561ff..f49c566 100644 --- a/src/common/text_box.hpp +++ b/src/common/text_box.hpp @@ -25,14 +25,19 @@ #include "SDL2/SDL_ttf.h" #include +#include 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 lineList; }; \ No newline at end of file diff --git a/src/example_scene.cpp b/src/example_scene.cpp index 46b81ba..b928e46 100644 --- a/src/example_scene.cpp +++ b/src/example_scene.cpp @@ -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; } } diff --git a/src/example_scene.hpp b/src/example_scene.hpp index e5c7da6..1feea6f 100644 --- a/src/example_scene.hpp +++ b/src/example_scene.hpp @@ -67,5 +67,5 @@ private: TTF_Font* font = nullptr; Image buttonBG; Button button; - TextLine textLine; + TextBox textBox; };