| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | #include <gui/guiengine.hh> |
| 38 | #include <util/glfwpp.hh> |
| 39 | #include <NotoSansCJKjp-Regular.h> |
| 40 | #include <NotoMono-Regular.h> |
| 41 | #include <TextEditor.h> |
| 42 | |
| 43 | namespace gui { |
| 44 | GuiEngine::GuiEngine(glfwpp::Window *window) { |
| 45 | m_Window = window; |
| 46 | IMGUI_CHECKVERSION(); |
| 47 | ImGui::CreateContext(); |
| 48 | ImGuiIO &io = ImGui::GetIO(); |
| 49 | (void)io; |
| 50 | |
| 51 | ImFontConfig cfg; |
| 52 | cfg.FontDataOwnedByAtlas = false; |
| 53 | |
| 54 | m_CjkFont = io.Fonts->AddFontFromMemoryTTF(__NotoSansCJKjp_Regular_otf, __NotoSansCJKjp_Regular_otf_len, 16.f, &cfg, io.Fonts->GetGlyphRangesJapanese()); |
| 55 | m_MonoFont = io.Fonts->AddFontFromMemoryTTF(__NotoMono_Regular_ttf, __NotoMono_Regular_ttf_len, 16.f, &cfg, io.Fonts->GetGlyphRangesJapanese()); |
| 56 | |
| 57 | |
| 58 | |
| 59 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 60 | io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; |
| 61 | io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 62 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; |
| 63 | |
| 64 | ImGui::StyleColorsDark(); |
| 65 | |
| 66 | ImGui_ImplGlfw_InitForOpenGL(window->Get(), true); |
| 67 | ImGui_ImplOpenGL3_Init("#version 460"); |
| 68 | } |
| 69 | |
| 70 | GuiEngine::~GuiEngine() { |
| 71 | ImGui_ImplOpenGL3_Shutdown(); |
| 72 | ImGui_ImplGlfw_Shutdown(); |
| 73 | ImGui::DestroyContext(); |
| 74 | } |
| 75 | |
| 76 | bool GuiEngine::ShouldCloseWindowOnEscape() { |
| 77 | return ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) |
| 78 | && !ImGui::GetIO().WantTextInput |
| 79 | && ImGui::IsKeyPressed(ImGuiKey_Escape); |
| 80 | } |
| 81 | |
| 82 | void DuplicateLine(TextEditor &editor) { |
| 83 | if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)) { |
| 84 | bool ctrl = ImGui::GetIO().KeyCtrl; |
| 85 | |
| 86 | if (ctrl && ImGui::IsKeyPressed(ImGuiKey_D, false)) { |
| 87 | cstr clipboard = ImGui::GetClipboardText(); |
| 88 | string copyClip = clipboard ? clipboard : ""; |
| 89 | auto cursor = editor.GetCurrentCursorPosition(); |
| 90 | auto lines = editor.GetLineText(cursor.line); |
| 91 | auto eol = lines.size(); |
| 92 | |
| 93 | editor.SelectLine(cursor.line); |
| 94 | editor.Copy(); |
| 95 | editor.Paste(); |
| 96 | editor.Paste(); |
| 97 | editor.SetCursor(cursor.line + 1, cursor.column); |
| 98 | ImGui::SetClipboardText(copyClip.c_str()); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |