Sprite component reflected

This commit is contained in:
WiggleWizard
2024-09-13 23:56:01 +01:00
parent dabd9c01a6
commit 5d0eb4a597
3 changed files with 23 additions and 81 deletions

View File

@@ -15,75 +15,6 @@ public:
void Draw(Nuake::Entity entity) override
{
if (!entity.HasComponent<Nuake::SpriteComponent>())
{
return;
}
auto& component = entity.GetComponent<Nuake::SpriteComponent>();
BeginComponentTable(SPRITE, Nuake::SpriteComponent);
{
{
ImGui::Text("Sprite");
ImGui::TableNextColumn();
std::string path = component.SpritePath;
ImGui::Button(path.empty() ? "Drag image" : component.SpritePath.c_str(), ImVec2(ImGui::GetContentRegionAvail().x, 0));
if (ImGui::BeginDragDropTarget())
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("_Image"))
{
char* file = (char*)payload->Data;
std::string fullPath = std::string(file, 512);
path = Nuake::FileSystem::AbsoluteToRelative(std::move(fullPath));
component.SpritePath = path;
component.LoadSprite();
}
ImGui::EndDragDropTarget();
}
ImGui::TableNextColumn();
ComponentTableReset(component.LockYRotation, false);
}
ImGui::TableNextColumn();
{
ImGui::Text("Billboard");
ImGui::TableNextColumn();
ImGui::Checkbox("##billboard", &component.Billboard);
ImGui::TableNextColumn();
ComponentTableReset(component.Billboard, false);
}
ImGui::TableNextColumn();
{
ImGui::Text("Lock Y rotation");
ImGui::TableNextColumn();
ImGui::Checkbox("##lockYRotation", &component.LockYRotation);
ImGui::TableNextColumn();
ComponentTableReset(component.LockYRotation, false);
}
ImGui::TableNextColumn();
{
ImGui::Text("Position Based");
if (ImGui::BeginItemTooltip())
{
ImGui::Text("Orientation is based on the position of the camera or the orientation of the camera.");
ImGui::EndTooltip();
}
ImGui::TableNextColumn();
ImGui::Checkbox("##positionbased", &component.PositionFacing);
ImGui::TableNextColumn();
ComponentTableReset(component.LockYRotation, false);
}
}
EndComponentTable();
}
};

View File

@@ -1,5 +1,6 @@
#include "SpriteComponent.h"
#include "src/FileSystem/File.h"
#include "src/FileSystem/FileSystem.h"
#include "src/Rendering/Textures/TextureManager.h"
#include "src/Rendering/Textures/MaterialManager.h"
@@ -10,8 +11,7 @@ namespace Nuake
{
SpriteComponent::SpriteComponent() :
Billboard(false),
LockYRotation(false),
SpritePath("")
LockYRotation(false)
{
}
@@ -32,7 +32,13 @@ namespace Nuake
SpriteMesh = CreateRef<Mesh>();
SpriteMesh->AddSurface(quadVertices, { 0, 1, 2, 3, 4, 5 });
Ref<Material> material = MaterialManager::Get()->GetMaterial(FileSystem::Root + SpritePath);
std::string absPath = "";
if (SpritePath.file != nullptr && SpritePath.file->Exist())
{
absPath = SpritePath.file->GetAbsolutePath();
}
Ref<Material> material = MaterialManager::Get()->GetMaterial(absPath);
bool hasNormal = material->HasNormal();
SpriteMesh->SetMaterial(material);
@@ -44,7 +50,7 @@ namespace Nuake
BEGIN_SERIALIZE();
SERIALIZE_VAL(Billboard);
SERIALIZE_VAL(LockYRotation);
SERIALIZE_VAL(SpritePath);
SERIALIZE_RES_FILE(SpritePath);
SERIALIZE_VAL(PositionFacing);
END_SERIALIZE();
}
@@ -61,17 +67,13 @@ namespace Nuake
LockYRotation = j["LockYRotation"];
}
DESERIALIZE_RES_FILE(SpritePath);
if (j.contains("PositionFacing"))
{
PositionFacing = j["PositionFacing"];
}
if (j.contains("SpritePath"))
{
SpritePath = j["SpritePath"];
LoadSprite();
}
return true;
}
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include "Component.h"
#include "FieldTypes.h"
#include "src/Core/Core.h"
#include "src/Resource/Serializable.h"
@@ -13,12 +14,20 @@ namespace Nuake
{
NUAKECOMPONENT(SpriteComponent, "Sprite")
static void InitializeComponentClass()
{
BindComponentField<&SpriteComponent::Billboard>("Billboard", "Billboard");
BindComponentField<&SpriteComponent::LockYRotation>("LockYRotation", "Lock Y Rotation");
BindComponentField<&SpriteComponent::PositionFacing>("PositionFacing", "Position Facing");
BindComponentField<&SpriteComponent::SpritePath>("SpritePath", "Sprite Path");
}
public:
bool Billboard;
bool LockYRotation;
bool PositionFacing;
std::string SpritePath;
ResourceFile SpritePath;
Ref<Mesh> SpriteMesh;
SpriteComponent();