Editor: UI panels and GameObject updates
This commit is contained in:
@@ -10,6 +10,7 @@ namespace Editor {
|
||||
|
||||
InspectorPanel::InspectorPanel() : Panel("Inspector") {
|
||||
m_selectionHandlerId = SelectionManager::Get().OnSelectionChanged.Subscribe([this](uint64_t) {
|
||||
m_selectedGameObject = SelectionManager::Get().GetSelectedEntity();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -20,10 +21,10 @@ InspectorPanel::~InspectorPanel() {
|
||||
void InspectorPanel::Render() {
|
||||
ImGui::Begin(m_name.c_str(), nullptr, ImGuiWindowFlags_None);
|
||||
|
||||
::XCEngine::Components::GameObject* entity = SelectionManager::Get().GetSelectedEntity();
|
||||
m_selectedGameObject = SelectionManager::Get().GetSelectedEntity();
|
||||
|
||||
if (entity) {
|
||||
RenderEntity(entity);
|
||||
if (m_selectedGameObject) {
|
||||
RenderGameObject(m_selectedGameObject);
|
||||
} else {
|
||||
ImGui::Text("No object selected");
|
||||
ImGui::TextColored(ImVec4(0.5f, 0.5f, 0.5f, 1.0f), "Select an object in Hierarchy");
|
||||
@@ -32,27 +33,80 @@ void InspectorPanel::Render() {
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
void InspectorPanel::RenderEntity(::XCEngine::Components::GameObject* entity) {
|
||||
ImGui::Text("%s", entity->GetName().c_str());
|
||||
ImGui::Separator();
|
||||
void InspectorPanel::RenderGameObject(::XCEngine::Components::GameObject* gameObject) {
|
||||
char nameBuffer[256];
|
||||
strcpy_s(nameBuffer, gameObject->GetName().c_str());
|
||||
ImGui::InputText("##Name", nameBuffer, sizeof(nameBuffer));
|
||||
if (ImGui::IsItemDeactivatedAfterEdit()) {
|
||||
gameObject->SetName(nameBuffer);
|
||||
}
|
||||
|
||||
auto components = entity->GetComponents<::XCEngine::Components::Component>();
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Add Component")) {
|
||||
ImGui::OpenPopup("AddComponent");
|
||||
}
|
||||
|
||||
RenderAddComponentPopup(gameObject);
|
||||
|
||||
auto components = gameObject->GetComponents<::XCEngine::Components::Component>();
|
||||
for (auto* component : components) {
|
||||
RenderComponent(component);
|
||||
ImGui::Separator();
|
||||
RenderComponent(component, gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
void InspectorPanel::RenderComponent(::XCEngine::Components::Component* component) {
|
||||
void InspectorPanel::RenderAddComponentPopup(::XCEngine::Components::GameObject* gameObject) {
|
||||
if (!ImGui::BeginPopup("AddComponent")) {
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(12.0f, 10.0f));
|
||||
|
||||
ImGui::SeparatorText("Components");
|
||||
|
||||
if (ImGui::MenuItem("Transform", nullptr, false, !gameObject->GetComponent<::XCEngine::Components::TransformComponent>())) {
|
||||
gameObject->AddComponent<::XCEngine::Components::TransformComponent>();
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
|
||||
ImGui::SeparatorText("Other");
|
||||
ImGui::TextDisabled("No more components available");
|
||||
|
||||
ImGui::PopStyleVar();
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
void InspectorPanel::RenderComponent(::XCEngine::Components::Component* component, ::XCEngine::Components::GameObject* gameObject) {
|
||||
if (!component) return;
|
||||
|
||||
const char* name = component->GetName().c_str();
|
||||
|
||||
std::string headerId = std::string(name) + "##" + std::to_string(reinterpret_cast<uintptr_t>(component));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{4, 2});
|
||||
|
||||
if (ImGui::CollapsingHeader(headerId.c_str(), ImGuiTreeNodeFlags_DefaultOpen)) {
|
||||
ImGui::Indent(10.0f);
|
||||
|
||||
ImGuiTreeNodeFlags flags =
|
||||
ImGuiTreeNodeFlags_DefaultOpen |
|
||||
ImGuiTreeNodeFlags_Framed |
|
||||
ImGuiTreeNodeFlags_SpanAvailWidth |
|
||||
ImGuiTreeNodeFlags_FramePadding |
|
||||
ImGuiTreeNodeFlags_AllowOverlap;
|
||||
|
||||
bool open = ImGui::TreeNodeEx((void*)typeid(*component).hash_code(), flags, "%s", name);
|
||||
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
bool removeComponent = false;
|
||||
if (ImGui::BeginPopupContextItem("ComponentSettings")) {
|
||||
if (ImGui::MenuItem("Remove Component")) {
|
||||
removeComponent = true;
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
if (removeComponent) {
|
||||
RemoveComponentByType(component, gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
if (open) {
|
||||
if (auto* transform = dynamic_cast<::XCEngine::Components::TransformComponent*>(component)) {
|
||||
::XCEngine::Math::Vector3 position = transform->GetLocalPosition();
|
||||
::XCEngine::Math::Vector3 rotation = transform->GetLocalEulerAngles();
|
||||
@@ -71,7 +125,23 @@ void InspectorPanel::RenderComponent(::XCEngine::Components::Component* componen
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::Unindent(10.0f);
|
||||
ImGui::TreePop();
|
||||
}
|
||||
}
|
||||
|
||||
void InspectorPanel::RemoveComponentByType(::XCEngine::Components::Component* component, ::XCEngine::Components::GameObject* gameObject) {
|
||||
if (!component || !gameObject) return;
|
||||
|
||||
if (dynamic_cast<::XCEngine::Components::TransformComponent*>(component)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto components = gameObject->GetComponents<::XCEngine::Components::Component>();
|
||||
for (auto* comp : components) {
|
||||
if (comp == component) {
|
||||
gameObject->RemoveComponent<::XCEngine::Components::Component>();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user