2020-06-03 03:36:45 +00:00
|
|
|
using ConformalDecals.Util;
|
2020-06-09 02:38:35 +00:00
|
|
|
using UnityEngine;
|
2020-06-03 03:36:45 +00:00
|
|
|
|
|
|
|
namespace ConformalDecals {
|
2020-06-06 20:20:50 +00:00
|
|
|
public class ModuleConformalFlag : ModuleConformalDecal {
|
2020-06-06 04:29:57 +00:00
|
|
|
private const string DefaultFlag = "Squad/Flags/default";
|
2020-06-03 03:36:45 +00:00
|
|
|
|
2020-06-09 02:38:35 +00:00
|
|
|
[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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 07:12:09 +00:00
|
|
|
public override void OnLoad(ConfigNode node) {
|
|
|
|
base.OnLoad(node);
|
2020-06-03 05:45:48 +00:00
|
|
|
|
2020-06-09 02:38:35 +00:00
|
|
|
if (useCustomFlag) {
|
|
|
|
SetFlag(flagUrl);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SetFlag(MissionFlagUrl);
|
|
|
|
}
|
2020-06-03 03:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnStart(StartState state) {
|
|
|
|
base.OnStart(state);
|
|
|
|
|
2020-06-04 07:12:09 +00:00
|
|
|
if (HighLogic.LoadedSceneIsGame) {
|
2020-06-09 02:38:35 +00:00
|
|
|
GameEvents.onMissionFlagSelect.Add(OnEditorFlagSelected);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HighLogic.LoadedSceneIsEditor) {
|
|
|
|
Events[nameof(ResetFlag)].guiActiveEditor = useCustomFlag;
|
2020-06-04 07:12:09 +00:00
|
|
|
}
|
2020-06-06 04:29:57 +00:00
|
|
|
|
2020-06-09 02:38:35 +00:00
|
|
|
if (useCustomFlag) {
|
|
|
|
SetFlag(flagUrl);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SetFlag(MissionFlagUrl);
|
|
|
|
}
|
2020-06-04 07:12:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 07:29:23 +00:00
|
|
|
public override void OnDestroy() {
|
2020-06-09 02:38:35 +00:00
|
|
|
GameEvents.onMissionFlagSelect.Remove(SetFlag);
|
2020-06-05 07:29:23 +00:00
|
|
|
base.OnDestroy();
|
|
|
|
}
|
|
|
|
|
2020-06-09 02:50:33 +00:00
|
|
|
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-select-flag")]
|
2020-06-09 02:38:35 +00:00
|
|
|
public void SelectFlag() {
|
|
|
|
var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject).GetComponent<FlagBrowser>();
|
|
|
|
flagBrowser.OnFlagSelected = OnCustomFlagSelected;
|
|
|
|
}
|
|
|
|
|
2020-06-09 02:50:33 +00:00
|
|
|
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-reset-flag")]
|
2020-06-09 02:38:35 +00:00
|
|
|
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) {
|
2020-06-12 04:58:04 +00:00
|
|
|
if (!useCustomFlag) {
|
2020-06-09 02:38:35 +00:00
|
|
|
SetFlag(newFlagUrl);
|
|
|
|
SetFlagSymmetryCounterparts(newFlagUrl);
|
2020-06-06 04:29:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 02:38:35 +00:00
|
|
|
private void SetFlag(string newFlagUrl) {
|
|
|
|
this.Log($"Loading flag texture '{newFlagUrl}'.");
|
2020-06-03 05:45:48 +00:00
|
|
|
|
2020-06-09 02:38:35 +00:00
|
|
|
flagUrl = newFlagUrl;
|
|
|
|
materialProperties.AddOrGetTextureProperty("_Decal", true).TextureUrl = newFlagUrl;
|
2020-06-03 03:36:45 +00:00
|
|
|
|
2020-06-04 07:12:09 +00:00
|
|
|
UpdateMaterials();
|
2020-06-17 20:57:36 +00:00
|
|
|
UpdateScale();
|
2020-06-03 03:36:45 +00:00
|
|
|
}
|
2020-06-09 02:38:35 +00:00
|
|
|
|
|
|
|
private void SetFlagSymmetryCounterparts(string newFlagUrl) {
|
|
|
|
foreach (var counterpart in part.symmetryCounterparts) {
|
|
|
|
var decal = counterpart.GetComponent<ModuleConformalFlag>();
|
|
|
|
|
|
|
|
decal.SetFlag(newFlagUrl);
|
|
|
|
decal.useCustomFlag = useCustomFlag;
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 03:36:45 +00:00
|
|
|
}
|
|
|
|
}
|