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
15// This is a low-level header for implementing GUIs that can be interacted with programmatically.
16// Most likely you don't need to touch this, just use widgets from `MRUIStyle.h`.
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
56// Call this every frame when drawing a button you want to track (regardless of whether it returns true of false).
57// If this returns true, simulate a button click.
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
63// Create a "value" (slider/drag/...).
64// `T` must be a scalar; vector support must be implemented manually.
65// Pass `min >= max` to disable the range checks.
66// If this returns true, use the new value in place of the current one.
67// \param consumeValueOverride If true - retrieves (deletes) a value from storage.
68// If false - copies the value from the storage (saves the original to the storage to be retrieved again, for example, in the next frame)
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}
85// This overload is for strings.
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
88// Usually you don't need this function.
89// This is for widgets that require you to specify the value override before drawing it, such as `ImGui::CollapsingHeader()`.
90// For those, call this version first to read the value override, then draw the widget, then call the normal `CreateValue()` with the same name
91// and with the new value, and discard its return value.
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
99// Use those to group buttons into named groups.
100MRVIEWER_API void pushTree( std::string_view name );
101MRVIEWER_API void popTree();
102
103struct Entry;
104
106{
107 // Set this to true to simulate a button click.
108 mutable bool simulateClick = false;
109
110 static constexpr std::string_view kindName = "button";
111};
112
113// For sliders, drags, etc.
115{
116 template <typename T>
117 struct Value
118 {
119 // The current value.
120 T value = 0;
121
122 // Min/max bounds, INCLUSIVE. If none, those are set to the min/max values representable in this type.
123 T min = 0;
124 T max = 0;
125
126 // Set to override the value.
127 mutable std::optional<T> simulatedValue;
128
129 Value() {} // Make `std::variant` below happy.
130 };
131 template <std::same_as<std::string> T> // GCC chokes on full specializations at class scope, hence this.
132 struct Value<T>
133 {
134 // The current value.
135 std::string value;
136
137 std::optional<std::vector<std::string>> allowedValues;
138
139 // Set to override the value.
140 mutable std::optional<std::string> simulatedValue;
141
142 Value() {} // Make `std::variant` below happy.
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{
152 // Using `std::map` over `std::unordered_map` to be able to search by `std::string_view` keys directly.
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
162 // Mostly for internal use.
163 // If this is false, the entry will be removed on the next frame.
164 bool visitedOnThisFrame = false;
165
166 // Returns a string describing the type currently stored in `value`, which is `T::kindName`.
167 [[nodiscard]] MRVIEWER_API std::string_view getKindName() const;
168
169 // Calls `std::get<T>(value)`, returns a user-friendly error on failure.
170 // The returned pointer is never null.
171 // If `selfName` is specified, it's added to the error message as the name of this entry.
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
190// Returns the current entry tree.
191[[nodiscard]] MRVIEWER_API const GroupEntry& getRootEntry();
192
193}
Definition MRUITestEngine.h:61
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()
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)
tl::expected< T, E > Expected
Definition MRExpected.h:28
tl::unexpected< E > Unexpected
Definition MRExpected.h:31
Definition MRUITestEngine.h:106
bool simulateClick
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
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
Definition MRUITestEngine.h:153
Value()
Definition MRUITestEngine.h:142
std::optional< std::string > simulatedValue
Definition MRUITestEngine.h:140
std::string 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
Definition MRUITestEngine.h:120
T min
Definition MRUITestEngine.h:123
std::optional< T > simulatedValue
Definition MRUITestEngine.h:127
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