void AssetBrowser::onGUI()
{
	if (m_wanted_resource.isValid())
	{
		selectResource(m_wanted_resource);
		m_wanted_resource = "";
	}

	if (!ImGui::BeginDock("Asset Browser", &m_is_opened))
	{
		if (m_activate) ImGui::SetDockActive();
		m_activate = false;
		ImGui::EndDock();
		return;
	}

	if (m_activate) ImGui::SetDockActive();
	m_activate = false;

	if (m_is_focus_requested)
	{
		m_is_focus_requested = false;
		ImGui::SetWindowFocus();
	}

	if (ImGui::Button("Refresh")) findResources();
	ImGui::SameLine();
	ImGui::Checkbox("Autoreload", &m_autoreload_changed_resource);

	auto getter = [](void* data, int idx, const char** out) -> bool
	{
		auto& browser = *static_cast<AssetBrowser*>(data);
		*out = browser.m_plugins[idx]->getName();
		return true;
	};

	ImGui::Combo("Type", &m_current_type, getter, this, m_plugins.size());
	ImGui::InputText("Filter", m_filter, sizeof(m_filter));

	ImGui::ListBoxHeader("Resources");
	auto& resources = m_resources[m_current_type + 1];

	for (auto& resource : resources)
	{
		if (m_filter[0] != '\0' && strstr(resource.c_str(), m_filter) == nullptr) continue;

		bool is_selected = m_selected_resource ? m_selected_resource->getPath() == resource : false;
		if (ImGui::Selectable(resource.c_str(), is_selected))
		{
			selectResource(resource);
		}
	}
	ImGui::ListBoxFooter();
	onGUIResource();
	ImGui::EndDock();
}
void AssetBrowser::onGUIResource()
{
	if (!m_selected_resource) return;

	const char* path = m_selected_resource->getPath().c_str();
	ImGui::Separator();
	ImGui::LabelText("Selected resource", "%s", path);
	if (!m_history.empty() && ImGui::Button("Back"))
	{
		selectResource(m_history.back());
		m_history.pop();
		m_history.pop();
		return;
	}
	ImGui::Separator();

	if (!m_selected_resource->isReady() && !m_selected_resource->isFailure())
	{
		ImGui::Text("Not ready");
		return;
	}

	char source[Lumix::MAX_PATH_LENGTH];
	if (m_metadata.getString(m_selected_resource->getPath().getHash(), SOURCE_HASH, source, Lumix::lengthOf(source)))
	{
		ImGui::LabelText("Source", "%s", source);
	}

	auto resource_type = getResourceType(path);
	for (auto* plugin : m_plugins)
	{
		if (plugin->onGUI(m_selected_resource, resource_type)) return;
	}
	ASSERT(resource_type == UNIVERSE_HASH); // unimplemented resource
}
Exemple #3
0
void AssetBrowser::selectResource(const Lumix::Path& resource, bool record_history)
{
	m_activate = true;
	char ext[30];
	Lumix::PathUtils::getExtension(ext, Lumix::lengthOf(ext), resource.c_str());

	auto& manager = m_editor.getEngine().getResourceManager();
	auto* resource_manager = manager.get(getResourceType(resource.c_str()));
	if (resource_manager) selectResource(resource_manager->load(resource), record_history);
}
void AssetBrowser::selectResource(const Lumix::Path& resource)
{
	m_activate = true;
	char ext[30];
	Lumix::PathUtils::getExtension(ext, Lumix::lengthOf(ext), resource.c_str());
	if (Lumix::compareString(ext, "unv") == 0) return;

	auto& manager = m_editor.getEngine().getResourceManager();
	auto* resource_manager = manager.get(getResourceType(resource.c_str()));
	if (resource_manager) selectResource(resource_manager->load(resource));
}
void AssetBrowser::goForward()
{
	m_history_index = Lumix::Math::minimum(m_history_index + 1, m_history.size() - 1);
	selectResource(m_history[m_history_index], false);
}
void AssetBrowser::goBack()
{
	if (m_history_index < 1) return;
	m_history_index = Lumix::Math::maximum(0, m_history_index - 1);
	selectResource(m_history[m_history_index], false);
}
void AssetBrowser::onGUI()
{
	if (m_wanted_resource.isValid())
	{
		selectResource(m_wanted_resource, true);
		m_wanted_resource = "";
	}

	if (!ImGui::BeginDock("Asset Browser", &m_is_opened))
	{
		if (m_activate) ImGui::SetDockActive();
		m_activate = false;
		ImGui::EndDock();
		return;
	}

	onToolbar();

	if (ImGui::BeginChild("content"))
	{
		if (m_activate) ImGui::SetDockActive();
		m_activate = false;

		if (m_is_focus_requested)
		{
			m_is_focus_requested = false;
			ImGui::SetWindowFocus();
		}

		auto getter = [](void* data, int idx, const char** out) -> bool
		{
			auto& browser = *static_cast<AssetBrowser*>(data);
			*out = browser.m_plugins[idx]->getName();
			return true;
		};

		ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
		ImGui::Combo("Type", &m_current_type, getter, this, m_plugins.size());
		ImGui::FilterInput("Filter", m_filter, sizeof(m_filter));

		static ImVec2 size(0, 200);
		ImGui::ListBoxHeader("Resources", size);
		auto& resources = m_resources[m_current_type + 1];

		for (auto& resource : resources)
		{
			if (m_filter[0] != '\0' && strstr(resource.c_str(), m_filter) == nullptr) continue;

			bool is_selected = m_selected_resource ? m_selected_resource->getPath() == resource : false;
			if (ImGui::Selectable(resource.c_str(), is_selected))
			{
				selectResource(resource, true);
			}
			if (ImGui::IsMouseDragging() && ImGui::IsItemActive())
			{
				m_app.startDrag(StudioApp::DragData::PATH, resource.c_str(), Lumix::stringLength(resource.c_str()) + 1);
			}
		}
		ImGui::ListBoxFooter();
		ImGui::HSplitter("splitter", &size);

		ImGui::PopItemWidth();
		onGUIResource();
	}
	ImGui::EndChild();
	ImGui::EndDock();
}