GameKit
0.0.1a
C++ gamedev tools
source
graphics
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
"
15
#include "
gk/graphics/TextInput.hpp
"
16
#include "
gk/resource/ResourceHandler.hpp
"
17
18
namespace
gk
{
19
20
TextInput::TextInput
() {
21
m_rectText
.
setColor
(
Color::Black
);
22
m_rectText
.
setOutlineThickness
(2);
23
m_rectText
.
setOutlineColor
(
Color::White
);
24
25
m_text
.
setFont
(
ResourceHandler::getInstance
().get<Font>(
"font-default"
));
26
m_text
.
setCharacterSize
(25);
27
m_text
.
setText
(
m_cursor
);
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())
44
AudioPlayer::playSound
(
m_keyboardSound
);
45
46
m_text
.
setText
(
m_content
+
m_cursor
);
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())
56
AudioPlayer::playSound
(
m_keyboardSound
);
57
}
58
59
m_text
.
setText
(
m_content
+
m_cursor
);
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
gk::TextInput::TextInput
TextInput()
Definition:
TextInput.cpp:20
gk::TextInput::m_text
Text m_text
Definition:
TextInput.hpp:45
gk::TextInput::m_rectText
RectangleShape m_rectText
Definition:
TextInput.hpp:43
gk::TextInput::m_content
std::string m_content
Definition:
TextInput.hpp:46
gk::ResourceHandler::getInstance
static ResourceHandler & getInstance()
Definition:
ResourceHandler.cpp:20
TextInput.hpp
gk::Text::setText
void setText(const std::string &text)
Definition:
Text.hpp:40
gk::RectangleShape::setOutlineThickness
void setOutlineThickness(int outlineThickness)
Definition:
RectangleShape.hpp:44
gk
Definition:
AudioPlayer.hpp:21
AudioPlayer.hpp
gk::RenderStates
Definition:
RenderStates.hpp:42
u16
unsigned short u16
Definition:
IntTypes.hpp:22
gk::AudioPlayer::playSound
static void playSound(const std::string &resourceName)
Definition:
AudioPlayer.cpp:28
gk::Transformable::setPosition
void setPosition(float x, float y, float z=0)
Definition:
Transformable.cpp:20
gk::RenderTarget::draw
void draw(const IDrawable &drawable, const RenderStates &states=RenderStates::Default)
Definition:
RenderTarget.cpp:26
ResourceHandler.hpp
gk::RectangleShape::setColor
void setColor(const Color &color)
Definition:
RectangleShape.hpp:32
gk::Color::White
static const Color White
White predefined color.
Definition:
Color.hpp:117
gk::RectangleShape::setOutlineColor
void setOutlineColor(const Color &color)
Definition:
RectangleShape.hpp:43
gk::TextInput::setPosition
void setPosition(float x, float y)
Definition:
TextInput.cpp:30
gk::TextInput::draw
void draw(RenderTarget &target, RenderStates states) const override
Draw the object to a render target.
Definition:
TextInput.cpp:64
gk::TextInput::m_cursor
std::string m_cursor
Definition:
TextInput.hpp:52
gk::RectangleShape::setSize
void setSize(float width, float height)
Definition:
RectangleShape.hpp:41
gk::RenderTarget
Definition:
RenderTarget.hpp:27
gk::TextInput::m_keyboardSound
std::string m_keyboardSound
Definition:
TextInput.hpp:50
gk::Text::setCharacterSize
void setCharacterSize(int size)
Definition:
Text.hpp:52
gk::Text::setFont
void setFont(const Font &font)
Definition:
Text.hpp:36
gk::Color::Black
static const Color Black
Black predefined color.
Definition:
Color.hpp:116
gk::TextInput::setSize
void setSize(u16 width, u16 height)
Definition:
TextInput.cpp:35
gk::TextInput::m_characterLimit
u16 m_characterLimit
Definition:
TextInput.hpp:48
gk::TextInput::onEvent
void onEvent(const SDL_Event &event)
Definition:
TextInput.cpp:39
Generated by
1.8.12