From b37b542b8931005d6d229607407b945406204f42 Mon Sep 17 00:00:00 2001 From: myT <> Date: Sun, 2 Feb 2025 04:38:54 +0100 Subject: [PATCH] improved shader edit (re)open/close logic fixed shader edit reopen keeping the old code --- code/renderer/tr_gui.cpp | 55 ++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/code/renderer/tr_gui.cpp b/code/renderer/tr_gui.cpp index e69d32b..d6191b1 100644 --- a/code/renderer/tr_gui.cpp +++ b/code/renderer/tr_gui.cpp @@ -92,6 +92,7 @@ struct ShaderEditWindow int currentLineCursor; int currentLineX; int cursor; + int delayedShaderOpen = -1; bool active; bool validCursorCoords; }; @@ -393,21 +394,31 @@ static void OpenShaderDetails(shader_t* shader) FormatShaderCode(shaderWindow.formattedCode, sizeof(shaderWindow.formattedCode), shader); } +static void CloseShaderEdit(bool force = false) +{ + ShaderEditWindow& window = shaderEditWindow; + if(window.active || force) + { + R_SetShaderData(window.shader, &window.originalShader); + window.active = false; + window.delayedShaderOpen = -1; + } +} + static void OpenShaderEdit(shader_t* shader) { + CloseShaderEdit(); + ImGui::SetWindowFocus(EDIT_WINDOW_NAME); - if(shaderEditWindow.active) - { - R_SetShaderData(shaderEditWindow.shader, &shaderEditWindow.originalShader); - } - - shaderEditWindow.active = true; - shaderEditWindow.shader = shader; - shaderEditWindow.originalShader = *shader; - shaderEditWindow.shaderFilePath[0] = '\0'; - FormatShaderCode(shaderEditWindow.code, sizeof(shaderEditWindow.code), shader); - Q_strncpyz(shaderEditWindow.originalCode, shaderEditWindow.code, sizeof(shaderEditWindow.originalCode)); + ShaderEditWindow& window = shaderEditWindow; + window.active = true; + window.shader = shader; + window.originalShader = *shader; + window.shaderFilePath[0] = '\0'; + window.delayedShaderOpen = -1; + FormatShaderCode(window.code, sizeof(window.code), shader); + Q_strncpyz(window.originalCode, window.code, sizeof(window.originalCode)); tr.shaderParseFailed = qfalse; tr.shaderParseNumWarnings = 0; } @@ -2083,20 +2094,21 @@ static void SaveShaderCodeToFile() static void DrawShaderEdit() { + ShaderEditWindow& window = shaderEditWindow; + // handle the keyboard shortcut and delayed opening - static int delayedShaderOpen = -1; - if(delayedShaderOpen >= 0) + if(window.delayedShaderOpen >= 0) { - delayedShaderOpen--; + window.delayedShaderOpen--; } const bool mapLoaded = tr.world != NULL; - if((mapLoaded && IsShortcutPressed(ImGuiKey_E, ShortcutOptions::Global)) || delayedShaderOpen == 0) + if((mapLoaded && IsShortcutPressed(ImGuiKey_E, ShortcutOptions::Global)) || window.delayedShaderOpen == 0) { const bool isCheating = IsEditingAllowed(); if(isCheating && !tr.traceWorldShader) { tr.traceWorldShader = qtrue; - delayedShaderOpen = 4; + window.delayedShaderOpen = 4; } else if( isCheating && @@ -2105,12 +2117,10 @@ static void DrawShaderEdit() tr.tracedWorldShaderIndex < tr.numShaders) { OpenShaderEdit(tr.shaders[tr.tracedWorldShaderIndex]); - delayedShaderOpen = -1; } } // handle the window itself - ShaderEditWindow& window = shaderEditWindow; const bool wasActive = window.active; shader_t* const shader = window.shader; if(window.active && shader != NULL) @@ -2185,7 +2195,7 @@ static void DrawShaderEdit() ImGuiInputTextFlags_CallbackCharFilter | ImGuiInputTextFlags_CallbackAlways; ImGui::InputTextMultiline( - "##Shader Code", window.code, sizeof(window.code), inputSize, + va("##Shader Code: %s", shader->name), window.code, sizeof(window.code), inputSize, callbackFlags, &ShaderEditCallback); ImGuiContext& imguiContext = *GImGui; @@ -2427,7 +2437,7 @@ static void DrawShaderEdit() if(wasActive && !window.active) { - R_SetShaderData(window.shader, &window.originalShader); + CloseShaderEdit(true); } } @@ -3283,10 +3293,7 @@ void R_ShutDownGUI() // this is necessary to avoid crashes in the detail windows // following a map change or video restart: // the renderer is shut down and the pointers become stale - if(shaderEditWindow.active) - { - R_SetShaderData(shaderEditWindow.shader, &shaderEditWindow.originalShader); - } + CloseShaderEdit(); memset(&imageWindow, 0, sizeof(imageWindow)); memset(&shaderWindow, 0, sizeof(shaderWindow)); memset(&shaderEditWindow, 0, sizeof(shaderEditWindow));