25#ifndef TGUI_COMPONENTS_HPP
26#define TGUI_COMPONENTS_HPP
28#include <TGUI/Text.hpp>
29#include <TGUI/Sprite.hpp>
30#include <TGUI/Texture.hpp>
31#include <TGUI/Outline.hpp>
33#include <unordered_map>
50 enum class ComponentState : std::uint8_t
59 FocusedActiveHover = 7,
67 enum class AlignLayout : std::uint8_t
82 enum class PositionAlignment : std::uint8_t
98 class TGUI_API MessageBroker
102 TGUI_NODISCARD
static std::uint64_t createTopic();
104 static void destroyTopic(std::uint64_t topicId);
106 TGUI_NODISCARD
static std::uint64_t subscribe(std::uint64_t topicId, std::function<
void()> func);
108 static void unsubscribe(std::uint64_t callbackId);
110 static void sendEvent(std::uint64_t topicId);
113 static std::unordered_map<std::uint64_t, std::set<std::uint64_t>> m_topicIdToCallbackIds;
114 static std::unordered_map<std::uint64_t, std::uint64_t> m_callbackIdToTopicId;
115 static std::unordered_map<std::uint64_t, std::function<void()>> m_listeners;
118 static std::uint64_t m_lastId;
123 class TGUI_API StylePropertyBase
126 virtual ~StylePropertyBase() =
default;
131 template <
typename ValueType>
132 class TGUI_API StyleProperty :
public StylePropertyBase
138 m_messageTopicId{MessageBroker::createTopic()}
142 explicit StyleProperty(ValueType defaultValue) :
143 m_defaultValue {std::move(defaultValue)},
144 m_messageTopicId{MessageBroker::createTopic()}
148 StyleProperty(
const StyleProperty& other) :
149 m_defaultValue {other.m_defaultValue},
150 m_messageTopicId{MessageBroker::createTopic()},
151 m_globalValues {other.m_globalValues}
155 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
156 const std::uint64_t oldBaseIndex = other.m_propertyData & 0xFFFFFFFFFFFF0000;
157 const auto oldStoredStates =
static_cast<std::uint16_t
>(other.m_propertyData & 0xFFFF);
159 std::uint16_t total = 0;
160 std::uint8_t bitIndex = 0;
161 while (total < oldStoredStates)
163 if (oldStoredStates & (1 << bitIndex))
165 m_globalValues[baseIndex + bitIndex] = m_globalValues[oldBaseIndex + bitIndex];
166 total +=
static_cast<std::uint16_t
>(1 << bitIndex);
171 m_propertyData = baseIndex | oldStoredStates;
174 StyleProperty(StyleProperty&& other) noexcept :
175 m_defaultValue {std::move(other.m_defaultValue)},
176 m_propertyData {std::move(other.m_propertyData)},
177 m_messageTopicId{std::move(other.m_messageTopicId)},
178 m_globalValues {std::move(other.m_globalValues)}
180 other.m_messageTopicId = 0;
183 ~StyleProperty()
override
185 if (m_messageTopicId)
186 MessageBroker::destroyTopic(m_messageTopicId);
190 StyleProperty& operator=(
const StyleProperty& other)
194 StyleProperty temp(other);
195 std::swap(m_defaultValue, temp.m_defaultValue);
196 std::swap(m_propertyData, temp.m_propertyData);
197 std::swap(m_messageTopicId, temp.m_messageTopicId);
198 std::swap(m_globalValues, temp.m_globalValues);
204 StyleProperty& operator=(StyleProperty&& other)
noexcept
208 m_defaultValue = std::move(other.m_defaultValue);
209 m_propertyData = std::move(other.m_propertyData);
210 m_messageTopicId = std::move(other.m_messageTopicId);
211 m_globalValues = std::move(other.m_globalValues);
213 other.m_messageTopicId = 0;
219 StyleProperty& operator=(
const ValueType& value)
222 setValue(value, ComponentState::Normal);
226 void setValue(
const ValueType& value, ComponentState state = ComponentState::Normal)
228 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
229 m_propertyData |=
static_cast<std::uint64_t
>(1) <<
static_cast<std::uint8_t
>(state);
230 m_globalValues[baseIndex +
static_cast<std::uint8_t
>(state)] = value;
232 MessageBroker::sendEvent(m_messageTopicId);
235 void unsetValue(ComponentState state)
237 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
238 m_propertyData &= ~(
static_cast<std::uint64_t
>(1) <<
static_cast<std::uint8_t
>(state));
239 m_globalValues.erase(baseIndex +
static_cast<std::uint8_t
>(state));
241 MessageBroker::sendEvent(m_messageTopicId);
247 MessageBroker::sendEvent(m_messageTopicId);
250 TGUI_NODISCARD
const ValueType& getValue(ComponentState state = ComponentState::Normal)
const
252 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
253 const auto storedStates =
static_cast<std::uint16_t
>(m_propertyData & 0xFFFF);
256 if (storedStates == 0)
257 return m_defaultValue;
260 if (storedStates == 1)
261 return m_globalValues.at(baseIndex);
263 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Disabled))
265 if ((
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Active)) && (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::DisabledActive))))
266 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::DisabledActive));
267 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::Disabled)))
268 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Disabled));
271 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Active))
273 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Hover))
275 if ((
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Focused)) && (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::FocusedActiveHover))))
276 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::FocusedActiveHover));
277 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::ActiveHover)))
278 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::ActiveHover));
281 if ((
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Focused)) && (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::FocusedActive))))
282 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::FocusedActive));
283 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::Active)))
284 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Active));
287 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Hover))
289 if ((
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Focused)) && (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::FocusedHover))))
290 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::FocusedHover));
291 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::Hover)))
292 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Hover));
295 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Focused))
297 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::Focused)))
298 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Focused));
301 if (storedStates & 1)
305 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Normal));
311 return m_defaultValue;
314 TGUI_NODISCARD std::uint64_t connectCallback(std::function<
void()> func)
316 return MessageBroker::subscribe(m_messageTopicId, std::move(func));
319 void disconnectCallback(std::uint64_t
id)
321 MessageBroker::unsubscribe(
id);
326 void unsetValueImpl()
328 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
329 const auto storedStates =
static_cast<std::uint16_t
>(m_propertyData & 0xFFFF);
331 std::uint16_t total = 0;
332 std::uint8_t bitIndex = 0;
333 while (total < storedStates)
335 if (storedStates & (1 << bitIndex))
337 m_globalValues.erase(baseIndex + bitIndex);
338 total +=
static_cast<std::uint16_t
>(1 << bitIndex);
343 m_propertyData = baseIndex;
348 ValueType m_defaultValue;
352 std::uint64_t m_propertyData = 0;
355 std::uint64_t m_messageTopicId = 0;
365 std::unordered_map<std::uint64_t, ValueType> m_globalValues;
370 struct TGUI_API StylePropertyBackground
372 StyleProperty<Color> borderColor{Color::Black};
373 StyleProperty<Color> color{Color::White};
374 StyleProperty<Texture> texture;
375 StyleProperty<Outline> borders;
376 StyleProperty<Outline> padding;
378 float roundedBorderRadius = 0;
383 struct TGUI_API StylePropertyText
385 StyleProperty<Color> color{Color::Black};
386 StyleProperty<TextStyles> style;
391 class GroupComponent;
393 class TGUI_API Component
397 Component() =
default;
398 virtual ~Component() =
default;
400 Component(
const Component&);
401 Component& operator=(
const Component&);
403 Component(Component&&) =
default;
404 Component& operator=(Component&&) =
default;
406 void setPosition(Vector2f position);
408 TGUI_NODISCARD Vector2f getPosition()
const;
410 TGUI_NODISCARD Vector2f getSize()
const;
412 void setPositionAlignment(PositionAlignment alignment);
414 void setVisible(
bool visible);
416 TGUI_NODISCARD
bool isVisible()
const;
418 void setParent(GroupComponent* parent);
420 virtual void draw(BackendRenderTarget& target, RenderStates states)
const = 0;
422 virtual void updateLayout();
424 TGUI_NODISCARD
virtual std::shared_ptr<Component> clone()
const = 0;
427 friend void swap(Component& first, Component& second)
noexcept;
431 ComponentState m_state = ComponentState::Normal;
432 PositionAlignment m_positionAlignment = PositionAlignment::None;
435 bool m_visible =
true;
437 GroupComponent* m_parent =
nullptr;
440 class TGUI_API GroupComponent :
public Component
444 GroupComponent(
const GroupComponent&);
445 GroupComponent& operator=(
const GroupComponent&);
447 GroupComponent(GroupComponent&&) =
default;
448 GroupComponent& operator=(GroupComponent&&) =
default;
450 TGUI_NODISCARD Vector2f getClientSize()
const;
452 void addComponent(
const std::shared_ptr<Component>& component);
454 TGUI_NODISCARD
const std::vector<std::shared_ptr<Component>>& getComponents()
const;
456 void draw(BackendRenderTarget& target, RenderStates states)
const override;
458 void updateLayout()
override;
460 TGUI_NODISCARD std::shared_ptr<Component> clone()
const override;
462 friend void swap(GroupComponent& first, GroupComponent& second)
noexcept;
466 GroupComponent() =
default;
469 std::vector<std::shared_ptr<Component>> m_children;
470 Vector2f m_clientSize;
475 class TGUI_API BackgroundComponent :
public GroupComponent
479 explicit BackgroundComponent(StylePropertyBackground* backgroundStyle);
481 ~BackgroundComponent()
override;
483 BackgroundComponent(
const BackgroundComponent& other, StylePropertyBackground* backgroundStyle =
nullptr);
484 BackgroundComponent& operator=(
const BackgroundComponent& other);
488 void setSize(Vector2f size);
490 void setBorders(
const Outline& border);
492 TGUI_NODISCARD
const Outline& getBorders()
const;
494 void setPadding(
const Outline& padding);
496 TGUI_NODISCARD
const Outline& getPadding()
const;
498 void setOpacity(
float opacity);
500 void setComponentState(ComponentState state);
502 TGUI_NODISCARD
bool isTransparentPixel(Vector2f pos,
bool transparentTexture)
const;
504 void draw(BackendRenderTarget& target, RenderStates states)
const override;
506 TGUI_NODISCARD Vector2f getSizeWithoutBorders()
const;
508 void updateLayout()
override;
510 TGUI_NODISCARD std::shared_ptr<Component> clone()
const override;
520 StylePropertyBackground* m_backgroundStyle;
522 ColorRect m_background{Color::White, {}};
523 Color m_borderColor = Color::Black;
528 std::uint64_t m_borderColorCallbackId = 0;
529 std::uint64_t m_backgroundColorCallbackId = 0;
530 std::uint64_t m_textureCallbackId = 0;
531 std::uint64_t m_bordersCallbackId = 0;
532 std::uint64_t m_paddingCallbackId = 0;
537 class TGUI_API TextComponent :
public Component
541 explicit TextComponent(StylePropertyText* textStyle);
543 ~TextComponent()
override;
545 TextComponent(
const TextComponent& other, StylePropertyText* textStyle =
nullptr);
546 TextComponent& operator=(
const TextComponent& other);
550 void setString(
const String& caption);
552 TGUI_NODISCARD
const String& getString()
const;
554 void setCharacterSize(
unsigned int size);
556 TGUI_NODISCARD
unsigned int getCharacterSize()
const;
558 void setFont(
const Font& font);
560 TGUI_NODISCARD Font getFont()
const;
562 void setOutlineColor(Color color);
564 TGUI_NODISCARD Color getOutlineColor()
const;
566 void setOutlineThickness(
float thickness);
568 TGUI_NODISCARD
float getOutlineThickness()
const;
570 TGUI_NODISCARD
float getLineHeight()
const;
572 void setOpacity(
float opacity);
574 void updateLayout()
override;
576 void setComponentState(ComponentState state);
578 void draw(BackendRenderTarget& target, RenderStates states)
const override;
580 TGUI_NODISCARD std::shared_ptr<Component> clone()
const override;
584 StylePropertyText* m_textStyle;
586 Color m_color = Color::Black;
587 TextStyles m_style = TextStyle::Regular;
589 std::uint64_t m_colorCallbackId = 0;
590 std::uint64_t m_styleCallbackId = 0;
595 class TGUI_API ImageComponent :
public Component
599 explicit ImageComponent(StyleProperty<Texture>* textureStyle);
601 ~ImageComponent()
override;
603 ImageComponent(
const ImageComponent& other, StyleProperty<Texture>* textureStyle =
nullptr);
604 ImageComponent& operator=(
const ImageComponent& other);
608 void setSize(Vector2f size);
610 void setOpacity(
float opacity);
612 void setComponentState(ComponentState state);
614 TGUI_NODISCARD
bool isTransparentPixel(Vector2f pos,
bool transparentTexture)
const;
616 void draw(BackendRenderTarget& target, RenderStates states)
const override;
618 TGUI_NODISCARD std::shared_ptr<Component> clone()
const override;
622 StyleProperty<Texture>* m_textureStyle;
625 std::uint64_t m_textureCallbackId = 0;
630 TGUI_NODISCARD
inline ComponentState getStateFromFlags(
bool hover,
bool active,
bool focused =
false,
bool enabled =
true)
635 return ComponentState::DisabledActive;
637 return ComponentState::Disabled;
645 return ComponentState::FocusedActiveHover;
647 return ComponentState::FocusedActive;
651 return ComponentState::FocusedHover;
653 return ComponentState::Focused;
659 return ComponentState::ActiveHover;
661 return ComponentState::Active;
665 return ComponentState::Hover;
667 return ComponentState::Normal;
672 inline void setOptionalPropertyValue(StyleProperty<Color>& property,
const Color& color, ComponentState state)
675 property.setValue(color, state);
677 property.unsetValue(state);
682 inline void setOptionalPropertyValue(StyleProperty<TextStyles>& property,
const TextStyles& style, ComponentState state)
685 property.setValue(style, state);
687 property.unsetValue(state);
692 inline void setOptionalPropertyValue(StyleProperty<Texture>& property,
const Texture& texture, ComponentState state)
694 if (texture.getData())
695 property.setValue(texture, state);
697 property.unsetValue(state);
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36