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
#include "game/structs.hpp"
#include "gui.hpp"
namespace gui::asset_list
{
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);
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/hook.hpp>
namespace gui::asset_list::assets::material
namespace gui::asset_list::material
{
namespace
{
std::unordered_set<std::string> opened_assets;
std::unordered_map<unsigned char, std::string> image_type_names =
{
{0x0, "2D"},
@ -38,11 +36,6 @@ namespace gui::asset_list::assets::material
{0xE, "PARALLAX_MAP"},
};
void add_material_view(const std::string& name)
{
opened_assets.insert(name);
}
std::string get_image_type_name(unsigned char type)
{
if (!image_type_names.contains(type))
@ -53,47 +46,38 @@ namespace gui::asset_list::assets::material
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)
ImGui::SetNextItemOpen(true, ImGuiCond_FirstUseEver);
if (ImGui::TreeNode("textures"))
{
return false;
}
auto is_open = true;
if (ImGui::Begin(name.data(), &is_open))
{
ImGui::SetNextItemOpen(true, ImGuiCond_FirstUseEver);
if (ImGui::TreeNode("textures"))
for (auto i = 0; i < asset->textureCount; i++)
{
for (auto i = 0; i < asset->textureCount; i++)
if (asset->textureTable && asset->textureTable->u.image && asset->textureTable->u.image->texture.shaderView)
{
if (asset->textureTable && asset->textureTable->u.image && asset->textureTable->u.image->texture.shaderView)
const auto type_name = get_image_type_name(asset->textureTable[i].semantic);
ImGui::Text("%s", type_name.data());
ImGui::SameLine();
if (ImGui::Button(asset->textureTable[i].u.image->name))
{
const auto type_name = get_image_type_name(asset->textureTable[i].semantic);
ImGui::Text("%s", type_name.data());
ImGui::SameLine();
if (ImGui::Button(asset->textureTable[i].u.image->name))
{
gui::copy_to_clipboard(asset->textureTable->u.image->name);
}
const auto width = asset->textureTable[i].u.image->width;
const auto height = asset->textureTable[i].u.image->height;
const auto ratio = static_cast<float>(width) / static_cast<float>(height);
constexpr auto size = 200.f;
ImGui::Image(asset->textureTable[i].u.image->texture.shaderView,
ImVec2(size * ratio, size)
);
gui::copy_to_clipboard(asset->textureTable->u.image->name);
}
}
ImGui::TreePop();
const auto width = asset->textureTable[i].u.image->width;
const auto height = asset->textureTable[i].u.image->height;
const auto ratio = static_cast<float>(width) / static_cast<float>(height);
constexpr auto size = 200.f;
ImGui::Image(asset->textureTable[i].u.image->texture.shaderView,
ImVec2(size * ratio, size)
);
}
}
ImGui::TreePop();
}
#define DRAW_ASSET_PROPERTY(__name__, __fmt__) \
ImGui::Text(#__name__ ": " __fmt__, asset->__name__); \
@ -105,36 +89,16 @@ namespace gui::asset_list::assets::material
gui::copy_to_clipboard(asset->__name__); \
} \
DRAW_ASSET_PROPERTY_COPY(name);
DRAW_ASSET_PROPERTY_COPY(techniqueSet->name);
DRAW_ASSET_PROPERTY(textureCount, "%i");
DRAW_ASSET_PROPERTY(constantCount, "%i");
DRAW_ASSET_PROPERTY(stateBitsCount, "%i");
DRAW_ASSET_PROPERTY(stateFlags, "%i");
DRAW_ASSET_PROPERTY(cameraRegion, "%i");
DRAW_ASSET_PROPERTY(materialType, "%i");
DRAW_ASSET_PROPERTY(layerCount, "%i");
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;
}
}
DRAW_ASSET_PROPERTY_COPY(name);
DRAW_ASSET_PROPERTY_COPY(techniqueSet->name);
DRAW_ASSET_PROPERTY(textureCount, "%i");
DRAW_ASSET_PROPERTY(constantCount, "%i");
DRAW_ASSET_PROPERTY(stateBitsCount, "%i");
DRAW_ASSET_PROPERTY(stateFlags, "%i");
DRAW_ASSET_PROPERTY(cameraRegion, "%i");
DRAW_ASSET_PROPERTY(materialType, "%i");
DRAW_ASSET_PROPERTY(layerCount, "%i");
DRAW_ASSET_PROPERTY(assetFlags, "%X");
}
}
@ -143,10 +107,9 @@ namespace gui::asset_list::assets::material
public:
void post_unpack() override
{
gui::asset_list::add_asset_view_callback(game::ASSET_TYPE_MATERIAL, add_material_view);
gui::register_callback(draw_materials, false);
gui::asset_list::add_asset_view<game::Material>(game::ASSET_TYPE_MATERIAL, draw_material_window);
}
};
}
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/hook.hpp>
namespace gui::asset_list::assets::xmodel
namespace gui::asset_list::xmodel
{
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,
bool rotate = false, float rotation_speed = 0.f)
{
@ -184,51 +177,42 @@ 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;
}
static bool flip_axis = false;
ImGui::Checkbox("flip axis", &flip_axis);
auto is_open = true;
if (ImGui::Begin(name.data(), &is_open))
ImGui::SetNextItemOpen(true, ImGuiCond_FirstUseEver);
if (ImGui::TreeNode("3d mesh"))
{
static bool flip_axis = false;
ImGui::Checkbox("flip axis", &flip_axis);
int vert_count = 0;
ImGui::SetNextItemOpen(true, ImGuiCond_FirstUseEver);
if (ImGui::TreeNode("3d mesh"))
game::vec3_t mins{};
game::vec3_t maxs{};
game::vec3_t origin{};
game::vec2_t maxs_2d{};
vert_count += sum_verts_in_xmodels(asset, mins, maxs, origin);
for (auto i = 0; i < asset->numCompositeModels; i++)
{
int vert_count = 0;
game::vec3_t mins{};
game::vec3_t maxs{};
game::vec3_t origin{};
game::vec2_t maxs_2d{};
vert_count += sum_verts_in_xmodels(asset, mins, maxs, origin);
for (auto i = 0; i < asset->numCompositeModels; i++)
{
vert_count += sum_verts_in_xmodels(asset->compositeModels[i], mins, maxs, origin);
}
for (auto o = 0; o < 3; o++)
{
origin[o] /= static_cast<float>(vert_count);
}
draw_xmodel(asset, flip_axis, mins, maxs, origin, maxs_2d);
for (auto i = 0; i < asset->numCompositeModels; i++)
{
draw_xmodel(asset->compositeModels[i], flip_axis, mins, maxs, origin, maxs_2d);
}
ImGui::Dummy(ImVec2(maxs_2d[0] + 800, maxs_2d[1] + 250));
ImGui::TreePop();
vert_count += sum_verts_in_xmodels(asset->compositeModels[i], mins, maxs, origin);
}
for (auto o = 0; o < 3; o++)
{
origin[o] /= static_cast<float>(vert_count);
}
draw_xmodel(asset, flip_axis, mins, maxs, origin, maxs_2d);
for (auto i = 0; i < asset->numCompositeModels; i++)
{
draw_xmodel(asset->compositeModels[i], flip_axis, mins, maxs, origin, maxs_2d);
}
ImGui::Dummy(ImVec2(maxs_2d[0] + 800, maxs_2d[1] + 250));
ImGui::TreePop();
}
#define DRAW_ASSET_PROPERTY(__name__, __fmt__) \
ImGui::Text(#__name__ ": " __fmt__, asset->__name__); \
@ -240,80 +224,60 @@ namespace gui::asset_list::assets::xmodel
gui::copy_to_clipboard(asset->__name__); \
} \
DRAW_ASSET_PROPERTY_COPY(name);
DRAW_ASSET_PROPERTY(numRootBones, "%i");
DRAW_ASSET_PROPERTY(numsurfs, "%i");
DRAW_ASSET_PROPERTY(lodRampType, "%i");
DRAW_ASSET_PROPERTY(numBonePhysics, "%i");
DRAW_ASSET_PROPERTY(numCompositeModels, "%i");
DRAW_ASSET_PROPERTY(unk_float, "%f");
DRAW_ASSET_PROPERTY(scale, "%f");
DRAW_ASSET_PROPERTY_COPY(name);
DRAW_ASSET_PROPERTY(numRootBones, "%i");
DRAW_ASSET_PROPERTY(numsurfs, "%i");
DRAW_ASSET_PROPERTY(lodRampType, "%i");
DRAW_ASSET_PROPERTY(numBonePhysics, "%i");
DRAW_ASSET_PROPERTY(numCompositeModels, "%i");
DRAW_ASSET_PROPERTY(unk_float, "%f");
DRAW_ASSET_PROPERTY(scale, "%f");
if (ImGui::TreeNode("bones"))
if (ImGui::TreeNode("bones"))
{
for (auto i = 0; i < asset->numBones; i++)
{
for (auto i = 0; i < asset->numBones; i++)
const auto bone_name = game::SL_ConvertToString(asset->boneNames[i]);
if (bone_name)
{
const auto bone_name = game::SL_ConvertToString(asset->boneNames[i]);
if (bone_name)
if (ImGui::Button(bone_name))
{
if (ImGui::Button(bone_name))
{
gui::copy_to_clipboard(bone_name);
}
gui::copy_to_clipboard(bone_name);
}
}
ImGui::TreePop();
}
if (ImGui::TreeNode("surface materials"))
{
for (auto i = 0; i < asset->numsurfs; i++)
{
if (ImGui::Button(asset->materialHandles[i]->name))
{
gui::copy_to_clipboard(asset->materialHandles[i]->name);
}
add_view_button(i, game::ASSET_TYPE_MATERIAL, asset->materialHandles[i]->name);
}
ImGui::TreePop();
}
if (asset->numCompositeModels > 0)
{
if (ImGui::TreeNode("composite models"))
{
for (auto i = 0; i < asset->numCompositeModels; i++)
{
if (ImGui::Button(asset->compositeModels[i]->name))
{
gui::copy_to_clipboard(asset->compositeModels[i]->name);
}
}
ImGui::TreePop();
}
}
ImGui::TreePop();
}
ImGui::End();
return is_open;
}
void draw_xmodels()
{
for (auto i = opened_assets.begin(); i != opened_assets.end(); )
if (ImGui::TreeNode("surface materials"))
{
if (!draw_material_window(*i))
for (auto i = 0; i < asset->numsurfs; i++)
{
i = opened_assets.erase(i);
if (ImGui::Button(asset->materialHandles[i]->name))
{
gui::copy_to_clipboard(asset->materialHandles[i]->name);
}
add_view_button(i, game::ASSET_TYPE_MATERIAL, asset->materialHandles[i]->name);
}
else
ImGui::TreePop();
}
if (asset->numCompositeModels > 0)
{
if (ImGui::TreeNode("composite models"))
{
++i;
for (auto i = 0; i < asset->numCompositeModels; i++)
{
if (ImGui::Button(asset->compositeModels[i]->name))
{
gui::copy_to_clipboard(asset->compositeModels[i]->name);
}
}
ImGui::TreePop();
}
}
}
@ -324,10 +288,9 @@ namespace gui::asset_list::assets::xmodel
public:
void post_unpack() override
{
gui::asset_list::add_asset_view_callback(game::ASSET_TYPE_XMODEL, add_xmodel_view);
gui::register_callback(draw_xmodels, false);
gui::asset_list::add_asset_view<game::XModel>(game::ASSET_TYPE_XMODEL, draw_xmodel_window);
}
};
}
REGISTER_COMPONENT(gui::asset_list::assets::xmodel::component)
REGISTER_COMPONENT(gui::asset_list::xmodel::component)