mirror-chatterino2/src/messages/ImageSet.cpp

124 lines
2.7 KiB
C++
Raw Normal View History

#include "messages/ImageSet.hpp"
2018-08-02 14:23:27 +02:00
#include "singletons/Settings.hpp"
namespace chatterino {
ImageSet::ImageSet()
: imageX1_(Image::getEmpty())
, imageX2_(Image::getEmpty())
, imageX3_(Image::getEmpty())
{
}
2018-08-06 21:17:03 +02:00
ImageSet::ImageSet(const ImagePtr &image1, const ImagePtr &image2,
const ImagePtr &image3)
2018-08-02 14:23:27 +02:00
: 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))
2018-08-02 14:23:27 +02:00
{
}
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)
2018-08-02 14:23:27 +02:00
{
scale *= getSettings()->emoteScale;
2018-12-02 18:26:21 +01:00
int quality = 1;
2018-12-02 18:26:21 +01:00
if (scale > 2.001f)
quality = 3;
else if (scale > 1.001f)
quality = 2;
2018-12-02 18:26:21 +01:00
if (!set.getImage3()->isEmpty() && quality == 3)
{
return set.getImage3();
}
2018-12-02 18:26:21 +01:00
if (!set.getImage2()->isEmpty() && quality >= 2)
{
return set.getImage2();
}
2018-12-02 18:26:21 +01:00
return set.getImage1();
}
const ImagePtr &ImageSet::getImageOrLoaded(float scale) const
{
auto &&result = getImagePriv(*this, scale);
// get best image based on scale
result->load();
2018-12-02 18:26:21 +01:00
// prefer other image if selected image is not loaded yet
if (result->loaded())
2018-12-02 18:26:21 +01:00
return result;
else if (this->imageX1_->loaded())
2018-12-02 18:26:21 +01:00
return this->imageX1_;
else if (this->imageX2_ && !this->imageX2_->isEmpty() &&
this->imageX2_->loaded())
2018-08-11 23:53:20 +02:00
return this->imageX2_;
2018-12-02 18:26:21 +01:00
else if (this->imageX3_ && !this->imageX3_->isEmpty() &&
this->imageX3_->loaded())
2018-12-02 18:26:21 +01:00
return this->imageX3_;
else
return result;
2018-08-02 14:23:27 +02:00
}
const ImagePtr &ImageSet::getImage(float scale) const
{
return getImagePriv(*this, scale);
}
2018-08-02 14:23:27 +02:00
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