Fixes
This commit is contained in:
170
Src/Editor/imp/cpp/UI/View/FilePreview.cpp
Normal file
170
Src/Editor/imp/cpp/UI/View/FilePreview.cpp
Normal 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
|
||||
Reference in New Issue
Block a user