[TextInput] Moved background and padding to Text.

This commit is contained in:
Quentin Bazin 2020-02-21 23:58:15 +09:00
parent e08af87563
commit 087d430acb
5 changed files with 23 additions and 10 deletions

View File

@ -25,6 +25,7 @@
#include <string>
#include <gk/graphics/RectangleShape.hpp>
#include <gk/graphics/Sprite.hpp>
class Text : public gk::Drawable, public gk::Transformable {
@ -39,6 +40,11 @@ class Text : public gk::Drawable, public gk::Transformable {
const gk::Vector2i &getSize() const { return m_size; }
void setBackgroundColor(const gk::Color &color) { m_background.setFillColor(color); }
void setBackgroundSize(unsigned int width, unsigned int height) { m_background.setSize(width, height); }
void setPadding(int x, int y) { m_padding.x = x; m_padding.y = y; }
private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
@ -54,8 +60,11 @@ class Text : public gk::Drawable, public gk::Transformable {
gk::VertexBuffer m_vbo;
gk::Vector2i m_size;
gk::Vector2i m_padding{0, 0};
gk::Color m_color = gk::Color::White;
gk::RectangleShape m_background;
};
#endif // TEXT_HPP_

View File

@ -24,7 +24,6 @@
#define TEXTINPUT_HPP_
#include <gk/core/SDLHeaders.hpp>
#include <gk/graphics/RectangleShape.hpp>
#include "Text.hpp"
@ -36,10 +35,10 @@ class TextInput : public gk::Drawable, public gk::Transformable {
const std::string &text() const { return m_content; }
void setBackgroundColor(const gk::Color &color) { m_background.setFillColor(color); }
void setBackgroundSize(unsigned int width, unsigned int height) { m_background.setSize(width, height); }
void setBackgroundColor(const gk::Color &color) { m_text.setBackgroundColor(color); }
void setBackgroundSize(unsigned int width, unsigned int height) { m_text.setBackgroundSize(width, height); }
void setPadding(int x, int y) { m_text.setPosition(x, y); }
void setPadding(int x, int y) { m_text.setPadding(x, y); }
private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
@ -49,8 +48,6 @@ class TextInput : public gk::Drawable, public gk::Transformable {
char m_cursor = '_';
u16 m_characterLimit = 0;
gk::RectangleShape m_background;
};
#endif // TEXTINPUT_HPP_

View File

@ -27,6 +27,8 @@
#include "Text.hpp"
Text::Text() : m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>("texture-font")) {
m_background.setFillColor(gk::Color::Transparent);
updateCharWidth();
}
@ -47,6 +49,10 @@ void Text::setColor(const gk::Color &color) {
void Text::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform();
target.draw(m_background, states);
states.transform.translate(m_padding.x, m_padding.y);
for(const gk::Sprite &sprite : m_textSprites) {
target.draw(sprite, states);
}

View File

@ -25,8 +25,6 @@
#include "TextInput.hpp"
TextInput::TextInput() {
m_background.setFillColor(gk::Color::Transparent);
m_text.setText(std::string{m_cursor});
}
@ -52,7 +50,6 @@ void TextInput::onEvent(const SDL_Event &event) {
void TextInput::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform();
target.draw(m_background, states);
target.draw(m_text, states);
}

View File

@ -22,6 +22,7 @@
*/
#include "Chat.hpp"
#include "Client.hpp"
#include "Config.hpp"
Chat::Chat(Client &client) {
setPosition(2, 2);
@ -34,8 +35,11 @@ Chat::Chat(Client &client) {
packet >> clientID >> message;
Text &text = m_chatMessages.back();
text.setText("<" + std::to_string(clientID) + "> " + message);
text.setText("<Client " + std::to_string(clientID) + "> " + message);
text.setPosition(0, 10 * (m_chatMessages.size() - 1));
text.setBackgroundColor(gk::Color{0, 0, 0, 127});
text.setBackgroundSize(200, 10);
text.setPadding(1, 1);
});
}