Skip to content

Instantly share code, notes, and snippets.

@LAK132
Last active March 23, 2022 07:25
Show Gist options
  • Select an option

  • Save LAK132/d60001c02e4c3b5b80a2976bda6ac91a to your computer and use it in GitHub Desktop.

Select an option

Save LAK132/d60001c02e4c3b5b80a2976bda6ac91a to your computer and use it in GitHub Desktop.

Revisions

  1. LAK132 revised this gist Aug 4, 2018. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions filebrowser.cpp
    Original file line number Diff line number Diff line change
    @@ -30,15 +30,18 @@ void windowOpenPath(complete<fs::path>& dir, bool file = true)
    ImGui::EndChild();
    ImGui::Separator();
    ImGui::Text(dir.value.u8string().c_str());
    if ((file && fs::is_regular_file(dir.value)) || (!file && fs::is_directory(dir.value)))
    if (file ? fs::is_regular_file(dir.value) : fs::is_directory(dir.value))
    {
    dir.done |= ImGui::Button("Open");
    ImGui::SameLine();
    }
    else if (!file && hasParent) if (ImGui::Button("Open"))
    else if (!file && hasParent)
    {
    dir.value = dir.value.parent_path();
    dir.done = true;
    if (ImGui::Button("Open"))
    {
    dir.value = dir.value.parent_path();
    dir.done = true;
    }
    ImGui::SameLine();
    }
    dir.good ^= ImGui::Button("Cancel");
  2. LAK132 revised this gist Aug 4, 2018. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions filebrowser.cpp
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,11 @@ void windowOpenPath(complete<fs::path>& dir, bool file = true)
    ImGui::Separator();
    }
    for(auto& d : fs::directory_iterator(p))
    if (ImGui::Button(d.path().filename().u8string().c_str()))
    {
    bool selected = dir.value == d.path();
    if (ImGui::Selectable(d.path().filename().u8string().c_str(), &selected))
    dir.value = d.path();
    }
    ImGui::EndChild();
    ImGui::Separator();
    ImGui::Text(dir.value.u8string().c_str());
    @@ -32,7 +35,7 @@ void windowOpenPath(complete<fs::path>& dir, bool file = true)
    dir.done |= ImGui::Button("Open");
    ImGui::SameLine();
    }
    else if (!file && hasParent && ImGui::Button("Open"))
    else if (!file && hasParent) if (ImGui::Button("Open"))
    {
    dir.value = dir.value.parent_path();
    dir.done = true;
  3. LAK132 revised this gist Aug 4, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions filebrowser.cpp
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ void windowOpenPath(complete<fs::path>& dir, bool file = true)
    ImGui::Text(dir.value.u8string().c_str());
    if ((file && fs::is_regular_file(dir.value)) || (!file && fs::is_directory(dir.value)))
    {
    ImGui::Button(&dir.done, "Open");
    dir.done |= ImGui::Button("Open");
    ImGui::SameLine();
    }
    else if (!file && hasParent && ImGui::Button("Open"))
    @@ -38,7 +38,7 @@ void windowOpenPath(complete<fs::path>& dir, bool file = true)
    dir.done = true;
    ImGui::SameLine();
    }
    ImGui::ButtonInv(&dir.good, "Cancel");
    dir.good ^= ImGui::Button("Cancel");
    }
    ImGui::End();
    }
  4. LAK132 created this gist Aug 3, 2018.
    44 changes: 44 additions & 0 deletions filebrowser.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    template<typename T> struct complete
    {
    bool good = true, done = false;
    T value;
    complete() {}
    complete(const T& t, bool g = true, bool d = false) : value(t), good(g), done(d) {}
    complete(T&& t, bool g = true, bool d = false) : value(t), good(g), done(d) {}
    };
    void windowOpenPath(complete<fs::path>& dir, bool file = true)
    {
    if (ImGui::Begin("Open File", &dir.good))
    {
    bool hasParent = dir.value.has_parent_path();
    bool isDir = fs::is_directory(dir.value);
    fs::path p = (isDir ? dir.value : (hasParent ? dir.value.parent_path() : fs::current_path()));
    const float footer = ImGui::GetStyle().ItemSpacing.y + (ImGui::GetFrameHeightWithSpacing() * 2);
    ImGui::BeginChild("Viewer", ImVec2(0, -footer));
    if ((hasParent && isDir) || (hasParent && dir.value.parent_path().has_parent_path()))
    {
    if (ImGui::Button("<- Back"))
    dir.value = isDir ? dir.value.parent_path() : dir.value.parent_path().parent_path();
    ImGui::Separator();
    }
    for(auto& d : fs::directory_iterator(p))
    if (ImGui::Button(d.path().filename().u8string().c_str()))
    dir.value = d.path();
    ImGui::EndChild();
    ImGui::Separator();
    ImGui::Text(dir.value.u8string().c_str());
    if ((file && fs::is_regular_file(dir.value)) || (!file && fs::is_directory(dir.value)))
    {
    ImGui::Button(&dir.done, "Open");
    ImGui::SameLine();
    }
    else if (!file && hasParent && ImGui::Button("Open"))
    {
    dir.value = dir.value.parent_path();
    dir.done = true;
    ImGui::SameLine();
    }
    ImGui::ButtonInv(&dir.good, "Cancel");
    }
    ImGui::End();
    }