/// <summary> /// Here when a part is attached. Is also called for the initial root part /// of a vessel. /// </summary> /// <param name="part"></param> private void OnPartAttached(Part part) { ModuleSimpleFuelSwitch module = ModuleSimpleFuelSwitch.TryFind(part); if (module != null) { module.OnPartAttached(); } }
/// <summary> /// Here when a variant is applied in the editor. /// </summary> /// <param name="part"></param> /// <param name="variant"></param> private void OnEditorVariantApplied(Part part, PartVariant variant) { ModuleSimpleFuelSwitch module = ModuleSimpleFuelSwitch.TryFind(part); if (module != null) { module.OnVariantApplied(variant); } }
/// <summary> /// Here when a part is copied. /// </summary> /// <param name="part"></param> private void OnPartCopied(Part newPartCopy) { ModuleSimpleFuelSwitch module = ModuleSimpleFuelSwitch.TryFind(newPartCopy); if (module != null) { module.OnPartCopied(); } }
/// <summary> /// Here when any part's PAW is popped up. /// </summary> /// <param name="window"></param> /// <param name="part"></param> private void OnPartActionUIShown(UIPartActionWindow window, Part part) { ModuleSimpleFuelSwitch module = ModuleSimpleFuelSwitch.TryFind(part); if (module != null) { module.OnPartActionUIShown(window); } }
/// <summary> /// Shenanigans, Step 3: /// Whew, the copy process is finally done! But the original part that we copied from had /// its resources nuked, and this is the first (and only) chance we have to restore them... /// but this method gets called ON THE CLONE, not on the part that was copied *from*. /// And (here's the silly part)... when we receive the "part copied" event, it doesn't /// say WHAT PART IT WAS COPIED FROM. So that's where we use our temporary storage /// variable to find it and fix it. /// </summary> internal void OnPartCopied() { if (temporaryCopiedFromPart == null) { return; // copied as a symmetry counterpart, or something } ModuleSimpleFuelSwitch module = TryFind(temporaryCopiedFromPart); temporaryCopiedFromPart = null; if (module != null) { module.OnCompletelyFinishedCopyingThisPart(); } }
/// <summary> /// Shenanigans, Step 2: /// Okay, we successfully cloned this part to another, stripping ourselves of resources /// in the process. Unfortunately, we can't yet restore our resources, because KSP is /// not yet finished copying all the stuff across from this one to the copy. So we /// have to tell the new copy to remember where it was copied *from*. Why? Because /// silly, that's why. See notes below. /// </summary> /// <param name="copyPartModule"></param> /// <param name="asSymCounterpart"></param> public override void OnWasCopied(PartModule copyPartModule, bool asSymCounterpart) { base.OnWasCopied(copyPartModule, asSymCounterpart); if (asSymCounterpart) { return; // we don't care about this case } ModuleSimpleFuelSwitch clone = (ModuleSimpleFuelSwitch)copyPartModule; if (clone.temporaryCopiedFromPart != null) { throw new InvalidOperationException("Clone already had a parent set, something's wrong"); } clone.temporaryCopiedFromPart = part; }
/// <summary> /// Try to find the first ModuleSimpleFuelSwitch on the part, or null if not found. /// </summary> /// <param name="part"></param> /// <returns></returns> internal static ModuleSimpleFuelSwitch TryFind(Part part) { if (part == null) { return(null); } for (int i = 0; i < part.Modules.Count; ++i) { ModuleSimpleFuelSwitch module = part.Modules[i] as ModuleSimpleFuelSwitch; if (module != null) { return(module); } } return(null); // not found }
/// <summary> /// Here when an event happens to the part in the editor. /// </summary> /// <param name="eventType"></param> /// <param name="part"></param> private void OnEditorPartEvent(ConstructionEventType eventType, Part part) { switch (eventType) { case ConstructionEventType.PartCreated: OnPartCreated(part); break; case ConstructionEventType.PartAttached: ModuleSimpleFuelSwitch.OnPartAttached(part); break; default: // don't care about anything else break; } }
/// <summary> /// Here when we need to set up the resources on a part (i.e. when first initializing, or /// when the player changes it by clicking the button). /// </summary> /// <param name="affectSymCounterparts"></param> private void UpdateSelectedResources(bool affectSymCounterparts) { InitializeAvailableResources(); if (currentResourcesId == DEFAULT_FLAG) { currentResourcesId = availableResources.DefaultResourcesId; } // Set the name displayed in the PAW. SwitchResourcesEvent.guiName = string.Format( "{0}: {1}", availableResources.selectorFieldName, availableResources.DisplayNameOf(currentResourcesId)); // Actually set up the resources on the part. SwitchableResource[] selectedResources = availableResources[currentResourcesId]; part.Resources.Clear(); for (int i = 0; i < selectedResources.Length; ++i) { part.Resources.Add(selectedResources[i].CreateResourceNode()); } if (affectSymCounterparts) { for (int i = 0; i < part.symmetryCounterparts.Count; ++i) { Part symCounterpart = part.symmetryCounterparts[i]; ModuleSimpleFuelSwitch switchModule = TryFind(symCounterpart); if (switchModule != null) { switchModule.currentResourcesId = currentResourcesId; switchModule.UpdateSelectedResources(false); } } } }
/// <summary> /// Here when a vessel in flight is loaded. /// </summary> /// <param name="data"></param> private void OnVesselLoaded(Vessel vessel) { ModuleSimpleFuelSwitch.OnVesselLoaded(vessel); }
/// <summary> /// Here when a vessel is rolled out to the launchpad ready for launch. /// </summary> /// <param name="data"></param> private void OnVesselRollout(ShipConstruct ship) { ModuleSimpleFuelSwitch.OnShipLoaded(ship); }
/// <summary> /// Here when a ship loads in the editor. /// </summary> /// <param name="ship"></param> /// <param name="loadType"></param> private void OnEditorLoad(ShipConstruct ship, CraftBrowserDialog.LoadType loadType) { ModuleSimpleFuelSwitch.OnShipLoaded(ship); }