master
ブランチ master
ShaderPlayground / src / gui / editor.cc
5.5 KB、168行 生 Blame 履歴
1/*
2# 076 License
3
4Copyright (c) 076 Studio LLC
5
6Permission is hereby granted to any person obtaining a copy of the software
7Shader Playground (the "Software") to use, modify, merge, copy, publish, distribute,
8sublicense, and/or sell copies of the Software, subject to the following conditions:
9
10 1. **Origin Attribution**:
11 - You must not misrepresent the origin of the Software; you must not claim
12 you created the original Software.
13 - If the Software is used in a product, you must either:
14 a. Provide clear attribution in the product's documentation, user interface,
15 or other visible areas, **OR**
16 b. Pay the original developers a fee they specify in writing.
17 2. **Usage Restriction**:
18 - The Software, or any derivative works, dependencies, or libraries
19 incorporating it, must not be used for censorship or to suppress freedom of
20 speech, expression, or creativity. Prohibited uses include, but are not
21 limited to:
22 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
23 ideas, or objective reality.
24 - Tools or systems designed to restrict access to information or
25 artistic works.
26 3. **Notice Preservation**:
27 - This license and the above copyright notice must remain intact in all copies
28 of the source code.
29
30THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
31INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
32PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
35OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37#include <gui/editor.hh>
38#include <gui/guiengine.hh>
39#include <iostream>
40
41namespace 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 // CTRL + D
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 //RenderStatusBar();
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 //buildAutocompleteTrie();
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 //auto color = isDirty() ? IM_COL32(164, 0, 0, 255) : IM_COL32(164, 164, 164, 255);
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} // namespace gui