-
Notifications
You must be signed in to change notification settings - Fork 3
MiniMods #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Spartwo
wants to merge
3
commits into
KSPModdingLibs:main
Choose a base branch
from
Spartwo:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
MiniMods #27
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,282 @@ | ||
| /* | ||
| Usecase: Part Gameobject Visibility based on Stack Attachment Node Occupancy with support for multiple nodes per part. | ||
| Originally By: Spartwo | ||
| Originally For: Kerbal Powers | ||
| License: GNU General Public License v3.0, see https://www.gnu.org/licenses/gpl-3.0.html | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using UnityEngine; | ||
|
|
||
| namespace KSPCommunityPartModules.Modules | ||
| { | ||
| public class ModuleAttachmentVisuals : PartModule | ||
| { | ||
| [KSPField] | ||
| public string requiredNodes; | ||
|
|
||
| //visible transforms when node occupied | ||
| [KSPField] | ||
| public string showAttached; | ||
|
|
||
| //visible transforms when node unoccupied | ||
| [KSPField] | ||
| public string showFree; | ||
|
|
||
| //"Enable/Disable <objectDisplayName>" in the editor | ||
| [KSPField] | ||
| public string objectDisplayName; | ||
|
|
||
| [KSPEvent( | ||
| guiActive = false, | ||
| guiActiveEditor = true, | ||
| guiName = "#KSPCPM_Capping" | ||
| )] | ||
| public void EventToggleVisual() => ToggleVisual(); | ||
|
|
||
| [KSPField(isPersistant = true)] | ||
| public bool transformEnabled = true; | ||
|
|
||
| // Nodes used as conditions | ||
| private List<AttachNode> nodes = new List<AttachNode>(); | ||
|
|
||
| // Transforms shown when nodes are occupied | ||
| private List<Transform> attachedTransforms = new List<Transform>(); | ||
|
|
||
| // Transforms shown when nodes are free | ||
| private List<Transform> freeTransforms = new List<Transform>(); | ||
|
|
||
| private HashSet<Part> directChildren = new HashSet<Part>(); | ||
|
|
||
| public override void OnStart(StartState state) | ||
| { | ||
| base.OnStart(state); | ||
|
|
||
| if (HighLogic.LoadedSceneIsEditor) | ||
| { | ||
| GameEvents.onEditorPartEvent.Add(OnEditorEvent); | ||
| } | ||
|
|
||
| CacheInitialChildren(); | ||
| ParseConfig(); | ||
| UpdateVisuals(); | ||
|
|
||
| // make this module cheaper in update loops | ||
| isEnabled = false; | ||
| enabled = false; | ||
| } | ||
|
|
||
| public void OnDestroy() | ||
| { | ||
| if (HighLogic.LoadedSceneIsEditor) | ||
| { | ||
| GameEvents.onEditorPartEvent.Remove(OnEditorEvent); | ||
| } | ||
| } | ||
|
|
||
| private void ParseConfig() | ||
| { | ||
| nodes.Clear(); | ||
| attachedTransforms.Clear(); | ||
| freeTransforms.Clear(); | ||
|
|
||
| // Parse attachment nodes | ||
| if (!string.IsNullOrWhiteSpace(requiredNodes)) | ||
| { | ||
| foreach (string nodeName in requiredNodes.Split(';')) | ||
| { | ||
| string nodeId = nodeName.Trim(); | ||
|
|
||
| if (string.IsNullOrEmpty(nodeId)) | ||
| continue; | ||
|
|
||
| AttachNode node = part.FindAttachNode(nodeId); | ||
|
|
||
| if (node != null) | ||
| { | ||
| nodes.Add(node); | ||
|
|
||
| Debug.Log( | ||
| $"[ModuleAttachmentVisuals] Found node '{nodeId}' " + | ||
| $"for part '{part.name}'" | ||
| ); | ||
| } | ||
| else | ||
| { | ||
| Debug.LogWarning( | ||
| $"[ModuleAttachmentVisuals] Node '{nodeId}' " + | ||
| $"not found on part '{part.name}'" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Parse transforms shown when condition is true | ||
| if (!string.IsNullOrWhiteSpace(showAttached)) | ||
| { | ||
| foreach (string transformName in showAttached.Split(',')) | ||
| { | ||
| string name = transformName.Trim(); | ||
|
|
||
| if (string.IsNullOrEmpty(name)) | ||
| continue; | ||
|
|
||
| Transform transform = part.FindModelTransform(name); | ||
|
|
||
| if (transform != null) | ||
| { | ||
| attachedTransforms.Add(transform); | ||
| } | ||
| else | ||
| { | ||
| Debug.LogWarning( | ||
| $"[ModuleAttachmentVisuals] Could not find attached transform " + | ||
| $"'{name}' on '{part.name}'" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Parse transforms shown when condition is false | ||
| if (!string.IsNullOrWhiteSpace(showFree)) | ||
| { | ||
| foreach (string transformName in showFree.Split(',')) | ||
| { | ||
| string name = transformName.Trim(); | ||
|
|
||
| if (string.IsNullOrEmpty(name)) | ||
| continue; | ||
|
|
||
| Transform transform = part.FindModelTransform(name); | ||
|
|
||
| if (transform != null) | ||
| { | ||
| freeTransforms.Add(transform); | ||
| } | ||
| else | ||
| { | ||
| Debug.LogWarning( | ||
| $"[ModuleAttachmentVisuals] Could not find free transform " + | ||
| $"'{name}' on '{part.name}'" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void UpdateVisuals() | ||
| { | ||
| // The objects only show when: | ||
| // - transform is enabled | ||
| // - all config nodes are occupied | ||
|
|
||
| bool allNodesAttached = | ||
| nodes.Count > 0 && | ||
| nodes.All(node => node != null && node.attachedPart != null); | ||
|
|
||
| bool visualActive = transformEnabled && allNodesAttached; | ||
|
|
||
| SetTransforms(attachedTransforms, visualActive); | ||
| SetTransforms(freeTransforms, !visualActive); | ||
|
|
||
| UpdateToggleEventUI(allNodesAttached); | ||
| } | ||
|
|
||
| private void UpdateToggleEventUI(bool allNodesAttached) | ||
| { | ||
| BaseEvent toggleEvent = Events["EventToggleVisual"]; | ||
|
|
||
| // Only offer the toggle when there's actually something capped to toggle | ||
| toggleEvent.active = allNodesAttached; | ||
|
|
||
| string verb = transformEnabled ? "Disable" : "Enable"; | ||
|
|
||
| string displayName = string.IsNullOrWhiteSpace(objectDisplayName) | ||
| ? $"{verb} Capping" | ||
| : $"{verb} {objectDisplayName}"; | ||
|
|
||
| toggleEvent.guiName = displayName; | ||
| } | ||
|
|
||
| private void SetTransforms(List<Transform> transforms, bool active) | ||
| { | ||
| foreach (Transform transform in transforms) | ||
| { | ||
| if (transform == null) | ||
| { | ||
| Debug.LogWarning( | ||
| $"[ModuleAttachmentVisuals] Transform is null on part '{part.name}'" | ||
| ); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| transform.gameObject.SetActive(active); | ||
| } | ||
| } | ||
|
|
||
| private void ToggleVisual() | ||
| { | ||
| transformEnabled = !transformEnabled; | ||
|
|
||
| UpdateVisuals(); | ||
| } | ||
|
|
||
| private void OnEditorEvent(ConstructionEventType evt, Part p) | ||
| { | ||
| if ( | ||
| evt != ConstructionEventType.PartAttached && | ||
| evt != ConstructionEventType.PartDetached | ||
| ) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Event directly involving this part | ||
| if (part == p) | ||
| { | ||
| CacheInitialChildren(); | ||
| UpdateVisuals(); | ||
| return; | ||
| } | ||
|
|
||
| bool wasDirectChild = directChildren.Contains(p); | ||
| bool isDirectChildNow = p.parent == part; | ||
|
|
||
| switch (evt) | ||
| { | ||
| case ConstructionEventType.PartAttached: | ||
|
|
||
| if (isDirectChildNow) | ||
| { | ||
| directChildren.Add(p); | ||
| UpdateVisuals(); | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| case ConstructionEventType.PartDetached: | ||
|
|
||
| if (wasDirectChild) | ||
| { | ||
| directChildren.Remove(p); | ||
| UpdateVisuals(); | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| private void CacheInitialChildren() | ||
| { | ||
| directChildren.Clear(); | ||
|
|
||
| foreach (Part child in part.children) | ||
| { | ||
| directChildren.Add(child); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| Usecase: Extends the stock resource converter so that only one type can run at a time. | ||
| Originally By: Spartwo | ||
| Originally For: Kerbal Powers | ||
| License: GNU General Public License v3.0, see https://www.gnu.org/licenses/gpl-3.0.html | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using UnityEngine; | ||
| using KSP.IO; | ||
| using KSP.UI.Screens; | ||
|
|
||
| namespace KSPCommunityPartModules.Modules | ||
| { | ||
| public class ModuleExclusiveResourceConverter : ModuleResourceConverter | ||
| { | ||
| public override void StartResourceConverter() | ||
| { | ||
| StopOtherConverters(); | ||
| base.StartResourceConverter(); | ||
| } | ||
|
|
||
| private void StopOtherConverters () | ||
| { | ||
| ModuleExclusiveResourceConverter[] otherConverters = part.GetComponents<ModuleExclusiveResourceConverter>(); | ||
| foreach (ModuleExclusiveResourceConverter e in otherConverters) | ||
| { | ||
| e.StopResourceConverter(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this module doesn't need to do anything in Update, it might be wise to disable it: https://github.com/KSP-KOS/KOS/blob/8f281a459b6ea0ba917c91bbbc117e68bb948199/src/kOS/Module/KOSNameTag.cs#L66
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. It's only driven by editor events