Skip to content

Commit

Permalink
feat: worldview object manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanbaburajan committed Oct 25, 2023
1 parent 225f4ad commit 8464250
Show file tree
Hide file tree
Showing 69 changed files with 18,004 additions and 13,954 deletions.
12 changes: 9 additions & 3 deletions Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ namespace DT
renderer = ctx.GetService<Renderer>().Fatal("Editor::Editor()");

ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL(window->GetRawWindowPointer(), true);
ImGui_ImplOpenGL3_Init("#version 440");

ImGuiIO &io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
io.ConfigDragClickToInputText = true;

ImGui_ImplGlfw_InitForOpenGL(window->GetRawWindowPointer(), true);
ImGui_ImplOpenGL3_Init("#version 440");

io.Fonts->AddFontFromFileTTF((ctx.projectPath / "Engine" / "Fonts" / "Roboto" / "Roboto-Regular.ttf").string().c_str(), 18.f);
SetupImGuiStyle();
MaximizeWindow();
Expand Down Expand Up @@ -99,6 +100,11 @@ namespace DT
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}

void Editor::Close()
Expand Down
5 changes: 0 additions & 5 deletions Editor/Panels/WorldOutliner.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ SOFTWARE.

#include <ECS/SceneManager.h>
#include <Panels/Panel.h>
#include <Panels/WorldView.h>

namespace DT
{
Expand All @@ -38,7 +37,6 @@ namespace DT
PROFILE();

sceneManager = ctx.GetService<SceneManager>().Fatal("WorldOutlinerPanel::Init()");
worldView = editor->GetPanel<WorldViewPanel>().Fatal("InspectorPanel::Init()");
}

void Tick(Context &ctx, const float &dt) override
Expand All @@ -47,8 +45,6 @@ namespace DT

ImGui::Begin("World Outliner", &isOpen);

worldView->editorCamera.transform.InspectorMenu(ctx, dt);

for (Entity entity : sceneManager->activeScene.GetEntities())
{
std::string label;
Expand All @@ -71,7 +67,6 @@ namespace DT

private:
SceneManager *sceneManager;
WorldViewPanel *worldView;
Entity selectedEntity = 0;

friend class InspectorPanel;
Expand Down
189 changes: 177 additions & 12 deletions Editor/Panels/WorldView.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SOFTWARE.

#include <Panels/Panel.h>
#include <EditorCamera.h>
#include <Panels/WorldOutliner.h>

namespace DT
{
Expand All @@ -34,24 +35,39 @@ namespace DT
public:
EditorCamera editorCamera;

WorldViewPanel(Context &ctx) : editorCamera(ctx) {}
WorldViewPanel(Context &ctx) : editorCamera(ctx),
translateIcon(Texture::Load(ctx.projectPath / "Engine" / "Icons" / "translate.png").Fatal("WorldViewPanel::WorldViewPanel()")),
rotateIcon(Texture::Load(ctx.projectPath / "Engine" / "Icons" / "rotate.png").Fatal("WorldViewPanel::WorldViewPanel()")),
scaleIcon(Texture::Load(ctx.projectPath / "Engine" / "Icons" / "scale.png").Fatal("WorldViewPanel::WorldViewPanel()")) {}

void Init(Context &ctx) override
{
PROFILE();

renderer = ctx.GetService<Renderer>().Fatal("WorldViewPanel::Init()");
window = ctx.GetService<Window>().Fatal("WorldViewPanel::Init()");
sceneManager = ctx.GetService<SceneManager>().Fatal("WorldViewPanel::Init()");
input = ctx.GetService<InputManager>().Fatal("WorldViewPanel::Init()");
worldOutliner = editor->GetPanel<WorldOutlinerPanel>().Fatal("WorldViewPanel::Init()");

editorCamera.transform.translation = glm::vec3(0.f, 0.f, -3.f);
input->OnKeyEvent(KEY_T, [&](int action)
{
gizmoOperation = ImGuizmo::TRANSLATE;
});
input->OnKeyEvent(KEY_R, [&](int action)
{
gizmoOperation = ImGuizmo::ROTATE;
});
input->OnKeyEvent(KEY_E, [&](int action)
{
gizmoOperation = ImGuizmo::SCALE;
});
}

void Tick(Context &ctx, const float &dt) override
void Tick(Context &ctx, const float &dt) override
{
PROFILE();

ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.f, 0.f));
ImGui::Begin("World View", &isOpen);

Expand Down Expand Up @@ -102,24 +118,54 @@ namespace DT
ImVec2(0, 1),
ImVec2(1, 0));

