ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
ImageContainer.hpp
Go to the documentation of this file.
1#pragma once
2
11
12#include "Common.hpp"
13#include "Layout.hpp"
14
15#include <algorithm>
16#include <optional>
17#include <string_view>
18#include <utility>
19
20namespace ZenitUI::UI
21{
22
26
36
46 class ImageContainer : public TLayout<ImageContainer>
47 {
48 public:
53 explicit ImageContainer(TextureHandle tex = {})
55 {
56 setInteractive(false);
57 setStyleTag("ImageContainer");
58 }
59
62 {
63 if (tex.id == t.id && tex.width == t.width && tex.height == t.height)
64 return;
65 tex = t;
67 }
68
70 TextureHandle getTexture() const noexcept { return tex; }
71
74 {
75 if (sourceRect_ &&
76 sourceRect_->x == r.x && sourceRect_->y == r.y &&
77 sourceRect_->width == r.width && sourceRect_->height == r.height)
78 return;
79 sourceRect_ = r;
81 }
82
85 {
86 if (!sourceRect_)
87 return;
88 sourceRect_.reset();
90 }
91
93 const std::optional<Rect> &getSourceRect() const noexcept { return sourceRect_; }
94
97 {
98 if (fit_ == fit)
99 return;
100 fit_ = fit;
102 }
103
105 ObjectFit getObjectFit() const noexcept { return fit_; }
106
108 static bool parseObjectFit(std::string_view s, ObjectFit &out) noexcept
109 {
110 if (s == "fill") { out = ObjectFit::Fill; return true; }
111 if (s == "contain") { out = ObjectFit::Contain; return true; }
112 if (s == "cover") { out = ObjectFit::Cover; return true; }
113 if (s == "none") { out = ObjectFit::None; return true; }
114 if (s == "scale-down") { out = ObjectFit::ScaleDown; return true; }
115 return false;
116 }
117
124 static std::pair<Rect, Rect> computeFitRects(Rect src, Rect dst, ObjectFit fit) noexcept
125 {
126 if (src.width <= 0.0f || src.height <= 0.0f || dst.width <= 0.0f || dst.height <= 0.0f)
127 return {src, dst};
128
129 ObjectFit effective = fit;
130 if (effective == ObjectFit::ScaleDown)
131 {
132 effective = (src.width <= dst.width && src.height <= dst.height)
135 }
136
137 switch (effective)
138 {
139 case ObjectFit::Fill:
140 return {src, dst};
141
143 {
144 const float scale = std::min(dst.width / src.width, dst.height / src.height);
145 const float w = src.width * scale;
146 const float h = src.height * scale;
147 Rect outDst{
148 dst.x + (dst.width - w) * 0.5f,
149 dst.y + (dst.height - h) * 0.5f,
150 w,
151 h};
152 return {src, outDst};
153 }
154
155 case ObjectFit::Cover:
156 {
157 const float scale = std::max(dst.width / src.width, dst.height / src.height);
158 const float visW = dst.width / scale;
159 const float visH = dst.height / scale;
160 Rect outSrc{
161 src.x + (src.width - visW) * 0.5f,
162 src.y + (src.height - visH) * 0.5f,
163 visW,
164 visH};
165 return {outSrc, dst};
166 }
167
168 case ObjectFit::None:
169 {
170 const float drawW = std::min(src.width, dst.width);
171 const float drawH = std::min(src.height, dst.height);
172 Rect outSrc{
173 src.x + (src.width - drawW) * 0.5f,
174 src.y + (src.height - drawH) * 0.5f,
175 drawW,
176 drawH};
177 Rect outDst{
178 dst.x + (dst.width - drawW) * 0.5f,
179 dst.y + (dst.height - drawH) * 0.5f,
180 drawW,
181 drawH};
182 return {outSrc, outDst};
183 }
184
186 break;
187 }
188 return {src, dst};
189 }
190
191 protected:
192 void renderContent(float op, const ComputedStyle &style) override
193 {
194 auto *ctx = context();
195 auto *r = ctx ? ctx->renderer : nullptr;
196 if (!r || !tex.valid() || rect.width <= 0.0f || rect.height <= 0.0f)
197 return;
198
199 const Rect baseSrc = (sourceRect_ && sourceRect_->width > 0.0f && sourceRect_->height > 0.0f)
200 ? *sourceRect_
201 : Rect{0.0f, 0.0f, static_cast<float>(tex.width), static_cast<float>(tex.height)};
202
203 const auto [drawSrc, drawDst] = computeFitRects(baseSrc, rect, fit_);
204 r->drawTexture(tex, drawSrc, drawDst, style.tint.withAlpha(op));
205 }
206
207 private:
208 TextureHandle tex;
209 std::optional<Rect> sourceRect_;
211 };
212
213} // namespace ZenitUI::UI
Centralized standard library includes used throughout ZenitUI.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
void invalidateLayerCache()
Mark any cached off-screen filter layer on this node and its ancestors as dirty so it is re-baked on ...
Definition Layout.hpp:979
void setStyleTag(const std::string &t)
Set the node's style tag.
UIContext * context() const
Return the UIContext this node is attached to, or nullptr if unmounted.
Definition Layout.hpp:899
CRTP base for components that want fluent builders and a static create() factory.
Definition Layout.hpp:1397
TLayout(Args &&...args)
Definition Layout.hpp:1401
A single-texture node supporting sub-rect cropping (sourceRect) and ObjectFit.
Definition ImageContainer.hpp:47
const std::optional< Rect > & getSourceRect() const noexcept
Current source sub-rectangle, or nullopt if using the full texture.
Definition ImageContainer.hpp:93
ImageContainer(TextureHandle tex={})
Construct with a texture.
Definition ImageContainer.hpp:53
void clearSourceRect()
Reset to drawing the full texture.
Definition ImageContainer.hpp:84
void setSourceRect(Rect r)
Set an explicit source sub-rectangle in texture pixels (for sprite sheets / cropping).
Definition ImageContainer.hpp:73
TextureHandle getTexture() const noexcept
Current texture handle.
Definition ImageContainer.hpp:70
static bool parseObjectFit(std::string_view s, ObjectFit &out) noexcept
Parse a fit keyword ("fill", "contain", "cover", "none", "scale-down").
Definition ImageContainer.hpp:108
void setObjectFit(ObjectFit fit)
Set how the texture fits inside the node rect (default: ObjectFit::Fill).
Definition ImageContainer.hpp:96
static std::pair< Rect, Rect > computeFitRects(Rect src, Rect dst, ObjectFit fit) noexcept
Compute the final (srcRect, dstRect) pair for src drawn into dst.
Definition ImageContainer.hpp:124
void setTexture(TextureHandle t)
Replace the texture.
Definition ImageContainer.hpp:61
void renderContent(float op, const ComputedStyle &style) override
Draw the node's content (text, images, custom geometry).
Definition ImageContainer.hpp:192
ObjectFit getObjectFit() const noexcept
Current fit mode.
Definition ImageContainer.hpp:105
LayoutType
Positioning strategy applied to a Layout node's direct children.
Definition UIEnums.hpp:29
@ Stack
Children overlap each other; Z-order controls paint order.
Definition UIEnums.hpp:30
ObjectFit
How ImageContainer fits the source texture into its layout rect.
Definition ImageContainer.hpp:29
@ Contain
Scale uniformly to fit entirely inside the box, centered.
Definition ImageContainer.hpp:31
@ None
Draw at 1:1 source pixels, centered and cropped if larger than the box.
Definition ImageContainer.hpp:33
@ ScaleDown
Like None if smaller than the box, otherwise Contain.
Definition ImageContainer.hpp:34
@ Cover
Scale uniformly to cover the entire box, centered and cropped.
Definition ImageContainer.hpp:32
@ Fill
Stretch to fill the box (ignores aspect ratio).
Definition ImageContainer.hpp:30
Dense style: every property has a concrete value.
Definition Style.hpp:1036
Axis-aligned rectangle in screen coordinates.
Definition CoreTypes.hpp:46
Opaque handle to a GPU/software texture owned by the asset provider.
Definition CoreTypes.hpp:127