26#ifndef TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
27#define TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
29#include <TGUI/Global.hpp>
30#include <TGUI/String.hpp>
32#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <type_traits>
38TGUI_MODULE_EXPORT
namespace tgui
60 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
63 m_value {static_cast<float>(constant)}
86 if (!expression.empty() && (expression.back() ==
'%'))
89 m_ratio = expression.substr(0, expression.length()-1).
toFloat() / 100.f;
94 m_value = expression.substr(0, expression.length()).
toFloat();
138 constexpr void updateParentSize(
float newParentSize)
142 m_parentValue = newParentSize;
143 m_value = m_ratio * newParentSize;
154 TGUI_NODISCARD String toString()
const
157 return String::fromNumber(m_value);
159 return String::fromNumber(m_ratio * 100) +
'%';
166 bool m_constant =
true;
169 float m_parentValue = 0;
Class to store the a value that is either a constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:46
TGUI_NODISCARD constexpr float getValue() const
Returns the value.
Definition AbsoluteOrRelativeValue.hpp:104
TGUI_NODISCARD constexpr float getRatio() const
Returns the stored ratio.
Definition AbsoluteOrRelativeValue.hpp:115
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:73
constexpr AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition AbsoluteOrRelativeValue.hpp:61
TGUI_NODISCARD constexpr bool isConstant() const
Returns whether the value is constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:126
AbsoluteOrRelativeValue(const String &expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:84
constexpr AbsoluteOrRelativeValue()=default
Default constructor.
Wrapper class to store strings.
Definition String.hpp:101
TGUI_NODISCARD float toFloat(float defaultValue=0) const
Converts the string to a float.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition AbsoluteOrRelativeValue.hpp:177