MeshLib C++ Docs
Loading...
Searching...
No Matches
MRImGuiMeasurementIndicators.h
Go to the documentation of this file.
1#pragma once
2
4#include "MRViewer/exports.h"
5#include "MRViewer/MRViewerFwd.h"
7
8#include <imgui.h>
9
10#include <cassert>
11#include <optional>
12#include <span>
13#include <variant>
14
16{
17
19struct Stipple
20{
23 float patternLength = 16;
24
25 struct Segment
26 {
31 float a = 0;
32 float b = 0;
33
34 [[nodiscard]] float get( bool end ) const { return end ? b : a; }
35 };
36 std::span<const Segment> segments;
37};
38
39struct Params
40{
41 ImDrawList* list = ImGui::GetBackgroundDrawList();
48
49 float pointDiameter = 6;
50
51 float width = 1.5f;
52 float smallWidth = 0.75f;
53 float outlineWidth = 1.5f;
54 float textOutlineWidth = 4.f;
56
60
61 float arrowLen = 12;
62 float arrowHalfWidth = 4;
67
69 ImVec2 textToLineSpacingA = ImVec2( 0, 0 );
70 ImVec2 textToLineSpacingB = ImVec2( 0, 0 );
73
77
79 float invertedOverhang = 24;
80
82 float leaderLineLen = 20;
83
85 float notchHalfLen = 8;
86
89
93
95 MRVIEWER_API Params();
96};
97
98enum class Element
99{
100 main = 1 << 0,
101 outline = 1 << 1,
102 both = main | outline,
103};
105
106
107MRVIEWER_API void point( Element elem, const Params& params, ImVec2 point );
108
109enum class TextIcon
110{
111 diameter,
112};
113
115{
117 std::optional<ImU32> color;
118
120 TextColor( ImVec4 color ) : color( ImGui::ColorConvertFloat4ToU32( color ) ) {}
121 TextColor( ImU32 color ) : color( color ) {}
122};
123
125{
127 ImFont* font = nullptr;
128 float size = 0.f;
129};
130
132struct Text
133{
134 using ElemVar = std::variant<std::string, TextIcon, TextColor, TextFont>;
135
136 struct Elem
137 {
139
142 ImVec2 size = ImVec2( 0, -1 );
143
146 ImVec2 align;
147
150 int columnId = -1;
151
153 mutable ImVec2 computedSize;
154
157
158 [[nodiscard]] bool hasDefaultParams() const
159 {
160 return size == ImVec2( 0, -1 ) && align == ImVec2() && columnId == -1;
161 }
162 };
163
164 struct Line
165 {
166 std::vector<Elem> elems;
167
170 ImVec2 size = ImVec2( -1, 0 );
171
174 ImVec2 align;
175
177 mutable ImVec2 computedSize;
178
181 };
182
183 std::vector<Line> lines;
184
187 ImVec2 size;
188
190 ImVec2 align;
191
192 using FontFunc = std::function<FontAndSize()>;
194 [[nodiscard]] MRVIEWER_API static const FontFunc& getStaticDefaultFontFunc();
195 MRVIEWER_API static void setStaticDefaultFontFunc( FontFunc func );
196
200
202 mutable ImVec2 computedSize;
203
205
206 mutable bool dirty = false;
207
208 Text() {}
209 Text( const std::string& text ) { addText( text ); }
210 Text( std::string_view text ) { addText( text ); }
211 Text( const char* text ) { addText( text ); }
212
213 [[nodiscard]] bool isEmpty() const { return lines.empty(); }
214
215 void addLine()
216 {
217 dirty = true;
218 lines.emplace_back();
219 lines.back().elems.emplace_back().var = std::string();
220 }
221
223 MRVIEWER_API void addText( std::string_view text );
224
227 void addElem( Elem elem )
228 {
229 dirty = true;
230 if ( lines.empty() )
231 addLine();
232 lines.back().elems.emplace_back( std::move( elem ) );
233 }
234
236 void add( auto&& elem )
237 {
238 addElem( { .var = decltype(elem)(elem) } );
239 }
240
243 MRVIEWER_API void update( bool force = false ) const;
244
246 {
247 ImVec2 cornerA;
248 ImVec2 cornerB;
249 };
250
253 MRVIEWER_API DrawResult draw( ImDrawList& list, ImVec2 pos, const TextColor& defaultTextColor = {} ) const;
254};
255
257{
258 none,
259 noOutline,
260 extend,
261 arrow,
262 point,
263};
264
265enum class LineFlags
266{
267 narrow = 1 << 0,
268 noBackwardArrowTipOffset = 1 << 1,
269 onlyOutline = 1 << 2,
270};
272
274{
275 LineFlags flags{};
276
277 std::optional<Color> colorOverride;
278
280 std::optional<Stipple> stipple;
281};
282
284{
288
296 std::optional<Line> line;
297
299 bool isHovered = false;
301 bool isActive = false;
302
304 bool isSelected = false;
305};
306
308{
309 ImVec2 textCornerA;
311 ImVec2 bgCornerA;
312 ImVec2 bgCornerB;
313};
314
320MRVIEWER_API std::optional<TextResult> text(
321 Element elem, const Params& params, ImVec2 pos,
322 const Text& text, const TextParams& textParams = {},
323 ImVec2 push = {}, ImVec2 pivot = { 0.5f, 0.5f }
324);
325
327MRVIEWER_API void arrowTriangle( Element elem, const Params& params, ImVec2 point, ImVec2 dir );
328
336
338{
342
343 std::span<const ImVec2> midPoints;
344};
345
347{
348 std::optional<TextResult> capA, capB;
349};
350
352MRVIEWER_API std::optional<LineResult> line( Element elem, const Params& params, ImVec2 a, ImVec2 b, const LineParams& lineParams = {} );
353
355{
357 std::optional<bool> moveTextToLineEndIndex;
358
360};
361
363{
364 std::optional<LineResult> line;
365 std::optional<TextResult> text;
366};
367
370MRVIEWER_API std::optional<DistanceResult> distance( Element elem, const Params& params, ImVec2 a, ImVec2 b, const Text& text, const DistanceParams& distanceParams = {} );
371
382
384{
385 ImVec2 a;
386 ImVec2 b;
387
388 [[nodiscard]] ImVec2 endPoint( bool second ) const { return second ? b : a; }
389
390 std::span<ImVec2> midPoints;
391};
392
406template <typename A, typename B, typename F, typename G, typename H = std::nullptr_t>
407[[nodiscard]] PreparedCurve prepareCurve( const CurveParams& curveParams, std::vector<ImVec2>& pointBuffer, const A& stateA, const B& stateB,
408 F&& stateToPoint, G&& bisectState, H&& onInsertPoint = nullptr
409)
410{
411 const float pixelStepSq = curveParams.subdivisionStepPixels * curveParams.subdivisionStepPixels;
412
413 std::size_t firstIndex = pointBuffer.size();
414
415 auto makeCurve = [&]( auto makeCurve, int depth, const auto& stateA, const auto& stateB, ImVec2 pointA, ImVec2 pointB ) -> void
416 {
418 if ( depth < curveParams.maxSubdivisionDepth && ( depth < curveParams.minSubdivisionDepth || ImGuiMath::lengthSq( pointB - pointA ) > pixelStepSq ) )
419 {
421
422 auto midState = bisectState( stateA, stateB, int( depth ) );
423 ImVec2 midPoint = stateToPoint( midState );
424
425 makeCurve( makeCurve, depth + 1, stateA, midState, pointA, midPoint );
426 makeCurve( makeCurve, depth + 1, midState, stateB, midPoint, pointB );
427 }
428 else
429 {
431
432 onInsertPoint( pointB, stateB );
433 pointBuffer.push_back( pointB );
434 }
435
436 };
437 ImVec2 firstPoint = stateToPoint( stateA );
438 makeCurve( makeCurve, 0, stateA, stateB, firstPoint, stateToPoint( stateB ) );
439
440 PreparedCurve ret{ .a = firstPoint, .b = pointBuffer.back() };
441 pointBuffer.pop_back();
442 ret.midPoints = { pointBuffer.data() + firstIndex, pointBuffer.data() + pointBuffer.size() };
443 return ret;
444}
445
446}
#define MR_MAKE_FLAG_OPERATORS(T)
Generates operators for a enum (at namespace scope).
Definition MRFlagOperators.h:6
MR_BIND_IGNORE_PY auto end(const BitSet &)
Definition MRBitSet.h:397
auto depth(const Box< V > &box)
returns size along z axis
Definition MRBox.h:368
static constexpr Color transparent() noexcept
Definition MRColor.h:37
Definition ImGuiHelpers.h:33
constexpr auto lengthSq(A a)
Definition MRImGuiVectorOperators.h:131
Definition MRImGuiMeasurementIndicators.h:16
LineCapDecoration
Definition MRImGuiMeasurementIndicators.h:257
@ arrow
No special decoration, but the line extends a bit after the target point.
@ extend
Almost exactly like none, but don't draw the tiny bit of outline at the end. This can look better in ...
MRVIEWER_API std::optional< LineResult > line(Element elem, const Params &params, ImVec2 a, ImVec2 b, const LineParams &lineParams={})
Draws a line or an arrow.
MRVIEWER_API void arrowTriangle(Element elem, const Params &params, ImVec2 point, ImVec2 dir)
Draws a triangle from an arrow.
PreparedCurve prepareCurve(const CurveParams &curveParams, std::vector< ImVec2 > &pointBuffer, const A &stateA, const B &stateB, F &&stateToPoint, G &&bisectState, H &&onInsertPoint=nullptr)
Definition MRImGuiMeasurementIndicators.h:407
LineFlags
Definition MRImGuiMeasurementIndicators.h:266
@ onlyOutline
Overrides params.arrowTipBackwardOffset to zero.
Element
Definition MRImGuiMeasurementIndicators.h:99
MRVIEWER_API std::optional< TextResult > text(Element elem, const Params &params, ImVec2 pos, const Text &text, const TextParams &textParams={}, ImVec2 push={}, ImVec2 pivot={ 0.5f, 0.5f })
TextIcon
Definition MRImGuiMeasurementIndicators.h:110
MRVIEWER_API std::optional< DistanceResult > distance(Element elem, const Params &params, ImVec2 a, ImVec2 b, const Text &text, const DistanceParams &distanceParams={})
Definition MRColor.h:12
Definition MRImGuiMeasurementIndicators.h:373
int maxSubdivisionDepth
How many times can be subdivide a curve (resuling in up to 2^depth segments).
Definition MRImGuiMeasurementIndicators.h:375
float subdivisionStepPixels
Definition MRImGuiMeasurementIndicators.h:380
int minSubdivisionDepth
A curve is always subdivided at least this many times.
Definition MRImGuiMeasurementIndicators.h:377
Definition MRImGuiMeasurementIndicators.h:355
std::optional< bool > moveTextToLineEndIndex
If this is set, the text is moved from the middle of the line to one of the line ends (false = A,...
Definition MRImGuiMeasurementIndicators.h:357
TextParams textParams
Definition MRImGuiMeasurementIndicators.h:359
Definition MRImGuiMeasurementIndicators.h:363
std::optional< TextResult > text
Definition MRImGuiMeasurementIndicators.h:365
std::optional< LineResult > line
Definition MRImGuiMeasurementIndicators.h:364
Definition MRImGuiMeasurementIndicators.h:274
std::optional< Stipple > stipple
For drawing dotted lines. You can get presets for this parameter from ImGuiMeasurementIndicators::Par...
Definition MRImGuiMeasurementIndicators.h:280
std::optional< Color > colorOverride
Definition MRImGuiMeasurementIndicators.h:277
Definition MRImGuiMeasurementIndicators.h:330
TextParams textParams
Definition MRImGuiMeasurementIndicators.h:334
LineCapDecoration decoration
Definition MRImGuiMeasurementIndicators.h:331
Text text
Definition MRImGuiMeasurementIndicators.h:333
Definition MRImGuiMeasurementIndicators.h:338
LineCap capA
Definition MRImGuiMeasurementIndicators.h:340
LineBodyParams body
Definition MRImGuiMeasurementIndicators.h:339
std::span< const ImVec2 > midPoints
Definition MRImGuiMeasurementIndicators.h:343
LineCap capB
Definition MRImGuiMeasurementIndicators.h:341
Definition MRImGuiMeasurementIndicators.h:347
std::optional< TextResult > capA
Definition MRImGuiMeasurementIndicators.h:348
std::optional< TextResult > capB
Definition MRImGuiMeasurementIndicators.h:348
Definition MRImGuiMeasurementIndicators.h:40
Color colorTextOutlineActive
Definition MRImGuiMeasurementIndicators.h:47
float width
Definition MRImGuiMeasurementIndicators.h:51
float smallWidth
Definition MRImGuiMeasurementIndicators.h:52
Stipple stippleDashed
Definition MRImGuiMeasurementIndicators.h:91
float totalLenThreshold
Definition MRImGuiMeasurementIndicators.h:76
Color colorTextOutlineHovered
Definition MRImGuiMeasurementIndicators.h:46
float arrowHalfWidth
Definition MRImGuiMeasurementIndicators.h:62
float arrowLen
Definition MRImGuiMeasurementIndicators.h:61
float textToLineSpacingRadius
Further, the lines around the text are shortened by this amount.
Definition MRImGuiMeasurementIndicators.h:72
float notchHalfLen
A small perpendicular line at the end of some arrows.
Definition MRImGuiMeasurementIndicators.h:85
float textOutlineWidth
Definition MRImGuiMeasurementIndicators.h:54
float leaderLineLen
The length of the leader lines (short lines attaching text to other things).
Definition MRImGuiMeasurementIndicators.h:82
Color colorOutline
Definition MRImGuiMeasurementIndicators.h:43
float arrowTipBackwardOffset
Definition MRImGuiMeasurementIndicators.h:66
Color colorMain
Definition MRImGuiMeasurementIndicators.h:42
Color colorTextOutline
Definition MRImGuiMeasurementIndicators.h:45
float clickableLabelLineWidth
Definition MRImGuiMeasurementIndicators.h:57
ImVec2 textToLineSpacingB
Top-left corner.
Definition MRImGuiMeasurementIndicators.h:70
float pointDiameter
Definition MRImGuiMeasurementIndicators.h:49
ImDrawList * list
Definition MRImGuiMeasurementIndicators.h:41
float clickableLabelOutlineWidth
Definition MRImGuiMeasurementIndicators.h:59
Color colorText
Definition MRImGuiMeasurementIndicators.h:44
ImVec2 textToLineSpacingA
The spacing box around the text is extended by this amount.
Definition MRImGuiMeasurementIndicators.h:69
float outlineWidth
Definition MRImGuiMeasurementIndicators.h:53
float clickableLabelLineWidthSelected
Definition MRImGuiMeasurementIndicators.h:58
float invertedOverhang
When drawing inverted (short) distances, the line is extended by this amount on both sides.
Definition MRImGuiMeasurementIndicators.h:79
float textOutlineRounding
Definition MRImGuiMeasurementIndicators.h:55
Definition MRImGuiMeasurementIndicators.h:384
ImVec2 a
Definition MRImGuiMeasurementIndicators.h:385
ImVec2 b
The first point.
Definition MRImGuiMeasurementIndicators.h:386
std::span< ImVec2 > midPoints
Definition MRImGuiMeasurementIndicators.h:390
ImVec2 endPoint(bool second) const
The last point.
Definition MRImGuiMeasurementIndicators.h:388
Definition MRImGuiMeasurementIndicators.h:26
float b
Definition MRImGuiMeasurementIndicators.h:32
float a
Definition MRImGuiMeasurementIndicators.h:31
float get(bool end) const
Definition MRImGuiMeasurementIndicators.h:34
Parameters for drawing dotted lines.
Definition MRImGuiMeasurementIndicators.h:20
float patternLength
Definition MRImGuiMeasurementIndicators.h:23
std::span< const Segment > segments
Definition MRImGuiMeasurementIndicators.h:36
Definition MRImGuiMeasurementIndicators.h:115
TextColor(ImU32 color)
Definition MRImGuiMeasurementIndicators.h:121
TextColor()
Definition MRImGuiMeasurementIndicators.h:119
std::optional< ImU32 > color
If null, resets the color.
Definition MRImGuiMeasurementIndicators.h:117
TextColor(ImVec4 color)
Definition MRImGuiMeasurementIndicators.h:120
Definition MRImGuiMeasurementIndicators.h:125
ImFont * font
If null, resets the font to the default.
Definition MRImGuiMeasurementIndicators.h:127
float size
Definition MRImGuiMeasurementIndicators.h:128
Definition MRImGuiMeasurementIndicators.h:290
LineCapDecoration capDecoration
Definition MRImGuiMeasurementIndicators.h:292
ImVec2 point
Definition MRImGuiMeasurementIndicators.h:291
LineBodyParams body
Definition MRImGuiMeasurementIndicators.h:293
Definition MRImGuiMeasurementIndicators.h:284
Color borderColor
Definition MRImGuiMeasurementIndicators.h:287
bool isSelected
This is independent from the bools above, but currently requires borderColor to be meaningful.
Definition MRImGuiMeasurementIndicators.h:304
bool isHovered
Should imply borderColor.
Definition MRImGuiMeasurementIndicators.h:299
std::optional< Line > line
Optional. Draw a line to this point from the text bubble.
Definition MRImGuiMeasurementIndicators.h:296
bool isActive
Should imply isHovered.
Definition MRImGuiMeasurementIndicators.h:301
Definition MRImGuiMeasurementIndicators.h:308
ImVec2 bgCornerB
The padded rect of the text background.
Definition MRImGuiMeasurementIndicators.h:312
ImVec2 bgCornerA
Definition MRImGuiMeasurementIndicators.h:311
ImVec2 textCornerB
The narrow rect right around the text.
Definition MRImGuiMeasurementIndicators.h:310
ImVec2 textCornerA
Definition MRImGuiMeasurementIndicators.h:309
Definition MRImGuiMeasurementIndicators.h:246
ImVec2 cornerA
Definition MRImGuiMeasurementIndicators.h:247
ImVec2 cornerB
Definition MRImGuiMeasurementIndicators.h:248
Definition MRImGuiMeasurementIndicators.h:137
bool hasDefaultParams() const
Definition MRImGuiMeasurementIndicators.h:158
ImVec2 computedSize
The computed content size. Read-only. Don't set manually, update() sets this.
Definition MRImGuiMeasurementIndicators.h:153
ImVec2 size
Definition MRImGuiMeasurementIndicators.h:142
ElemVar var
Definition MRImGuiMeasurementIndicators.h:138
int columnId
Definition MRImGuiMeasurementIndicators.h:150
ImVec2 computedSizeWithPadding
Read-only, don't set manually. This is typically max( size, computedSize ), with additional adjustmen...
Definition MRImGuiMeasurementIndicators.h:156
ImVec2 align
Definition MRImGuiMeasurementIndicators.h:146
Definition MRImGuiMeasurementIndicators.h:165
ImVec2 computedSizeWithPadding
Read-only, don't set manually. This is typically max( size, computedSize ), with additional adjustmen...
Definition MRImGuiMeasurementIndicators.h:180
ImVec2 align
Definition MRImGuiMeasurementIndicators.h:174
ImVec2 size
Definition MRImGuiMeasurementIndicators.h:170
std::vector< Elem > elems
Definition MRImGuiMeasurementIndicators.h:166
ImVec2 computedSize
The computed content size. Read-only. Don't set manually, update() sets this.
Definition MRImGuiMeasurementIndicators.h:177
Represents an arbitrary block of text, possibly with icons, colors, etc.
Definition MRImGuiMeasurementIndicators.h:133
MRVIEWER_API void addText(std::string_view text)
Adds some string to the text. Automatically splits by newlines.
void addElem(Elem elem)
Definition MRImGuiMeasurementIndicators.h:227
Text()
Definition MRImGuiMeasurementIndicators.h:208
FontAndSize defaultFont
Definition MRImGuiMeasurementIndicators.h:199
Text(std::string_view text)
Definition MRImGuiMeasurementIndicators.h:210
std::function< FontAndSize()> FontFunc
Definition MRImGuiMeasurementIndicators.h:192
MRVIEWER_API DrawResult draw(ImDrawList &list, ImVec2 pos, const TextColor &defaultTextColor={}) const
MRVIEWER_API void update(bool force=false) const
std::vector< Line > lines
Definition MRImGuiMeasurementIndicators.h:183
void addLine()
Definition MRImGuiMeasurementIndicators.h:215
ImVec2 size
Definition MRImGuiMeasurementIndicators.h:187
static MRVIEWER_API void setStaticDefaultFontFunc(FontFunc func)
Text(const char *text)
Definition MRImGuiMeasurementIndicators.h:211
Text(const std::string &text)
Definition MRImGuiMeasurementIndicators.h:209
void add(auto &&elem)
Adds any element type of ElemVar.
Definition MRImGuiMeasurementIndicators.h:236
bool isEmpty() const
Definition MRImGuiMeasurementIndicators.h:213
bool dirty
No computedSizeWithPadding, because for the entire Text we use size exactly, without max( size,...
Definition MRImGuiMeasurementIndicators.h:206
ImVec2 computedSize
The computed content size. Read-only. Don't set manually, update() sets this.
Definition MRImGuiMeasurementIndicators.h:202
static MRVIEWER_API const FontFunc & getStaticDefaultFontFunc()
Get the default value for .defaultFont. This can be null to default to null, which means the current ...
std::variant< std::string, TextIcon, TextColor, TextFont > ElemVar
Definition MRImGuiMeasurementIndicators.h:134
ImVec2 align
Alignment. [0,0] = top-left, [1,1] = bottom-right.
Definition MRImGuiMeasurementIndicators.h:190