| 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/editor.hh> |
| 38 | #include <gui/guiengine.hh> |
| 39 | #include <iostream> |
| 40 | |
| 41 | namespace gui { |
| 42 | Editor::Editor(Info *info, string title, string renderName, string code, Languages lang, ImFont *cjkFont, ImFont *monoFont) : m_Info(info) { |
| 43 | m_Editor.SetTabSize(2); |
| 44 | m_Editor.SetInsertSpacesOnTabs(true); |
| 45 | m_CjkFont = cjkFont; |
| 46 | m_MonoFont = monoFont; |
| 47 | m_Title = title; |
| 48 | m_RenderName = renderName; |
| 49 | m_Code = code; |
| 50 | m_Editor.SetText(code); |
| 51 | m_Lang = lang; |
| 52 | |
| 53 | if (lang == Glsl) { |
| 54 | m_Editor.SetLanguage(TextEditor::Language::Glsl()); |
| 55 | } else if (lang == Hlsl) { |
| 56 | |
| 57 | m_Editor.SetLanguage(TextEditor::Language::Hlsl()); |
| 58 | } else if (lang == C) { |
| 59 | |
| 60 | m_Editor.SetLanguage(TextEditor::Language::C()); |
| 61 | } else if (lang == Cs) { |
| 62 | |
| 63 | m_Editor.SetLanguage(TextEditor::Language::Cs()); |
| 64 | } else if (lang == Lua) { |
| 65 | m_Editor.SetLanguage(TextEditor::Language::Lua()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | Editor::~Editor() {} |
| 70 | |
| 71 | void Editor::Render() { |
| 72 | ImGui::PushFont(m_CjkFont); |
| 73 | ImGui::Begin(m_Title.c_str()); |
| 74 | ImGui::PopFont(); |
| 75 | |
| 76 | ImGui::PushFont(m_MonoFont); |
| 77 | m_Editor.Render(m_RenderName.c_str()); |
| 78 | |
| 79 | if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) { |
| 80 | m_Info->activeEditor = &m_Editor; |
| 81 | } |
| 82 | |
| 83 | ImGui::PopFont(); |
| 84 | |
| 85 | |
| 86 | if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows)) { |
| 87 | bool ctrl = ImGui::GetIO().KeyCtrl; |
| 88 | |
| 89 | if (ctrl && ImGui::IsKeyPressed(ImGuiKey_D, false)) { |
| 90 | DuplicateLine(m_Editor); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | ImGui::End(); |
| 96 | } |
| 97 | |
| 98 | void Editor::RenderStatusBar() { |
| 99 | static const char *languages[] = { "Lua", "GLSL", "HLSL" }; |
| 100 | |
| 101 | static const TextEditor::Language *definitions[] = { |
| 102 | TextEditor::Language::Lua(), |
| 103 | TextEditor::Language::Glsl(), |
| 104 | TextEditor::Language::Hlsl(), |
| 105 | }; |
| 106 | |
| 107 | std::string language = m_Editor.GetLanguageName(); |
| 108 | |
| 109 | ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.15f, 0.15f, 0.15f, 1.0f)); |
| 110 | ImGui::BeginChild("StatusBar", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Borders); |
| 111 | ImGui::SetNextItemWidth(120.0f); |
| 112 | |
| 113 | if (ImGui::BeginCombo("##LanguageSelector", language.c_str())) { |
| 114 | for (int n = 0; n < IM_ARRAYSIZE(languages); n++) { |
| 115 | bool selected = (language == languages[n]); |
| 116 | |
| 117 | if (ImGui::Selectable(languages[n], selected)) { |
| 118 | m_Editor.SetLanguage(definitions[n]); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | if (selected) { |
| 123 | ImGui::SetItemDefaultFocus(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | ImGui::EndCombo(); |
| 128 | } |
| 129 | |
| 130 | ImGui::SameLine(0.0f, 0.0f); |
| 131 | ImGui::AlignTextToFramePadding(); |
| 132 | |
| 133 | int line; |
| 134 | int column; |
| 135 | int tabSize = m_Editor.GetTabSize(); |
| 136 | float glyphWidth = ImGui::CalcTextSize("#").x; |
| 137 | m_Editor.GetCurrentCursor(line, column); |
| 138 | |
| 139 | char status[256]; |
| 140 | auto statusSize = std::snprintf( |
| 141 | status, |
| 142 | sizeof(status), |
| 143 | "Ln %d, Col %d Tab Size: %d File: %s", |
| 144 | line + 1, |
| 145 | column + 1, |
| 146 | tabSize, |
| 147 | "default" |
| 148 | ); |
| 149 | |
| 150 | auto size = glyphWidth * (statusSize + 3); |
| 151 | auto width = ImGui::GetContentRegionAvail().x; |
| 152 | |
| 153 | ImGui::SameLine(0.0f, width - size); |
| 154 | ImGui::TextUnformatted(status); |
| 155 | |
| 156 | ImGui::SameLine(0.0f, glyphWidth * 1.0f); |
| 157 | auto drawlist = ImGui::GetWindowDrawList(); |
| 158 | auto pos = ImGui::GetCursorScreenPos(); |
| 159 | auto offset = ImGui::GetFrameHeight() * 0.5f; |
| 160 | auto radius = offset * 0.6f; |
| 161 | |
| 162 | auto color = IM_COL32(164, 0, 0, 255); |
| 163 | drawlist->AddCircleFilled(ImVec2(pos.x + offset, pos.y + offset), radius, color); |
| 164 | |
| 165 | ImGui::EndChild(); |
| 166 | ImGui::PopStyleColor(); |
| 167 | } |
| 168 | } |