MeshLib C++ Docs
Loading...
Searching...
No Matches
MRUITestEngine.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMesh/MRExpected.h"
4#include "MRViewer/exports.h"
5
6#include <cstdint>
7#include <limits>
8#include <map>
9#include <optional>
10#include <string_view>
11#include <string>
12#include <variant>
13#include <vector>
14
17
19{
20
21namespace detail
22{
23 template <typename T>
25 {
26 T value{};
27 T min{};
28 T max{};
29 };
30 template <>
31 struct BoundedValue<std::string>
32 {
33 std::string value;
34
35 std::optional<std::vector<std::string>> allowedValues;
36 };
37
38 template <typename T>
39 [[nodiscard]] MRVIEWER_API std::optional<T> createValueLow( std::string_view name, std::optional<BoundedValue<T>> value, bool consumeValueOverride = true );
40
41 extern template MRVIEWER_API std::optional<std::int64_t> createValueLow( std::string_view name, std::optional<BoundedValue<std::int64_t>> value, bool consumeValueOverride );
42 extern template MRVIEWER_API std::optional<std::uint64_t> createValueLow( std::string_view name, std::optional<BoundedValue<std::uint64_t>> value, bool consumeValueOverride );
43 extern template MRVIEWER_API std::optional<double> createValueLow( std::string_view name, std::optional<BoundedValue<double>> value, bool consumeValueOverride );
44 extern template MRVIEWER_API std::optional<std::string> createValueLow( std::string_view name, std::optional<BoundedValue<std::string>> value, bool consumeValueOverride );
45
46 template <typename T, typename = void> struct UnderlyingValueTypeHelper {};
47 template <typename T> struct UnderlyingValueTypeHelper<T, std::enable_if_t<std::is_floating_point_v<T>>> {using type = double;};
48 template <typename T> struct UnderlyingValueTypeHelper<T, std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>>> {using type = std::int64_t;};
49 template <typename T> struct UnderlyingValueTypeHelper<T, std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>>> {using type = std::uint64_t;};
50 template <> struct UnderlyingValueTypeHelper<std::string> {using type = std::string;};
51
52 template <typename T>
54}
55
58[[nodiscard]] MRVIEWER_API bool createButton( std::string_view name );
59
60template <typename T>
61concept AllowedValueType = std::is_arithmetic_v<T> || std::is_same_v<T, std::string>;
62
69template <AllowedValueType T>
70requires std::is_arithmetic_v<T>
71[[nodiscard]] std::optional<T> createValue( std::string_view name, T value, T min, T max, bool consumeValueOverride = true )
72{
73 if ( !( min < max ) )
74 {
75 min = std::numeric_limits<T>::lowest();
76 max = std::numeric_limits<T>::max();
77 }
78
80 static_assert(sizeof(T) <= sizeof(U), "The used type is too large.");
81
82 auto ret = detail::createValueLow<U>( name, detail::BoundedValue<U>{ .value = U( value ), .min = U( min ), .max = U( max ) }, consumeValueOverride );
83 return ret ? std::optional<T>( T( *ret ) ) : std::nullopt;
84}
86[[nodiscard]] MRVIEWER_API std::optional<std::string> createValue( std::string_view name, std::string value, bool consumeValueOverride = true, std::optional<std::vector<std::string>> allowedValues = std::nullopt );
87
92template <AllowedValueType T>
93[[nodiscard]] std::optional<T> createValueTentative( std::string_view name, bool consumeValueOverride = true )
94{
95 auto ret = detail::createValueLow<detail::UnderlyingValueType<T>>( name, std::nullopt, consumeValueOverride );
96 return ret ? std::optional<T>( T( *ret ) ) : std::nullopt;
97}
98
100MRVIEWER_API void pushTree( std::string_view name );
101MRVIEWER_API void popTree();
102
103struct Entry;
104
106{
108 mutable bool simulateClick = false;
109
110 static constexpr std::string_view kindName = "button";
111};
112
115{
116 template <typename T>
117 struct Value
118 {
120 T value = 0;
121
123 T min = 0;
124 T max = 0;
125
127 mutable std::optional<T> simulatedValue;
128
129 Value() {}
130 };
131 template <std::same_as<std::string> T>
132 struct Value<T>
133 {
135 std::string value;
136
137 std::optional<std::vector<std::string>> allowedValues;
138
140 mutable std::optional<std::string> simulatedValue;
141
142 Value() {}
143 };
144 using ValueVar = std::variant<Value<std::int64_t>, Value<std::uint64_t>, Value<double>, Value<std::string>>;
146
147 static constexpr std::string_view kindName = "value";
148};
149
151{
153 std::map<std::string, Entry, std::less<>> elems;
154
155 static constexpr std::string_view kindName = "group";
156};
157
158struct Entry
159{
160 std::variant<ButtonEntry, ValueEntry, GroupEntry> value;
161
164 bool visitedOnThisFrame = false;
165
167 [[nodiscard]] MRVIEWER_API std::string_view getKindName() const;
168
172 template <typename T>
173 [[nodiscard]] Expected<T *> getAs( std::string_view selfName = {} )
174 {
175 Expected<T *> ret = std::get_if<T>( &value );
176 if ( !*ret )
177 ret = unexpected_( selfName, T::kindName );
178 return ret;
179 }
180 template <typename T>
181 [[nodiscard]] Expected<const T *> getAs( std::string_view selfName = {} ) const
182 {
183 return const_cast<Entry *>( this )->template getAs<T>( selfName );
184 }
185
186private:
187 [[nodiscard]] MRVIEWER_API Unexpected<std::string> unexpected_( std::string_view selfName, std::string_view tKindName );
188};
189
191[[nodiscard]] MRVIEWER_API const GroupEntry& getRootEntry();
192
193}
Definition MRUITestEngine.h:61
tl::expected< T, E > Expected
Definition MRExpected.h:31
tl::unexpected< E > Unexpected
Definition MRExpected.h:34
MRVIEWER_API std::optional< T > createValueLow(std::string_view name, std::optional< BoundedValue< T > > value, bool consumeValueOverride=true)
typename UnderlyingValueTypeHelper< T >::type UnderlyingValueType
Definition MRUITestEngine.h:53
Definition MRUITestEngine.h:19
MRVIEWER_API bool createButton(std::string_view name)
std::optional< T > createValueTentative(std::string_view name, bool consumeValueOverride=true)
Definition MRUITestEngine.h:93
MRVIEWER_API const GroupEntry & getRootEntry()
Returns the current entry tree.
std::optional< T > createValue(std::string_view name, T value, T min, T max, bool consumeValueOverride=true)
Definition MRUITestEngine.h:71
MRVIEWER_API void popTree()
MRVIEWER_API void pushTree(std::string_view name)
Use those to group buttons into named groups.
Definition MRUITestEngine.h:106
bool simulateClick
Set this to true to simulate a button click.
Definition MRUITestEngine.h:108
static constexpr std::string_view kindName
Definition MRUITestEngine.h:110
Definition MRUITestEngine.h:159
std::variant< ButtonEntry, ValueEntry, GroupEntry > value
Definition MRUITestEngine.h:160
Expected< const T * > getAs(std::string_view selfName={}) const
Definition MRUITestEngine.h:181
MRVIEWER_API std::string_view getKindName() const
Returns a string describing the type currently stored in value, which is T::kindName.
bool visitedOnThisFrame
Definition MRUITestEngine.h:164
Expected< T * > getAs(std::string_view selfName={})
Definition MRUITestEngine.h:173
Definition MRUITestEngine.h:151
static constexpr std::string_view kindName
Definition MRUITestEngine.h:155
std::map< std::string, Entry, std::less<> > elems
Using std::map over std::unordered_map to be able to search by std::string_view keys directly.
Definition MRUITestEngine.h:153
Value()
Definition MRUITestEngine.h:142
std::optional< std::string > simulatedValue
Set to override the value.
Definition MRUITestEngine.h:140
std::string value
The current value.
Definition MRUITestEngine.h:135
std::optional< std::vector< std::string > > allowedValues
Definition MRUITestEngine.h:137
Definition MRUITestEngine.h:118
Value()
Definition MRUITestEngine.h:129
T max
Definition MRUITestEngine.h:124
T value
The current value.
Definition MRUITestEngine.h:120
T min
Min/max bounds, INCLUSIVE. If none, those are set to the min/max values representable in this type.
Definition MRUITestEngine.h:123
std::optional< T > simulatedValue
Set to override the value.
Definition MRUITestEngine.h:127
For sliders, drags, etc.
Definition MRUITestEngine.h:115
std::variant< Value< std::int64_t >, Value< std::uint64_t >, Value< double >, Value< std::string > > ValueVar
Definition MRUITestEngine.h:144
ValueVar value
Definition MRUITestEngine.h:145
static constexpr std::string_view kindName
Definition MRUITestEngine.h:147
Definition MRUITestEngine.h:25
T min
Definition MRUITestEngine.h:27
T max
Definition MRUITestEngine.h:28
T value
Definition MRUITestEngine.h:26