13 ImageView::ImageView(Image* image,
bool preserveRatio)
16 Component* ImageView::getHitComponent(int32_t x, int32_t y)
18 if (Sml::isPointInsideRectangle({x, y}, m_LayoutBounds))
26 void ImageView::render(
const Sml::Rectangle<int32_t>& targetRegion)
28 if (m_Image !=
nullptr)
30 Sml::Rectangle<int32_t> translatedRegion = targetRegion;
31 translatedRegion.pos += {getLayoutX(), getLayoutY()};
32 renderImage(m_Image, translatedRegion, getLayoutWidth(), getLayoutHeight());
36 void ImageView::layout() {}
37 void ImageView::prerender() {}
39 bool ImageView::isResizable()
const {
return true; }
41 const Image* ImageView::getImage()
const {
return m_Image; }
42 void ImageView::setImage(
const Image* image) { m_Image = image; }
44 float ImageView::getRatio()
const
46 return getImage()->getHeight() == 0 ? 0 : getImage()->getWidth() / getImage()->getHeight();
49 bool ImageView::getPreserveRatio()
const {
return m_PreserveRatio; }
50 void ImageView::setPreserveRatio(
bool preserveRatio) { m_PreserveRatio = preserveRatio; }
52 int32_t ImageView::getFitWidth()
const {
return m_FitWidth; }
53 void ImageView::setFitWidth(int32_t width) { m_FitWidth = width; }
55 int32_t ImageView::getFitHeight()
const {
return m_FitHeight; }
56 void ImageView::setFitHeight(int32_t height) { m_FitHeight = height; }
58 int32_t getWidthFixedRatio(int32_t height,
float ratio)
60 assert(fabs(ratio) > FLT_EPSILON);
62 return height / ratio;
65 int32_t getHeightFixedRatio(int32_t width,
float ratio)
70 int32_t ImageView::computePrefWidth(int32_t height)
const
72 if (m_Image ==
nullptr) {
return 0; }
74 if (!getPreserveRatio() || height == -1)
76 return (m_FitWidth != USE_COMPUTED_SIZE) ? m_FitWidth : getImage()->getWidth();
79 return (m_FitWidth != USE_COMPUTED_SIZE) ? m_FitWidth : getWidthFixedRatio(height, getRatio());
82 int32_t ImageView::computePrefHeight(int32_t width)
const
84 if (m_Image ==
nullptr) {
return 0; }
86 if (!getPreserveRatio() || width == -1)
88 return (m_FitHeight != USE_COMPUTED_SIZE) ? m_FitHeight : getImage()->getHeight();
91 return (m_FitHeight != USE_COMPUTED_SIZE) ? m_FitHeight : getHeightFixedRatio(width, getRatio());
94 int32_t ImageView::computeMinWidth(int32_t height)
const {
return 0; }
95 int32_t ImageView::computeMinHeight(int32_t width)
const {
return 0; }
97 int32_t ImageView::computeMaxWidth(int32_t height)
const {
return UNLIMITED_SIZE; }
98 int32_t ImageView::computeMaxHeight(int32_t width)
const {
return UNLIMITED_SIZE; }