From 492a7d031730aee1f9cf0a323c24e3fc63f265e9 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Mon, 8 Jun 2020 19:38:35 -0700 Subject: [PATCH] Add flag selection to flag decals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Add flag selection code blatantly stolen from WBDecals • Fix attachment of symmetrically attached decals --- .../Plugins/ConformalDecals.dll | 4 +- .../ConformalDecals/ModuleConformalDecal.cs | 2 +- Source/ConformalDecals/ModuleConformalFlag.cs | 90 ++++++++++++++++--- 3 files changed, 80 insertions(+), 16 deletions(-) diff --git a/Distribution/GameData/ConformalDecals/Plugins/ConformalDecals.dll b/Distribution/GameData/ConformalDecals/Plugins/ConformalDecals.dll index 47720eb..30007da 100644 --- a/Distribution/GameData/ConformalDecals/Plugins/ConformalDecals.dll +++ b/Distribution/GameData/ConformalDecals/Plugins/ConformalDecals.dll @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:65795d6785bc2a83fd9bf3c37d31aa010b21ac3e05e4522533d3fad2e1b8c8df -size 34304 +oid sha256:13a7bd932cbb3d6a0313eab6825908755df077b4ad0ff822dd0140fc3b69260c +size 35840 diff --git a/Source/ConformalDecals/ModuleConformalDecal.cs b/Source/ConformalDecals/ModuleConformalDecal.cs index 38a1b65..f6e01fb 100644 --- a/Source/ConformalDecals/ModuleConformalDecal.cs +++ b/Source/ConformalDecals/ModuleConformalDecal.cs @@ -324,7 +324,7 @@ namespace ConformalDecals { } protected void OnEditorEvent(ConstructionEventType eventType, Part eventPart) { - if (eventPart != this.part) return; + if (this.part != eventPart && !part.symmetryCounterparts.Contains(eventPart)) return; switch (eventType) { case ConstructionEventType.PartAttached: OnAttach(); diff --git a/Source/ConformalDecals/ModuleConformalFlag.cs b/Source/ConformalDecals/ModuleConformalFlag.cs index 3cbaaef..e0996a2 100644 --- a/Source/ConformalDecals/ModuleConformalFlag.cs +++ b/Source/ConformalDecals/ModuleConformalFlag.cs @@ -1,45 +1,109 @@ using ConformalDecals.Util; +using UnityEngine; namespace ConformalDecals { public class ModuleConformalFlag : ModuleConformalDecal { private const string DefaultFlag = "Squad/Flags/default"; + [KSPField(isPersistant = true)] public string flagUrl = DefaultFlag; + + [KSPField(isPersistant = true)] public bool useCustomFlag; + + public string MissionFlagUrl { + get { + if (HighLogic.LoadedSceneIsEditor) { + return string.IsNullOrEmpty(EditorLogic.FlagURL) ? HighLogic.CurrentGame.flagURL : EditorLogic.FlagURL; + } + + if (HighLogic.LoadedSceneIsFlight) { + return string.IsNullOrEmpty(part.flagURL) ? HighLogic.CurrentGame.flagURL : part.flagURL; + } + + return DefaultFlag; + } + } + public override void OnLoad(ConfigNode node) { base.OnLoad(node); - UpdateFlag(GetDefaultFlag()); + if (useCustomFlag) { + SetFlag(flagUrl); + } + else { + SetFlag(MissionFlagUrl); + } } public override void OnStart(StartState state) { base.OnStart(state); if (HighLogic.LoadedSceneIsGame) { - GameEvents.onMissionFlagSelect.Add(UpdateFlag); + GameEvents.onMissionFlagSelect.Add(OnEditorFlagSelected); + } + + if (HighLogic.LoadedSceneIsEditor) { + Events[nameof(ResetFlag)].guiActiveEditor = useCustomFlag; } - UpdateFlag(GetDefaultFlag()); + if (useCustomFlag) { + SetFlag(flagUrl); + } + else { + SetFlag(MissionFlagUrl); + } } public override void OnDestroy() { - GameEvents.onMissionFlagSelect.Remove(UpdateFlag); + GameEvents.onMissionFlagSelect.Remove(SetFlag); base.OnDestroy(); } - private string GetDefaultFlag() { - if (HighLogic.LoadedSceneIsGame) { - return EditorLogic.FlagURL != string.Empty ? EditorLogic.FlagURL : HighLogic.CurrentGame.flagURL; - } - else { - return DefaultFlag; + [KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "Select Flag")] + public void SelectFlag() { + var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject).GetComponent(); + flagBrowser.OnFlagSelected = OnCustomFlagSelected; + } + + [KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "Reset Flag")] + public void ResetFlag() { + SetFlag(MissionFlagUrl); + SetFlagSymmetryCounterparts(MissionFlagUrl); + + useCustomFlag = false; + Events[nameof(ResetFlag)].guiActiveEditor = false; + } + + private void OnCustomFlagSelected(FlagBrowser.FlagEntry newFlagEntry) { + SetFlag(newFlagEntry.textureInfo.name); + SetFlagSymmetryCounterparts(newFlagEntry.textureInfo.name); + + useCustomFlag = true; + Events[nameof(ResetFlag)].guiActiveEditor = true; + } + + private void OnEditorFlagSelected(string newFlagUrl) { + if (useCustomFlag) { + SetFlag(newFlagUrl); + SetFlagSymmetryCounterparts(newFlagUrl); } } - private void UpdateFlag(string flagUrl) { - this.Log($"Loading flag texture '{flagUrl}'."); + private void SetFlag(string newFlagUrl) { + this.Log($"Loading flag texture '{newFlagUrl}'."); - materialProperties.AddOrGetTextureProperty("_Decal", true).TextureUrl = flagUrl; + flagUrl = newFlagUrl; + materialProperties.AddOrGetTextureProperty("_Decal", true).TextureUrl = newFlagUrl; UpdateMaterials(); } + + private void SetFlagSymmetryCounterparts(string newFlagUrl) { + foreach (var counterpart in part.symmetryCounterparts) { + var decal = counterpart.GetComponent(); + + decal.SetFlag(newFlagUrl); + decal.useCustomFlag = useCustomFlag; + } + } } } \ No newline at end of file