aboutsummaryrefslogtreecommitdiff
path: root/src/ImagePreview.cpp
blob: 424aefa206aadc16f3e79a00daaa4f8ad7bf71d4 (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
#include "../include/ImagePreview.hpp"
#include "../include/Settings.hpp"
#include "../include/Gif.hpp"
#include <SFML/Graphics/RectangleShape.hpp>
#include <dchat/Process.hpp>
#include <cassert>

namespace dchat
{
    static const float PADDING_VERTICAL = 40.0f;
    static const float PADDING_HORIZONTAL = 40.0f;
    
    static ImagePreview *instance = nullptr;
    static std::string imagePreviewUrl;
    
    ImagePreview* ImagePreview::getInstance()
    {
        if(!instance)
            instance = new ImagePreview();
        return instance;
    }
    
    void ImagePreview::preview(sf::Texture *texture, const std::string &url)
    {
        ImagePreview *instance = getInstance();
        if(texture == instance->texture) return;
        instance->texture = texture;
        instance->contentType = texture ? ContentType::TEXTURE : ContentType::NONE;
        imagePreviewUrl = url;
        if(texture)
            instance->sprite.setTexture(*texture, true);
        else
            instance->sprite = sf::Sprite();
    }
    
    void ImagePreview::preview(Gif *gif, const std::string &url)
    {
        ImagePreview *instance = getInstance();
        if(gif == instance->gif) return;
        instance->gif = gif;
        instance->contentType = gif ? ContentType::GIF : ContentType::NONE;
        imagePreviewUrl = url;
        instance->sprite = sf::Sprite();
    }
    
    void* ImagePreview::getPreviewContentPtr()
    {
        return getInstance()->texture;
    }
    
    sf::Int32 ImagePreview::getTimeSinceLastSeenMs()
    {
        return getInstance()->lastSeenTimer.getElapsedTime().asMilliseconds();
    }
    
    void ImagePreview::processEvent(const sf::Event &event)
    {
        ImagePreview *instance = getInstance();
        if(instance->contentType == ContentType::NONE) return;
        
        if(event.mouseButton.button == sf::Mouse::Button::Left)
        {
            const auto &imagePos = instance->position;
            const auto &imageSize = instance->size;

            bool mouseInside = false;
            if(event.mouseButton.x >= imagePos.x && event.mouseButton.x <= imagePos.x + imageSize.x && 
                event.mouseButton.y >= imagePos.y && event.mouseButton.y <= imagePos.y + imageSize.y)
            {
                mouseInside = true;
            }
            
            if(event.type == sf::Event::MouseButtonPressed && mouseInside && !imagePreviewUrl.empty())
            {
                // TODO: Implement for other platforms than linux
                std::string cmd = "xdg-open ";
                cmd += escapeCommandArg(imagePreviewUrl);
                printf("Clicked on web page preview, opening web page by running command: %s\n", cmd.c_str());
                system(cmd.c_str());
            }
            else if(event.type == sf::Event::MouseButtonReleased && !mouseInside)
            {
                ImagePreview::preview((sf::Texture*)nullptr);
            }
        }
        else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
        {
            ImagePreview::preview((sf::Texture*)nullptr);
        }
    }
    
    void ImagePreview::draw(sf::RenderWindow &window)
    {
        ImagePreview *instance = getInstance();
        if(instance->contentType == ContentType::NONE) return;
        
        auto windowSize = window.getSize();
        sf::RectangleShape background(sf::Vector2f(windowSize.x, windowSize.y));
        background.setFillColor(sf::Color(0, 0, 0, 200));
        
        auto imageSize = instance->calculateImageSize(windowSize);
        instance->size = imageSize;
        
        window.draw(background);
        switch(instance->contentType)
        {
            case ContentType::TEXTURE:
            {
                auto textureSize = instance->sprite.getTexture()->getSize();
                instance->position = sf::Vector2f(windowSize.x / 2 - imageSize.x / 2, windowSize.y / 2 - imageSize.y / 2);
                instance->sprite.setPosition(instance->position);
                instance->sprite.setScale((double)imageSize.x / (double)textureSize.x, (double)imageSize.y / (double)textureSize.y);
                window.draw(instance->sprite);
                break;
            }
            case ContentType::GIF:
            {
                auto *gif = static_cast<SfmlGif*>(instance->gif);
                gif->update();
                auto textureSize = gif->getSize();

                instance->position = sf::Vector2f(windowSize.x / 2 - imageSize.x / 2, windowSize.y / 2 - imageSize.y / 2);
                sf::Sprite sprite(gif->texture);
                sprite.setPosition(instance->position);
                sprite.setScale(sf::Vector2f((double)imageSize.x / (double)textureSize.x, (double)imageSize.y / (double)textureSize.y));
                window.draw(sprite);
                break;
            }
        }
        instance->lastSeenTimer.restart();
    }
    
    sf::Vector2u ImagePreview::calculateImageSize(sf::Vector2u windowSize) const
    {
        assert(contentType != ContentType::NONE);
        sf::Vector2u imageMaxSize(windowSize.x - PADDING_HORIZONTAL * Settings::getScaling() * 2.0f, windowSize.y - PADDING_VERTICAL * Settings::getScaling() * 2.0f);
        sf::Vector2u textureSize;
        switch(contentType)
        {
            case ContentType::TEXTURE:
                textureSize = texture->getSize();
                break;
            case ContentType::GIF:
                auto size = gif->getSize();
                textureSize.x = size.x;
                textureSize.y = size.y;
                break;
        }
        auto imageSize = textureSize;
        double textureWidthHeightRatio = (double)imageSize.x / (double)imageSize.y;
        double textureHeightWidthRatio = (double)imageSize.y / (double)imageSize.x;
        
        int overflowVertical = (int)imageSize.y - (int)imageMaxSize.y;
        if(overflowVertical > 0)
        {
            imageSize.y = imageMaxSize.y;
            imageSize.x -= (overflowVertical * textureWidthHeightRatio);
        }
        
        int overflowHorizontal = (int)imageSize.x - (int)imageMaxSize.x;
        if(overflowHorizontal > 0)
        {
            imageSize.x = imageMaxSize.x;
            imageSize.y -= (overflowHorizontal * textureHeightWidthRatio);
        }
        return imageSize;
    }
}