Add flag selection to flag decals

• Add flag selection code blatantly stolen from WBDecals
• Fix attachment of symmetrically attached decals
feature-multiSDF
Andrew Cassidy 4 years ago
parent e42ff52fb5
commit a4c46b94d2

@ -324,7 +324,7 @@ namespace ConformalDecals {
} }
protected void OnEditorEvent(ConstructionEventType eventType, Part eventPart) { protected void OnEditorEvent(ConstructionEventType eventType, Part eventPart) {
if (eventPart != this.part) return; if (this.part != eventPart && !part.symmetryCounterparts.Contains(eventPart)) return;
switch (eventType) { switch (eventType) {
case ConstructionEventType.PartAttached: case ConstructionEventType.PartAttached:
OnAttach(); OnAttach();

@ -1,45 +1,109 @@
using ConformalDecals.Util; using ConformalDecals.Util;
using UnityEngine;
namespace ConformalDecals { namespace ConformalDecals {
public class ModuleConformalFlag : ModuleConformalDecal { public class ModuleConformalFlag : ModuleConformalDecal {
private const string DefaultFlag = "Squad/Flags/default"; 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) { public override void OnLoad(ConfigNode node) {
base.OnLoad(node); base.OnLoad(node);
UpdateFlag(GetDefaultFlag()); if (useCustomFlag) {
SetFlag(flagUrl);
}
else {
SetFlag(MissionFlagUrl);
}
} }
public override void OnStart(StartState state) { public override void OnStart(StartState state) {
base.OnStart(state); base.OnStart(state);
if (HighLogic.LoadedSceneIsGame) { 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() { public override void OnDestroy() {
GameEvents.onMissionFlagSelect.Remove(UpdateFlag); GameEvents.onMissionFlagSelect.Remove(SetFlag);
base.OnDestroy(); base.OnDestroy();
} }
private string GetDefaultFlag() { [KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "Select Flag")]
if (HighLogic.LoadedSceneIsGame) { public void SelectFlag() {
return EditorLogic.FlagURL != string.Empty ? EditorLogic.FlagURL : HighLogic.CurrentGame.flagURL; var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject).GetComponent<FlagBrowser>();
} flagBrowser.OnFlagSelected = OnCustomFlagSelected;
else { }
return DefaultFlag;
[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) { private void SetFlag(string newFlagUrl) {
this.Log($"Loading flag texture '{flagUrl}'."); this.Log($"Loading flag texture '{newFlagUrl}'.");
materialProperties.AddOrGetTextureProperty("_Decal", true).TextureUrl = flagUrl; flagUrl = newFlagUrl;
materialProperties.AddOrGetTextureProperty("_Decal", true).TextureUrl = newFlagUrl;
UpdateMaterials(); UpdateMaterials();
} }
private void SetFlagSymmetryCounterparts(string newFlagUrl) {
foreach (var counterpart in part.symmetryCounterparts) {
var decal = counterpart.GetComponent<ModuleConformalFlag>();
decal.SetFlag(newFlagUrl);
decal.useCustomFlag = useCustomFlag;
}
}
} }
} }
Loading…
Cancel
Save