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>
31#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <type_traits>
37TGUI_MODULE_EXPORT
namespace tgui
58 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
61 m_value {static_cast<float>(constant)}
82 if (!expression.empty() && (expression.back() ==
'%'))
85 m_ratio = expression.substr(0, expression.length()-1).
toFloat() / 100.f;
90 m_value = expression.substr(0, expression.length()).
toFloat();
99 TGUI_NODISCARD
constexpr float getValue()
const
130 constexpr void updateParentSize(
float newParentSize)
134 m_parentValue = newParentSize;
135 m_value = m_ratio * newParentSize;
145 TGUI_NODISCARD String toString()
const
148 return String::fromNumber(m_value);
150 return String::fromNumber(m_ratio * 100) +
'%';
156 bool m_constant =
true;
159 float m_parentValue = 0;
Class to store the a value that is either a constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:45
constexpr float getRatio() const
Returns the stored ratio.
Definition AbsoluteOrRelativeValue.hpp:109
constexpr bool isConstant() const
Returns whether the value is constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:119
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:70
constexpr AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition AbsoluteOrRelativeValue.hpp:59
AbsoluteOrRelativeValue(const String &expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:80
constexpr float getValue() const
Returns the value.
Definition AbsoluteOrRelativeValue.hpp:99
constexpr AbsoluteOrRelativeValue()=default
Default constructor.
Wrapper class to store strings.
Definition String.hpp:96
float toFloat(float defaultValue=0) const
Converts the string to a float.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition AbsoluteOrRelativeValue.hpp:166
constexpr RelativeValue(float ratio)
Default constructor that sets the given ratio.
Definition AbsoluteOrRelativeValue.hpp:171