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

117 lines
4.0 KiB
C
Raw Normal View History

#pragma once
#include "IComponentEditor.h"
#include "UI/UI.h"
#include <XCEngine/Components/LightComponent.h>
namespace XCEngine {
namespace Editor {
class LightComponentEditor : public IComponentEditor {
public:
const char* GetDisplayName() const override {
return "Light";
}
bool CanEdit(::XCEngine::Components::Component* component) const override {
return dynamic_cast<::XCEngine::Components::LightComponent*>(component) != nullptr;
}
bool Render(::XCEngine::Components::Component* component, IUndoManager* undoManager) override {
auto* light = dynamic_cast<::XCEngine::Components::LightComponent*>(component);
if (!light) {
return false;
}
int lightType = static_cast<int>(light->GetLightType());
const char* lightTypeLabels[] = { "Directional", "Point", "Spot" };
bool changed = false;
if (ImGui::Combo("Type", &lightType, lightTypeLabels, 3)) {
if (undoManager) {
undoManager->BeginInteractiveChange("Modify Light");
}
light->SetLightType(static_cast<::XCEngine::Components::LightType>(lightType));
changed = true;
}
float color[4] = {
light->GetColor().r,
light->GetColor().g,
light->GetColor().b,
light->GetColor().a
};
if (UI::DrawColor4("Color", color)) {
if (undoManager) {
undoManager->BeginInteractiveChange("Modify Light");
}
light->SetColor(::XCEngine::Math::Color(color[0], color[1], color[2], color[3]));
changed = true;
}
float intensity = light->GetIntensity();
if (UI::DrawFloat("Intensity", intensity, 100.0f, 0.1f, 0.0f)) {
if (undoManager) {
undoManager->BeginInteractiveChange("Modify Light");
}
light->SetIntensity(intensity);
changed = true;
}
if (light->GetLightType() != ::XCEngine::Components::LightType::Directional) {
float range = light->GetRange();
if (UI::DrawFloat("Range", range, 100.0f, 0.1f, 0.001f)) {
if (undoManager) {
undoManager->BeginInteractiveChange("Modify Light");
}
light->SetRange(range);
changed = true;
}
}
if (light->GetLightType() == ::XCEngine::Components::LightType::Spot) {
float spotAngle = light->GetSpotAngle();
if (UI::DrawSliderFloat("Spot Angle", spotAngle, 1.0f, 179.0f, 100.0f, "%.1f")) {
if (undoManager) {
undoManager->BeginInteractiveChange("Modify Light");
}
light->SetSpotAngle(spotAngle);
changed = true;
}
}
bool castsShadows = light->GetCastsShadows();
if (UI::DrawBool("Cast Shadows", castsShadows)) {
if (undoManager) {
undoManager->BeginInteractiveChange("Modify Light");
}
light->SetCastsShadows(castsShadows);
changed = true;
}
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;
}
::XCEngine::Components::Component* AddTo(::XCEngine::Components::GameObject* gameObject) const override {
return gameObject ? gameObject->AddComponent<::XCEngine::Components::LightComponent>() : nullptr;
}
bool CanRemove(::XCEngine::Components::Component* component) const override {
return CanEdit(component);
}
};
} // namespace Editor
} // namespace XCEngine