|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using UnityEngine; |
| 6 | + |
| 7 | +namespace Firespitter.gui |
| 8 | +{ |
| 9 | + public class FSmeshSwitch : PartModule |
| 10 | + { |
| 11 | + [KSPField] |
| 12 | + public string buttonName = "Next part variant"; |
| 13 | + |
| 14 | + [KSPField] |
| 15 | + public string objects = string.Empty; |
| 16 | + |
| 17 | + // in case of multiple instances of this module, on will be the master, the rest slaves. |
| 18 | + [KSPField] |
| 19 | + public bool isController = true; |
| 20 | + |
| 21 | + // in case of multiple sets of master/slaves, only affect ones on the same channel. |
| 22 | + [KSPField] |
| 23 | + public int channel = 0; |
| 24 | + |
| 25 | + [KSPField(isPersistant = true)] |
| 26 | + public int selectedObject = 0; |
| 27 | + |
| 28 | + private string[] objectNames; |
| 29 | + private List<Transform> objectTransforms = new List<Transform>(); |
| 30 | + |
| 31 | + [KSPEvent(guiActive = false, guiActiveEditor = true, guiActiveUnfocused = false, guiName = "Next part variant")] |
| 32 | + public void nextObjectEvent() |
| 33 | + { |
| 34 | + selectedObject++; |
| 35 | + if (selectedObject >= objectTransforms.Count) |
| 36 | + { |
| 37 | + selectedObject = 0; |
| 38 | + } |
| 39 | + switchToObject(selectedObject); |
| 40 | + } |
| 41 | + |
| 42 | + private void parseObjectNames() |
| 43 | + { |
| 44 | + objectNames = objects.Split(';'); |
| 45 | + if (objectNames.Length < 1) |
| 46 | + Debug.Log("FSmeshSwitch: Found no object names in the object list"); |
| 47 | + else |
| 48 | + { |
| 49 | + objectTransforms.Clear(); |
| 50 | + for (int i = 0; i < objectNames.Length; i++) |
| 51 | + { |
| 52 | + Transform newTransform = part.FindModelTransform(objectNames[i]); |
| 53 | + if (newTransform != null) |
| 54 | + { |
| 55 | + objectTransforms.Add(newTransform); |
| 56 | + //Debug.Log("FSmeshSwitch: added object to list: " + objectNames[i]); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + Debug.Log("FSmeshSwitch: could not find object " + objectNames[i]); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private void switchToObject(int objectNumber) |
| 67 | + { |
| 68 | + if (objectNumber >= objectTransforms.Count) return; |
| 69 | + |
| 70 | + for (int i = 0; i < objectTransforms.Count; i++) |
| 71 | + { |
| 72 | + objectTransforms[i].gameObject.renderer.enabled = false; |
| 73 | + } |
| 74 | + |
| 75 | + // enable the selected one last because there might be several entries with the same object, and we don't want to disable it after it's been enabled. |
| 76 | + objectTransforms[objectNumber].gameObject.renderer.enabled = true; |
| 77 | + } |
| 78 | + |
| 79 | + public override void OnStart(PartModule.StartState state) |
| 80 | + { |
| 81 | + parseObjectNames(); |
| 82 | + switchToObject(selectedObject); |
| 83 | + Events["nextObjectEvent"].guiName = buttonName; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments