26#ifndef TGUI_COMPONENTS_HPP
27#define TGUI_COMPONENTS_HPP
29#include <TGUI/Text.hpp>
30#include <TGUI/Sprite.hpp>
31#include <TGUI/Texture.hpp>
32#include <TGUI/Outline.hpp>
34#include <unordered_map>
42 class BackendRenderTargetBase;
51 enum class ComponentState : std::uint8_t
60 FocusedActiveHover = 7,
68 enum class AlignLayout
83 enum class PositionAlignment
99 class TGUI_API MessageBroker
103 static std::uint64_t createTopic();
105 static void destroyTopic(std::uint64_t topicId);
107 static std::uint64_t subscribe(std::uint64_t topicId, std::function<
void()> func);
109 static void unsubscribe(std::uint64_t callbackId);
111 static void sendEvent(std::uint64_t topicId);
114 static std::unordered_map<std::uint64_t, std::set<std::uint64_t>> m_topicIdToCallbackIds;
115 static std::unordered_map<std::uint64_t, std::uint64_t> m_callbackIdToTopicId;
116 static std::unordered_map<std::uint64_t, std::function<void()>> m_listeners;
119 static std::uint64_t m_lastId;
124 class TGUI_API StylePropertyBase
127 virtual ~StylePropertyBase() =
default;
132 template <
typename ValueType>
133 class TGUI_API StyleProperty :
public StylePropertyBase
140 m_messageTopicId{MessageBroker::createTopic()}
144 explicit StyleProperty(
const ValueType& defaultValue) :
145 m_defaultValue {defaultValue},
147 m_messageTopicId{MessageBroker::createTopic()}
151 StyleProperty(
const StyleProperty& other) :
152 m_defaultValue {other.m_defaultValue},
154 m_messageTopicId{MessageBroker::createTopic()},
155 m_globalValues {other.m_globalValues}
159 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
160 const std::uint64_t oldBaseIndex = other.m_propertyData & 0xFFFFFFFFFFFF0000;
161 const std::uint16_t oldStoredStates =
static_cast<std::uint16_t
>(other.m_propertyData & 0xFFFF);
163 std::uint16_t total = 0;
164 std::uint8_t bitIndex = 0;
165 while (total < oldStoredStates)
167 if (oldStoredStates & (1 << bitIndex))
169 m_globalValues[baseIndex + bitIndex] = m_globalValues[oldBaseIndex + bitIndex];
170 total += (1 << bitIndex);
175 m_propertyData = baseIndex | oldStoredStates;
178 StyleProperty(StyleProperty&& other) :
179 m_defaultValue {std::move(other.m_defaultValue)},
180 m_propertyData {std::move(other.m_propertyData)},
181 m_messageTopicId{std::move(other.m_messageTopicId)},
182 m_globalValues {std::move(other.m_globalValues)}
184 other.m_messageTopicId = 0;
189 if (m_messageTopicId)
190 MessageBroker::destroyTopic(m_messageTopicId);
194 StyleProperty& operator=(
const StyleProperty& other)
198 StyleProperty temp(other);
199 std::swap(m_defaultValue, temp.m_defaultValue);
200 std::swap(m_propertyData, temp.m_propertyData);
201 std::swap(m_messageTopicId, temp.m_messageTopicId);
202 std::swap(m_globalValues, temp.m_globalValues);
208 StyleProperty& operator=(StyleProperty&& other)
212 m_defaultValue = std::move(other.m_defaultValue);
213 m_propertyData = std::move(other.m_propertyData);
214 m_messageTopicId = std::move(other.m_messageTopicId);
215 m_globalValues = std::move(other.m_globalValues);
217 other.m_messageTopicId = 0;
223 StyleProperty& operator=(
const ValueType& value)
226 setValue(value, ComponentState::Normal);
230 void setValue(
const ValueType& value, ComponentState state = ComponentState::Normal)
232 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
233 m_propertyData |=
static_cast<std::uint64_t
>(1) <<
static_cast<std::uint8_t
>(state);
234 m_globalValues[baseIndex +
static_cast<std::uint8_t
>(state)] = value;
236 MessageBroker::sendEvent(m_messageTopicId);
239 void unsetValue(ComponentState state)
241 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
242 m_propertyData &= ~(1 <<
static_cast<std::uint8_t
>(state));
243 m_globalValues.erase(baseIndex +
static_cast<std::uint8_t
>(state));
245 MessageBroker::sendEvent(m_messageTopicId);
251 MessageBroker::sendEvent(m_messageTopicId);
254 const ValueType& getValue(ComponentState state = ComponentState::Normal)
const
256 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
257 const std::uint16_t storedStates =
static_cast<std::uint16_t
>(m_propertyData & 0xFFFF);
260 if (storedStates == 0)
261 return m_defaultValue;
264 if (storedStates == 1)
265 return m_globalValues.at(baseIndex);
267 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Disabled))
269 if ((
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Active)) && (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::DisabledActive))))
270 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::DisabledActive));
271 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::Disabled)))
272 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Disabled));
275 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Active))
277 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Hover))
279 if ((
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Focused)) && (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::FocusedActiveHover))))
280 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::FocusedActiveHover));
281 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::ActiveHover)))
282 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::ActiveHover));
285 if ((
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Focused)) && (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::FocusedActive))))
286 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::FocusedActive));
287 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::Active)))
288 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Active));
291 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Hover))
293 if ((
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Focused)) && (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::FocusedHover))))
294 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::FocusedHover));
295 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::Hover)))
296 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Hover));
299 if (
static_cast<std::uint8_t
>(state) &
static_cast<std::uint8_t
>(ComponentState::Focused))
301 if (storedStates & (1 <<
static_cast<std::uint8_t
>(ComponentState::Focused)))
302 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Focused));
305 if (storedStates & 1)
309 return m_globalValues.at(baseIndex +
static_cast<std::uint8_t
>(ComponentState::Normal));
315 return m_defaultValue;
319 std::uint64_t connectCallback(std::function<
void()> func)
321 return MessageBroker::subscribe(m_messageTopicId, std::move(func));
324 void disconnectCallback(std::uint64_t
id)
326 return MessageBroker::unsubscribe(
id);
331 void unsetValueImpl()
333 const std::uint64_t baseIndex = m_propertyData & 0xFFFFFFFFFFFF0000;
334 const std::uint16_t storedStates =
static_cast<std::uint16_t
>(m_propertyData & 0xFFFF);
336 std::uint16_t total = 0;
337 std::uint8_t bitIndex = 0;
338 while (total < storedStates)
340 if (storedStates & (1 << bitIndex))
342 m_globalValues.erase(baseIndex + bitIndex);
343 total += (1 << bitIndex);
348 m_propertyData = baseIndex;
353 ValueType m_defaultValue;
357 std::uint64_t m_propertyData = 0;
360 std::uint64_t m_messageTopicId = 0;
370 std::unordered_map<std::uint64_t, ValueType> m_globalValues;
375 struct TGUI_API StylePropertyBackground
377 StyleProperty<Color> borderColor{Color::Black};
378 StyleProperty<Color> color{Color::White};
379 StyleProperty<Texture> texture;
380 StyleProperty<Outline> borders;
381 StyleProperty<Outline> padding;
383 float roundedBorderRadius = 0;
388 struct TGUI_API StylePropertyText
390 StyleProperty<Color> color{Color::Black};
391 StyleProperty<TextStyles> style;
396 class GroupComponent;
398 class TGUI_API Component
402 Component(
const Component&);
403 Component& operator=(
const Component&);
405 Component(Component&&) =
default;
406 Component& operator=(Component&&) =
default;
408 void setPosition(Vector2f position);
410 Vector2f getPosition()
const;
412 Vector2f getSize()
const;
414 void setPositionAlignment(PositionAlignment alignment);
416 void setVisible(
bool visible);
418 bool isVisible()
const;
420 void setParent(GroupComponent* parent);
422 virtual void draw(BackendRenderTargetBase& target, RenderStates states)
const = 0;
424 virtual void updateLayout();
426 virtual std::shared_ptr<Component> clone()
const = 0;
430 Component() =
default;
432 virtual ~Component() =
default;
434 friend void swap(Component& first, Component& second);
438 ComponentState m_state = ComponentState::Normal;
439 PositionAlignment m_positionAlignment = PositionAlignment::None;
442 bool m_visible =
true;
444 GroupComponent* m_parent =
nullptr;
447 class TGUI_API GroupComponent :
public Component
451 GroupComponent(
const GroupComponent&);
452 GroupComponent& operator=(
const GroupComponent&);
454 GroupComponent(GroupComponent&&) =
default;
455 GroupComponent& operator=(GroupComponent&&) =
default;
457 Vector2f getClientSize()
const;
459 void addComponent(
const std::shared_ptr<Component>& component);
461 const std::vector<std::shared_ptr<Component>>& getComponents()
const;
463 void draw(BackendRenderTargetBase& target, RenderStates states)
const override;
465 void updateLayout()
override;
467 std::shared_ptr<Component> clone()
const override;
469 friend void swap(GroupComponent& first, GroupComponent& second);
473 GroupComponent() =
default;
476 std::vector<std::shared_ptr<Component>> m_children;
477 Vector2f m_clientSize;
482 class TGUI_API BackgroundComponent :
public GroupComponent
486 BackgroundComponent(StylePropertyBackground* backgroundStyle);
488 ~BackgroundComponent();
490 BackgroundComponent(
const BackgroundComponent& other, StylePropertyBackground* backgroundStyle =
nullptr);
491 BackgroundComponent& operator=(
const BackgroundComponent& other);
495 void setSize(Vector2f size);
497 void setBorders(
const Outline& border);
499 const Outline& getBorders()
const;
501 void setPadding(
const Outline& padding);
503 const Outline& getPadding()
const;
505 void setOpacity(
float opacity);
507 void setComponentState(ComponentState state);
509 bool isTransparentPixel(Vector2f pos,
bool transparentTexture)
const;
511 void draw(BackendRenderTargetBase& target, RenderStates states)
const override;
513 Vector2f getSizeWithoutBorders()
const;
515 void updateLayout()
override;
517 std::shared_ptr<Component> clone()
const override;
527 StylePropertyBackground* m_backgroundStyle;
529 ColorRect m_background{Color::White, {}};
530 Color m_borderColor = Color::Black;
535 std::uint64_t m_borderColorCallbackId = 0;
536 std::uint64_t m_backgroundColorCallbackId = 0;
537 std::uint64_t m_textureCallbackId = 0;
538 std::uint64_t m_bordersCallbackId = 0;
539 std::uint64_t m_paddingCallbackId = 0;
544 class TGUI_API TextComponent :
public Component
548 TextComponent(StylePropertyText* textStyle);
552 TextComponent(
const TextComponent& other, StylePropertyText* textStyle =
nullptr);
553 TextComponent& operator=(
const TextComponent& other);
557 void setString(
const String& caption);
559 const String& getString()
const;
561 void setCharacterSize(
unsigned int size);
563 unsigned int getCharacterSize()
const;
565 void setFont(Font font);
567 Font getFont()
const;
569 void setOutlineColor(Color color);
571 Color getOutlineColor()
const;
573 void setOutlineThickness(
float thickness);
575 float getOutlineThickness()
const;
577 float getLineHeight()
const;
579 void setOpacity(
float opacity);
581 void updateLayout()
override;
583 void setComponentState(ComponentState state);
585 void draw(BackendRenderTargetBase& target, RenderStates states)
const override;
587 std::shared_ptr<Component> clone()
const override;
591 StylePropertyText* m_textStyle;
593 Color m_color = Color::Black;
594 TextStyles m_style = TextStyle::Regular;
596 std::uint64_t m_colorCallbackId = 0;
597 std::uint64_t m_styleCallbackId = 0;
602 class TGUI_API ImageComponent :
public Component
606 ImageComponent(StyleProperty<Texture>* textureStyle);
610 ImageComponent(
const ImageComponent& other, StyleProperty<Texture>* textureStyle =
nullptr);
611 ImageComponent& operator=(
const ImageComponent& other);
615 void setSize(Vector2f size);
617 void setOpacity(
float opacity);
619 void setComponentState(ComponentState state);
621 bool isTransparentPixel(Vector2f pos,
bool transparentTexture)
const;
623 void draw(BackendRenderTargetBase& target, RenderStates states)
const override;
625 std::shared_ptr<Component> clone()
const override;
629 StyleProperty<Texture>* m_textureStyle;
632 std::uint64_t m_textureCallbackId = 0;
637 inline ComponentState getStateFromFlags(
bool hover,
bool active,
bool focused =
false,
bool enabled =
true)
642 return ComponentState::DisabledActive;
644 return ComponentState::Disabled;
651 return ComponentState::FocusedActiveHover;
653 return ComponentState::FocusedActive;
656 return ComponentState::FocusedHover;
658 return ComponentState::Focused;
663 return ComponentState::ActiveHover;
665 return ComponentState::Active;
668 return ComponentState::Hover;
670 return ComponentState::Normal;
675 inline void setOptionalPropertyValue(StyleProperty<Color>& property,
const Color& color, ComponentState state)
678 property.setValue(color, state);
680 property.unsetValue(state);
685 inline void setOptionalPropertyValue(StyleProperty<TextStyles>& property,
const TextStyles& style, ComponentState state)
688 property.setValue(style, state);
690 property.unsetValue(state);
695 inline void setOptionalPropertyValue(StyleProperty<Texture>& property,
const Texture& texture, ComponentState state)
697 if (texture.getData())
698 property.setValue(texture, state);
700 property.unsetValue(state);
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36