Skip to content

Instantly share code, notes, and snippets.

@colesnicov
Last active January 10, 2017 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colesnicov/638b8e605ed21fe981f197a768561bae to your computer and use it in GitHub Desktop.
Save colesnicov/638b8e605ed21fe981f197a768561bae to your computer and use it in GitHub Desktop.
Dirty (HARD) text wrapping for ImGui::InputTexImplements
/*
* Considerations on the possible implementation of word-wrap text for ImGui :: InputTextMultiline.
*
* Implements a "hard" line wrapping depending on the length of the text.
*
* Use, copying, modification and distribution without any liability to the author is possible.
*
* The discussion leads here: https://github.com/ocornut/imgui/issues/952
*
* But it is still a problems:
*
* - One is a "hard" breaking a line
* - Line breaks in the wrong place
* - Disregards the integrity of words
*
* Ala google translate
*/
static char buf[1024*16];
ImVec2 areaSize = ImVec2(-1, ImGui::GetWindowContentRegionMax().y - ImGui::GetCursorPosY() - 50);
float areaWidth = ImGui::CalcItemSize(areaSize, 0.0, 0.0).x;
ImGui::InputTextMultiline("##text", buf, IM_ARRAYSIZE(buf), areaSize, ImGuiInputTextFlags_CallbackAlways, CLB, &(areaWidth));
bool isAscii(char ch)
{
if((ch > 47 && ch < 58)
|| (ch > 64 && ch < 91)
|| (ch > 97 && ch < 122))
{
return true;
}
else
{
return false;
}
}
int CLB(ImGuiTextEditCallbackData* data)
{
bool wrapped = false;
float controlWidth = *(float*)data->UserData,
textWidth = ImGui::CalcTextSize(data->Buf).x;
if(controlWidth <= (textWidth + 5))
{
// pokud posledni znak je ascii, projit ve smycce k predchozimu znaku. a overit jestli je ascii.
// pokud posledni znak neni ascii, zalomit.
int i;
bool wrap = true;
for(i = data->BufTextLen-1; isAscii(data->Buf[i]); i--)
{
if(i==0)
{
wrap = false;
break;
}
}
if(wrap)
{
data->InsertChars(i, "\n");
data->BufDirty = true;
}
}
return data->BufTextLen;
}
@colesnicov
Copy link
Author

Many many many mistakes. But I fuck it now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment