How do I do something like a text box?

Is there something built in I that can use or some third party library that allows a text box? Like if I want the player to enter their name.

Thank you.

Comments

  • Hi Markz,

    There is raygui, a simple library to create basic interfaze elements but it's undocumented by now, you can check code and example tools: https://github.com/raysan5/raygui

    Also, here it is a quick example for a text box I did in one of my classes:


    int main()
    {
    // Initialization
    //--------------------------------------------------------------------------------------
    int screenWidth = 800;
    int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");

    char name[9] = "\0";
    int letterCount = 0;

    Rectangle textBox = { screenWidth/2 - 100, 180, 225, 50 };
    bool mouseOnText = false;

    int framesCounter = 0;

    SetTargetFPS(60);
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose()) // Detect window close button or ESC key
    {
    // Update
    //----------------------------------------------------------------------------------
    if (CheckCollisionPointRec(GetMousePosition(), textBox)) mouseOnText = true;
    else mouseOnText = false;

    if (mouseOnText)
    {
    int key = GetKeyPressed();

    if ((key >= 32) && (key <= 125) && (letterCount < 8))
    {
    name[letterCount] = (char)key;
    letterCount++;
    }

    if (key == KEY_BACKSPACE)
    {
    letterCount--;
    name[letterCount] = '\0';

    if (letterCount < 0) letterCount = 0;
    }
    }

    if (mouseOnText) framesCounter++;
    else framesCounter = 0;
    //----------------------------------------------------------------------------------

    // Draw
    //----------------------------------------------------------------------------------
    BeginDrawing();

    ClearBackground(RAYWHITE);

    DrawRectangleRec(textBox, LIGHTGRAY);
    DrawRectangleLines(textBox.x, textBox.y, textBox.width, textBox.height, DARKGRAY);

    DrawText(name, textBox.x + 5, textBox.y + 8, 40, MAROON);

    if (mouseOnText)
    {
    if ((letterCount < 8) &&((framesCounter/20)%2)) DrawText("_", textBox.x + 8 + MeasureText(name, 40), textBox.y + 12, 40, MAROON);
    }

    EndDrawing();
    //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow(); // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
    }


    Hope it helps... :smile:
Sign In or Register to comment.