Simple GUI Library
image_view.cpp
Go to the documentation of this file.
1/**
2 * @author Nikita Mochalov (github.com/tralf-strues)
3 * @file image_view.cpp
4 * @date 2021-11-08
5 *
6 * @copyright Copyright (c) 2021
7 */
8
9#include "scene/image_view.h"
10
11namespace Sgl
12{
13 ImageView::ImageView(Image* image, bool preserveRatio)
14 : m_Image(image) {}
15
16 Component* ImageView::getHitComponent(int32_t x, int32_t y)
17 {
18 if (Sml::isPointInsideRectangle({x, y}, m_LayoutBounds))
19 {
20 return this;
21 }
22
23 return nullptr;
24 }
25
26 void ImageView::render(const Sml::Rectangle<int32_t>& targetRegion)
27 {
28 if (m_Image != nullptr)
29 {
30 Sml::Rectangle<int32_t> translatedRegion = targetRegion;
31 translatedRegion.pos += {getLayoutX(), getLayoutY()};
32 renderImage(m_Image, translatedRegion, getLayoutWidth(), getLayoutHeight());
33 }
34 }
35
36 void ImageView::layout() {}
37 void ImageView::prerender() {}
38
39 bool ImageView::isResizable() const { return true; }
40
41 const Image* ImageView::getImage() const { return m_Image; }
42 void ImageView::setImage(const Image* image) { m_Image = image; }
43
44 float ImageView::getRatio() const
45 {
46 return getImage()->getHeight() == 0 ? 0 : getImage()->getWidth() / getImage()->getHeight();
47 }
48
49 bool ImageView::getPreserveRatio() const { return m_PreserveRatio; }
50 void ImageView::setPreserveRatio(bool preserveRatio) { m_PreserveRatio = preserveRatio; }
51
52 int32_t ImageView::getFitWidth() const { return m_FitWidth; }
53 void ImageView::setFitWidth(int32_t width) { m_FitWidth = width; }
54
55 int32_t ImageView::getFitHeight() const { return m_FitHeight; }
56 void ImageView::setFitHeight(int32_t height) { m_FitHeight = height; }
57
58 int32_t getWidthFixedRatio(int32_t height, float ratio)
59 {
60 assert(fabs(ratio) > FLT_EPSILON);
61
62 return height / ratio;
63 }
64
65 int32_t getHeightFixedRatio(int32_t width, float ratio)
66 {
67 return width * ratio;
68 }
69
70 int32_t ImageView::computePrefWidth(int32_t height) const
71 {
72 if (m_Image == nullptr) { return 0; }
73
74 if (!getPreserveRatio() || height == -1)
75 {
76 return (m_FitWidth != USE_COMPUTED_SIZE) ? m_FitWidth : getImage()->getWidth();
77 }
78
79 return (m_FitWidth != USE_COMPUTED_SIZE) ? m_FitWidth : getWidthFixedRatio(height, getRatio());
80 }
81
82 int32_t ImageView::computePrefHeight(int32_t width) const
83 {
84 if (m_Image == nullptr) { return 0; }
85
86 if (!getPreserveRatio() || width == -1)
87 {
88 return (m_FitHeight != USE_COMPUTED_SIZE) ? m_FitHeight : getImage()->getHeight();
89 }
90
91 return (m_FitHeight != USE_COMPUTED_SIZE) ? m_FitHeight : getHeightFixedRatio(width, getRatio());
92 }
93
94 int32_t ImageView::computeMinWidth(int32_t height) const { return 0; }
95 int32_t ImageView::computeMinHeight(int32_t width) const { return 0; }
96
97 int32_t ImageView::computeMaxWidth(int32_t height) const { return UNLIMITED_SIZE; }
98 int32_t ImageView::computeMaxHeight(int32_t width) const { return UNLIMITED_SIZE; }
99}