Skip to content

IP entry window #388

Closed
Closed
@adam4813

Description

@adam4813

I have developed an IP entry box as I couldn't find any already made.

ImGui::SetNextWindowPosCenter();
ImGui::Begin("IP entry", false, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse);

int octets[4] = {123, 123, 123, 123};

float width = ImGui::CalcItemWidth();
ImGui::BeginGroup();
ImGui::PushID("IP");
ImGui::TextUnformatted("IP");
ImGui::SameLine();
for (int i = 0; i < 4; i++) {
    ImGui::PushItemWidth(width / 4.0f);
    ImGui::PushID(i);
    bool invalid_octet = false;
    if (octets[i] > 255) {
        // Make values over 255 red, and when focus is lost reset it to 255.
        octets[i] = 255;
        invalid_octet = true;
        ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.0f, 0.0f, 1.0f));
    }
    if (octets[i] < 0) {
        // Make values below 0 yellow, and when focus is lost reset it to 0.
        octets[i] = 0;
        invalid_octet = true;
        ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 0.0f, 1.0f));
    }
    ImGui::InputInt("##v", &octets[i], 0, 0, ImGuiInputTextFlags_CharsDecimal);
    if (invalid_octet) {
        ImGui::PopStyleColor();
    }
    ImGui::SameLine();
    ImGui::PopID();
    ImGui::PopItemWidth();
}
ImGui::PopID();
ImGui::EndGroup();

// Example action button and way to build the IP string
ImGui::SameLine();
if (ImGui::Button("Connect")) {
    std::stringstream ip;
    ip << octets[0] << "." << octets[1] << "." << octets[2] << "." << octets[3];
}
ImGui::End();
ImGui::SetWindowSize("IP entry", ImVec2(0, 0));

ip_box

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions