2020-12-02 09:40:46 +00:00
|
|
|
using ConformalDecals.MaterialProperties;
|
|
|
|
using UniLinq;
|
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;
|
|
|
|
|
2020-12-02 09:40:46 +00:00
|
|
|
private MaterialTextureProperty _flagTextureProperty;
|
|
|
|
|
2020-06-09 02:38:35 +00:00
|
|
|
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-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-05 07:29:23 +00:00
|
|
|
public override void OnDestroy() {
|
2020-12-02 09:40:46 +00:00
|
|
|
GameEvents.onMissionFlagSelect.Remove(OnEditorFlagSelected);
|
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() {
|
2021-01-01 22:28:30 +00:00
|
|
|
var flagBrowser = (Instantiate((Object) (new FlagBrowserGUIButton(null, null, null, null)).FlagBrowserPrefab) as GameObject)?.GetComponent<FlagBrowser>();
|
|
|
|
if (flagBrowser is { }) flagBrowser.OnFlagSelected = OnCustomFlagSelected;
|
2020-06-09 02:38:35 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
Events[nameof(ResetFlag)].guiActiveEditor = false;
|
2020-12-02 09:40:46 +00:00
|
|
|
flagUrl = MissionFlagUrl;
|
|
|
|
useCustomFlag = false;
|
|
|
|
UpdateAll();
|
|
|
|
foreach (var decal in part.symmetryCounterparts.Select(o => o.GetComponent<ModuleConformalFlag>())) {
|
|
|
|
decal.Events[nameof(ResetFlag)].guiActiveEditor = false;
|
|
|
|
decal.flagUrl = flagUrl;
|
|
|
|
decal.useCustomFlag = false;
|
|
|
|
decal.UpdateAll();
|
|
|
|
}
|
2020-06-09 02:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnCustomFlagSelected(FlagBrowser.FlagEntry newFlagEntry) {
|
|
|
|
Events[nameof(ResetFlag)].guiActiveEditor = true;
|
2020-12-02 09:40:46 +00:00
|
|
|
flagUrl = newFlagEntry.textureInfo.name;
|
|
|
|
useCustomFlag = true;
|
|
|
|
UpdateAll();
|
2020-06-09 02:38:35 +00:00
|
|
|
|
2020-12-02 09:40:46 +00:00
|
|
|
foreach (var decal in part.symmetryCounterparts.Select(o => o.GetComponent<ModuleConformalFlag>())) {
|
|
|
|
decal.Events[nameof(ResetFlag)].guiActiveEditor = true;
|
|
|
|
decal.flagUrl = flagUrl;
|
|
|
|
decal.useCustomFlag = true;
|
|
|
|
decal.UpdateAll();
|
2020-06-06 04:29:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:40:46 +00:00
|
|
|
private void OnEditorFlagSelected(string newFlagUrl) {
|
|
|
|
if (!useCustomFlag) UpdateAll();
|
2020-06-03 03:36:45 +00:00
|
|
|
}
|
2020-06-09 02:38:35 +00:00
|
|
|
|
2020-12-02 09:40:46 +00:00
|
|
|
protected override void UpdateTextures() {
|
2020-12-05 01:05:37 +00:00
|
|
|
_flagTextureProperty ??= materialProperties.AddOrGetTextureProperty("_Decal", true);
|
2020-12-05 00:16:11 +00:00
|
|
|
|
2020-12-02 09:40:46 +00:00
|
|
|
base.UpdateTextures();
|
|
|
|
if (useCustomFlag) {
|
|
|
|
_flagTextureProperty.TextureUrl = flagUrl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_flagTextureProperty.TextureUrl = MissionFlagUrl;
|
2020-06-09 02:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-03 03:36:45 +00:00
|
|
|
}
|
|
|
|
}
|