// Manipulate
Entity selectedEntity = worldOutliner->GetSelectedEntity();
if (sceneManager->activeScene.IsValid(selectedEntity) != 0 && sceneManager->activeScene.Has<Transform>(selectedEntity))
{
std::shared_ptr<Transform> transform = sceneManager->activeScene.Get<Transform>(selectedEntity).Fatal("WorldView::Tick()");

glm::mat4 model = transform->GetModelMatrix();

ImGuizmo::SetOrthographic(editorCamera.isOrthographic);
ImGuizmo::SetDrawlist();
ImGuizmo::SetRect(vMin.x, vMin.y, window->GetWindowSize().x, window->GetWindowSize().y);

float snap = 1.f;
if (gizmoOperation == ImGuizmo::OPERATION::ROTATE)
snap = 45.f;

ImGuizmo::Manipulate(glm::value_ptr(editorCamera.view), glm::value_ptr(editorCamera.projection), gizmoOperation, currentGizmoMode, glm::value_ptr(model), NULL, input->IsKeyHeld(KEY_LEFT_CONTROL) ? &snap : NULL);

if (ImGuizmo::IsUsing())
{
transform->SetModelMatrix(model);
}
}

ImGui::End();
ImGui::PopStyleVar();

RenderToolbarPanel();
}

private:
bool worldViewActive;
ImVec2 windowPos, windowSize;

ImVec2 windowPos, windowSize;

bool isFreeLookActive = false;
glm::vec3 orientation;
float speed = 2.5f, sensitivity = 75.f;
float lastx;

bool isFreeLookActive = false;
Texture translateIcon, rotateIcon, scaleIcon;
ImGuizmo::OPERATION gizmoOperation = ImGuizmo::TRANSLATE;
ImGuizmo::MODE currentGizmoMode = ImGuizmo::WORLD;

Renderer *renderer;
Window *window;
SceneManager *sceneManager;
InputManager *input;
WorldOutlinerPanel *worldOutliner;

void FreeMove(const float &dt)
{
Expand Down Expand Up @@ -148,10 +194,6 @@ namespace DT
editorCamera.transform.translation += speed * dt * glm::vec3(0.f, 1.f, 0.f);
if (input->IsKeyHeld(KEY_LEFT_SHIFT))
editorCamera.transform.translation -= speed * dt * glm::vec3(0.f, 1.f, 0.f);
if (input->IsKeyHeld(KEY_E))
orientation.z += sensitivity * dt;
if (input->IsKeyHeld(KEY_Q))
orientation.z -= sensitivity * dt;
}
}

Expand All @@ -176,5 +218,128 @@ namespace DT
editorCamera.transform.SetEulerRotation(orientation);
}
}

void RenderToolbarPanel()
{
const float padding = 10.f;
const float iconSize = 25.f;

ImGui::SetNextWindowPos(ImVec2(windowPos.x + padding, windowPos.y + padding));

ImGuiWindowFlags windowFlags = 0 | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings;
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0);
ImGui::Begin("Tool Bar", NULL, windowFlags);
ImGui::PopStyleVar();

// Transform control
ImGuizmo::OPERATION currentGizmoOperation = gizmoOperation;

if (currentGizmoOperation == ImGuizmo::TRANSLATE)
{
ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyleColorVec4(ImGuiCol_Button));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyleColorVec4(ImGuiCol_Button));
}
if (ImGui::ImageButton((ImTextureID)(uintptr_t)translateIcon.id, ImVec2(iconSize, iconSize)))
gizmoOperation = ImGuizmo::TRANSLATE;
if (currentGizmoOperation == ImGuizmo::TRANSLATE)
{
ImGui::PopStyleColor();
ImGui::PopStyleColor();
ImGui::PopStyleColor();
}

