private void OnSwitcherWidgetClick() { ButtonWidget handler = EventSystem.current.currentSelectedGameObject.GetComponent <ButtonWidget>(); if (handler.switcherModule is ModuleB9PartSwitch) { ModuleB9PartSwitch b9Switch = (ModuleB9PartSwitch)handler.switcherModule; int nextSubtype = b9Switch.currentSubtypeIndex == b9Switch.SubtypesCount - 1 ? 0 : b9Switch.currentSubtypeIndex + 1; b9Switch.SwitchSubtype(b9Switch.subtypes[nextSubtype].subtypeName); delayedTooltipUpdate = 1; } else if (handler.switcherModule is ModulePartVariants) { ModulePartVariants pmv = (ModulePartVariants)handler.switcherModule; int currentVariantIndex = pmv.variantList.IndexOf(pmv.SelectedVariant); int nextvariant = currentVariantIndex == pmv.variantList.Count - 1 ? 0 : currentVariantIndex + 1; string nextVariantName = pmv.variantList[nextvariant].Name; pmv.SetVariant(nextVariantName); for (int i = 0; i < pmv.part.symmetryCounterparts.Count; i++) { PartModule pmSym = pmv.part.symmetryCounterparts[i].Modules[pmv.part.Modules.IndexOf(pmv)]; if (pmSym != null && pmSym is ModulePartVariants) { ((ModulePartVariants)pmSym).SetVariant(nextVariantName); } } UpdateInfoToolTip(false); } }
public string GetSelectedVariant() { ModulePartVariants mpv = Modules.GetModule <ModulePartVariants>(); ModSegSRBs.GetExtraInfo(mpv.SelectedVariant, ref segmentHeight, ref segmentWidth); Log.Info("Variant height, width: " + segmentHeight + ", " + segmentWidth); return(mpv.SelectedVariant.Name); }
private void GetVariantsInfo(ModulePartVariants mv, out string title, out string info) { title = mv.GetModuleDisplayName(); info = string.Empty; for (int i = 0; i < mv.variantList.Count; i++) { if (mv.SelectedVariant == mv.variantList[i]) { info += $"<b><color=#ffd200>> {Localizer.Format(mv.variantList[i].DisplayName)}</color></b>\n"; } else { info += $"- {Localizer.Format(mv.variantList[i].DisplayName)}\n"; } } }
/// <summary>Applies variant settinsg to the part attach nodes.</summary> /// <remarks> /// The stock apply variant method only does it when the active scene is editor. So if there is a /// part in the flight scene with a variant, it needs to be updated for the proper KIS behavior. /// </remarks> /// <param name="part">The part to apply the chnages to.</param> /// <param name="variant">The variant to apply.</param> /// <param name="updatePartPosition"> /// Tells if any connected parts at the attach nodes need to be repositioned accordingly. This may /// trigger collisions in the scene, so use carefully. /// </param> public static void ApplyVariantOnAttachNodes(Part part, PartVariant variant, bool updatePartPosition = false) { foreach (AttachNode partAttachNode in part.attachNodes) { foreach (AttachNode variantAttachNode in variant.AttachNodes) { if (partAttachNode.id == variantAttachNode.id) { if (updatePartPosition) { ModulePartVariants.UpdatePartPosition(partAttachNode, variantAttachNode); } partAttachNode.originalPosition = variantAttachNode.originalPosition; partAttachNode.position = variantAttachNode.position; partAttachNode.size = variantAttachNode.size; } } } }
/// <summary>Returns the part's model, used to make the perview icon.</summary> /// <remarks> /// Note, that this is not the actual part appearance. It's an optimized version, specifically /// made for the icon preview. In particular, the model is scaled to fit the icon's constrains. /// </remarks> /// <param name="avPart">The part proto to get the model from.</param> /// <param name="variant"> /// The part's variant to apply. If <c>null</c>, then variant will be extracted from /// <paramref name="partNode"/>. /// </param> /// <param name="partNode"> /// The part's persistent state. It's used to extract the part's variant. It can be <c>null</c>. /// </param> /// <param name="skipVariantsShader"> /// Tells if the variant shaders must not be applied to the model. For the purpose of making a /// preview icon it's usually undesirable to have the shaders changed. /// </param> /// <returns>The model of the part. Don't forget to destroy it when not needed.</returns> public GameObject GetIconPrefab( AvailablePart avPart, PartVariant variant = null, ConfigNode partNode = null, bool skipVariantsShader = true) { var iconPrefab = UnityEngine.Object.Instantiate(avPart.iconPrefab); iconPrefab.SetActive(true); if (variant == null && partNode != null) { variant = VariantsUtils.GetCurrentPartVariant(avPart, partNode); } if (variant != null) { DebugEx.Fine( "Applying variant to the iconPrefab: part={0}, variant={1}", avPart.name, variant.Name); ModulePartVariants.ApplyVariant( null, Hierarchy.FindTransformByPath(iconPrefab.transform, "**/model"), variant, KSP.UI.Screens.EditorPartIcon.CreateMaterialArray(iconPrefab), skipVariantsShader); } return(iconPrefab); }
/// <summary> /// Updates properties that change linearly with scale. /// </summary> /// <param name="moveParts">Whether or not to move attached parts.</param> /// <param name="absolute">Whether to use absolute or relative scaling.</param> private void ScalePart(bool moveParts, bool absolute) { ScalePartTransform(); int len = part.attachNodes.Count; for (int i = 0; i < len; i++) { AttachNode node = part.attachNodes[i]; AttachNode[] nodesWithSameId = part.attachNodes .Where(a => a.id == node.id) .ToArray(); int idIdx = Array.FindIndex(nodesWithSameId, a => a == node); AttachNode[] baseNodesWithSameId = _prefabPart.attachNodes .Where(a => a.id == node.id) .ToArray(); if (idIdx < baseNodesWithSameId.Length) { AttachNode baseNode = baseNodesWithSameId[idIdx]; MoveNode(node, baseNode, moveParts, absolute); } else { Log.warn("Error scaling part. Node {0} does not have counterpart in base part.", node.id); } } try { // support for ModulePartVariants (the stock texture switch module) if (_prefabPart.Modules.Contains("ModulePartVariants")) { ModulePartVariants pm = _prefabPart.Modules["ModulePartVariants"] as ModulePartVariants; ModulePartVariants m = part.Modules["ModulePartVariants"] as ModulePartVariants; int n = pm.variantList.Count; for (int i = 0; i < n; i++) { PartVariant v = m.variantList[i]; PartVariant pv = pm.variantList[i]; for (int j = 0; j < v.AttachNodes.Count; j++) { // the module contains attachNodes, so we need to scale those MoveNode(v.AttachNodes[j], pv.AttachNodes[j], false, true); } } } } catch (Exception e) { Log.warn("Exception during ModulePartVariants interaction" + e.ToString()); } if (part.srfAttachNode != null) { MoveNode(part.srfAttachNode, _prefabPart.srfAttachNode, moveParts, absolute); } if (moveParts) { int numChilds = part.children.Count; for (int i = 0; i < numChilds; i++) { Part child = part.children[i]; if (child.srfAttachNode == null || child.srfAttachNode.attachedPart != part) { continue; } Vector3 attachedPosition = child.transform.localPosition + child.transform.localRotation * child.srfAttachNode.position; Vector3 targetPosition = attachedPosition * ScalingFactor.relative.linear; child.transform.Translate(targetPosition - attachedPosition, part.transform); } } }