Skip to content

Commit

Permalink
Progress...
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasnoble committed Aug 20, 2021
1 parent 52c89c1 commit 42815c8
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 33 deletions.
55 changes: 34 additions & 21 deletions src/gui/widgets/shader-editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ void main() {
Frag_UV = UV;
Frag_Color = Color;
gl_Position = u_projMatrix * vec4(Position.xy, 0, 1);
})";
}
)";

static const GLchar *const c_defaultPixelShader = GL_SHADER_VERSION R"(
/* The Pixel Shader is most likely what the user will want to change. */
Expand All @@ -58,7 +59,8 @@ layout (location = 0) out vec4 Out_Color;
void main() {
Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
Out_Color.a = 1.0;
})";
}
)";

static const char *const c_defaultLuaInvoker = R"(
-- All of this code is sandboxed, as in any global variable it
Expand Down Expand Up @@ -113,11 +115,10 @@ PCSX::Widgets::ShaderEditor::ShaderEditor(const std::string &base, std::string_v
std::ostringstream code;
code << in.rdbuf();
in.close();
m_vertexShaderEditor.SetText(code.str());
m_vertexShaderEditor.setText(code.str());
} else {
m_vertexShaderEditor.SetText(c_defaultVertexShader);
m_vertexShaderEditor.setText(c_defaultVertexShader);
}
m_vertexShaderEditor.SetLanguageDefinition(TextEditor::LanguageDefinition::GLSL());
}
{
f.replace_extension("glslp");
Expand All @@ -126,11 +127,10 @@ PCSX::Widgets::ShaderEditor::ShaderEditor(const std::string &base, std::string_v
std::ostringstream code;
code << in.rdbuf();
in.close();
m_pixelShaderEditor.SetText(code.str());
m_pixelShaderEditor.setText(code.str());
} else {
m_pixelShaderEditor.SetText(c_defaultPixelShader);
m_pixelShaderEditor.setText(c_defaultPixelShader);
}
m_pixelShaderEditor.SetLanguageDefinition(TextEditor::LanguageDefinition::GLSL());
}
{
f.replace_extension("lua");
Expand All @@ -139,11 +139,10 @@ PCSX::Widgets::ShaderEditor::ShaderEditor(const std::string &base, std::string_v
std::ostringstream code;
code << in.rdbuf();
in.close();
m_luaEditor.SetText(code.str());
m_luaEditor.setText(code.str());
} else {
m_luaEditor.SetText(c_defaultLuaInvoker);
m_luaEditor.setText(c_defaultLuaInvoker);
}
m_luaEditor.SetLanguageDefinition(TextEditor::LanguageDefinition::Lua());
}
}

Expand Down Expand Up @@ -305,17 +304,17 @@ std::optional<GLuint> PCSX::Widgets::ShaderEditor::compile(const std::vector<std
{
f.replace_extension("glslv");
std::ofstream out(f, std::ofstream::out);
out << m_vertexShaderEditor.GetText();
out << m_vertexShaderEditor.getText();
}
{
f.replace_extension("glslp");
std::ofstream out(f, std::ofstream::out);
out << m_pixelShaderEditor.GetText();
out << m_pixelShaderEditor.getText();
}
{
f.replace_extension("lua");
std::ofstream out(f, std::ofstream::out);
out << m_luaEditor.GetText();
out << m_luaEditor.getText();
}
}

