GameKit  0.0.1a
C++ gamedev tools
TextInput.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: TextInput.cpp
5  *
6  * Description:
7  *
8  * Created: 22/01/2018 14:52:35
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include "gk/audio/AudioPlayer.hpp"
17 
18 namespace gk {
19 
24 
25  m_text.setFont(ResourceHandler::getInstance().get<Font>("font-default"));
28 }
29 
30 void TextInput::setPosition(float x, float y) {
31  m_rectText.setPosition(x, y);
32  m_text.setPosition(x + 7, y + 7);
33 }
34 
35 void TextInput::setSize(u16 width, u16 height) {
36  m_rectText.setSize(width, height);
37 }
38 
39 void TextInput::onEvent(const SDL_Event &event) {
40  if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_BACKSPACE && !m_content.empty()) {
41  m_content.erase(m_content.begin() + m_content.length() - 1);
42 
43  if (!m_keyboardSound.empty())
45 
47  }
48 
49  if (event.type == SDL_TEXTINPUT) {
50  std::string text = event.text.text;
51  for (char c : text) {
52  if (isprint(c) && (!m_characterLimit || m_content.size() < m_characterLimit)) {
53  m_content += c;
54 
55  if (!m_keyboardSound.empty())
57  }
58 
60  }
61  }
62 }
63 
64 void TextInput::draw(RenderTarget &target, RenderStates states) const {
65  target.draw(m_rectText, states);
66  target.draw(m_text, states);
67 }
68 
69 } // namespace gk
70 
RectangleShape m_rectText
Definition: TextInput.hpp:43
std::string m_content
Definition: TextInput.hpp:46
static ResourceHandler & getInstance()
void setText(const std::string &text)
Definition: Text.hpp:40
void setOutlineThickness(int outlineThickness)
unsigned short u16
Definition: IntTypes.hpp:22
static void playSound(const std::string &resourceName)
Definition: AudioPlayer.cpp:28
void setPosition(float x, float y, float z=0)
void draw(const IDrawable &drawable, const RenderStates &states=RenderStates::Default)
void setColor(const Color &color)
static const Color White
White predefined color.
Definition: Color.hpp:117
void setOutlineColor(const Color &color)
void setPosition(float x, float y)
Definition: TextInput.cpp:30
void draw(RenderTarget &target, RenderStates states) const override
Draw the object to a render target.
Definition: TextInput.cpp:64
std::string m_cursor
Definition: TextInput.hpp:52
void setSize(float width, float height)
std::string m_keyboardSound
Definition: TextInput.hpp:50
void setCharacterSize(int size)
Definition: Text.hpp:52
void setFont(const Font &font)
Definition: Text.hpp:36
static const Color Black
Black predefined color.
Definition: Color.hpp:116
void setSize(u16 width, u16 height)
Definition: TextInput.cpp:35
u16 m_characterLimit
Definition: TextInput.hpp:48
void onEvent(const SDL_Event &event)
Definition: TextInput.cpp:39