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 <filesystem>
8#include <limits>
9#include <map>
10#include <optional>
11#include <string_view>
12#include <string>
13#include <variant>
14#include <vector>
15
18
20{
21
24{
26 std::string_view disabledReason;
27};
28
29namespace detail
30{
31 template <typename T>
33 {
34 T value{};
35 T min{};
36 T max{};
37 };
38 template <>
39 struct BoundedValue<std::string>
40 {
41 std::string value;
42
43 std::optional<std::vector<std::string>> allowedValues;
44 };
45
46 template <typename T>
47 [[nodiscard]] MRVIEWER_API std::optional<T> createValueLow( std::string_view name, std::optional<BoundedValue<T>> value, bool consumeValueOverride = true, const EntryAttributes& attrs = {} );
48
49 extern template MRVIEWER_API std::optional<std::int64_t> createValueLow( std::string_view name, std::optional<BoundedValue<std::int64_t>> value, bool consumeValueOverride, const EntryAttributes& attrs );
50 extern template MRVIEWER_API std::optional<std::uint64_t> createValueLow( std::string_view name, std::optional<BoundedValue<std::uint64_t>> value, bool consumeValueOverride, const EntryAttributes& attrs );
51 extern template MRVIEWER_API std::optional<double> createValueLow( std::string_view name, std::optional<BoundedValue<double>> value, bool consumeValueOverride, const EntryAttributes& attrs );
52 extern template MRVIEWER_API std::optional<std::string> createValueLow( std::string_view name, std::optional<BoundedValue<std::string>> value, bool consumeValueOverride, const EntryAttributes& attrs );
53
54 template <typename T, typename = void> struct UnderlyingValueTypeHelper {};
55 template <typename T> struct UnderlyingValueTypeHelper<T, std::enable_if_t<std::is_floating_point_v<T>>> {using type = double;};
56 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;};
57 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;};
58 template <> struct UnderlyingValueTypeHelper<std::string> {using type = std::string;};
59
60 template <typename T>
62}
63
66[[nodiscard]] MRVIEWER_API bool createButton( std::string_view name, const EntryAttributes& attrs = {} );
67
68template <typename T>
69concept AllowedValueType = std::is_arithmetic_v<T> || std::is_same_v<T, std::string>;
70
79template <AllowedValueType T>
80requires std::is_arithmetic_v<T>
81[[nodiscard]] std::optional<T> createValue( std::string_view name, T value, T min, T max, bool consumeValueOverride = true, const EntryAttributes& attrs = {} )
82{
83 if ( !( min < max ) )
84 {
85 min = std::numeric_limits<T>::lowest();
86 max = std::numeric_limits<T>::max();
87 }
88
90 static_assert(sizeof(T) <= sizeof(U), "The used type is too large.");
91
92 auto ret = detail::createValueLow<U>( name, detail::BoundedValue<U>{ .value = U( value ), .min = U( min ), .max = U( max ) }, consumeValueOverride, attrs );
93 return ret ? std::optional<T>( T( *ret ) ) : std::nullopt;
94}
95
96[[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, const EntryAttributes& attrs = {} );
97
102template <AllowedValueType T>
103[[nodiscard]] std::optional<T> createValueTentative( std::string_view name, bool consumeValueOverride = true, const EntryAttributes& attrs = {} )
104{
105 auto ret = detail::createValueLow<detail::UnderlyingValueType<T>>( name, std::nullopt, consumeValueOverride, attrs );
106 return ret ? std::optional<T>( T( *ret ) ) : std::nullopt;
107}
108
110MRVIEWER_API void pushTree( std::string_view name );
111MRVIEWER_API void popTree();
112
116{
117public:
118 explicit TreeGuard( std::string_view name ) { pushTree( name ); }
120 TreeGuard( const TreeGuard& ) = delete;
121 TreeGuard& operator=( const TreeGuard& ) = delete;
122};
123
124struct Entry;
125
127{
129 mutable bool simulateClick = false;
130
133 std::string disabledReason;
134
135 static constexpr std::string_view kindName = "button";
136};
137
140{
141 template <typename T>
142 struct Value
143 {
145 T value = 0;
146
148 T min = 0;
149 T max = 0;
150
152 mutable std::optional<T> simulatedValue;
153
154 Value() {}
155 };
156 template <std::same_as<std::string> T>
157 struct Value<T>
158 {
160 std::string value;
161
162 std::optional<std::vector<std::string>> allowedValues;
163
165 mutable std::optional<std::string> simulatedValue;
166
167 Value() {}
168 };
169 using ValueVar = std::variant<Value<std::int64_t>, Value<std::uint64_t>, Value<double>, Value<std::string>>;
171
174 std::string disabledReason;
175
176 static constexpr std::string_view kindName = "value";
177};
178
180{
182 std::map<std::string, Entry, std::less<>> elems;
183
184 static constexpr std::string_view kindName = "group";
185};
186
187struct Entry
188{
189 std::variant<ButtonEntry, ValueEntry, GroupEntry> value;
190
193 bool visitedOnThisFrame = false;
194
196 [[nodiscard]] MRVIEWER_API std::string_view getKindName() const;
197
201 template <typename T>
202 [[nodiscard]] Expected<T *> getAs( std::string_view selfName = {} )
203 {
204 Expected<T *> ret = std::get_if<T>( &value );
205 if ( !*ret )
206 ret = unexpected_( selfName, T::kindName );
207 return ret;
208 }
209 template <typename T>
210 [[nodiscard]] Expected<const T *> getAs( std::string_view selfName = {} ) const
211 {
212 return const_cast<Entry *>( this )->template getAs<T>( selfName );
213 }
214
215private:
216 [[nodiscard]] MRVIEWER_API Unexpected<std::string> unexpected_( std::string_view selfName, std::string_view tKindName );
217};
218
220[[nodiscard]] MRVIEWER_API const GroupEntry& getRootEntry();
221
228[[nodiscard]] MRVIEWER_API bool wasFrameTriggered();
229
235MRVIEWER_API void markFrameTriggered();
236
240MRVIEWER_API void stageFileDialogPaths( std::vector<std::filesystem::path> paths );
241
244[[nodiscard]] MRVIEWER_API std::vector<std::filesystem::path> consumeStagedFileDialogPaths();
245
249MRVIEWER_API void appendStatusMessage( std::string msg );
250
252[[nodiscard]] MRVIEWER_API std::vector<std::string> consumeStatusMessages();
253
254}
TreeGuard & operator=(const TreeGuard &)=delete
TreeGuard(const TreeGuard &)=delete
TreeGuard(std::string_view name)
Definition MRUITestEngine.h:118
~TreeGuard()
Definition MRUITestEngine.h:119
Definition MRUITestEngine.h:69
tl::expected< T, E > Expected
Definition MRExpected.h:31
tl::unexpected< E > Unexpected
Definition MRExpected.h:34
Definition MRUITestEngine.h:30
std::optional< T > createValueLow(std::string_view name, std::optional< BoundedValue< T > > value, bool consumeValueOverride=true, const EntryAttributes &attrs={})
typename UnderlyingValueTypeHelper< T >::type UnderlyingValueType
Definition MRUITestEngine.h:61
Definition MRUITestEngine.h:20
const GroupEntry & getRootEntry()
Returns the current entry tree.
void stageFileDialogPaths(std::vector< std::filesystem::path > paths)
std::optional< T > createValueTentative(std::string_view name, bool consumeValueOverride=true, const EntryAttributes &attrs={})
Definition MRUITestEngine.h:103
std::optional< T > createValue(std::string_view name, T value, T min, T max, bool consumeValueOverride=true, const EntryAttributes &attrs={})
Definition MRUITestEngine.h:81
std::vector< std::filesystem::path > consumeStagedFileDialogPaths()
bool createButton(std::string_view name, const EntryAttributes &attrs={})
void pushTree(std::string_view name)
Use those to group buttons into named groups.
std::vector< std::string > consumeStatusMessages()
Drain and return all status messages accumulated since the last drain.
void appendStatusMessage(std::string msg)
Definition MRUITestEngine.h:127
bool simulateClick
Set this to true to simulate a button click.
Definition MRUITestEngine.h:129
std::string disabledReason
Definition MRUITestEngine.h:133
static constexpr std::string_view kindName
Definition MRUITestEngine.h:135
Optional attributes reported to the test engine each frame alongside a widget registration.
Definition MRUITestEngine.h:24
std::string_view disabledReason
Non-empty marks the widget as disabled with this reason. Only read during the call.
Definition MRUITestEngine.h:26
Definition MRUITestEngine.h:188
std::variant< ButtonEntry, ValueEntry, GroupEntry > value
Definition MRUITestEngine.h:189
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:210
bool visitedOnThisFrame
Definition MRUITestEngine.h:193
Expected< T * > getAs(std::string_view selfName={})
Definition MRUITestEngine.h:202
Definition MRUITestEngine.h:180
static constexpr std::string_view kindName
Definition MRUITestEngine.h:184
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:182
Value()
Definition MRUITestEngine.h:167
std::optional< std::string > simulatedValue
Set to override the value.
Definition MRUITestEngine.h:165
std::string value
The current value.
Definition MRUITestEngine.h:160
std::optional< std::vector< std::string > > allowedValues
Definition MRUITestEngine.h:162
Definition MRUITestEngine.h:143
Value()
Definition MRUITestEngine.h:154
T max
Definition MRUITestEngine.h:149
T value
The current value.
Definition MRUITestEngine.h:145
T min
Min/max bounds, INCLUSIVE. If none, those are set to the min/max values representable in this type.
Definition MRUITestEngine.h:148
std::optional< T > simulatedValue
Set to override the value.
Definition MRUITestEngine.h:152
For sliders, drags, etc.
Definition MRUITestEngine.h:140
std::variant< Value< std::int64_t >, Value< std::uint64_t >, Value< double >, Value< std::string > > ValueVar
Definition MRUITestEngine.h:169
ValueVar value
Definition MRUITestEngine.h:170
std::string disabledReason
Definition MRUITestEngine.h:174
static constexpr std::string_view kindName
Definition MRUITestEngine.h:176
Definition MRUITestEngine.h:33
T min
Definition MRUITestEngine.h:35
T max
Definition MRUITestEngine.h:36
T value
Definition MRUITestEngine.h:34