This commit is contained in:
Isuru Samarathunga
2025-11-14 09:43:09 +05:30
parent 9ff39d7245
commit a2b80ef600
32 changed files with 928 additions and 295 deletions

View File

@ -16,13 +16,13 @@
#include <UI/View/Asset.hpp>
#include <IAEngine/Asset/AssetManager.hpp>
#include <RenderCore/RenderCore.hpp>
#include <Editor.hpp>
namespace ia::iae
{
struct AssetCacheEntry
{
};
VOID View_Asset::Initialize()
{
SetName("Asset");
@ -37,13 +37,47 @@ namespace ia::iae
{
PreRender();
if(m_assetPath.empty())
if (!m_asset)
{
PostRender();
return;
}
ImGui::Text("%s", m_assetPath.filename().string().c_str());
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();
}
@ -56,14 +90,125 @@ namespace ia::iae
{
}
VOID View_Asset::Open(IN Path path)
VOID View_Asset::Open(IN String assetName)
{
Close();
m_assetPath = path;
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_assetPath = Path();
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

View File

@ -147,6 +147,6 @@ namespace ia::iae
VOID View_AssetBrowser::OpenAsset(IN CONST std::filesystem::path &path)
{
Editor::Instance().OpenAsset(path);
Editor::Instance().OpenFile(path);
}
} // namespace ia::iae

View File

@ -0,0 +1,170 @@
// 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/FilePreview.hpp>
#include <IAEngine/Asset/AssetManager.hpp>
#include <RenderCore/RenderCore.hpp>
#include <Editor.hpp>
#include <fstream>
namespace ia::iae
{
BOOL is_extension_one_of(IN PCCHAR ext, IN std::initializer_list<PCCHAR> values)
{
for (const auto &t : values)
{
if (!strcmp(ext, t))
return true;
}
return false;
}
Vector<String> get_lines_of_file(IN CONST Path &path)
{
Vector<String> result;
std::string line;
std::ifstream file(path);
while (std::getline(file, line))
result.pushBack(line.c_str());
return result;
}
VOID View_FilePreview::Initialize()
{
SetName("File Preview");
SetIcon(ICON_FA_MAGNIFYING_GLASS);
}
VOID View_FilePreview::Terminate()
{
}
VOID View_FilePreview::Render()
{
PreRender();
if (m_filePath.empty())
{
PostRender();
return;
}
switch (m_fileType)
{
case EFileType::UNKNOWN:
RenderUnknownFile();
break;
case EFileType::TEXT:
RenderTextFile();
break;
case EFileType::IMAGE:
RenderImageFile();
break;
}
PostRender();
}
VOID View_FilePreview::Update()
{
}
VOID View_FilePreview::OnEvent(IN SDL_Event *event)
{
}
VOID View_FilePreview::Open(IN Path path)
{
Close();
m_filePath = path;
m_fileType = EFileType::UNKNOWN;
const auto ext = path.extension().string();
if (is_extension_one_of(&ext[1], {"txt", "xml", "md", "cpp", "c", "hpp", "h"}))
{
m_fileType = EFileType::TEXT;
const auto lines = get_lines_of_file(path);
m_fileDataSize = lines.size();
const auto fileData = new String[m_fileDataSize];
for (SIZE_T i = 0; i < m_fileDataSize; i++)
fileData[i] = lines[i];
m_fileData = fileData;
}
else if (is_extension_one_of(&ext[1], {"png", "jpg", "webp"}))
{
m_fileType = EFileType::IMAGE;
m_fileDataSize = SIZE_MAX;
const auto image = AssetManager::LoadTexture(path.string().c_str())->GetHandle()->ImagePtr;
m_fileDataExtent = IVec2{image->Width, image->Height};
m_fileData = (PVOID)RDC::BakeTexture(image);
}
}
VOID View_FilePreview::Close()
{
switch (m_fileType)
{
case EFileType::UNKNOWN:
break;
case EFileType::TEXT:
delete[] ((String *) m_fileData);
m_fileDataSize = 0;
m_fileData = nullptr;
break;
case EFileType::IMAGE:
m_fileDataSize = 0;
m_fileData = nullptr;
m_fileDataExtent = {};
break;
}
m_filePath = Path();
m_fileType = EFileType::UNKNOWN;
}
VOID View_FilePreview::RenderTextFile()
{
const auto lines = (String*)m_fileData;
for(SIZE_T i = 0; i < m_fileDataSize; i++)
ImGui::Text("%s", lines[i].c_str());
}
VOID View_FilePreview::RenderImageFile()
{
UI::PadX();
UI::PadY();
if(ImGui::Button("Mark as Asset"))
Editor::Instance().MarkAsAsset(m_filePath);
UI::PadY();
const auto ImageWidth = m_extent.x * 0.75f;
UI::AlignCursorHCenter(ImRect{{}, m_extent}, ImageWidth);
const auto aspectRatio = static_cast<FLOAT32>(m_fileDataExtent.y)/static_cast<FLOAT32>(m_fileDataExtent.x);
ImGui::Image((ImTextureRef)m_fileData, {ImageWidth, aspectRatio * ImageWidth});
}
VOID View_FilePreview::RenderUnknownFile()
{
UI::DrawTextCentered(m_extent, "Unsupported File Format");
}
} // namespace ia::iae

View File

@ -0,0 +1,99 @@
// 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 <Editor.hpp>
#include <UI/View/Game.hpp>
#include <RenderCore/RenderCore.hpp>
#include <IAEngine/LibInterface.hpp>
namespace ia::iae
{
VOID View_Game::Initialize()
{
SetName("Game");
SetIcon(ICON_FA_GAMEPAD);
IAEngine::__Initialize(SCENE_EDITOR_RESOULTION, Editor::Instance().GetActiveProject()->AssetDirectory());
m_gameRenderTexture = new RDC_Texture(RDC_Texture::EType::SAMPLED, SCENE_EDITOR_RESOULTION.x, SCENE_EDITOR_RESOULTION.y);
}
VOID View_Game::Terminate()
{
delete m_gameRenderTexture;
}
VOID View_Game::Render()
{
PreRender();
ImGui::Image(m_gameRenderTexture->GetHandle(), m_extent);
PostRender();
}
VOID View_Game::Update()
{
STATIC INT32 frameCounter{0};
IAEngine::__Update();
frameCounter++;
if (frameCounter >= 60)
{
frameCounter = 0;
IAEngine::__FixedUpdate();
}
IAEngine::__RenderToTexture(m_gameRenderTexture->GetHandle());
}
VOID View_Game::OnEvent(IN SDL_Event *event)
{
IAEngine::__ProcessEvent(event);
}
} // namespace ia::iae
C_DECL(GameRequestedConfig *Game_GetConfigRequest())
{
return nullptr;
}
C_DECL(VOID Game_OnInitialize())
{
}
C_DECL(VOID Game_OnTerminate())
{
}
C_DECL(VOID Game_OnDebugDraw())
{
}
C_DECL(VOID Game_OnFixedUpdate())
{
}
C_DECL(VOID Game_OnUpdate(IN FLOAT32 deltaTime))
{
}
C_DECL(VOID Game_OnResize(IN INT32 newWidth, IN INT32 newHeight))
{
}

View File

@ -19,8 +19,6 @@
#include <RenderCore/RenderCore.hpp>
#include <IAEngine/LibInterface.hpp>
namespace ia::iae
{
VOID View_Scene::Initialize()
@ -28,21 +26,16 @@ namespace ia::iae
SetName("Scene");
SetIcon(ICON_FA_HASHTAG);
IAEngine::__Initialize(SCENE_EDITOR_RESOULTION, Editor::Instance().GetActiveProject()->AssetDirectory());
m_gamePreviewTexture = new RDC_Texture(RDC_Texture::EType::SAMPLED, SCENE_EDITOR_RESOULTION.x, SCENE_EDITOR_RESOULTION.y);
}
VOID View_Scene::Terminate()
{
delete m_gamePreviewTexture;
}
VOID View_Scene::Render()
{
PreRender();
ImGui::Image(m_gamePreviewTexture->GetHandle(), {SCENE_EDITOR_RESOULTION.x, SCENE_EDITOR_RESOULTION.y});
PostRender();
}
@ -51,49 +44,9 @@ namespace ia::iae
{
STATIC INT32 frameCounter{0};
IAEngine::__Update();
frameCounter++;
if (frameCounter >= 60)
{
frameCounter = 0;
IAEngine::__FixedUpdate();
}
IAEngine::__RenderToTexture(m_gamePreviewTexture->GetHandle());
}
VOID View_Scene::OnEvent(IN SDL_Event *event)
{
IAEngine::__ProcessEvent(event);
}
} // namespace ia::iae
C_DECL(GameRequestedConfig *Game_GetConfigRequest())
{
return nullptr;
}
C_DECL(VOID Game_OnInitialize())
{
}
C_DECL(VOID Game_OnTerminate())
{
}
C_DECL(VOID Game_OnDebugDraw())
{
}
C_DECL(VOID Game_OnFixedUpdate())
{
}
C_DECL(VOID Game_OnUpdate(IN FLOAT32 deltaTime))
{
}
C_DECL(VOID Game_OnResize(IN INT32 newWidth, IN INT32 newHeight))
{
}