RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
NoteEditorUI.cpp File Reference
#include "NoteEditorUI.h"
#include "ImGuiToaster.h"
#include <algorithm>
Include dependency graph for NoteEditorUI.cpp:

Go to the source code of this file.

Classes

struct  InputTextWrapCtx
 

Functions

static int InputTextMultilineWrapCallback (ImGuiInputTextCallbackData *data)
 
static bool InputTextMultilineString_HardWrap (const char *label, std::string *str, const ImVec2 &size, float max_px_line, ImGuiInputTextFlags flags=0)
 

Function Documentation

◆ InputTextMultilineString_HardWrap()

static bool InputTextMultilineString_HardWrap ( const char * label,
std::string * str,
const ImVec2 & size,
float max_px_line,
ImGuiInputTextFlags flags = 0 )
static

Definition at line 104 of file NoteEditorUI.cpp.

109{
110 IM_ASSERT(str);
111 if (str->capacity() == 0)
112 str->reserve(1);
113
115 ctx.buf = str;
116 ctx.max_px = std::max(0.0f, max_px_line);
117
118 flags |= ImGuiInputTextFlags_CallbackResize;
119 flags |= ImGuiInputTextFlags_CallbackEdit;
120 flags |= ImGuiInputTextFlags_NoHorizontalScroll;
121
122 return ImGui::InputTextMultiline(label,
123 str->data(),
124 str->capacity() + 1, // bind to capacity; resize callback will keep this valid
125 size,
126 flags,
128 &ctx);
129}
static int InputTextMultilineWrapCallback(ImGuiInputTextCallbackData *data)
std::string * buf
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InputTextMultilineWrapCallback()

static int InputTextMultilineWrapCallback ( ImGuiInputTextCallbackData * data)
static

Definition at line 34 of file NoteEditorUI.cpp.

35{
36 auto* ctx = static_cast<InputTextWrapCtx*>(data->UserData);
37 if (!ctx || !ctx->buf)
38 return 0;
39
40 // Keep Dear ImGui's string-resize contract
41 if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
42 {
43 ctx->buf->resize(static_cast<size_t>(data->BufTextLen));
44 data->Buf = ctx->buf->data();
45 return 0;
46 }
47
48 if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit)
49 {
50 // Current line range [start, end)
51 const int len = data->BufTextLen;
52 int cur = data->CursorPos;
53 int start = cur;
54 while (start > 0 && data->Buf[start - 1] != '\n')
55 start--;
56 int end = cur;
57 while (end < len && data->Buf[end] != '\n')
58 end++;
59
60 const char* line_start = data->Buf + start;
61 const char* line_end = data->Buf + end;
62
63 // If current line fits, done.
64 ImVec2 sz = ImGui::CalcTextSize(line_start, line_end, false, FLT_MAX);
65 if (sz.x <= ctx->max_px || ctx->max_px <= 0.0f)
66 return 0;
67
68 // Find a nice break point (space/tab/hyphen/slash) before cursor
69 int break_pos = -1;
70 for (int i = cur - 1; i >= start; --i)
71 {
72 char c = data->Buf[i];
73 if (c == ' ' || c == '\t' || c == '-' || c == '/')
74 {
75 // Would the line up to here fit?
76 ImVec2 sz2 = ImGui::CalcTextSize(line_start, data->Buf + i, false, FLT_MAX);
77 if (sz2.x <= ctx->max_px)
78 {
79 break_pos = i;
80 break;
81 }
82 }
83 }
84
85 if (break_pos >= start)
86 {
87 // Replace that separator with a newline
88 data->DeleteChars(break_pos, 1);
89 data->InsertChars(break_pos, "\n");
90 // Let Dear ImGui re-evaluate next frame; avoid chaining multiple edits now
91 return 0;
92 }
93 else
94 {
95 // Force break at cursor if no separator found that fits
96 data->InsertChars(cur, "\n");
97 data->CursorPos = cur + 1;
98 return 0;
99 }
100 }
101 return 0;
102}
Here is the caller graph for this function: