Files
XCEngine/editor/src/ComponentEditors/LightComponentEditor.h

117 lines
3.7 KiB
C
Raw Normal View History

#pragma once
#include "IComponentEditor.h"
#include "Core/IUndoManager.h"
#include "UI/UI.h"
#include <XCEngine/Components/LightComponent.h>
namespace XCEngine {
namespace Editor {
class LightComponentEditor : public IComponentEditor {
public:
const char* GetComponentTypeName() const override {
return "Light";
}
const char* GetDisplayName() const override {
return "Light";
}
bool Render(::XCEngine::Components::Component* component, IUndoManager* undoManager) override {
auto* light = dynamic_cast<::XCEngine::Components::LightComponent*>(component);
if (!light) {
return false;
}
2026-03-26 21:18:33 +08:00
constexpr const char* kUndoLabel = "Modify Light";
int lightType = static_cast<int>(light->GetLightType());
const char* lightTypeLabels[] = { "Directional", "Point", "Spot" };
bool changed = false;
2026-03-26 21:18:33 +08:00
const int newLightType = UI::DrawPropertyCombo("Type", lightType, lightTypeLabels, 3);
changed |= UI::ApplyPropertyChange(
newLightType != lightType,
undoManager,
kUndoLabel,
[&]() {
light->SetLightType(static_cast<::XCEngine::Components::LightType>(newLightType));
});
float color[4] = {
light->GetColor().r,
light->GetColor().g,
light->GetColor().b,
light->GetColor().a
};
2026-03-26 21:18:33 +08:00
changed |= UI::ApplyPropertyChange(
UI::DrawPropertyColor4("Color", color),
undoManager,
kUndoLabel,
[&]() {
light->SetColor(::XCEngine::Math::Color(color[0], color[1], color[2], color[3]));
2026-03-26 21:18:33 +08:00
});
float intensity = light->GetIntensity();
2026-03-26 21:18:33 +08:00
changed |= UI::ApplyPropertyChange(
UI::DrawPropertyFloat("Intensity", intensity, 0.1f, 0.0f),
undoManager,
kUndoLabel,
[&]() {
light->SetIntensity(intensity);
2026-03-26 21:18:33 +08:00
});
if (light->GetLightType() != ::XCEngine::Components::LightType::Directional) {
float range = light->GetRange();
2026-03-26 21:18:33 +08:00
changed |= UI::ApplyPropertyChange(
UI::DrawPropertyFloat("Range", range, 0.1f, 0.001f),
undoManager,
kUndoLabel,
[&]() {
light->SetRange(range);
2026-03-26 21:18:33 +08:00
});
}
if (light->GetLightType() == ::XCEngine::Components::LightType::Spot) {
float spotAngle = light->GetSpotAngle();
2026-03-26 21:18:33 +08:00
changed |= UI::ApplyPropertyChange(
UI::DrawPropertySliderFloat("Spot Angle", spotAngle, 1.0f, 179.0f, "%.1f"),
undoManager,
kUndoLabel,
[&]() {
light->SetSpotAngle(spotAngle);
2026-03-26 21:18:33 +08:00
});
}
bool castsShadows = light->GetCastsShadows();
2026-03-26 21:18:33 +08:00
changed |= UI::ApplyPropertyChange(
UI::DrawPropertyBool("Cast Shadows", castsShadows),
undoManager,
kUndoLabel,
[&]() {
light->SetCastsShadows(castsShadows);
2026-03-26 21:18:33 +08:00
});
return changed;
}
bool CanAddTo(::XCEngine::Components::GameObject* gameObject) const override {
return gameObject && !gameObject->GetComponent<::XCEngine::Components::LightComponent>();
}
const char* GetAddDisabledReason(::XCEngine::Components::GameObject* gameObject) const override {
if (!gameObject) {
return "Invalid";
}
return gameObject->GetComponent<::XCEngine::Components::LightComponent>() ? "Already Added" : nullptr;
}
bool CanRemove(::XCEngine::Components::Component* component) const override {
return CanEdit(component);
}
};
} // namespace Editor
} // namespace XCEngine