aboutsummaryrefslogtreecommitdiff
path: root/src/MessageBoard.cpp
blob: 031bbdff23172c061f22d3ad784b84f320e9ebce (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "../include/MessageBoard.hpp"
#include "../include/Settings.hpp"
#include "../include/ResourceCache.hpp"
#include "../include/Gif.hpp"
#include "../include/ChannelSidePanel.hpp"
#include "../include/UsersSidePanel.hpp"
#include "../include/ChannelTopPanel.hpp"
#include "../include/Chatbar.hpp"
#include "../include/ColorScheme.hpp"
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Window/Mouse.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Text.hpp>
#include <cmath>

using namespace std;

namespace dchat
{
    struct LineColor
    {
        sf::Color sideColor, centerColor;
    };
    
    const float USERNAME_PADDING_BOTTOM = 5.0f;
    const float MESSAGE_PADDING_TOP = 25.0f;
    const float MESSAGE_PADDING_BOTTOM = 30.0f;
    
    const float PADDING_SIDE = 40.0f;
    const float PADDING_TOP = 0.0f;
    const float LINE_SIDE_PADDING = 20.0f;
    const float LINE_HEIGHT = 1.0f;
    const float USERNAME_TIMESTAMP_SIDE_PADDING = 10.0f;
    
    const LineColor LINE_COLOR
    {
        .sideColor = ColorScheme::getBackgroundColor() + sf::Color(10, 10, 10),
        .centerColor = ColorScheme::getBackgroundColor() + sf::Color(10, 10, 10)
    };
    
    static void drawGradientLine(const sf::Vector2f &position, const sf::Vector2f &size, const LineColor &color, sf::RenderWindow &window)
    {
        sf::Vertex rectangle[] = 
        {
            sf::Vertex(position, color.sideColor),
            sf::Vertex(sf::Vector2f(position.x + size.x * 0.5f, position.y), color.centerColor),
            sf::Vertex(sf::Vector2f(position.x + size.x * 0.5f, position.y + size.y), color.centerColor),
            sf::Vertex(sf::Vector2f(position.x, position.y + size.y), color.sideColor),
            
            sf::Vertex(sf::Vector2f(position.x + size.x * 0.5f, position.y), color.centerColor),
            sf::Vertex(sf::Vector2f(position.x + size.x, position.y), color.sideColor),
            sf::Vertex(sf::Vector2f(position.x + size.x, position.y + size.y), color.sideColor),
            sf::Vertex(sf::Vector2f(position.x + size.x * 0.5f, position.y + size.y), color.centerColor)
        };
        window.draw(rectangle, 8, sf::Quads);
    }
    
    MessageBoard::MessageBoard(const sf::Vector2u &size) : 
        selectingText(false),
        leftMouseButtonPressed(false),
        scroll(0.0),
        scrollSpeed(0.0),
        totalHeight(0.0),
        scrollToBottom(false)
    {
        updateStaticContentTexture(size);
    }
    
    MessageBoard::~MessageBoard()
    {
        for(Message *message : messages)
        {
            delete message;
        }
    }
    
    void MessageBoard::updateStaticContentTexture(const sf::Vector2u &newSize)
    {
        //if(!staticContentTexture.create(newSize.x, newSize.y))
        //    throw std::runtime_error("Failed to create render target for message board!");
        dirty = true;
    }
    
    void MessageBoard::addMessage(Message *message)
    {
        messages.push_back(message);
        dirty = true;
        scrollToBottom = true;
    }
    
    void MessageBoard::processEvent(const sf::Event &event)
    {
        if(event.type == sf::Event::MouseButtonPressed)
        {
            if(event.mouseButton.button == sf::Mouse::Button::Left)
            {
                leftMouseButtonPressed = true;
                mousePos.x = event.mouseButton.x;
                mousePos.y = event.mouseButton.y;
            }
        }
        else if(event.type == sf::Event::MouseButtonReleased)
        {
            if(event.mouseButton.button == sf::Mouse::Button::Left)
            {
                leftMouseButtonPressed = false;
                mousePos.x = event.mouseButton.x;
                mousePos.y = event.mouseButton.y;
            }
        }
        else if(event.type == sf::Event::LostFocus)
        {
            leftMouseButtonPressed = false;
        }
        else if(event.type == sf::Event::MouseMoved)
        {
            mousePos.x = event.mouseMove.x;
            mousePos.y = event.mouseMove.y;
        }
        else if(event.type == sf::Event::MouseWheelScrolled && event.mouseWheelScroll.wheel == sf::Mouse::Wheel::VerticalWheel)
        {
            scrollSpeed += (event.mouseWheelScroll.delta * 5.0);
        }
        
        if(selectingText && !leftMouseButtonPressed)
        {
            selectingText = false;
        }
        else if(!selectingText && leftMouseButtonPressed)
        {
            selectingText = true;
            selectingTextStart.x = mousePos.x;
            selectingTextStart.y = mousePos.y;
        }
    }
    
    void MessageBoard::draw(sf::RenderWindow &window, Cache &cache)
    {
        auto windowSize = window.getSize();
        sf::Vector2f backgroundSizeWithoutPadding(floor(windowSize.x - ChannelSidePanel::getWidth() - UsersSidePanel::getWidth()), floor(windowSize.y - ChannelTopPanel::getHeight() - Chatbar::getHeight()));
        sf::Vector2u backgroundSize(floor(windowSize.x - ChannelSidePanel::getWidth() - UsersSidePanel::getWidth() - PADDING_SIDE * 2.0f), floor(windowSize.y - ChannelTopPanel::getHeight() - Chatbar::getHeight() - PADDING_TOP));
        sf::Vector2f backgroundPos(ChannelSidePanel::getWidth(), ChannelTopPanel::getHeight());
        
        //if(backgroundSize != staticContentTexture.getSize())
        //    updateStaticContentTexture(backgroundSize);
        
        // TODO: Remove this when dchat::Text can render to static and dynamic render target
        dirty = true;
        //if(dirty)
        //    staticContentTexture.clear(BACKGROUND_COLOR);
        
        sf::RectangleShape backgroundRect(backgroundSizeWithoutPadding);
        backgroundRect.setFillColor(ColorScheme::getBackgroundColor());
        backgroundRect.setPosition(ChannelSidePanel::getWidth(), ChannelTopPanel::getHeight());
        window.draw(backgroundRect);
        
        const sf::Font *usernameFont = ResourceCache::getFont("fonts/Roboto-Regular.ttf");
        const int usernameTextCharacterSize = 20 * Settings::getScaling();
        const float usernameTextHeight = usernameFont->getLineSpacing(usernameTextCharacterSize);
        
        const sf::Font *timestampFont = ResourceCache::getFont("fonts/Roboto-Regular.ttf");
        const int timestampTextCharacterSize = 15 * Settings::getScaling();
        const float timestampTextHeight = timestampFont->getLineSpacing(timestampTextCharacterSize);
        
        double deltaTimeMicro = (double)frameTimer.getElapsedTime().asMicroseconds();
        frameTimer.restart();
        
        if(dirty)
        {
            sf::RectangleShape lineRect(sf::Vector2f(backgroundSizeWithoutPadding.x - LINE_SIDE_PADDING * 2.0f, LINE_HEIGHT));
            lineRect.setFillColor(ColorScheme::getBackgroundColor() + sf::Color(10, 10, 10));
            
            sf::Vector2<double> position(ChannelSidePanel::getWidth(), ChannelTopPanel::getHeight() + PADDING_TOP);
            double startHeight = position.y;
            position.y += scroll;
            for(Message *message : messages)
            {
                position.y += MESSAGE_PADDING_TOP;
                if(position.y + usernameTextHeight > 0.0f && position.y < backgroundPos.y + backgroundSize.y)
                {
                    sf::Text usernameText(message->user->getName(), *usernameFont, usernameTextCharacterSize);
                    usernameText.setFillColor(sf::Color(15, 192, 252));
                    usernameText.setPosition(sf::Vector2f(floor(position.x + PADDING_SIDE), floor(position.y)));
                    window.draw(usernameText);
                    
                    if(message->timestampSeconds)
                    {
                        time_t time = (time_t)message->timestampSeconds;
                        struct tm *localTimePtr = localtime(&time);
                        char date[30];
                        strftime(date, sizeof(date), "%Y-%m-%d at %T", localTimePtr);
                        
                        sf::Text timestamp(date, *timestampFont, timestampTextCharacterSize);
                        timestamp.setFillColor(ColorScheme::getTextRegularColor() * sf::Color(255, 255, 255, 30));
                        timestamp.setPosition(sf::Vector2f(floor(position.x + PADDING_SIDE + usernameText.getLocalBounds().width + USERNAME_TIMESTAMP_SIDE_PADDING * Settings::getScaling()), floor(position.y + 2.0f * Settings::getScaling() + usernameTextHeight * 0.5f - timestampTextHeight * 0.5f)));
                        window.draw(timestamp);
                    }
                }
                position.y += usernameTextHeight + USERNAME_PADDING_BOTTOM * Settings::getScaling();
                
                // No need to perform culling here, that is done in @Text draw function
                message->text.setCharacterSize(18 * Settings::getScaling());
                message->text.setMaxWidth(backgroundSize.x);
                message->text.setPosition(sf::Vector2f(floor(position.x + PADDING_SIDE), floor(position.y)));
                message->text.draw(window, cache);
                position.y += message->text.getHeight() + MESSAGE_PADDING_BOTTOM;
                
                if(position.y + LINE_HEIGHT > 0.0f && position.y < backgroundPos.y + backgroundSize.y)
                {
                    lineRect.setPosition(sf::Vector2f(position.x + LINE_SIDE_PADDING, floor(position.y)));
                    window.draw(lineRect);
                    //drawGradientLine(sf::Vector2f(position.x + LINE_SIDE_PADDING, floor(position.y)), sf::Vector2f(backgroundSizeWithoutPadding.x - LINE_SIDE_PADDING * 2.0f, LINE_HEIGHT), LINE_COLOR, window);
                }
                
                position.y += LINE_HEIGHT;
            }
            totalHeight = (position.y - scroll) - startHeight;
        }
        
        scroll += scrollSpeed;
        
        double deltaTimeScrollMultiplier = deltaTimeMicro * 0.00004;
        if(scrollSpeed > 0.0)
        {
            scrollSpeed -= deltaTimeScrollMultiplier;
        }
        else
        {
            scrollSpeed += deltaTimeScrollMultiplier;
        }
        
        if(abs(scrollSpeed - deltaTimeScrollMultiplier) <= deltaTimeScrollMultiplier)
            scrollSpeed = 0.0;
        
        double textOverflow = backgroundSize.y - totalHeight;
        if(scroll > 0.0 || textOverflow > 0.0)
        {
            scroll = 0.0;
            scrollSpeed = 0.0;
        }
        else if(textOverflow < 0.0 && scroll < textOverflow)
        {
            scroll = textOverflow;
            scrollSpeed = 0.0;
        }
        
        if(scrollToBottom)
        {
            scrollToBottom = false;
            if(textOverflow < 0.0)
                scroll = textOverflow;
            else
                scroll = 0.0;
        }
        
        //staticContentTexture.display();
        dirty = false;
        
        // TODO: Save this, expensive to create on fly?
        //sf::Sprite textureSprite(staticContentTexture.getTexture());
        //textureSprite.setPosition(backgroundPos);
        //window.draw(textureSprite);
        
        if(!selectingText) return;
        
        sf::Vector2f selectionRectStart(min((float)mousePos.x, selectingTextStart.x), min((float)mousePos.y, selectingTextStart.y));
        sf::Vector2f selectionRectEnd(max((float)mousePos.x, selectingTextStart.x), max((float)mousePos.y, selectingTextStart.y));
        sf::FloatRect selectionRect(selectionRectStart, selectionRectEnd - selectionRectStart);
    }
}