Expand Down Expand Up @@ -379,23 +378,37 @@ bool PCSX::Widgets::ShaderEditor::draw(std::string_view title, GUI *gui) {
float width = contents.x / 3 - style.ItemInnerSpacing.x;
gui->useMonoFont();
if (m_showAll) {
m_vertexShaderEditor.Render(_("Vertex Shader"), {width, -footerHeight}, true);
ImVec2 size = {width, contents.y - footerHeight};
ImGui::BeginChild("VertexShaderEditor", size);
m_vertexShaderEditor.draw();
ImGui::EndChild();
ImGui::SameLine();
m_pixelShaderEditor.Render(_("Pixel Shader"), {width, -footerHeight}, true);
ImGui::BeginChild("PixelShaderEditor", size);
m_pixelShaderEditor.draw();
ImGui::EndChild();
ImGui::SameLine();
m_luaEditor.Render(_("Lua Invoker"), {width, -footerHeight}, true);
ImGui::BeginChild("LuaInvoker", size);
m_luaEditor.draw();
ImGui::EndChild();
} else {
if (ImGui::BeginTabBar("MyTabBar")) {
ImVec2 size = {contents.x, contents.y - footerHeight};
if (ImGui::BeginTabItem(_("Vertex Shader"))) {
m_vertexShaderEditor.Render(_("Vertex Shader"), {0, -footerHeight}, true);
ImGui::BeginChild("VertexShaderEditor", size);
m_vertexShaderEditor.draw();
ImGui::EndChild();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem(_("Pixel Shader"))) {
m_pixelShaderEditor.Render(_("Pixel Shader"), {0, -footerHeight}, true);
ImGui::BeginChild("PixelShaderEditor", size);
m_pixelShaderEditor.draw();
ImGui::EndChild();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem(_("Lua Invoker"))) {
m_luaEditor.Render(_("Lua Invoker"), {0, -footerHeight}, true);
ImGui::BeginChild("LuaInvoker", size);
m_luaEditor.draw();
ImGui::EndChild();
ImGui::EndTabItem();
}
ImGui::EndTabBar();
Expand All @@ -413,7 +426,7 @@ bool PCSX::Widgets::ShaderEditor::draw(std::string_view title, GUI *gui) {

ImGui::End();

return m_vertexShaderEditor.IsTextChanged() || m_pixelShaderEditor.IsTextChanged() || m_luaEditor.IsTextChanged();
return m_vertexShaderEditor.hasTextChanged() || m_pixelShaderEditor.hasTextChanged() || m_luaEditor.hasTextChanged();
}

void PCSX::Widgets::ShaderEditor::getRegistry(std::unique_ptr<Lua> &L) {
Expand Down
20 changes: 10 additions & 10 deletions src/gui/widgets/shader-editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <vector>

#include "GL/gl3w.h"
#include "ImGuiColorTextEdit/TextEditor.h"
#include "gui/widgets/zep.h"
#include "lua/luawrapper.h"

namespace PCSX {
Expand All @@ -44,9 +44,9 @@ class ShaderEditor {
bool m_show = false;

void setText(std::string_view VS, std::string_view PS, std::string_view L) {
m_vertexShaderEditor.SetText(VS.data());
m_pixelShaderEditor.SetText(PS.data());
m_luaEditor.SetText(L.data());
m_vertexShaderEditor.setText(VS.data());
m_pixelShaderEditor.setText(PS.data());
m_luaEditor.setText(L.data());
}

bool draw(std::string_view title, GUI* gui);
Expand All @@ -55,9 +55,9 @@ class ShaderEditor {
const ImVec2& dstSize);

private:
std::string getVertexText() { return m_vertexShaderEditor.GetText(); }
std::string getPixelText() { return m_pixelShaderEditor.GetText(); }
std::string getLuaText() { return m_luaEditor.GetText(); }
std::string getVertexText() { return m_vertexShaderEditor.getText(); }
std::string getPixelText() { return m_pixelShaderEditor.getText(); }
std::string getLuaText() { return m_luaEditor.getText(); }

void getRegistry(std::unique_ptr<Lua>& L);

Expand All @@ -69,9 +69,9 @@ class ShaderEditor {

const std::string m_baseFilename;

TextEditor m_vertexShaderEditor;
TextEditor m_pixelShaderEditor;
TextEditor m_luaEditor;
ZepEditor m_vertexShaderEditor = {"VertexShader.vert"};
ZepEditor m_pixelShaderEditor = {"PixelShader.frag"};
ZepEditor m_luaEditor = {"LuaInvoker.lua"};
GLuint m_shaderProgram = 0;
std::string m_errorMessage;
std::vector<std::string> m_lastLuaErrors;
Expand Down
39 changes: 39 additions & 0 deletions src/gui/widgets/zep.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/***************************************************************************
* Copyright (C) 2021 PCSX-Redux authors *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/

#include "gui/widgets/zep.h"

bool PCSX::Widgets::ZepEditor::draw() {
auto dpiScale = ImGui::GetWindowDpiScale();
auto min = ImGui::GetCursorScreenPos();
auto max = ImGui::GetContentRegionAvail();
max.x = std::max(1.0f, max.x);
max.y = std::max(1.0f, max.y);

// Fill the window
max.x = min.x + max.x;
max.y = min.y + max.y;
m_editor->SetDisplayRegion(Zep::NVec2f(min.x, min.y), Zep::NVec2f(max.x, max.y));

// Display the editor inside this window
m_editor->Display();
if (ImGui::IsWindowFocused()) m_editor->HandleInput();

return true;
}
84 changes: 84 additions & 0 deletions src/gui/widgets/zep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/***************************************************************************
* Copyright (C) 2021 PCSX-Redux authors *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/

#pragma once

#include <optional>

#include "zep/filesystem.h"
#include "zep/imgui/display_imgui.h"
#include "zep/imgui/editor_imgui.h"
#include "zep/mode_standard.h"
#include "zep/mode_vim.h"
#include "zep/tab_window.h"
#include "zep/theme.h"
#include "zep/window.h"

namespace PCSX {
namespace Widgets {

static Zep::NVec2f GetPixelScale() { return Zep::NVec2f(2.25f); }

class ZepEditor final : public Zep::IZepComponent {
public:
ZepEditor(const std::string &name)
: m_editor(std::make_unique<Zep::ZepEditor_ImGui>(Zep::ZepPath(""), GetPixelScale())) {
m_editor->RegisterCallback(this);

m_editor->InitWithText(name, "");
m_editor->SetGlobalMode(Zep::ZepMode_Standard::StaticName());
}

virtual ~ZepEditor() {}

void Destroy() {
m_editor->UnRegisterCallback(this);
m_editor.reset();
}

bool draw();

virtual Zep::ZepEditor &GetEditor() const override final { return *m_editor; }

void setText(const std::string &str) {
auto buffer = m_editor->GetMRUBuffer();
buffer->SetText(str);
}

std::string getText() {
auto buffer = m_editor->GetMRUBuffer();
return buffer->GetBufferText(buffer->Begin(), buffer->End());
}

bool hasTextChanged() {
auto currentTime = m_editor->GetMRUBuffer()->GetLastUpdateTime();
if (m_lastUpdateTime.has_value() && (m_lastUpdateTime.value() == currentTime)) {
return false;
}
m_lastUpdateTime = currentTime;
return true;
}

private:
std::unique_ptr<Zep::ZepEditor_ImGui> m_editor;
std::optional<decltype(m_editor->GetMRUBuffer()->GetLastUpdateTime())> m_lastUpdateTime;
};

} // namespace Widgets
} // namespace PCSX
2 changes: 1 addition & 1 deletion third_party/zep
2 changes: 1 addition & 1 deletion vsprojects/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<AdditionalIncludeDirectories>$(SolutionDir)..\;$(SolutionDir)..\src;$(SolutionDir)..\third_party;$(SolutionDir)..\third_party\ELFIO;$(SolutionDir)..\third_party\fmt\include;$(SolutionDir)..\third_party\freetype\include;$(SolutionDir)..\third_party\imgui;$(SolutionDir)..\third_party\imgui\backends;$(SolutionDir)..\third_party\imgui\examples\libs\gl3w;$(SolutionDir)..\third_party\imgui\misc\cpp;$(SolutionDir)..\third_party\imgui_club;$(SolutionDir)..\third_party\libelfin;$(SolutionDir)..\third_party\libuv\include;$(SolutionDir)..\third_party\libuv\src;$(SolutionDir)..\third_party\SDL\include;$(SolutionDir)..\third_party\uvw\src;$(SolutionDir)..\third_party\zlib;$(SolutionDir)..\third_party\zstr\src;$(SolutionDir)..\third_party\xbyak\xbyak;$(SolutionDir)..\third_party\zep\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<PreprocessorDefinitions>IMGUI_ENABLE_FREETYPE;HAVE_STDINT_H;_CONSOLE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_GNU_SOURCE;NOMINMAX;WIN32;_WINDOWS;_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>IMGUI_ENABLE_FREETYPE;HAVE_STDINT_H;_CONSOLE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_GNU_SOURCE;NOMINMAX;WIN32;_WINDOWS;_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING;ZEP_FEATURE_CPP_FILE_SYSTEM;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />
Expand Down
2 changes: 2 additions & 0 deletions vsprojects/gui/gui.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
<ClCompile Include="..\..\src\gui\widgets\source.cc" />
<ClCompile Include="..\..\src\gui\widgets\types.cc" />
<ClCompile Include="..\..\src\gui\widgets\vram-viewer.cc" />
<ClCompile Include="..\..\src\gui\widgets\zep.cc" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\gui\gui.h" />
Expand All @@ -215,6 +216,7 @@
<ClInclude Include="..\..\src\gui\widgets\source.h" />
<ClInclude Include="..\..\src\gui\widgets\types.h" />
<ClInclude Include="..\..\src\gui\widgets\vram-viewer.h" />
<ClInclude Include="..\..\src\gui\widgets\zep.h" />
<ClInclude Include="..\..\third_party\imgui_club\imgui_memory_editor\imgui_memory_editor.h" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions vsprojects/gui/gui.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<ClCompile Include="..\..\src\gui\widgets\shader-editor.cc">
<Filter>Source Files\widgets</Filter>
</ClCompile>
<ClCompile Include="..\..\src\gui\widgets\zep.cc">
<Filter>Source Files\widgets</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\gui\gui.h">
Expand Down Expand Up @@ -114,6 +117,9 @@
<ClInclude Include="..\..\src\gui\widgets\shader-editor.h">
<Filter>Header Files\widgets</Filter>
</ClInclude>
<ClInclude Include="..\..\src\gui\widgets\zep.h">
<Filter>Header Files\widgets</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down

0 comments on commit 42815c8

Please sign in to comment.