-
-
Notifications
You must be signed in to change notification settings - Fork 450
/
ImageSet.cpp
125 lines (101 loc) · 2.76 KB
/
ImageSet.cpp
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
#include "messages/ImageSet.hpp"
#include "singletons/Settings.hpp"
namespace chatterino {
ImageSet::ImageSet()
: imageX1_(Image::getEmpty())
, imageX2_(Image::getEmpty())
, imageX3_(Image::getEmpty())
{
}
ImageSet::ImageSet(const ImagePtr &image1, const ImagePtr &image2,
const ImagePtr &image3)
: imageX1_(image1)
, imageX2_(image2)
, imageX3_(image3)
{
}
ImageSet::ImageSet(const Url &image1, const Url &image2, const Url &image3)
: imageX1_(Image::fromUrl(image1, 1))
, imageX2_(image2.string.isEmpty() ? Image::getEmpty()
: Image::fromUrl(image2, 0.5))
, imageX3_(image3.string.isEmpty() ? Image::getEmpty()
: Image::fromUrl(image3, 0.25))
{
}
void ImageSet::setImage1(const ImagePtr &image)
{
this->imageX1_ = image;
}
void ImageSet::setImage2(const ImagePtr &image)
{
this->imageX2_ = image;
}
void ImageSet::setImage3(const ImagePtr &image)
{
this->imageX3_ = image;
}
const ImagePtr &ImageSet::getImage1() const
{
return this->imageX1_;
}
const ImagePtr &ImageSet::getImage2() const
{
return this->imageX2_;
}
const ImagePtr &ImageSet::getImage3() const
{
return this->imageX3_;
}
const std::shared_ptr<Image> &getImagePriv(const ImageSet &set, float scale)
{
#ifndef CHATTERINO_TEST
scale *= getSettings()->emoteScale;
#endif
int quality = 1;
if (scale > 2.001f)
quality = 3;
else if (scale > 1.001f)
quality = 2;
if (!set.getImage3()->isEmpty() && quality == 3)
{
return set.getImage3();
}
if (!set.getImage2()->isEmpty() && quality >= 2)
{
return set.getImage2();
}
return set.getImage1();
}
const ImagePtr &ImageSet::getImageOrLoaded(float scale) const
{
auto &&result = getImagePriv(*this, scale);
// get best image based on scale
result->load();
// prefer other image if selected image is not loaded yet
if (result->loaded())
return result;
else if (this->imageX3_ && !this->imageX3_->isEmpty() &&
this->imageX3_->loaded())
return this->imageX3_;
else if (this->imageX2_ && !this->imageX2_->isEmpty() &&
this->imageX2_->loaded())
return this->imageX2_;
else if (this->imageX1_->loaded())
return this->imageX1_;
else
return result;
}
const ImagePtr &ImageSet::getImage(float scale) const
{
return getImagePriv(*this, scale);
}
bool ImageSet::operator==(const ImageSet &other) const
{
return std::tie(this->imageX1_, this->imageX2_, this->imageX3_) ==
std::tie(other.imageX1_, other.imageX2_, other.imageX3_);
}
bool ImageSet::operator!=(const ImageSet &other) const
{
return !this->operator==(other);
}
} // namespace chatterino