aboutsummaryrefslogtreecommitdiff
path: root/src/ImagePreview.cpp
blob: 0b36dbd8bbc35338afa227957d515a6f6007339b (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/StringUtils.hpp"
#include "../include/Gif.hpp"
#include <SFML/Graphics/RectangleShape.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)
    {
        if(texture == getInstance()->texture) return;
        getInstance()->texture = texture;
        getInstance()->contentType = texture ? ContentType::TEXTURE : ContentType::NONE;
        imagePreviewUrl = url;
        if(texture)
            getInstance()->sprite.setTexture(*texture, true);
        else
            getInstance()->sprite = sf::Sprite();
    }
    
    void ImagePreview::preview(Gif *gif, const std::string &url)
    {
        if(gif == getInstance()->gif) return;
        getInstance()->gif = gif;
        getInstance()->contentType = gif ? ContentType::GIF : ContentType::NONE;
        imagePreviewUrl = url;
        getInstance()->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)
    {
        if(getInstance()->contentType == ContentType::NONE) return;
        
        if(event.mouseButton.button == sf::Mouse::Button::Left)
        {
            sf::Vector2f imagePos;
            switch(getInstance()->contentType)
            {
                case ContentType::TEXTURE:
                    imagePos = getInstance()->sprite.getPosition();
                    break;
                case ContentType::GIF:
                    imagePos = getInstance()->gif->getPosition();
                    break;
            }
            getInstance()->sprite.getPosition();
            const auto &imageSize = getInstance()->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 escapedUrl = stringReplaceChar(imagePreviewUrl, "'", "");
                escapedUrl = stringReplaceChar(escapedUrl, "\\", "");
                std::string cmd = "xdg-open '";
                cmd += escapedUrl;
                cmd += "'";
                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)
    {
        if(getInstance()->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 = getInstance()->calculateImageSize(windowSize);
        getInstance()->size = imageSize;
        
        window.draw(background);
        switch(getInstance()->contentType)
        {
            case ContentType::TEXTURE:
            {
                auto textureSize = getInstance()->sprite.getTexture()->getSize();
                getInstance()->sprite.setPosition(windowSize.x / 2 - imageSize.x / 2, windowSize.y / 2 - imageSize.y / 2);
                getInstance()->sprite.setScale((double)imageSize.x / (double)textureSize.x, (double)imageSize.y / (double)textureSize.y);
                window.draw(getInstance()->sprite);
                break;
            }
            case ContentType::GIF:
            {
                auto textureSize = getInstance()->gif->getSize();
                getInstance()->gif->setPosition(sf::Vector2f(windowSize.x / 2 - imageSize.x / 2, windowSize.y / 2 - imageSize.y / 2));
                getInstance()->gif->setScale(sf::Vector2f((double)imageSize.x / (double)textureSize.x, (double)imageSize.y / (double)textureSize.y));
                getInstance()->gif->draw(window);
                break;
            }
        }
        getInstance()->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:
                textureSize = gif->getSize();
                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;
    }
}