mirror of
https://github.com/Drezil/imgui.git
synced 2024-11-15 09:27:00 +00:00
InputText: added comments and somehow clarified the optimized code that calculate text position + updated demo
This commit is contained in:
parent
2b3fb5c0f7
commit
bc34ac882f
37
imgui.cpp
37
imgui.cpp
@ -7458,35 +7458,38 @@ bool ImGui::InputTextEx(const char* label, char* buf, int buf_size, const ImVec2
|
|||||||
|
|
||||||
{
|
{
|
||||||
// Count lines + find lines numbers of cursor and select_start
|
// Count lines + find lines numbers of cursor and select_start
|
||||||
int matches_remaining = 0;
|
const ImWchar* searches_input_cursor_ptr[2];
|
||||||
int matches_line_no[2] = { -1, -999 };
|
searches_input_cursor_ptr[0] = text_begin + edit_state.StbState.cursor;
|
||||||
const ImWchar* matches_ptr[2] = { NULL, NULL };
|
searches_input_cursor_ptr[1] = NULL;
|
||||||
matches_ptr[0] = text_begin + edit_state.StbState.cursor; matches_remaining++;
|
int searches_remaining = 1;
|
||||||
|
int searches_result_line_number[2] = { -1, -999 };
|
||||||
if (edit_state.StbState.select_start != edit_state.StbState.select_end)
|
if (edit_state.StbState.select_start != edit_state.StbState.select_end)
|
||||||
{
|
{
|
||||||
matches_ptr[1] = text_begin + ImMin(edit_state.StbState.select_start, edit_state.StbState.select_end);
|
searches_input_cursor_ptr[1] = text_begin + ImMin(edit_state.StbState.select_start, edit_state.StbState.select_end);
|
||||||
matches_line_no[1] = -1;
|
searches_result_line_number[1] = -1;
|
||||||
matches_remaining++;
|
searches_remaining++;
|
||||||
}
|
}
|
||||||
matches_remaining += is_multiline ? 1 : 0; // So that we never exit the loop until all lines are counted.
|
|
||||||
|
|
||||||
|
// Iterate all lines to find our line numbers
|
||||||
|
// In multi-line mode, we never exit the loop until all lines are counted, so add one extra to the counter.
|
||||||
|
searches_remaining += is_multiline ? 1 : 0;
|
||||||
int line_count = 0;
|
int line_count = 0;
|
||||||
for (const ImWchar* s = text_begin; s < text_end+1; s++)
|
for (const ImWchar* s = text_begin; s < text_end+1; s++)
|
||||||
if ((*s) == '\n' || s == text_end)
|
if ((*s) == '\n' || s == text_end)
|
||||||
{
|
{
|
||||||
line_count++;
|
line_count++;
|
||||||
if (matches_line_no[0] == -1 && s >= matches_ptr[0]) { matches_line_no[0] = line_count; if (--matches_remaining <= 0) break; }
|
if (searches_result_line_number[0] == -1 && s >= searches_input_cursor_ptr[0]) { searches_result_line_number[0] = line_count; if (--searches_remaining <= 0) break; }
|
||||||
if (matches_line_no[1] == -1 && s >= matches_ptr[1]) { matches_line_no[1] = line_count; if (--matches_remaining <= 0) break; }
|
if (searches_result_line_number[1] == -1 && s >= searches_input_cursor_ptr[1]) { searches_result_line_number[1] = line_count; if (--searches_remaining <= 0) break; }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate 2d position
|
// Calculate 2d position by finding the beginning of the line and measuring distance
|
||||||
IM_ASSERT(matches_line_no[0] != -1);
|
IM_ASSERT(searches_result_line_number[0] != -1);
|
||||||
cursor_offset.x = InputTextCalcTextSizeW(ImStrbolW(matches_ptr[0], text_begin), matches_ptr[0]).x;
|
cursor_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_cursor_ptr[0], text_begin), searches_input_cursor_ptr[0]).x;
|
||||||
cursor_offset.y = matches_line_no[0] * g.FontSize;
|
cursor_offset.y = searches_result_line_number[0] * g.FontSize;
|
||||||
if (matches_line_no[1] >= 0)
|
if (searches_result_line_number[1] >= 0)
|
||||||
{
|
{
|
||||||
select_start_offset.x = InputTextCalcTextSizeW(ImStrbolW(matches_ptr[1], text_begin), matches_ptr[1]).x;
|
select_start_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_cursor_ptr[1], text_begin), searches_input_cursor_ptr[1]).x;
|
||||||
select_start_offset.y = matches_line_no[1] * g.FontSize;
|
select_start_offset.y = searches_result_line_number[1] * g.FontSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate text height
|
// Calculate text height
|
||||||
|
@ -422,7 +422,18 @@ void ImGui::ShowTestWindow(bool* opened)
|
|||||||
if (ImGui::TreeNode("Multi-line Text Input"))
|
if (ImGui::TreeNode("Multi-line Text Input"))
|
||||||
{
|
{
|
||||||
static bool read_only = false;
|
static bool read_only = false;
|
||||||
static char text[1024*16] = "// F00F bug\nlabel:\n\tlock cmpxchg8b eax\n";
|
static char text[1024*16] =
|
||||||
|
"/*\n"
|
||||||
|
" The Pentium F00F bug, shorthand for F0 0F C7 C8,\n"
|
||||||
|
" the hexadecimal encoding of one offending instruction,\n"
|
||||||
|
" more formally, the invalid operand with locked CMPXCHG8B\n"
|
||||||
|
" instruction bug, is a design flaw in the majority of\n"
|
||||||
|
" Intel Pentium, Pentium MMX, and Pentium OverDrive\n"
|
||||||
|
" processors (all in the P5 microarchitecture).\n"
|
||||||
|
"*/\n\n"
|
||||||
|
"label:\n"
|
||||||
|
"\tlock cmpxchg8b eax\n";
|
||||||
|
|
||||||
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0,0));
|
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0,0));
|
||||||
ImGui::Checkbox("Read-only", &read_only);
|
ImGui::Checkbox("Read-only", &read_only);
|
||||||
ImGui::PopStyleVar();
|
ImGui::PopStyleVar();
|
||||||
|
Loading…
Reference in New Issue
Block a user