Skip to main content

TimeFormatterComponent Class

Component that formats elapsed or remaining time into a display string. More...

Declaration

class helios::engine::modules::ui::layout::components::TimeFormatterComponent { ... }

Public Member Functions Index

voidsetFormat (std::string format, const TimeDisplayMode displayMode=TimeDisplayMode::Elapsed)

Sets the format string and display mode. More...

std::stringformat (const float elapsed, const float duration=0) const

Formats the given time values into a display string. More...

voidsetElapsedLabel (std::string label)

Sets the label shown in place of "00:00" when remaining time reaches zero. More...

std::stringelapsedLabel ()

Returns the elapsed label. More...

boolhasElapsedLabel () const

Returns whether an elapsed label is configured. More...

voidsetHideWhenZero (const bool hideWhenZero) noexcept

Sets whether to hide the output when remaining time reaches zero. More...

boolhideWhenZero () const

Returns whether the output is hidden at zero remaining time. More...

Private Member Attributes Index

std::stringformat_

The format string used by std::vformat (expects minutes, seconds). More...

std::stringelapsedLabel_

Label displayed instead of "00:00" when remaining time reaches zero. More...

boolhasElapsedLabel_ = false

True if an elapsed label has been configured. More...

boolhideWhenZero_ = false

True if the output should be empty when remaining time reaches zero. More...

TimeDisplayModedisplayMode_ = TimeDisplayMode::Elapsed

The active display mode (elapsed or remaining). More...

Description

Component that formats elapsed or remaining time into a display string.

Holds a format string compatible with std::vformat and a TimeDisplayMode. The format string receives two integer arguments: minutes and seconds.

See Also

TimeDisplayMode

See Also

GameTimer2UiTextUpdateSystem

Definition at line 27 of file TimeFormatterComponent.ixx.

Public Member Functions

elapsedLabel()

std::string helios::engine::modules::ui::layout::components::TimeFormatterComponent::elapsedLabel ()
inline

Returns the elapsed label.

Returns

The configured elapsed label string.

Definition at line 134 of file TimeFormatterComponent.ixx.

134 std::string elapsedLabel() {
135 return elapsedLabel_;
136 }

format()

std::string helios::engine::modules::ui::layout::components::TimeFormatterComponent::format (const float elapsed, const float duration=0)
inline

Formats the given time values into a display string.

If duration is zero, the display mode falls back to Elapsed.

Parameters
elapsed

Elapsed time in seconds.

duration

Total duration in seconds. Zero disables Remaining mode.

Returns

The formatted time string.

Definition at line 81 of file TimeFormatterComponent.ixx.

81 std::string format(const float elapsed, const float duration = 0) const {
82
83 const auto effectiveDisplayMode = duration == 0 ? TimeDisplayMode::Elapsed : displayMode_;
84
85
86 switch (effectiveDisplayMode) {
88 const auto minutes = static_cast<unsigned int>(std::max(0.0f, elapsed)) / 60;
89 const auto seconds = static_cast<unsigned int>(std::max(0.0f, elapsed)) % 60;
90 return std::vformat(format_, std::make_format_args(minutes, seconds));
91 }
92
94
95 const auto minutes = static_cast<unsigned int>(std::max(0.0f, duration - elapsed)) / 60;
96 const auto seconds = static_cast<unsigned int>(std::max(0.0f, duration - elapsed)) % 60;
97
98 if (hideWhenZero_ && std::max(0.0f, duration - elapsed) <= 0.0f) {
99 return "";
100 }
101
102 if (hasElapsedLabel_ && std::max(1.0f, duration - elapsed) <= 1.0f) {
103 return elapsedLabel_;
104 }
105
106
107 return std::vformat(format_, std::make_format_args(minutes, seconds));
108
109 }
110 }
111
112 std::unreachable();
113 }

References helios::engine::modules::ui::layout::types::Elapsed and helios::engine::modules::ui::layout::types::Remaining.

Referenced by setFormat.

hasElapsedLabel()

bool helios::engine::modules::ui::layout::components::TimeFormatterComponent::hasElapsedLabel ()
inline

Returns whether an elapsed label is configured.