if (currentGizmoOperation == ImGuizmo::ROTATE)
{
ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyleColorVec4(ImGuiCol_Button));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyleColorVec4(ImGuiCol_Button));
}
ImGui::SameLine();
if (ImGui::ImageButton((ImTextureID)(uintptr_t)rotateIcon.id, ImVec2(iconSize, iconSize)))
gizmoOperation = ImGuizmo::ROTATE;
if (currentGizmoOperation == ImGuizmo::ROTATE)
{
ImGui::PopStyleColor();
ImGui::PopStyleColor();
ImGui::PopStyleColor();
}

if (currentGizmoOperation == ImGuizmo::SCALE)
{
ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyleColorVec4(ImGuiCol_Button));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyleColorVec4(ImGuiCol_Button));
}
ImGui::SameLine();
if (ImGui::ImageButton((ImTextureID)(uintptr_t)scaleIcon.id, ImVec2(iconSize, iconSize)))
gizmoOperation = ImGuizmo::SCALE;
if (currentGizmoOperation == ImGuizmo::SCALE)
{
ImGui::PopStyleColor();
ImGui::PopStyleColor();
ImGui::PopStyleColor();
}

// Runtime state control
// RuntimeState currentRuntimeState = runtimeState;

// if (currentRuntimeState == RuntimeState::Play)
// {
// ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered));
// ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyleColorVec4(ImGuiCol_Button));
// ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyleColorVec4(ImGuiCol_Button));
// }
// if (ImGui::ImageButton((ImTextureID)(uintptr_t)playIconId, ImVec2(iconSize, iconSize)))
// {
// runtimeState = RuntimeState::Play;
// ctx.loopManager->gameTick = true;
// }
// if (currentRuntimeState == RuntimeState::Play)
// {
// ImGui::PopStyleColor();
// ImGui::PopStyleColor();
// ImGui::PopStyleColor();
// }

// if (currentRuntimeState == RuntimeState::Pause)
// {
// ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered));
// ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyleColorVec4(ImGuiCol_Button));
// ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyleColorVec4(ImGuiCol_Button));
// }
// if (ImGui::ImageButton((ImTextureID)(uintptr_t)pauseIconId, ImVec2(iconSize, iconSize)))
// {
// runtimeState = RuntimeState::Pause;
// ctx.loopManager->gameTick = false;
// }
// if (currentRuntimeState == RuntimeState::Pause)
// {
// ImGui::PopStyleColor();
// ImGui::PopStyleColor();
// ImGui::PopStyleColor();
// }

// if (currentRuntimeState == RuntimeState::Stop)
// {
// ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered));
// ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyleColorVec4(ImGuiCol_Button));
// ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyleColorVec4(ImGuiCol_Button));
// }
// if (ImGui::ImageButton((ImTextureID)(uintptr_t)stopIconId, ImVec2(iconSize, iconSize)))
// {
// runtimeState = RuntimeState::Stop;
// ctx.loopManager->gameTick = false;
// ctx.loopManager->sceneTick = true;
// }
// if (currentRuntimeState == RuntimeState::Stop)
// {
// ImGui::PopStyleColor();
// ImGui::PopStyleColor();
// ImGui::PopStyleColor();
// }

ImGui::End();
}
};
}
2 changes: 2 additions & 0 deletions Engine/dtpch.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ SOFTWARE.
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtc/type_ptr.hpp>

#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui/imgui.h>
#include <imgui/imgui_internal.h>
#include <imgui/misc/cpp/imgui_stdlib.h>
#include <imgui/ImGuizmo.h>

#define JSON_DIAGNOSTICS 1
#include <json/json.hpp>
Expand Down
2 changes: 1 addition & 1 deletion Extern/imgui/ImGuizmo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
// SOFTWARE.
//

#include "imgui.h"
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include "imgui.h"
#include "imgui_internal.h"
#include "ImGuizmo.h"

Expand Down
21 changes: 21 additions & 0 deletions Extern/imgui/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014-2023 Omar Cornut

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 8464250

Please sign in to comment.