Skip to content

Instantly share code, notes, and snippets.

@JSandusky
Created July 15, 2018 18:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JSandusky/1f12766c15e77be4af919e5f8d6bfe56 to your computer and use it in GitHub Desktop.
Save JSandusky/1f12766c15e77be4af919e5f8d6bfe56 to your computer and use it in GitHub Desktop.
ImGui file list
std::vector<String> excludedExtensions = {
".exe",
".dll",
".bat",
".ini",
".layout",
".ilk",
".lib",
".pdb",
};
void GUI_AssetBrowserView::ShowDirectory(const Urho3D::String& path, const Urho3D::StringVector& content, ImGuiTextFilter* filter, bool asDetail)
{
auto imElem = BlockExe::GetInst()->GetImGuiElem();
float contentWidth = ImGui::GetContentRegionAvailWidth();
float contentHeight = ImGui::GetContentRegionAvail().y;
ImGui::BeginChild("##ContentRegion", { contentWidth, contentHeight }, true);
const float w = contentWidth;
const float itemSize = asDetail ? 48 : 128;
int index = 0;
int columns = contentWidth / (asDetail ? 256 : 128);
columns = columns < 1 ? 1 : columns;
float curX = 0.0f;
ImGui::Columns(columns, nullptr, false);
static String assetPopupPath;
for (size_t i = 0; i < content.Size(); ++i)
{
String item = content[i];
String itemPath = path + "/" + item;
if (item == "." || item == "..")
continue;
bool cancel = false;
for (auto exclude : excludedExtensions)
if (item.EndsWith(exclude))
cancel = true;
if (cancel)
continue;
if (!filter->PassFilter(item.CString()))
continue;
ImGui::PushID(i);
// filtering makes this really really tricky to cache
Urho3D::SharedPtr<Texture2D> thumb = GetImage(itemPath);
auto filName = GetFileNameAndExtension(item);
ImGui::BeginGroup();
ImGui::ImageButton(thumb.Get(), { itemSize - 20, itemSize - 16 });// , { 0, 1 }, { 1,0 });
if (ImGui::IsMouseDoubleClicked(0) && ImGui::IsItemHovered()) // change target directory
{
if (IsFile(itemPath))
{
if (!DocumentManager::Get()->OpenPath(itemPath))
BlockExe::GetInst()->GetContext()->GetSubsystem<FileSystem>()->SystemOpen(itemPath);
}
else
directory_ = itemPath;
}
else if (ImGui::IsMouseClicked(1) && ImGui::IsItemHovered())
{
ImGui::OpenPopup("##asset_browser_popup");
assetPopupPath = itemPath;
}
else if (ImGui::IsItemHovered() && asDetail && filName.Contains('.'))
{
ImGui::BeginTooltip();
ImGui::Image(thumb.Get(), { 128, 128 });
ImGui::EndTooltip();
}
if (ImGui::BeginPopup("##asset_browser_popup"))
{
auto ext = Urho3D::GetExtension(assetPopupPath);
auto found = fileHandlers_.Find(ext);
if (found != fileHandlers_.End())
found->second_(assetPopupPath);
else
{
if (ImGui::MenuItem(ICON_FA_EDIT " Open (system)"))
BlockExe::GetInst()->GetSubsystem<FileSystem>()->SystemOpen(assetPopupPath);
}
// Script extension point
auto& eventData = BlockExe::GetInst()->GetEventDataMap();
eventData["SelectedAssetPath"] = assetPopupPath;
eventData["SelectedAssetExt"] = ext;
if (ext == ".xml")
{
XMLFile file(BlockExe::GetInst()->GetContext());
if (file.LoadFile(assetPopupPath))
eventData["XmlRoot"] = file.GetRoot().GetName().ToLower();
}
BlockExe::GetInst()->SendEvent("ASSET_BROWSER_CONTEXT", eventData);
ImGui::EndPopup();
}
if (asDetail)
ImGui::SameLine();
ImGui::TextWrapped(filName.CString());
if (ImGui::IsItemHovered() && !asDetail)
ImGui::SetTooltip(filName.CString());
ImGui::EndGroup();
if (!ImGui::IsPopupOpen((ImGuiID)0) && ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID))
{
if (std::find(recentDirectories_.begin(), recentDirectories_.end(), directory_) == recentDirectories_.end())
recentDirectories_.insert(recentDirectories_.begin(), directory_);
ImGui::SetDragDropPayload("U_RES", itemPath.CString(), itemPath.Length());
ImGui::Image(thumb.Get(), { 128, 128 });
ImGui::Text(filName.CString());
ImGui::EndDragDropSource();
}
ImGui::PopID();
ImGui::NextColumn();
}
ImGui::EndChild();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment