aboutsummaryrefslogtreecommitdiff
path: root/include/MessageBoard.hpp
blob: 1d1f523bbaa460823ccb96716fea20825ef87a40 (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
#pragma once

#include "Message.hpp"
#include "types.hpp"
#include "../include/Cache.hpp"
#include "../include/Scrollbar.hpp"
#include <SFML/Graphics/RenderTexture.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
#include <odhtdb/Hash.hpp>
#include <odhtdb/Signature.hpp>
#include <stdexcept>
#include <mutex>

namespace dchat
{
    class Channel;
    
    class MessageBoard
    {
        friend class Channel;
    public:
        MessageBoard(Channel *channel);
        ~MessageBoard();
        
        void processEvent(const sf::Event &event, Cache &cache);
        void draw(sf::RenderWindow &window, Cache &cache);
        
        Message* getLatestMessage();
        const std::vector<Message*>& getMessages() const;
    private:
        usize findPositionToInsertMessageByTimestamp(Message *message);
        
        void updateStaticContentTexture(const sf::Vector2u &newSize);
        bool addMessage(Message *message, const odhtdb::Hash &id);
        void deleteMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser);
        
        void drawDefault(sf::RenderWindow &window, Cache &cache);
        void drawSimple(sf::RenderWindow &window, Cache &cache);
    private:
        Channel *channel;
        bool dirty;
        std::vector<Message*> messages;
        odhtdb::MapHash<Message*> messageIdMap;
        double scroll;
        double scrollSpeed;
        double totalHeight;
        sf::Clock frameTimer;
        bool scrollToBottom;
        sf::Vector2f backgroundSizeWithoutPadding;
        sf::Vector2f backgroundSize;
        sf::Vector2f backgroundPos;
        std::mutex messageProcessMutex;
        usize visibleMessageStartIndex;
        usize visibleMessageEndIndex;
        Scrollbar scrollbar;
    };
}