KSP-Conformal-Decals/Source/ConformalDecals/ModuleConformalFlag.cs

97 lines
3.6 KiB
C#
Raw Normal View History

2020-12-02 09:40:46 +00:00
using ConformalDecals.MaterialProperties;
using ConformalDecals.Util;
2020-12-02 09:40:46 +00:00
using UniLinq;
using UnityEngine;
namespace ConformalDecals {
2020-06-06 20:20:50 +00:00
public class ModuleConformalFlag : ModuleConformalDecal {
private const string DefaultFlag = "Squad/Flags/default";
[KSPField(isPersistant = true)] public string flagUrl = DefaultFlag;
[KSPField(isPersistant = true)] public bool useCustomFlag;
2020-12-02 09:40:46 +00:00
private MaterialTextureProperty _flagTextureProperty;
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 OnStart(StartState state) {
base.OnStart(state);
if (HighLogic.LoadedSceneIsGame) {
GameEvents.onMissionFlagSelect.Add(OnEditorFlagSelected);
}
if (HighLogic.LoadedSceneIsEditor) {
Events[nameof(ResetFlag)].guiActiveEditor = useCustomFlag;
}
}
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")]
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")]
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();
}
}
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-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-12-02 09:40:46 +00:00
private void OnEditorFlagSelected(string newFlagUrl) {
if (!useCustomFlag) UpdateAll();
}
2020-12-02 09:40:46 +00:00
protected override void UpdateTextures() {
_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;
}
}
}
}