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

#include "types.hpp"
#include "Message.hpp"
#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>
#include <memory>

namespace dchat
{
    class Room;
    struct RoomMessage;
    class Cache;
    class User;
    
    class MessageBoard
    {
    public:
        MessageBoard(std::shared_ptr<Room> room);
        ~MessageBoard();
        
        void processEvent(const sf::Event &event, Cache *cache);
        void draw(sf::RenderWindow &window, Cache *cache);

        bool addMessage(Message *message);
        void deleteMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser);

        void addOfflineUserMessage(std::string msg, bool plainText);
        void addSystemUserMessage(std::string msg, bool plainText);
    private:
        usize findPositionToInsertMessageByTimestamp(Message *message);
        
        void updateStaticContentTexture(const sf::Vector2u &newSize);
        
        void drawDefault(sf::RenderWindow &window, Cache *cache);
        void drawSimple(sf::RenderWindow &window, Cache *cache);
    private:
        std::shared_ptr<Room> room;
        bool dirty;
        odhtdb::MapHash<Message*> messageIdMap;
        std::vector<Message*> messages;
        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;

        std::shared_ptr<User> offlineUser;
        std::shared_ptr<User> systemUser;
    };
}