From 39555d92d1fd6858d23a53b4c40653ae78415061 Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Fri, 18 Mar 2022 21:35:25 -0700 Subject: [PATCH] Clean up things broken by the merge And refactor flag module yet again --- Source/ConformalDecals/ConformalDecals.csproj | 10 +- .../MaterialTextureProperty.cs | 1 + .../ConformalDecals/ModuleConformalDecal.cs | 14 +-- Source/ConformalDecals/ModuleConformalFlag.cs | 117 +++++++----------- Source/ConformalDecals/ModuleConformalText.cs | 37 ++---- 5 files changed, 65 insertions(+), 114 deletions(-) diff --git a/Source/ConformalDecals/ConformalDecals.csproj b/Source/ConformalDecals/ConformalDecals.csproj index ff77c09..7417085 100644 --- a/Source/ConformalDecals/ConformalDecals.csproj +++ b/Source/ConformalDecals/ConformalDecals.csproj @@ -1,4 +1,4 @@ - + net48 8 @@ -36,22 +36,22 @@ - + - + - + - + diff --git a/Source/ConformalDecals/MaterialProperties/MaterialTextureProperty.cs b/Source/ConformalDecals/MaterialProperties/MaterialTextureProperty.cs index e7310ab..3662b71 100644 --- a/Source/ConformalDecals/MaterialProperties/MaterialTextureProperty.cs +++ b/Source/ConformalDecals/MaterialProperties/MaterialTextureProperty.cs @@ -27,6 +27,7 @@ namespace ConformalDecals.MaterialProperties { public string TextureUrl { get => _textureUrl; set { + if (_textureUrl == value) return; // URL hasnt changed _texture = LoadTexture(value, isNormal); _textureUrl = value; } diff --git a/Source/ConformalDecals/ModuleConformalDecal.cs b/Source/ConformalDecals/ModuleConformalDecal.cs index f0825f6..1540177 100644 --- a/Source/ConformalDecals/ModuleConformalDecal.cs +++ b/Source/ConformalDecals/ModuleConformalDecal.cs @@ -441,14 +441,12 @@ namespace ConformalDecals { } } - // find all valid renderers - var renderers = part.parent.FindModelComponents(); - foreach (var renderer in renderers) { - // skip disabled renderers - if (renderer.gameObject.activeInHierarchy == false) continue; - - // skip blacklisted shaders - if (DecalConfig.IsBlacklisted(renderer.material.shader)) continue; + /// Setup decal by calling update functions relevent for the current situation + protected virtual void SetupDecal() { + if (HighLogic.LoadedSceneIsEditor) { + // Update tweakables in editor mode + UpdateTweakables(); + } if (HighLogic.LoadedSceneIsGame) { UpdateAll(); diff --git a/Source/ConformalDecals/ModuleConformalFlag.cs b/Source/ConformalDecals/ModuleConformalFlag.cs index 4729fcb..cbc0aeb 100644 --- a/Source/ConformalDecals/ModuleConformalFlag.cs +++ b/Source/ConformalDecals/ModuleConformalFlag.cs @@ -1,5 +1,3 @@ -using ConformalDecals.MaterialProperties; -using UniLinq; using UnityEngine; namespace ConformalDecals { @@ -10,8 +8,6 @@ namespace ConformalDecals { [KSPField(isPersistant = true)] public bool useCustomFlag; - private MaterialTextureProperty _flagTextureProperty; - public string MissionFlagUrl { get { if (HighLogic.LoadedSceneIsEditor) { @@ -27,28 +23,16 @@ namespace ConformalDecals { } } - public override void OnLoad(ConfigNode node) { - base.OnLoad(node); - - // Since OnLoad is called for all modules, we only need to update this module - // Updating symmetry counterparts would be redundent - UpdateFlag(); - } - public override void OnStart(StartState state) { - base.OnStart(state); - if (HighLogic.LoadedSceneIsEditor) { // Register flag change event GameEvents.onMissionFlagSelect.Add(OnEditorFlagSelected); - + // Register reset button event - Events[nameof(ResetFlag)].guiActiveEditor = useCustomFlag; + Events[nameof(ResetFlagButton)].guiActiveEditor = useCustomFlag; } - // Since OnStart is called for all modules, we only need to update this module - // Updating symmetry counterparts would be redundent - UpdateFlag(); + base.OnStart(state); } public override void OnDestroy() { @@ -56,92 +40,81 @@ namespace ConformalDecals { // Unregister flag change event GameEvents.onMissionFlagSelect.Remove(OnEditorFlagSelected); } - + base.OnDestroy(); } [KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-select-flag")] - public void SelectFlag() { + public void SelectFlagButton() { // Button for selecting a flag // This is a bit of a hack to bring up the stock flag selection menu // When its done, it calls OnCustomFlagSelected() - + // ReSharper disable once PossibleNullReferenceException var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject).GetComponent(); flagBrowser.OnFlagSelected = OnCustomFlagSelected; } [KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-reset-flag")] - public void ResetFlag() { - + public void ResetFlagButton() { // we are no longer using a custom flag, so instead use the mission or agency flag - useCustomFlag = false; - flagUrl = "Mission"; - UpdateFlag(true); - + SetFlag("Mission", false, true); + // disable the reset button, since it no longer makes sense - Events[nameof(ResetFlag)].guiActiveEditor = false; - flagUrl = MissionFlagUrl; - useCustomFlag = false; - UpdateAll(); - foreach (var decal in part.symmetryCounterparts.Select(o => o.GetComponent())) { - decal.Events[nameof(ResetFlag)].guiActiveEditor = false; - decal.flagUrl = flagUrl; - decal.useCustomFlag = false; - decal.UpdateAll(); - } + Events[nameof(ResetFlagButton)].guiActiveEditor = false; } private void OnCustomFlagSelected(FlagBrowser.FlagEntry newFlagEntry) { // Callback for when a flag is selected in the menu spawned by SelectFlag() - + // we are now using a custom flag with the URL of the new flag entry - useCustomFlag = true; - flagUrl = newFlagEntry.textureInfo.name; - UpdateFlag(true); - + SetFlag(newFlagEntry.textureInfo.name, true, true); + // make sure the reset button is now available - Events[nameof(ResetFlag)].guiActiveEditor = true; - flagUrl = newFlagEntry.textureInfo.name; - useCustomFlag = true; - UpdateAll(); + Events[nameof(ResetFlagButton)].guiActiveEditor = true; + } private void OnEditorFlagSelected(string newFlagUrl) { + // Callback for when a new mission flag is selected in the editor + // Since this callback is called for all modules, we only need to update this module + // Updating symmetry counterparts would be redundent + + // if we are using the mission flag, update it. otherwise ignore the call if (!useCustomFlag) { - flagUrl = newFlagUrl; - // Since this callback is called for all modules, we only need to update this module - // Updating symmetry counterparts would be redundent - UpdateFlag(); + SetFlag(newFlagUrl, false, false); } } - // Update the displayed flag texture for this decal or optionally any symmetry counterparts - private void UpdateFlag(bool recursive = false) { - // get the decal material property for the decal texture - var textureProperty = materialProperties.AddOrGetTextureProperty("_Decal", true); + private void SetFlag(string newFlagUrl, bool isCustom, bool recursive) { + // Function to set the flag URL, the custom flag - if(useCustomFlag) { - // set the texture to the custom flag - textureProperty.TextureUrl = flagUrl; - } - else { - // set the texture to the mission flag - textureProperty.TextureUrl = MissionFlagUrl; - } + // Set values + flagUrl = newFlagUrl; + useCustomFlag = isCustom; - UpdateMaterials(); - UpdateScale(); - + // Update material and projection + UpdateAll(); + + // Update symmetry counterparts if called to if (recursive) { - // for each symmetry counterpart, copy this part's properties and update it in turn foreach (var counterpart in part.symmetryCounterparts) { var decal = counterpart.GetComponent(); - - decal.useCustomFlag = useCustomFlag; - decal.flagUrl = flagUrl; - decal.UpdateFlag(); + decal.SetFlag(newFlagUrl, isCustom, false); } } } + + protected override void UpdateMaterials() { + // get the decal material property for the decal texture + var textureProperty = materialProperties.AddOrGetTextureProperty("_Decal", true); + + if (useCustomFlag) { // set the texture to the custom flag + textureProperty.TextureUrl = flagUrl; + } else { // set the texture to the mission flag + textureProperty.TextureUrl = MissionFlagUrl; + } + + base.UpdateMaterials(); + } } -} +} \ No newline at end of file diff --git a/Source/ConformalDecals/ModuleConformalText.cs b/Source/ConformalDecals/ModuleConformalText.cs index 89628c1..10082cd 100644 --- a/Source/ConformalDecals/ModuleConformalText.cs +++ b/Source/ConformalDecals/ModuleConformalText.cs @@ -29,8 +29,7 @@ namespace ConformalDecals { public void SetText() { if (_textEntryController == null) { _textEntryController = TextEntryController.Create(text, font, style, vertical, lineSpacing, charSpacing, lineSpacingRange, charSpacingRange, OnTextUpdate); - } - else { + } else { _textEntryController.Close(); } } @@ -47,8 +46,7 @@ namespace ConformalDecals { public void SetFillColor() { if (_fillColorPickerController == null) { _fillColorPickerController = ColorPickerController.Create(fillColor, OnFillColorUpdate); - } - else { + } else { _fillColorPickerController.Close(); } } @@ -70,8 +68,7 @@ namespace ConformalDecals { public void SetOutlineColor() { if (_outlineColorPickerController == null) { _outlineColorPickerController = ColorPickerController.Create(outlineColor, OnOutlineColorUpdate); - } - else { + } else { _outlineColorPickerController.Close(); } } @@ -234,8 +231,7 @@ namespace ConformalDecals { string fontName = ""; if (ParseUtil.ParseStringIndirect(ref fontName, node, "fontName")) { font = DecalConfig.GetFont(fontName); - } - else if (font == null) font = DecalConfig.GetFont("Calibri SDF"); + } else if (font == null) font = DecalConfig.GetFont("Calibri SDF"); int styleInt = 0; if (ParseUtil.ParseIntIndirect(ref styleInt, node, "style")) { @@ -247,28 +243,11 @@ namespace ConformalDecals { } protected override void SetupDecal() { - if (HighLogic.LoadedSceneIsEditor) { - // Update tweakables in editor mode - UpdateTweakables(); - } - if (HighLogic.LoadedSceneIsGame) { // For some reason text rendering fails on the first frame of a scene, so this is my workaround StartCoroutine(UpdateTextLate()); - } - else { - scale = defaultScale; - depth = defaultDepth; - opacity = defaultOpacity; - cutoff = defaultCutoff; - wear = defaultWear; - - UpdateTextures(); - UpdateMaterials(); - UpdateProjection(); - - // QUEUE PART FOR ICON FIXING IN VAB - DecalIconFixer.QueuePart(part.name); + } else { + base.SetupDecal(); } } @@ -281,7 +260,7 @@ namespace ConformalDecals { // Render text var newText = new DecalText(text, font, style, vertical, lineSpacing, charSpacing); var output = TextRenderer.UpdateText(_currentText, newText); - + // update the _currentText state variable // this is the ONLY place this variable should be set! otherwise the current state is lost _currentText = newText; @@ -320,4 +299,4 @@ namespace ConformalDecals { base.UpdateTweakables(); } } -} +} \ No newline at end of file