Files
IAEngine/Src/Editor/imp/cpp/UI/View/Asset.cpp
Isuru Samarathunga a2b80ef600 Fixes
2025-11-14 09:43:09 +05:30

214 lines
6.2 KiB
C++

// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <UI/View/Asset.hpp>
#include <IAEngine/Asset/AssetManager.hpp>
#include <RenderCore/RenderCore.hpp>
#include <Editor.hpp>
namespace ia::iae
{
VOID View_Asset::Initialize()
{
SetName("Asset");
SetIcon(ICON_FA_STAR);
}
VOID View_Asset::Terminate()
{
}
VOID View_Asset::Render()
{
PreRender();
if (!m_asset)
{
PostRender();
return;
}
STATIC CHAR NAME_BUFFER[256];
memcpy(NAME_BUFFER, m_assetName.c_str(), m_assetName.size() + 1);
ImGui::Text("Name: ");
ImGui::SameLine();
ImGui::InputText("##name", NAME_BUFFER, sizeof(NAME_BUFFER));
m_assetName = NAME_BUFFER;
DrawAssetTypePicker();
switch (m_asset->GetType())
{
case EAssetType::TEXTURE:
RenderTextureAsset();
break;
case EAssetType::SPRITE:
RenderSpriteAsset();
break;
case EAssetType::TILESHEET:
RenderTileSheetAsset();
break;
case EAssetType::SPRITESHEET:
RenderSpriteSheetAsset();
break;
case EAssetType::INVALID:
case EAssetType::SOUND:
case EAssetType::SCENE:
case EAssetType::PLUGIN:
case EAssetType::PACKAGE:
break;
}
PostRender();
}
VOID View_Asset::Update()
{
}
VOID View_Asset::OnEvent(IN SDL_Event *event)
{
}
VOID View_Asset::Open(IN String assetName)
{
m_assetName = assetName;
const auto asset = AssetManager::GetAssetByName<Asset_Texture>(m_assetName);
m_assetImageHandle = RDC::BakeTexture(asset->GetHandle()->ImagePtr);
m_assetImageExtent = {asset->GetHandle()->ImagePtr->Width, asset->GetHandle()->ImagePtr->Height};
m_asset = asset;
m_assetType = asset->GetType();
}
VOID View_Asset::Close()
{
m_assetName = "";
m_asset = nullptr;
m_assetImageHandle = 0;
m_assetImageExtent = {};
m_assetType = EAssetType::INVALID;
}
VOID View_Asset::RenderTextureAsset()
{
const auto asset = (Asset_Texture *) m_asset;
DrawAssetImage(0.75f, {0, 0});
}
VOID View_Asset::RenderSpriteAsset()
{
const auto asset = (Asset_Sprite *) m_asset;
DrawAssetImage(0.75f, {0, 0});
}
VOID View_Asset::RenderTileSheetAsset()
{
const auto asset = (Asset_TileSheet *) m_asset;
ImGui::Text("Tile Width: ");
ImGui::SameLine();
ImGui::InputInt("##TileWidth", &asset->TileWidth());
if (asset->TileWidth() < 1)
asset->TileWidth() = 1;
if (asset->TileWidth() > m_assetImageExtent.x)
asset->TileWidth() = m_assetImageExtent.x;
ImGui::Text("Tile Height: ");
ImGui::SameLine();
ImGui::InputInt("##TileHeight", &asset->TileHeight());
if (asset->TileHeight() < 1)
asset->TileHeight() = 1;
if (asset->TileHeight() > m_assetImageExtent.y)
asset->TileHeight() = m_assetImageExtent.y;
DrawAssetImage(0.75f, {asset->TileWidth(), asset->TileHeight()});
}
VOID View_Asset::RenderSpriteSheetAsset()
{
const auto asset = (Asset_SpriteSheet *) m_asset;
}
VOID View_Asset::DrawAssetImage(IN FLOAT32 relativeWidth, IN IVec2 gridSize)
{
ImVec2 base_pos = ImGui::GetCursorScreenPos();
const auto ImageWidth = m_extent.x * relativeWidth;
const auto aspectRatio =
static_cast<FLOAT32>(m_assetImageExtent.y) / static_cast<FLOAT32>(m_assetImageExtent.x);
const auto ImageHeight = aspectRatio * ImageWidth;
ImGui::Image((ImTextureRef) m_assetImageHandle, {ImageWidth, ImageHeight});
if((!gridSize.x) || (!gridSize.y))
return;
gridSize.x *= ImageWidth/m_assetImageExtent.x;
gridSize.y *= ImageHeight/m_assetImageExtent.y;
ImDrawList *draw_list = ImGui::GetWindowDrawList();
auto gridCountX = ImageWidth / gridSize.x;
auto gridCountY = ImageHeight / gridSize.y;
ImU32 grid_color = IM_COL32(200, 200, 200, 50);
float grid_thickness = 1.0f;
ImGui::Dummy(ImVec2(ImageWidth, ImageHeight));
for (int i = 0; i <= gridCountX; ++i)
{
ImVec2 p1 = ImVec2(base_pos.x + i * gridSize.x, base_pos.y);
ImVec2 p2 = ImVec2(base_pos.x + i * gridSize.x, base_pos.y + ImageHeight);
draw_list->AddLine(p1, p2, grid_color, grid_thickness);
}
for (int i = 0; i <= gridCountY; ++i)
{
ImVec2 p1 = ImVec2(base_pos.x, base_pos.y + i * gridSize.y);
ImVec2 p2 = ImVec2(base_pos.x + ImageWidth, base_pos.y + i * gridSize.y);
draw_list->AddLine(p1, p2, grid_color, grid_thickness);
}
}
VOID View_Asset::DrawAssetTypePicker()
{
ImGui::Text("Asset Type: ");
ImGui::SameLine();
ImGui::Combo("##", (INT32 *) &m_assetType, "None\0Texture\0Sprite\0TileSheet\0SpriteSheet\0");
if (m_assetType != m_asset->GetType())
{
ImGui::SameLine();
if (ImGui::Button("Apply"))
{
if (m_assetType == EAssetType::INVALID)
{
Editor::Instance().RemoveFromAssets(m_assetName);
Close();
UI::CloseAssetView();
}
m_asset = AssetManager::ChangeAssetType(m_assetName, m_assetType);
}
}
}
} // namespace ia::iae