Improve some code for #543

This commit is contained in:
halx99 2022-02-08 21:54:21 +08:00
parent 6bde63a054
commit 93e250889f
3 changed files with 1 additions and 89 deletions

View File

@ -700,78 +700,4 @@ int ImGuiEXT::getCCRefId(Ref* p)
return (int)hash;
}
#if defined(HAVE_IMGUI_MARKDOWN)
# include "imgui_markdown/imgui_markdown.h"
static ImGuiEXT::MdLinkCallback ImGuiMarkdownLinkCallback = nullptr;
static ImGuiEXT::MdImageCallback ImGuiMarkdownImageCallback = nullptr;
static ImGui::MarkdownImageData ImGuiMarkdownInvalidImageData = {false, false, nullptr, {0.f, 0.f}};
void MarkdownLinkCallback(ImGui::MarkdownLinkCallbackData data)
{
if (ImGuiMarkdownLinkCallback)
{
ImGuiMarkdownLinkCallback({data.text, (size_t)data.textLength}, {data.link, (size_t)data.linkLength},
data.isImage);
}
}
ImGui::MarkdownImageData MarkdownImageCallback(ImGui::MarkdownLinkCallbackData data)
{
if (!data.isImage || !ImGuiMarkdownImageCallback)
return ImGuiMarkdownInvalidImageData;
Sprite* sp;
ImVec2 size;
ImVec4 tint_col;
ImVec4 border_col;
std::tie(sp, size, tint_col, border_col) =
ImGuiMarkdownImageCallback({data.text, (size_t)data.textLength}, {data.link, (size_t)data.linkLength});
if (!sp || !sp->getTexture())
return ImGuiMarkdownInvalidImageData;
auto size_ = size;
const auto rect = sp->getTextureRect();
if (size_.x <= 0.f)
size_.x = rect.size.width;
if (size_.y <= 0.f)
size_.y = rect.size.height;
ImVec2 uv0, uv1;
std::tie(uv0, uv1) = getTextureUV(sp);
ImGuiEXT::getInstance()->getCCRefId(sp);
return {true, true, (ImTextureID)sp->getTexture(), size_, uv0, uv1, tint_col, border_col};
}
static std::string ImGuiMarkdownLinkIcon;
static ImGui::MarkdownConfig ImGuiMarkdownConfig = {MarkdownLinkCallback, MarkdownImageCallback, ""};
void ImGuiEXT::setMarkdownLinkCallback(const MdLinkCallback& f)
{
ImGuiMarkdownLinkCallback = f;
}
void ImGuiEXT::setMarkdownImageCallback(const MdImageCallback& f)
{
ImGuiMarkdownImageCallback = f;
}
void ImGuiEXT::setMarkdownFont(int index, ImFont* font, bool seperator, float scale)
{
if (index < 0 || index >= ImGui::MarkdownConfig::NUMHEADINGS)
return;
ImGuiMarkdownConfig.headingFormats[index] = {font, seperator};
ImGuiMarkdownConfig.headingScales[index] = scale;
}
void ImGuiEXT::setMarkdownLinkIcon(std::string_view icon)
{
ImGuiMarkdownLinkIcon = icon;
ImGuiMarkdownConfig.linkIcon = ImGuiMarkdownLinkIcon.c_str();
}
void ImGuiEXT::markdown(std::string_view content)
{
ImGui::Markdown(content.c_str(), content.size(), ImGuiMarkdownConfig);
}
#endif
NS_CC_EXT_END

View File

@ -116,20 +116,6 @@ public:
static void mergeFontGlyphs(ImFont* dst, ImFont* src, ImWchar start, ImWchar end);
int getCCRefId(Ref* p);
#if defined(HAVE_IMGUI_MARKDOWN)
// markdown
using MdLinkCallback = std::function<void(const std::string&, const std::string&, bool)>;
using MdImageCallback =
std::function<std::tuple<Sprite*, ImVec2, ImVec4, ImVec4>(const std::string&, const std::string&)>;
void setMarkdownLinkCallback(const MdLinkCallback& f);
void setMarkdownImageCallback(const MdImageCallback& f);
void setMarkdownFont(int index, ImFont* font, bool seperator, float scale = 1.f);
void setMarkdownLinkIcon(std::string_view icon);
void markdown(std::string_view content);
#endif
private:
static void loadCustomFonts(void*);

View File

@ -803,7 +803,7 @@ void AssetsManagerEx::startUpdate()
Manifest::AssetDiff diff = it->second;
if (diff.type != Manifest::DiffType::DELETED)
{
const std::string& path = diff.asset.path;
auto& path = diff.asset.path;
DownloadUnit unit;
unit.customId = it->first;
unit.srcUrl = packageUrl;