26#ifndef TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
27#define TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
29#include <TGUI/Global.hpp>
30#include <TGUI/to_string.hpp>
61 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
64 m_value {static_cast<float>(constant)}
87 if (!expression.empty() && (expression.back() ==
'%'))
90 m_ratio =
strToFloat(expression.substr(0, expression.length()-1)) / 100.f;
95 m_value =
strToFloat(expression.substr(0, expression.length()));
139 void updateParentSize(
float newParentSize)
143 m_parentValue = newParentSize;
144 m_value = m_ratio * newParentSize;
155 std::string toString()
const
158 return to_string(m_value);
160 return to_string(m_ratio * 100) +
'%';
167 bool m_constant =
true;
170 float m_parentValue = 0;
Class to store the a value that is either a constant or a ratio.
Definition: AbsoluteOrRelativeValue.hpp:44
TGUI_CONSTEXPR float getValue() const
Returns the value.
Definition: AbsoluteOrRelativeValue.hpp:105
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition: AbsoluteOrRelativeValue.hpp:74
TGUI_CONSTEXPR float getRatio() const
Returns the stored ratio.
Definition: AbsoluteOrRelativeValue.hpp:116
TGUI_CONSTEXPR bool isConstant() const
Returns whether the value is constant or a ratio.
Definition: AbsoluteOrRelativeValue.hpp:127
TGUI_CONSTEXPR AbsoluteOrRelativeValue()
Default constructor.
Definition: AbsoluteOrRelativeValue.hpp:50
AbsoluteOrRelativeValue(const std::string &expression)
Construct the value from a string that either contains a value or a percentage.
Definition: AbsoluteOrRelativeValue.hpp:85
TGUI_CONSTEXPR AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition: AbsoluteOrRelativeValue.hpp:62
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
TGUI_API float strToFloat(const std::string &str, float defaultValue=0)
Converts a string to a float.
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition: AbsoluteOrRelativeValue.hpp:178