Refactor asset view stuff

This commit is contained in:
fed 2023-07-15 18:26:50 +02:00
parent 9e17d7fc10
commit ca4cdc31ff
3 changed files with 149 additions and 182 deletions

View File

@ -1,9 +1,50 @@
#pragma once #pragma once
#include "game/structs.hpp" #include "game/structs.hpp"
#include "gui.hpp"
namespace gui::asset_list namespace gui::asset_list
{ {
void add_asset_view_callback(game::XAssetType, const std::function<void(const std::string&)>& callback); void add_asset_view_callback(game::XAssetType, const std::function<void(const std::string&)>& callback);
void add_view_button(int id, game::XAssetType type, const char* name); void add_view_button(int id, game::XAssetType type, const char* name);
template <typename T>
void add_asset_view(game::XAssetType type, const std::function<void(T*)>& draw_callback)
{
static std::unordered_set<std::string> opened_assets;
add_asset_view_callback(type, [](const std::string& name)
{
opened_assets.insert(name);
});
gui::register_callback([=]()
{
for (auto i = opened_assets.begin(); i != opened_assets.end(); )
{
const auto& name = *i;
const auto header = reinterpret_cast<T*>(game::DB_FindXAssetHeader(type, name.data(), 0).data);
if (header == nullptr)
{
i = opened_assets.erase(i);
continue;
}
auto is_open = true;
if (ImGui::Begin(name.data(), &is_open))
{
draw_callback(header);
}
ImGui::End();
if (is_open)
{
++i;
}
else
{
i = opened_assets.erase(i);
}
}
}, false);
}
} }

View File

@ -13,12 +13,10 @@
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
namespace gui::asset_list::assets::material namespace gui::asset_list::material
{ {
namespace namespace
{ {
std::unordered_set<std::string> opened_assets;
std::unordered_map<unsigned char, std::string> image_type_names = std::unordered_map<unsigned char, std::string> image_type_names =
{ {
{0x0, "2D"}, {0x0, "2D"},
@ -38,11 +36,6 @@ namespace gui::asset_list::assets::material
{0xE, "PARALLAX_MAP"}, {0xE, "PARALLAX_MAP"},
}; };
void add_material_view(const std::string& name)
{
opened_assets.insert(name);
}
std::string get_image_type_name(unsigned char type) std::string get_image_type_name(unsigned char type)
{ {
if (!image_type_names.contains(type)) if (!image_type_names.contains(type))
@ -53,16 +46,7 @@ namespace gui::asset_list::assets::material
return image_type_names[type]; return image_type_names[type];
} }
bool draw_material_window(const std::string& name) void draw_material_window(game::Material* asset)
{
const auto asset = game::DB_FindXAssetHeader(game::ASSET_TYPE_MATERIAL, name.data(), 0).material;
if (asset == nullptr)
{
return false;
}
auto is_open = true;
if (ImGui::Begin(name.data(), &is_open))
{ {
ImGui::SetNextItemOpen(true, ImGuiCond_FirstUseEver); ImGui::SetNextItemOpen(true, ImGuiCond_FirstUseEver);
if (ImGui::TreeNode("textures")) if (ImGui::TreeNode("textures"))
@ -116,26 +100,6 @@ namespace gui::asset_list::assets::material
DRAW_ASSET_PROPERTY(layerCount, "%i"); DRAW_ASSET_PROPERTY(layerCount, "%i");
DRAW_ASSET_PROPERTY(assetFlags, "%X"); DRAW_ASSET_PROPERTY(assetFlags, "%X");
} }
ImGui::End();
return is_open;
}
void draw_materials()
{
for (auto i = opened_assets.begin(); i != opened_assets.end(); )
{
if (!draw_material_window(*i))
{
i = opened_assets.erase(i);
}
else
{
++i;
}
}
}
} }
class component final : public component_interface class component final : public component_interface
@ -143,10 +107,9 @@ namespace gui::asset_list::assets::material
public: public:
void post_unpack() override void post_unpack() override
{ {
gui::asset_list::add_asset_view_callback(game::ASSET_TYPE_MATERIAL, add_material_view); gui::asset_list::add_asset_view<game::Material>(game::ASSET_TYPE_MATERIAL, draw_material_window);
gui::register_callback(draw_materials, false);
} }
}; };
} }
REGISTER_COMPONENT(gui::asset_list::assets::material::component) REGISTER_COMPONENT(gui::asset_list::material::component)

View File

@ -13,17 +13,10 @@
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
namespace gui::asset_list::assets::xmodel namespace gui::asset_list::xmodel
{ {
namespace namespace
{ {
std::unordered_set<std::string> opened_assets;
void add_xmodel_view(const std::string& name)
{
opened_assets.insert(name);
}
ImVec2 project_vertex(game::vec3_t v, bool flip_axis, float scale = 1.f, ImVec2 project_vertex(game::vec3_t v, bool flip_axis, float scale = 1.f,
bool rotate = false, float rotation_speed = 0.f) bool rotate = false, float rotation_speed = 0.f)
{ {
@ -184,16 +177,7 @@ namespace gui::asset_list::assets::xmodel
} }
} }
bool draw_material_window(const std::string& name) void draw_xmodel_window(game::XModel* asset)
{
const auto asset = game::DB_FindXAssetHeader(game::ASSET_TYPE_XMODEL, name.data(), 0).model;
if (asset == nullptr)
{
return false;
}
auto is_open = true;
if (ImGui::Begin(name.data(), &is_open))
{ {
static bool flip_axis = false; static bool flip_axis = false;
ImGui::Checkbox("flip axis", &flip_axis); ImGui::Checkbox("flip axis", &flip_axis);
@ -297,26 +281,6 @@ namespace gui::asset_list::assets::xmodel
} }
} }
} }
ImGui::End();
return is_open;
}
void draw_xmodels()
{
for (auto i = opened_assets.begin(); i != opened_assets.end(); )
{
if (!draw_material_window(*i))
{
i = opened_assets.erase(i);
}
else
{
++i;
}
}
}
} }
class component final : public component_interface class component final : public component_interface
@ -324,10 +288,9 @@ namespace gui::asset_list::assets::xmodel
public: public:
void post_unpack() override void post_unpack() override
{ {
gui::asset_list::add_asset_view_callback(game::ASSET_TYPE_XMODEL, add_xmodel_view); gui::asset_list::add_asset_view<game::XModel>(game::ASSET_TYPE_XMODEL, draw_xmodel_window);
gui::register_callback(draw_xmodels, false);
} }
}; };
} }
REGISTER_COMPONENT(gui::asset_list::assets::xmodel::component) REGISTER_COMPONENT(gui::asset_list::xmodel::component)