5 days ago

Hi everyone, I'm trying to make a search bar with buffwr. If we go through something simple like this:

module searchbar;

import raylib;
import std.string;
import std.stdio;
import std.array;
import std.algorithm;

struct Person {
    string name;
    string email;
}

class SearchBar {
private:
    Rectangle bounds;
    char[256] inputBuffer;
    bool isActive;
    Person[] personList;
    Person* selectedPerson;
    Color backgroundColor;
    Color textColor;
    Color cursorColor;
    Font font;

public:
    this(Rectangle rect, Font displayFont) {
        bounds = rect;
        font = displayFont;
        backgroundColor = LIGHTGRAY;
        textColor = BLACK;
        cursorColor = BLUE;
        isActive = false;
        personList = [
            Person("John Doe", "john.doe@example.com"),
            Person("Jane Smith", "jane.smith@example.com"),
            Person("Mike Johnson", "mike.johnson@example.com")
        ];
    }

    void draw() {
        // Draw search bar background
        DrawRectangleRec(bounds, backgroundColor);
        DrawRectangleLinesEx(bounds, 2, DARKGRAY);

        // Draw input text
        Vector2 textPos = Vector2(bounds.x + 10, bounds.y + bounds.height / 4);

        // Show placeholder if no text
        if (inputBuffer[0] == '\0') {
            DrawTextEx(font, "Search...", textPos, font.baseSize, 1, GRAY);
        } else {
            DrawTextEx(font, inputBuffer.ptr, textPos, font.baseSize, 1, textColor);
        }

        // Draw cursor if active
        if (isActive && (GetTime() * 2 % 2 < 1)) {
            float cursorX = textPos.x + MeasureTextEx(font, inputBuffer.ptr, font.baseSize, 1).x;
            DrawLineEx(
                Vector2(cursorX, textPos.y),
                Vector2(cursorX, textPos.y + font.baseSize),
                2,
                cursorColor
            );
        }

        // If a person is selected, draw their name
        if (selectedPerson !is null) {
            Vector2 personPos = Vector2(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
            DrawTextEx(font, selectedPerson.name.ptr, personPos, font.baseSize, 1, BLUE);
        }
    }

    void handleInput() {
        // Check if search bar is clicked
        if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
            Vector2 mousePoint = GetMousePosition();
            isActive = CheckCollisionPointRec(mousePoint, bounds);
        }

        // Handle text input when active
        if (isActive) {
            int key = GetCharPressed();
            while (key > 0) {
                // Only accept printable characters
                if (key >= 32 && key <= 125) {
                    int len = cast(int)strlen(inputBuffer.ptr);
                    if (len < inputBuffer.length - 1) {
                        inputBuffer[len] = cast(char)key;
                        inputBuffer[len + 1] = '\0';
                    }
                }
                key = GetCharPressed();
            }

            // Handle backspace
            if (IsKeyPressed(KEY_BACKSPACE)) {
                int len = cast(int)strlen(inputBuffer.ptr);
                if (len > 0) {
                    inputBuffer[len - 1] = '\0';
                }
            }

            // Search for matching person
            searchPerson();
        }
    }

    void searchPerson() {
        string searchQuery = to!string(inputBuffer.ptr);

        // Clear previous selection
        selectedPerson = null;

        // Find person matching the search query
        foreach (ref person; personList) {
            if (person.name.toLower.canFind(searchQuery.toLower) ||
                person.email.toLower.canFind(searchQuery.toLower)) {
                selectedPerson = &person;
                break;
            }
        }
    }
}

void main() {
    // Initialize window
    enum SCREEN_WIDTH = 800;
    enum SCREEN_HEIGHT = 600;

    InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "D SearchBar Example");
    SetTargetFPS(60);

    // Load font
    Font font = LoadFontEx("resources/roboto.ttf", 20, null, 0);

    // Create search bar
    Rectangle searchBarBounds = Rectangle(
        SCREEN_WIDTH / 2 - 200,
        SCREEN_HEIGHT / 2 - 20,
        400,
        40
    );
    auto searchBar = new SearchBar(searchBarBounds, font);

    // Main game loop
    while (!WindowShouldClose()) {
        // Update
        searchBar.handleInput();

        // Draw
        BeginDrawing();
        ClearBackground(RAYWHITE);

        searchBar.draw();

        EndDrawing();
    }

    // Cleanup
    UnloadFont(font);
    CloseWindow();
}

Would you mind if I could use your suggestions?

Thanks...

SDB@79