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

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/System/Clock.hpp>
#include <string>

namespace dchat
{
    class Gif;
    
    class ImagePreview
    {
    public:
        // set @texture to nullptr if you wish to end preview
        static void preview(sf::Texture *texture, const std::string &url = "");
        // set gif to nullptr if you wish to end preview
        static void preview(Gif *gif, const std::string &url = "");
        static void* getPreviewContentPtr();
        static sf::Int32 getTimeSinceLastSeenMs();
        
        static void processEvent(const sf::Event &event);
        static void draw(sf::RenderWindow &window);
    private:
        enum class ContentType
        {
            NONE,
            TEXTURE,
            GIF
        };
        
        ImagePreview() : texture(nullptr), contentType(ContentType::NONE) {}
        ImagePreview(const ImagePreview&) = delete;
        ImagePreview& operator=(const ImagePreview&) = delete;
        static ImagePreview* getInstance();
        
        sf::Vector2u calculateImageSize(sf::Vector2u windowSize) const;
    private:
        sf::Sprite sprite;
        sf::Vector2u size;
        sf::Clock lastSeenTimer;
        
        union
        {
            sf::Texture *texture;
            Gif *gif;
        };
        ContentType contentType;
    };
}