25#ifndef TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
26#define TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
28#include <TGUI/Global.hpp>
29#include <TGUI/String.hpp>
56 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
59 m_value {static_cast<float>(constant)}
80 if (!expression.empty() && (expression.back() ==
'%'))
83 m_ratio = expression.substr(0, expression.length()-1).
toFloat() / 100.f;
88 m_value = expression.substr(0, expression.length()).
toFloat();
97 TGUI_NODISCARD
constexpr float getValue()
const
128 constexpr void updateParentSize(
float newParentSize)
132 m_parentValue = newParentSize;
133 m_value = m_ratio * newParentSize;
143 TGUI_NODISCARD String toString()
const
146 return String::fromNumber(m_value);
148 return String::fromNumber(m_ratio * 100) +
'%';
154 bool m_constant =
true;
157 float m_parentValue = 0;
180 inline namespace literals
182 inline namespace percent
184 constexpr RelativeValue
operator""_percent(
long double n)
186 return RelativeValue{
static_cast<float>(n / 100.0L)};
189 constexpr RelativeValue
operator""_percent(
unsigned long long n)
191 return RelativeValue{
static_cast<float>(n) / 100.f};
constexpr float getRatio() const
Returns the stored ratio.
Definition AbsoluteOrRelativeValue.hpp:107
constexpr bool isConstant() const
Returns whether the value is constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:117
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:68
constexpr AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition AbsoluteOrRelativeValue.hpp:57
AbsoluteOrRelativeValue(const String &expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:78
constexpr float getValue() const
Returns the value.
Definition AbsoluteOrRelativeValue.hpp:97
constexpr AbsoluteOrRelativeValue()=default
Default constructor.
Wrapper class to store strings.
Definition String.hpp:94
float toFloat(float defaultValue=0) const
Converts the string to a float.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
constexpr RelativeValue(float ratio)
Default constructor that sets the given ratio.
Definition AbsoluteOrRelativeValue.hpp:169