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
71template <AllowedValueType T>
72requires std::is_arithmetic_v<T>
73[[nodiscard]] std::optional<T> createValue( std::string_view name, T value, T min, T max, bool consumeValueOverride = true )
74{
75 if ( !( min < max ) )
76 {
77 min = std::numeric_limits<T>::lowest();
78 max = std::numeric_limits<T>::max();
79 }
80
82 static_assert(sizeof(T) <= sizeof(U), "The used type is too large.");
83
84 auto ret = detail::createValueLow<U>( name, detail::BoundedValue<U>{ .value = U( value ), .min = U( min ), .max = U( max ) }, consumeValueOverride );
85 return ret ? std::optional<T>( T( *ret ) ) : std::nullopt;
86}
87
88[[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 );
89
94template <AllowedValueType T>
95[[nodiscard]] std::optional<T> createValueTentative( std::string_view name, bool consumeValueOverride = true )
96{
97 auto ret = detail::createValueLow<detail::UnderlyingValueType<T>>( name, std::nullopt, consumeValueOverride );
98 return ret ? std::optional<T>( T( *ret ) ) : std::nullopt;
99}
100
102MRVIEWER_API void pushTree( std::string_view name );
103MRVIEWER_API void popTree();
104
105struct Entry;
106
108{
110 mutable bool simulateClick = false;
111
112 static constexpr std::string_view kindName = "button";
113};
114
117{
118 template <typename T>
119 struct Value
120 {
122 T value = 0;
123
125 T min = 0;
126 T max = 0;
127
129 mutable std::optional<T> simulatedValue;
130
131 Value() {}
132 };
133 template <std::same_as<std::string> T>
134 struct Value<T>
135 {
137 std::string value;
138
139 std::optional<std::vector<std::string>> allowedValues;
140
142 mutable std::optional<std::string> simulatedValue;
143
144 Value() {}
145 };
146 using ValueVar = std::variant<Value<std::int64_t>, Value<std::uint64_t>, Value<double>, Value<std::string>>;
148
149 static constexpr std::string_view kindName = "value";
150};
151
153{
155 std::map<std::string, Entry, std::less<>> elems;
156
157 static constexpr std::string_view kindName = "group";
158};
159
160struct Entry
161{
162 std::variant<ButtonEntry, ValueEntry, GroupEntry> value;
163
166 bool visitedOnThisFrame = false;
167
169 [[nodiscard]] MRVIEWER_API std::string_view getKindName() const;
170
174 template <typename T>
175 [[nodiscard]] Expected<T *> getAs( std::string_view selfName = {} )
176 {
177 Expected<T *> ret = std::get_if<T>( &value );
178 if ( !*ret )
179 ret = unexpected_( selfName, T::kindName );
180 return ret;
181 }
182 template <typename T>
183 [[nodiscard]] Expected<const T *> getAs( std::string_view selfName = {} ) const
184 {
185 return const_cast<Entry *>( this )->template getAs<T>( selfName );
186 }
187
188private:
189 [[nodiscard]] MRVIEWER_API Unexpected<std::string> unexpected_( std::string_view selfName, std::string_view tKindName );
190};
191
193[[nodiscard]] MRVIEWER_API const GroupEntry& getRootEntry();
194
195}
Definition MRUITestEngine.h:61
tl::expected< T, E > Expected
Definition MRExpected.h:31
tl::unexpected< E > Unexpected
Definition MRExpected.h:34
Definition MRUITestEngine.h:22
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
const GroupEntry & getRootEntry()
Returns the current entry tree.
bool createButton(std::string_view name)
std::optional< T > createValueTentative(std::string_view name, bool consumeValueOverride=true)
Definition MRUITestEngine.h:95
void pushTree(std::string_view name)
Use those to group buttons into named groups.
std::optional< T > createValue(std::string_view name, T value, T min, T max, bool consumeValueOverride=true)
Definition MRUITestEngine.h:73
Definition MRUITestEngine.h:108
bool simulateClick
Set this to true to simulate a button click.
Definition MRUITestEngine.h:110
static constexpr std::string_view kindName
Definition MRUITestEngine.h:112
Definition MRUITestEngine.h:161
std::variant< ButtonEntry, ValueEntry, GroupEntry > value
Definition MRUITestEngine.h:162
std::string_view getKindName() const
Returns a string describing the type currently stored in value, which is T::kindName.
Expected< const T * > getAs(std::string_view selfName={}) const
Definition MRUITestEngine.h:183
bool visitedOnThisFrame
Definition MRUITestEngine.h:166
Expected< T * > getAs(std::string_view selfName={})
Definition MRUITestEngine.h:175
Definition MRUITestEngine.h:153
static constexpr std::string_view kindName
Definition MRUITestEngine.h:157
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:155
Value()
Definition MRUITestEngine.h:144
std::optional< std::string > simulatedValue
Set to override the value.
Definition MRUITestEngine.h:142
std::string value
The current value.
Definition MRUITestEngine.h:137
std::optional< std::vector< std::string > > allowedValues
Definition MRUITestEngine.h:139
Definition MRUITestEngine.h:120
Value()
Definition MRUITestEngine.h:131
T max
Definition MRUITestEngine.h:126
T value
The current value.
Definition MRUITestEngine.h:122
T min
Min/max bounds, INCLUSIVE. If none, those are set to the min/max values representable in this type.
Definition MRUITestEngine.h:125
std::optional< T > simulatedValue
Set to override the value.
Definition MRUITestEngine.h:129
For sliders, drags, etc.
Definition MRUITestEngine.h:117
std::variant< Value< std::int64_t >, Value< std::uint64_t >, Value< double >, Value< std::string > > ValueVar
Definition MRUITestEngine.h:146
ValueVar value
Definition MRUITestEngine.h:147
static constexpr std::string_view kindName
Definition MRUITestEngine.h:149
Definition MRUITestEngine.h:25
T min
Definition MRUITestEngine.h:27
T max
Definition MRUITestEngine.h:28
T value
Definition MRUITestEngine.h:26