Skip to content

Instantly share code, notes, and snippets.

@hb3p8
Created December 22, 2016 08:13
Show Gist options
  • Save hb3p8/d97b7afe9e037339027121ff6c0eb6d9 to your computer and use it in GitHub Desktop.
Save hb3p8/d97b7afe9e037339027121ff6c0eb6d9 to your computer and use it in GitHub Desktop.
Question regarding custom UnformatedText-like widget implementation #949
// Create text input in place of a text
static void TextAsWidgetReplacement(const ImRect& aabb, const char* label, ImGuiID id)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();
// Our replacement widget will override the focus ID (registered previously to allow for a TAB focus to happen)
SetActiveID(g.ScalarAsInputTextId, window);
SetHoveredID(0);
FocusableItemUnregister(window);
InputText (label, (char*)label, strlen (label), ImGuiInputTextFlags_ReadOnly);
if (g.ScalarAsInputTextId == 0)
{
// First frame
IM_ASSERT(g.ActiveId == id); // InputText ID expected to match the Slider ID (else we'd need to store them both, which is also possible)
g.ScalarAsInputTextId = id;
SetHoveredID(id);
}
else if (g.ActiveId != id)
{
// Release
g.ScalarAsInputTextId = 0;
}
}
void TextSelectable(const char* text)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return;
ImGuiContext& g = *GImGui;
const ImGuiID id = window->GetID(text);
const ImVec2 label_size = CalcTextSize(text, NULL, true);
const ImRect text_bb(window->DC.CursorPos, window->DC.CursorPos + label_size);
const bool hovered = IsHovered(text_bb, id);
if (hovered)
SetHoveredID(id);
bool start_text_input = false;
if (hovered && g.IO.MouseDoubleClicked[0])
{
SetActiveID(id, window);
FocusWindow(window);
start_text_input = true;
g.ScalarAsInputTextId = 0;
}
if (start_text_input || (g.ActiveId == id && g.ScalarAsInputTextId == id))
{
TextAsWidgetReplacement(text_bb, text, id);
}
else
{
TextUnformatted (text, NULL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment