DeerEngine/Deer/src/Plattform/ImGUI/ImGuiLayer.cpp

141 lines
4.1 KiB
C++
Executable File

#include "DeerRender/ImGui/ImGuiLayer.h"
#include "Deer/Application.h"
#include "Deer/Log.h"
#include "DeerRender/Events/Event.h"
#include "DeerRender/Input.h"
// THIS ORDER
#include "Plattform/OpenGL/imgui_impl_opengl3.h"
// THIS ORDER
#include "glad/glad.h"
// THIS ORDER
#include "GLFW/glfw3.h"
// THIS ORDER
#include "imgui.h"
// THIS ORDER
#include "backends/imgui_impl_glfw.h"
// THIS ORDER
#include "ImGuizmo.h"
namespace Deer {
void ImGuiLayer::onAttach() {
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGuiIO& io = ImGui::GetIO();
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGui_ImplOpenGL3_Init("#version 410");
Application::s_application->m_window->initImGUI();
io.DisplaySize =
ImVec2(Application::s_application->m_window->getWitdth(),
Application::s_application->m_window->getHeight());
}
void ImGuiLayer::onDetach() {}
void ImGuiLayer::begin() {
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
ImGuizmo::BeginFrame();
}
void ImGuiLayer::end() {
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void ImGuiLayer::onEvent(Event& e) {
EventDispatcher dispacher(e);
// dispacher.dispatch<MouseButtonPressedEvent>(std::bind(&ImGuiLayer::onMouseButtonPressedEvent,
// this, std::placeholders::_1));
// dispacher.dispatch<MouseButtonReleasedEvent>(std::bind(&ImGuiLayer::onMouseButtonReleasedEvent,
// this, std::placeholders::_1));
// dispacher.dispatch<MouseMovedEvent>(std::bind(&ImGuiLayer::onMouseMovedEvent,
// this, std::placeholders::_1));
// dispacher.dispatch<MouseScrolledEvent>(std::bind(&ImGuiLayer::onMouseScrollEvent,
// this, std::placeholders::_1));
// dispacher.dispatch<KeyPressedEvent>(std::bind(&ImGuiLayer::onKeyPressedEvent,
// this, std::placeholders::_1));
// dispacher.dispatch<KeyTypedEvent>(std::bind(&ImGuiLayer::onKeyTypedEvent,
// this, std::placeholders::_1));
dispacher.dispatch<WindowResizeEvent>(std::bind(
&ImGuiLayer::onWindowResizeEvent, this, std::placeholders::_1));
}
bool ImGuiLayer::onMouseButtonPressedEvent(MouseButtonPressedEvent& e) {
ImGuiIO& io = ImGui::GetIO();
io.MouseDown[e.getMouseButton()] = true;
return false;
}
bool ImGuiLayer::onMouseButtonReleasedEvent(MouseButtonReleasedEvent& e) {
ImGuiIO& io = ImGui::GetIO();
io.MouseDown[e.getMouseButton()] = false;
return false;
}
bool ImGuiLayer::onMouseMovedEvent(MouseMovedEvent& e) {
ImGuiIO& io = ImGui::GetIO();
io.MousePos = ImVec2(e.getX(), e.getY());
return false;
}
bool ImGuiLayer::onMouseScrollEvent(MouseScrolledEvent& e) {
ImGuiIO& io = ImGui::GetIO();
io.MouseWheel += e.getYOffset();
io.MouseWheelH += e.getXOffset();
return false;
}
bool ImGuiLayer::onKeyPressedEvent(KeyPressedEvent& e) {
ImGuiIO& io = ImGui::GetIO();
io.KeysDown[e.getKeyCode()] = true;
io.KeysDown[ImGuiKey::ImGuiKey_Backspace] =
e.getKeyCode() == DEER_KEY_BACKSPACE;
io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] ||
io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] ||
io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
io.KeyAlt =
io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] ||
io.KeysDown[GLFW_KEY_RIGHT_SUPER];
return false;
}
bool ImGuiLayer::onKeyReleasedEvent(KeyReleasedEvent& e) {
ImGuiIO& io = ImGui::GetIO();
io.KeysDown[e.getKeyCode()] = false;
return false;
}
bool ImGuiLayer::onKeyTypedEvent(KeyTypedEvent& e) {
ImGuiIO& io = ImGui::GetIO();
io.AddInputCharacter((unsigned short)e.getKeyCode());
if (e.getKeyCode() == DEER_KEY_BACKSPACE) {
io.AddInputCharacter(ImGuiKey::ImGuiKey_Backspace);
}
return false;
}
bool ImGuiLayer::onWindowResizeEvent(WindowResizeEvent& e) {
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(e.getWidth(), (int)e.getHeight());
glViewport(0, 0, (int)e.getWidth(), (int)e.getHeight());
return false;
}
} // namespace Deer