Add a screen to download gltf models

This commit is contained in:
Guillaume Meunier
2025-10-25 10:30:29 +02:00
parent 33a5bf7494
commit cb1d0d7c2c
28 changed files with 1698 additions and 399 deletions
+70 -3
View File
@@ -1246,10 +1246,18 @@ void ScrollWhenDragging()
void CenterTextH(const std::string & text)
{
float win_width = ImGui::GetWindowSize().x;
float text_width = ImGui::CalcTextSize(text.c_str()).x;
ImGui::SetCursorPosX((win_width - text_width) / 2);
ImGui::Text("%s", text.c_str());
std::vector<std::string> lines = utils::split(text);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {0, 0});
for (const auto & i: lines)
{
float text_width = ImGui::CalcTextSize(i.c_str()).x;
ImGui::SetCursorPosX((win_width - text_width) / 2);
ImGui::Text("%s", i.c_str());
}
ImGui::PopStyleVar();
ImGui::Dummy({}); // Make sure the original vertical ItemSpacing is respected
}
void CenterTextHV(const std::string & text)
@@ -1273,3 +1281,62 @@ void CenterTextHV(const std::string & text)
}
ImGui::PopStyleVar();
}
void InputText(const char * label, std::string & text, const ImVec2 & size, ImGuiInputTextFlags flags)
{
auto callback = [](ImGuiInputTextCallbackData * data) -> int {
std::string & text = *reinterpret_cast<std::string *>(data->UserData);
if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
{
assert(text.data() == data->Buf);
text.resize(data->BufTextLen);
data->Buf = text.data();
}
return 0;
};
ImGui::InputTextEx(label, nullptr, text.data(), text.size() + 1, size, flags | ImGuiInputTextFlags_CallbackResize, callback, &text);
}
bool RadioButtonWithoutCheckBox(const std::string & label, bool active, ImVec2 size_arg)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext & g = *GImGui;
const ImGuiStyle & style = g.Style;
const ImGuiID id = window->GetID(label.c_str());
const ImVec2 label_size = ImGui::CalcTextSize(label.c_str(), NULL, true);
const ImVec2 pos = window->DC.CursorPos;
ImVec2 size = ImGui::CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f);
const ImRect bb(pos, pos + size);
ImGui::ItemSize(bb, style.FramePadding.y);
if (!ImGui::ItemAdd(bb, id))
return false;
bool hovered, held;
bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held);
ImGuiCol_ col;
if ((held && hovered) || active)
col = ImGuiCol_ButtonActive;
else if (hovered)
col = ImGuiCol_ButtonHovered;
else
col = ImGuiCol_Button;
ImGui::RenderNavHighlight(bb, id);
ImGui::RenderFrame(bb.Min, bb.Max, ImGui::GetColorU32(col), true, style.FrameRounding);
ImVec2 TextAlign{0, 0.5f};
ImGui::RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, label.c_str(), NULL, &label_size, TextAlign, &bb);
IMGUI_TEST_ENGINE_ITEM_INFO(id, label.c_str(), g.LastItemData.StatusFlags);
return pressed;
}
+15
View File
@@ -243,5 +243,20 @@ public:
};
void ScrollWhenDragging();
void CenterTextH(const std::string & text);
void CenterTextHV(const std::string & text);
void InputText(const char * label, std::string & text, const ImVec2 & size, ImGuiInputTextFlags flags);
bool RadioButtonWithoutCheckBox(const std::string & label, bool active, ImVec2 size_arg);
template <typename T, typename U>
static bool RadioButtonWithoutCheckBox(const std::string & label, T & v, U v_button, ImVec2 size_arg)
{
const bool pressed = RadioButtonWithoutCheckBox(label, v == v_button, size_arg);
if (pressed)
v = v_button;
return pressed;
}
+91 -74
View File
@@ -1139,6 +1139,81 @@ std::shared_ptr<entt::registry> scene_loader::operator()(
return loaded_scene;
}
struct cache_entry
{
std::string filename;
int64_t timestamp = 0;
int64_t size = 0;
};
static cache_entry read_cache_entry(const std::filesystem::path & path)
{
cache_entry entry;
std::string json = utils::read_whole_file<std::string>(path / "index.json");
simdjson::dom::parser parser;
simdjson::dom::element root = parser.parse(json);
if (auto val = root["filename"]; val.is_string())
entry.filename = val.get_string().value();
if (auto val = root["size"]; val.is_int64())
entry.size = val.get_int64();
if (auto val = root["timestamp"]; val.is_int64())
entry.timestamp = val.get_int64();
return entry;
}
static void write_cache_entry(const std::filesystem::path & path, const cache_entry & entry)
{
std::ofstream json{path / "index.json"};
json << "{\"filename\":" << json_string(entry.filename);
json << ",\"size\":" << entry.size;
// TODO get a timestamp for assets
if (not entry.filename.starts_with("assets://"))
{
auto mtime = std::chrono::file_clock::to_sys(std::filesystem::last_write_time(entry.filename));
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(mtime.time_since_epoch()).count();
auto size = entry.size;
json << ",\"timestamp\":" << timestamp;
}
json << "}";
}
// Returns false if the cache is out of date and must be deleted
static bool check_cache_entry(const std::filesystem::path & path)
{
try
{
cache_entry entry = read_cache_entry(path);
utils::mapped_file file{entry.filename};
int64_t real_size = file.size();
if (real_size != entry.size)
return false;
// TODO get a timestamp for assets
if (not entry.filename.starts_with("assets://"))
{
auto mtime = std::chrono::file_clock::to_sys(std::filesystem::last_write_time(entry.filename));
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(mtime.time_since_epoch()).count();
if (timestamp != entry.timestamp)
return false;
}
}
catch (...)
{
return false;
}
return true;
}
std::shared_ptr<entt::registry> scene_loader::operator()(
const std::filesystem::path & gltf_path,
std::function<void(float)> progress_cb)
@@ -1169,6 +1244,14 @@ std::shared_ptr<entt::registry> scene_loader::operator()(
spdlog::debug("\"{}\" is not a directory: creating cache", gltf_texture_cache.native());
create_cache = true;
}
else if (not check_cache_entry(gltf_texture_cache))
{
std::error_code ec;
std::filesystem::remove_all(gltf_texture_cache, ec);
std::filesystem::create_directories(gltf_texture_cache);
spdlog::debug("\"{}\" is out of date: recreating cache", gltf_texture_cache.native());
create_cache = true;
}
}
else
{
@@ -1186,20 +1269,12 @@ std::shared_ptr<entt::registry> scene_loader::operator()(
if (create_cache)
{
std::ofstream json{gltf_texture_cache / "index.json"};
json << "{\"filename\":" << json_string(gltf_path.native());
json << ",\"size\":" << gltf_file.size();
// TODO get a timestamp for assets
if (not gltf_path.native().starts_with("assets://"))
{
auto mtime = std::chrono::file_clock::to_sys(std::filesystem::last_write_time(gltf_path));
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(mtime.time_since_epoch()).count();
auto size = gltf_file.size();
json << ",\"timestamp\":" << timestamp;
}
json << "}";
write_cache_entry(
gltf_texture_cache,
cache_entry{
.filename = gltf_path.native(),
.size = (int64_t)gltf_file.size(),
});
}
}
@@ -1215,69 +1290,11 @@ void scene_loader::clear_texture_cache()
if (not entry.is_directory())
continue;
std::string saved_filename;
int64_t saved_timestamp = 0;
int64_t saved_size = 0;
try
if (not check_cache_entry(entry))
{
std::string json = utils::read_whole_file<std::string>(entry.path() / "index.json");
simdjson::dom::parser parser;
simdjson::dom::element root = parser.parse(json);
if (auto val = root["filename"]; val.is_string())
saved_filename = val.get_string().value();
if (auto val = root["size"]; val.is_int64())
saved_size = val.get_int64();
if (auto val = root["timestamp"]; val.is_int64())
saved_timestamp = val.get_int64();
}
catch (std::exception & e)
{
// If the index file is not found, remove the cache
spdlog::info("{}: cannot open index.json ({}), removing cache directory", entry.path().native(), e.what());
spdlog::info("{}: removing cache directory", entry.path().native());
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
continue;
}
int64_t real_size = 0;
try
{
utils::mapped_file file{saved_filename};
real_size = file.size();
}
catch (std::exception & e)
{
spdlog::info("{}: cannot open {} ({}), removing cache directory", entry.path().native(), saved_filename, e.what());
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
continue;
}
if (real_size != saved_size)
{
spdlog::info("{}: wrong size for {} (cache: {}, current: {}), removing cache directory", entry.path().native(), saved_filename, saved_size, real_size);
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
continue;
}
// TODO get a timestamp for assets
if (not saved_filename.starts_with("assets://"))
{
auto mtime = std::chrono::file_clock::to_sys(std::filesystem::last_write_time(saved_filename));
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(mtime.time_since_epoch()).count();
if (timestamp != saved_timestamp)
{
spdlog::info("{}: wrong timestamp for {} (cache: {}, current: {}), removing cache directory", entry.path().native(), saved_filename, saved_timestamp, timestamp);
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
continue;
}
}
}
}