#include "overlay.h" #include "application.h" static void check_vk_result(VkResult err) { if (err == 0) return; fprintf(stderr, "[vulkan] Error: VkResult = %d\n", err); if (err < 0) abort(); } Overlay::Overlay() { } Overlay::~Overlay() { } void Overlay::onAttach() { init(); } void Overlay::onDetach() { } void Overlay::onUpdate() { newFrame(); define(nullptr, [](Overlay& overlay, void** data) { if(App::Get().show) ImGui::ShowDemoWindow(); { static char filename[24] = "test.py"; static std::vector text(2048); static std::fstream file; ImGui::Begin("Main Editor"); ImGui::InputText("File Name", filename, 24); ImGui::SameLine(); if(ImGui::Button("load", ImVec2(48, 16))) { file.open(filename, std::ios::in); if(file.is_open()) { for(int i = 0; i < text.size(); i++) { if(!file.eof()) text.at(i) = file.get(); else text.at(i) = '\r'; } file.close(); } else { std::cout << "file does not exist!\n"; } } ImGui::InputTextMultiline("Editor", text.data(), text.size(), ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), ImGuiInputTextFlags_AllowTabInput); if(ImGui::Button("save", ImVec2(48, 16))) { file.open(filename, std::ios::out | std::ios::trunc); if(file.is_open()){ for(char c : text) file.put(c == '\0' ? ' ' : c); file.close(); } else { std::cout << "file could not be opened!\n"; } } ImGui::SameLine(); if(ImGui::Button("run", ImVec2(48, 16))) { if (system(("python " + std::string(filename)).c_str())) std::cout << "command failed!\n"; } ImGui::End(); } return 0; }); render(); } bool Overlay::onEvent(const Event* event) { EventHandler eh; return eh.handleEvent(event, this); } bool Overlay::onMouseButtonPressed(const MouseButtonEvent* event) { ImGuiIO& io = ImGui::GetIO(); if(io.WantCaptureMouse) io.AddMouseButtonEvent(event->getButton(), event->getPressed()); return io.WantCaptureMouse; } bool Overlay::onMouseButtonReleased(const MouseButtonEvent* event) { ImGuiIO& io = ImGui::GetIO(); if(io.WantCaptureMouse) io.AddMouseButtonEvent(event->getButton(), event->getPressed()); return io.WantCaptureMouse; } bool Overlay::onMouseMoved(const MouseMovedEvent* event) { ImGuiIO& io = ImGui::GetIO(); if(io.WantCaptureMouse) io.AddMousePosEvent(event->getX(), event->getY()); return io.WantCaptureMouse; } bool Overlay::onMouseScrolled(const MouseScrolledEvent* event) { ImGuiIO& io = ImGui::GetIO(); if(io.WantCaptureMouse) io.AddMouseWheelEvent(event->getX(), event->getY()); return io.WantCaptureMouse; } bool Overlay::onKeyPressed(const KeyEvent* event) { ImGuiIO& io = ImGui::GetIO(); if(io.WantCaptureKeyboard) { ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->getMod()); io.AddKeyEvent(ImGui_ImplSDL2_KeyEventToImGuiKey(event->getKey(), (SDL_Scancode)event->getScancode()), event->getPressed()); } return io.WantCaptureKeyboard; } bool Overlay::onKeyReleased(const KeyEvent* event) { ImGuiIO& io = ImGui::GetIO(); if(io.WantCaptureKeyboard) { ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->getMod()); io.AddKeyEvent(ImGui_ImplSDL2_KeyEventToImGuiKey(event->getKey(), (SDL_Scancode)event->getScancode()), event->getPressed()); } return io.WantCaptureKeyboard; } bool Overlay::onKeyTyped(const KeyTypedEvent *e) { ImGuiIO& io = ImGui::GetIO(); if(io.WantCaptureKeyboard) io.AddInputCharactersUTF8(e->getText()); return io.WantCaptureKeyboard; } bool Overlay::onWindowClose(const WindowEvent *e) { return false; } bool Overlay::onWindowFocus(const WindowEvent *e) { return false; } bool Overlay::onWindowFocusLost(const WindowEvent *e) { return false; } bool Overlay::onWindowMoved(const WindowEvent *e) { return false; } bool Overlay::onWindowResize(const WindowEvent *e) { Window& window = App::Get().getWindow(); window.getSize(); ImGui::GetIO().DisplaySize = ImVec2(window.x, window.y); return false; } void Overlay::init() { App& app = App::Get(); Renderer& renderer = app.getRenderer(); IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows ImGui::StyleColorsDark(); ImGuiStyle& style = ImGui::GetStyle(); if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { style.WindowRounding = 0.0f; style.Colors[ImGuiCol_WindowBg].w = 1.0f; } ImGui_ImplSDL2_InitForVulkan(app.getWindow()); ImGui_ImplVulkan_InitInfo init_info = {}; init_info.Instance = renderer.getInstance(); init_info.Queue = renderer.getPresentQueue(); init_info.DescriptorPool = renderer.getDescriptorPool(); init_info.RenderPass = renderer.getRenderPass(); init_info.Device = renderer.getDevice(); init_info.PhysicalDevice = renderer.getPhysicalDevice(); // init_info.MSAASamples = renderer.getMSAASamples(); init_info.MinImageCount = renderer.getNumImages(); init_info.ImageCount = renderer.getNumImages(); ImGui_ImplVulkan_Init(&init_info); ImGui_ImplVulkan_CreateFontsTexture(); } void Overlay::shutdown() { ImGui_ImplVulkan_Shutdown(); ImGui_ImplSDL2_Shutdown(); ImGui::DestroyContext(); } void Overlay::newFrame() { ImGui_ImplVulkan_NewFrame(); ImGui_ImplSDL2_NewFrame(); ImGui::NewFrame(); } void Overlay::renderPlatformWindows() { if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { ImGui::UpdatePlatformWindows(); ImGui::RenderPlatformWindowsDefault(); } } void Overlay::render() { Renderer& renderer = App::Get().getRenderer(); ImGui::Render(); main_draw_data = ImGui::GetDrawData(); mainMinimized = (main_draw_data->DisplaySize.x <= 0.0f || main_draw_data->DisplaySize.y <= 0.0f); ImGui_ImplVulkan_RenderDrawData(main_draw_data, renderer.getCommandBuffer(renderer.getFrame())); renderPlatformWindows(); } bool Overlay::isMinimized() { return mainMinimized; }