guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
image.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include <memory>
24#include <stdexcept>
25
26#include "guillaume/entities/image.hpp"
27#include "guillaume/entities/builder_base.hpp"
28
29namespace guillaume::entities
30{
31
32 Image::Image::Builder::Builder(ecs::ComponentRegistry &componentRegistry,
33 ecs::EntityRegistry &entityRegistry)
34 : EntityBuilderBase<Image>(componentRegistry, entityRegistry)
35 {
36 reset();
37 }
38
39 std::shared_ptr<Image> Image::Builder::buildEntity(void)
40 {
41 auto entity = Image::create(this->getComponentRegistry(), _texturePath);
42 entity->setTexturePath(_texturePath);
43 entity->setFallbackPath(_fallbackPath);
44 if (_width > 0.0f && _height > 0.0f) {
45 entity->setSize(_width, _height);
46 }
47 return entity;
48 }
49
51 {
52 _texturePath.clear();
53 _fallbackPath.clear();
54 _width = 0.0f;
55 _height = 0.0f;
56 }
57
59 Image::Builder::withFallbackPath(const std::string &fallbackPath)
60 {
61 _fallbackPath = fallbackPath;
62 return *this;
63 }
64
65 Image::Builder &Image::Builder::withSize(float width, float height)
66 {
67 _width = width;
68 _height = height;
69 return *this;
70 }
71
73 Image::Builder::withTexturePath(const std::string &texturePath)
74 {
75 _texturePath = texturePath;
76 return *this;
77 }
78
79 std::shared_ptr<Image>
80 Image::Builder::makeImage(std::shared_ptr<Entity> parent,
81 const std::string &texturePath)
82 {
83 return withTexturePath(texturePath).registerEntity(parent);
84 }
85
86 Image::Image(ecs::ComponentRegistry &registry,
87 const std::string &texturePath)
88 : ecs::EntityFiller<components::Transform, components::Bound,
89 components::Image, components::Accessibility>(
90 registry)
91 {
92 setTexturePath(texturePath);
96 }
97
98 const std::string &Image::getTexturePath(void) const
99 {
100 return getComponentRegistry()
103 }
104
105 Image &Image::setFallbackPath(const std::string &fallbackPath)
106 {
109 .setFallbackPath(fallbackPath);
110 return *this;
111 }
112
113 Image &Image::setSize(float width, float height)
114 {
115 if (width <= 0.0f || height <= 0.0f) {
116 throw std::invalid_argument("Image size must be positive");
117 }
120 .setWidth(width)
121 .setHeight(height);
122 return *this;
123 }
124
125 Image &Image::setAspectRatio(float width, float aspectRatio)
126 {
127 if (width <= 0.0f || aspectRatio <= 0.0f) {
128 throw std::invalid_argument(
129 "Image width and aspect ratio must be positive");
130 }
131 return setSize(width, width / aspectRatio);
132 }
133
140
141 Image &Image::setAccessibilityLabel(const std::string &label)
142 {
145 .setLabel(label);
146 return *this;
147 }
148
150 {
151 }
152
153 Image &Image::setTexturePath(const std::string &texturePath)
154 {
157 .setTexturePath(texturePath);
158
159 return *this;
160 }
161
162} // namespace guillaume::entities
What an entity means to a user who does not see it: its name, its role, its state and its place in th...
Size of an entity: the width and height, in dp, of the rectangle that starts at the position of its c...
Definition bound.hpp:62
Bound & setHeight(float height) noexcept
Set the height.
Definition bound.cpp:42
A picture drawn in the entity's rectangle: the texture to show, a fallback texture,...
Definition image.hpp:56
LoadState
Whether the texture could be loaded, as recorded by systems::ImageRender.
Definition image.hpp:62
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
ComponentType & getComponent(const Entity::Identifier &entityIdentifier)
Get a component of an entity, to read or modify it.
ComponentRegistry & getComponentRegistry(void)
Get the registry in which the builder creates components.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
Base of every widget Builder: turns a configuration into a widget that is attached to a parent and ow...
Configures Image widgets and registers them in the scene's entity registry.
Definition image.hpp:84
Builder & withFallbackPath(const std::string &fallbackPath)
Set the texture drawn when the main one fails to load.
Definition image.cpp:59
std::shared_ptr< Image > makeImage(std::shared_ptr< Entity > parent, const std::string &texturePath)
Build an image, register it under parent and reset the builder.
Definition image.cpp:80
std::shared_ptr< Image > buildEntity(void) override
Build an image from the current configuration, without registering it or giving it a parent.
Definition image.cpp:39
Builder & withTexturePath(const std::string &texturePath)
Choose the texture to draw.
Definition image.cpp:73
Builder & withSize(float width, float height)
Set the displayed size.
Definition image.cpp:65
void reset(void) override
Restore the defaults: empty texture path, no fallback texture, and no size (width and height 0).
Definition image.cpp:50
A picture loaded from a texture file and stretched over a rectangle.
Definition image.hpp:77
Image & setAccessibilityLabel(const std::string &label)
Describe the picture for assistive technologies.
Definition image.cpp:141
Image & setSize(float width, float height)
Set the displayed size, written to components::Bound.
Definition image.cpp:113
~Image(void)
Destructor.
Definition image.cpp:149
Image & setAspectRatio(float width, float aspectRatio)
Set the displayed size from a width and an aspect ratio.
Definition image.cpp:125
Image & setTexturePath(const std::string &texturePath)
Change the texture to draw.
Definition image.cpp:153
Image & setFallbackPath(const std::string &fallbackPath)
Set the texture drawn when the main one fails to load.
Definition image.cpp:105
components::Image::LoadState getLoadState(void) const
Tell whether the texture could be loaded.
Definition image.cpp:134
const std::string & getTexturePath(void) const
Get the path of the texture the image was asked to draw.
Definition image.cpp:98
static std::shared_ptr< Image > create(Arguments &&...arguments)
Build an image without registering it in an entity registry.
Definition image.hpp:177
State. Every class here is plain data attached to an entity.
@ Image
Image or icon with meaning.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46