aboutsummaryrefslogtreecommitdiff
path: root/src/Chatbar.cpp
blob: d775db42d8b81c19190d1c85e6e944a31dd3a24b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "../include/Chatbar.hpp"
#include "../include/ResourceCache.hpp"
#include "../include/Settings.hpp"
#include "../include/Channel.hpp"
#include <cmath>
#include <cstring>

using namespace std;

namespace dchat
{
    const float FONT_SIZE = 24;
    const float BOX_PADDING_X = 15.0f;
    const float BOX_PADDING_Y = 5.0f;
    const int BLINK_TIME_VISIBLE_MS = 500;
    const int BLINK_TIME_INVISIBLE_MS = 500;
    
    Chatbar::Chatbar() : 
        text("", ResourceCache::getFont("fonts/Roboto-Regular.ttf"), FONT_SIZE * Settings::getScaling()),
        caretIndex(0),
        focused(true)
    {
        text.setFillColor(sf::Color(240, 240, 240));
        background.setFillColor(sf::Color(60, 60, 60));
    }
    
    void Chatbar::addChar(sf::Uint32 codePoint)
    {
        auto str = text.getString();
        str.insert(caretIndex, codePoint);
        text.setString(str);
        ++caretIndex;
        caretOffset = text.findCharacterPos(caretIndex) - text.getPosition();
        blinkTimer.restart();
    }
    
    const sf::String& Chatbar::getString() const
    {
        return text.getString();
    }
    
    void Chatbar::removePreviousChar()
    {
        if(caretIndex > 0)
        {
            auto str = text.getString();
            str.erase(caretIndex - 1);
            text.setString(str);
            --caretIndex;
            caretOffset = text.findCharacterPos(caretIndex) - text.getPosition();
        }
        blinkTimer.restart();
    }
    
    void Chatbar::removeNextChar()
    {
        if(caretIndex < text.getString().getSize())
        {
            auto str = text.getString();
            str.erase(caretIndex);
            text.setString(str);
        }
        blinkTimer.restart();
    }
    
    void Chatbar::clear()
    {
        text.setString("");
        caretIndex = 0;
        caretOffset.x = 0.0f;
        caretOffset.y = 0.0f;
        blinkTimer.restart();
    }
    
    void Chatbar::moveCaretLeft()
    {
        caretIndex = max(0, caretIndex - 1);
        // TODO: Use glyph size to optimize this, no need to iterate all glyphs
        caretOffset = text.findCharacterPos(caretIndex) - text.getPosition();
        blinkTimer.restart();
    }
    
    void Chatbar::moveCaretRight()
    {
        caretIndex = min((int)text.getString().getSize(), caretIndex + 1);
        caretOffset = text.findCharacterPos(caretIndex) - text.getPosition();
        blinkTimer.restart();
    }
    
    bool Chatbar::isFocused() const
    {
        return focused;
    }
    
    void Chatbar::processEvent(const sf::Event &event, Channel *channel)
    {
        if(!focused) return;
        
        if(event.type == sf::Event::TextEntered)
        {
            if(event.text.unicode == 8) // backspace
                removePreviousChar();
            else if(event.text.unicode == 13) // enter
            {
                if(!getString().isEmpty())
                {
                    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::RShift))
                    {
                        addChar('\n');
                    }
                    else
                    {
                        auto chatbarMsgUtf8 = getString().toUtf8();
                        string msg;
                        msg.resize(chatbarMsgUtf8.size());
                        memcpy(&msg[0], chatbarMsgUtf8.data(), chatbarMsgUtf8.size());
                        channel->getMessageBoard().addMessage(new Message(channel->getLocalUser(), msg));
                        clear();
                    }
                }
            }
            else if(event.text.unicode == 127) // delete
            {
                removeNextChar();
            }
            else
            {
                addChar(event.text.unicode);
            }
        }
        else if(event.type == sf::Event::KeyPressed)
        {
            if(event.key.code == sf::Keyboard::Left)
                moveCaretLeft();
            else if(event.key.code == sf::Keyboard::Right)
                moveCaretRight();
        }
    }
    
    void Chatbar::draw(sf::RenderWindow &window)
    {
        auto windowSize = window.getSize();
        
        sf::Vector2f backgroundSize(floor(windowSize.x * 0.7f), floor(text.getCharacterSize() * 1.7f + BOX_PADDING_Y * 2.0f));
        sf::Vector2f backgroundPos(floor(windowSize.x * 0.5f - backgroundSize.x * 0.5f), floor(windowSize.y - backgroundSize.y - 20.0f));
        background.setSize(backgroundSize);
        background.setPosition(backgroundPos);
        text.setPosition(floor(backgroundPos.x + BOX_PADDING_X), floor(backgroundPos.y + backgroundSize.y * 0.5f - text.getCharacterSize() * 0.5f));
        
        window.draw(background);
        window.draw(text);
        
        int blinkElapsedTime = blinkTimer.getElapsedTime().asMilliseconds();
        if(focused && blinkElapsedTime <= BLINK_TIME_VISIBLE_MS)
        {
            sf::RectangleShape caretShape(sf::Vector2f(2.0f, backgroundSize.y - BOX_PADDING_Y * 2.0f));
            caretShape.setPosition(floor(text.getPosition().x + caretOffset.x), (caretOffset.y + backgroundPos.y + BOX_PADDING_Y));
            window.draw(caretShape);
        }
        
        if(blinkElapsedTime > BLINK_TIME_VISIBLE_MS + BLINK_TIME_INVISIBLE_MS)
            blinkTimer.restart();
    }
}