imguiTextInput

Define a text input field.

bool
imguiTextInput
(
const(char)[] label
,
char[] buffer
,
ref char[] usedSlice
,
bool forceInputable = false
,)

Parameters

buffer char[]

Buffer to store entered text.

usedSlice char[]

Slice of buffer that stores text entered so far.

forceInputable bool

Force the text input field to be inputable regardless of whether it has been selected by the user? Useful to e.g. make a text field inputable immediately after it appears in a newly opened dialog.

colorScheme ColorScheme

Optionally override the current default color scheme for this element.

Return Value

Type: bool

true if the user has entered and confirmed the text (by pressing Enter), false otherwise.

Example (using GLFW):

static dchar staticUnicode;
// Buffer to store text input
char[128] textInputBuffer;
// Slice of textInputBuffer
char[] textEntered;

extern(C) static void getUnicode(GLFWwindow* w, uint unicode)
{
    staticUnicode = unicode;
}

extern(C) static void getKey(GLFWwindow* w, int key, int scancode, int action, int mods)
{
    if(action != GLFW_PRESS) { return; }
    if(key == GLFW_KEY_ENTER)          { staticUnicode = 0x0D; }
    else if(key == GLFW_KEY_BACKSPACE) { staticUnicode = 0x08; }
}

void init()
{
    GLFWwindow* window;

    // ... init the window here ...

    // Not really needed, but makes it obvious what we're doing
    textEntered = textInputBuffer[0 .. 0];
    glfwSetCharCallback(window, &getUnicode);
    glfwSetKeyCallback(window, &getKey);
}

void frame()
{
    // These should be defined somewhere
    int mouseX, mouseY, mouseScroll;
    ubyte mousebutton;

    // .. code here ..

    // Pass text input to imgui
    imguiBeginFrame(cast(int)mouseX, cast(int)mouseY, mousebutton, mouseScroll, staticUnicode);
    // reset staticUnicode for the next frame

    staticUnicode = 0;

    if(imguiTextInput("Text input:", textInputBuffer, textEntered))
    {
        import std.stdio;
        writeln("Entered text is: ", textEntered);
        // Reset entered text for next input (use e.g. textEntered.dup if you need a copy).
        textEntered = textInputBuffer[0 .. 0];
    }

    // .. more code here ..
}

Meta