Returns

True if an elapsed label has been set.

Definition at line 143 of file TimeFormatterComponent.ixx.

143 bool hasElapsedLabel() const {
144 return hasElapsedLabel_;
145 }

hideWhenZero()

bool helios::engine::modules::ui::layout::components::TimeFormatterComponent::hideWhenZero ()
inline

Returns whether the output is hidden at zero remaining time.

Returns

True if hidden when zero.

Definition at line 161 of file TimeFormatterComponent.ixx.

161 bool hideWhenZero() const {
162 return hideWhenZero_;
163 }

Referenced by setHideWhenZero.

setElapsedLabel()

void helios::engine::modules::ui::layout::components::TimeFormatterComponent::setElapsedLabel (std::string label)
inline

Sets the label shown in place of "00:00" when remaining time reaches zero.

In Remaining mode, once the remaining time drops to one second or below, this label is returned by format() instead of the numeric output. Implicitly enables the elapsed label.

Parameters
label

The label string to display (e.g. "TIME UP").

Definition at line 124 of file TimeFormatterComponent.ixx.

124 void setElapsedLabel(std::string label) {
125 elapsedLabel_ = std::move(label);
126 hasElapsedLabel_ = true;
127 }

setFormat()

void helios::engine::modules::ui::layout::components::TimeFormatterComponent::setFormat (std::string format, const TimeDisplayMode displayMode=TimeDisplayMode::Elapsed)
inline

Sets the format string and display mode.

Parameters
format

A std::vformat-compatible string (e.g. "{:02d}:{:02d}").

displayMode

The display mode to use. Defaults to Elapsed.

Definition at line 66 of file TimeFormatterComponent.ixx.

66 void setFormat(std::string format, const TimeDisplayMode displayMode = TimeDisplayMode::Elapsed) {
67 format_ = std::move(format);
68 displayMode_ = displayMode;
69 }

References helios::engine::modules::ui::layout::types::Elapsed and format.

Referenced by helios::engine::builder::gameObject::builders::configs::TextRenderableConfig::build.

setHideWhenZero()

void helios::engine::modules::ui::layout::components::TimeFormatterComponent::setHideWhenZero (const bool hideWhenZero)
inline noexcept

Sets whether to hide the output when remaining time reaches zero.

Parameters
hideWhenZero

True to return an empty string at zero remaining time.

Definition at line 152 of file TimeFormatterComponent.ixx.

152 void setHideWhenZero(const bool hideWhenZero) noexcept {
153 hideWhenZero_ = hideWhenZero;
154 }

Reference hideWhenZero.

Private Member Attributes

displayMode_

TimeDisplayMode helios::engine::modules::ui::layout::components::TimeFormatterComponent::displayMode_ = TimeDisplayMode::Elapsed
mutable

The active display mode (elapsed or remaining).

Declared mutable because format() may fall back to Elapsed when duration is zero.

Definition at line 55 of file TimeFormatterComponent.ixx.

55 mutable TimeDisplayMode displayMode_ = TimeDisplayMode::Elapsed;

elapsedLabel_

std::string helios::engine::modules::ui::layout::components::TimeFormatterComponent::elapsedLabel_

Label displayed instead of "00:00" when remaining time reaches zero.

Definition at line 37 of file TimeFormatterComponent.ixx.

37 std::string elapsedLabel_;

format_

std::string helios::engine::modules::ui::layout::components::TimeFormatterComponent::format_

The format string used by std::vformat (expects minutes, seconds).

Definition at line 32 of file TimeFormatterComponent.ixx.

32 std::string format_;

hasElapsedLabel_

bool helios::engine::modules::ui::layout::components::TimeFormatterComponent::hasElapsedLabel_ = false

True if an elapsed label has been configured.

Definition at line 42 of file TimeFormatterComponent.ixx.

42 bool hasElapsedLabel_ = false;

hideWhenZero_

bool helios::engine::modules::ui::layout::components::TimeFormatterComponent::hideWhenZero_ = false

True if the output should be empty when remaining time reaches zero.

Definition at line 47 of file TimeFormatterComponent.ixx.

47 bool hideWhenZero_ = false;

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.