Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IP entry window #388

Closed
adam4813 opened this issue Oct 24, 2015 · 7 comments
Closed

IP entry window #388

adam4813 opened this issue Oct 24, 2015 · 7 comments

Comments

@adam4813
Copy link

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

@adam4813
Copy link
Author

The 2 values are both highlighted and wrong in the picture only to demonstrate how those work. In use it will clamp the invalid octets to the range of 0-255 when it loses focus.

@adam4813
Copy link
Author

Moved to a wiki page IP Entry

@ocornut
Copy link
Owner

ocornut commented Oct 24, 2015

It seems unnecessary complicated to create 4 text fields instead of one (and parse the text there).

However if you do that you'd probably want to have the cursor automatically go from one field to the next when typing 3 characters or a space. Could be accomplished using the text callback to query cursor position (bit some complication).

Prior to "IP" you can call AlignFirstTextLineToWidget(). Also I am not sure why you are calling BeginGroup() it has no use there. I've seem many people use groups not as intended so my conclusion is that I need to write proper documentation about it :)

@adam4813
Copy link
Author

Ah thanks. Also I did use an InputInt4 but you don't have access to the
callback at that point. I suppose one could do a single TextInput and
filter/format it as text comes in, but this seemed a cleaner and simpler
approach that could be easily understood what is going on.

On Sat, Oct 24, 2015 at 3:45 PM, omar notifications@github.com wrote:

It seems unnecessary complicated to create 4 text fields instead of one
(and parse the text there).

However if you do that you'd probably want to have the cursor
automatically go from one field to the next when typing 3 characters or a
space. Could be accomplished using the text callback to query cursor
position (bit some complication).

Prior to "IP" you can call AlignFirstTextLineToWidget(). Also I am not
sure why you are calling BeginGroup() it has no use there. I've seem many
people use groups not as intended so my conclusion is that I need to write
proper documentation about it :)


Reply to this email directly or view it on GitHub
#388 (comment).

-Adam

@adam4813
Copy link
Author

AlignFirstTextLineToWidget() doesn't seem to exist?

@paultech
Copy link

Believe Omar meant AlignFirstTextHeightToWidgets()
You can use the CallbackCharFilter on a standard input to filter the input as its entered

@adam4813
Copy link
Author

adam4813 commented Oct 25, 2015 via email

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

No branches or pull requests

3 participants