1 #include "messages/ImageSet.hpp"
2 
3 #include "singletons/Settings.hpp"
4 
5 namespace chatterino {
6 
ImageSet()7 ImageSet::ImageSet()
8     : imageX1_(Image::getEmpty())
9     , imageX2_(Image::getEmpty())
10     , imageX3_(Image::getEmpty())
11 {
12 }
13 
ImageSet(const ImagePtr & image1,const ImagePtr & image2,const ImagePtr & image3)14 ImageSet::ImageSet(const ImagePtr &image1, const ImagePtr &image2,
15                    const ImagePtr &image3)
16     : imageX1_(image1)
17     , imageX2_(image2)
18     , imageX3_(image3)
19 {
20 }
21 
ImageSet(const Url & image1,const Url & image2,const Url & image3)22 ImageSet::ImageSet(const Url &image1, const Url &image2, const Url &image3)
23     : imageX1_(Image::fromUrl(image1, 1))
24     , imageX2_(image2.string.isEmpty() ? Image::getEmpty()
25                                        : Image::fromUrl(image2, 0.5))
26     , imageX3_(image3.string.isEmpty() ? Image::getEmpty()
27                                        : Image::fromUrl(image3, 0.25))
28 {
29 }
30 
setImage1(const ImagePtr & image)31 void ImageSet::setImage1(const ImagePtr &image)
32 {
33     this->imageX1_ = image;
34 }
35 
setImage2(const ImagePtr & image)36 void ImageSet::setImage2(const ImagePtr &image)
37 {
38     this->imageX2_ = image;
39 }
40 
setImage3(const ImagePtr & image)41 void ImageSet::setImage3(const ImagePtr &image)
42 {
43     this->imageX3_ = image;
44 }
45 
getImage1() const46 const ImagePtr &ImageSet::getImage1() const
47 {
48     return this->imageX1_;
49 }
50 
getImage2() const51 const ImagePtr &ImageSet::getImage2() const
52 {
53     return this->imageX2_;
54 }
55 
getImage3() const56 const ImagePtr &ImageSet::getImage3() const
57 {
58     return this->imageX3_;
59 }
60 
getImagePriv(const ImageSet & set,float scale)61 const std::shared_ptr<Image> &getImagePriv(const ImageSet &set, float scale)
62 {
63 #ifndef CHATTERINO_TEST
64     scale *= getSettings()->emoteScale;
65 #endif
66 
67     int quality = 1;
68 
69     if (scale > 2.001f)
70         quality = 3;
71     else if (scale > 1.001f)
72         quality = 2;
73 
74     if (!set.getImage3()->isEmpty() && quality == 3)
75     {
76         return set.getImage3();
77     }
78 
79     if (!set.getImage2()->isEmpty() && quality >= 2)
80     {
81         return set.getImage2();
82     }
83 
84     return set.getImage1();
85 }
86 
getImageOrLoaded(float scale) const87 const ImagePtr &ImageSet::getImageOrLoaded(float scale) const
88 {
89     auto &&result = getImagePriv(*this, scale);
90 
91     // get best image based on scale
92     result->load();
93 
94     // prefer other image if selected image is not loaded yet
95     if (result->loaded())
96         return result;
97     else if (this->imageX3_ && !this->imageX3_->isEmpty() &&
98              this->imageX3_->loaded())
99         return this->imageX3_;
100     else if (this->imageX2_ && !this->imageX2_->isEmpty() &&
101              this->imageX2_->loaded())
102         return this->imageX2_;
103     else if (this->imageX1_->loaded())
104         return this->imageX1_;
105     else
106         return result;
107 }
108 
getImage(float scale) const109 const ImagePtr &ImageSet::getImage(float scale) const
110 {
111     return getImagePriv(*this, scale);
112 }
113 
operator ==(const ImageSet & other) const114 bool ImageSet::operator==(const ImageSet &other) const
115 {
116     return std::tie(this->imageX1_, this->imageX2_, this->imageX3_) ==
117            std::tie(other.imageX1_, other.imageX2_, other.imageX3_);
118 }
119 
operator !=(const ImageSet & other) const120 bool ImageSet::operator!=(const ImageSet &other) const
121 {
122     return !this->operator==(other);
123 }
124 
125 }  // namespace chatterino
126