-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using UnityEngine; | ||
|
||
namespace Firespitter.gui | ||
{ | ||
public class FSmeshSwitch : PartModule | ||
{ | ||
[KSPField] | ||
public string buttonName = "Next part variant"; | ||
|
||
[KSPField] | ||
public string objects = string.Empty; | ||
|
||
// in case of multiple instances of this module, on will be the master, the rest slaves. | ||
[KSPField] | ||
public bool isController = true; | ||
|
||
// in case of multiple sets of master/slaves, only affect ones on the same channel. | ||
[KSPField] | ||
public int channel = 0; | ||
|
||
[KSPField(isPersistant = true)] | ||
public int selectedObject = 0; | ||
|
||
private string[] objectNames; | ||
private List<Transform> objectTransforms = new List<Transform>(); | ||
|
||
[KSPEvent(guiActive = false, guiActiveEditor = true, guiActiveUnfocused = false, guiName = "Next part variant")] | ||
public void nextObjectEvent() | ||
{ | ||
selectedObject++; | ||
if (selectedObject >= objectTransforms.Count) | ||
{ | ||
selectedObject = 0; | ||
} | ||
switchToObject(selectedObject); | ||
} | ||
|
||
private void parseObjectNames() | ||
{ | ||
objectNames = objects.Split(';'); | ||
if (objectNames.Length < 1) | ||
Debug.Log("FSmeshSwitch: Found no object names in the object list"); | ||
else | ||
{ | ||
objectTransforms.Clear(); | ||
for (int i = 0; i < objectNames.Length; i++) | ||
{ | ||
Transform newTransform = part.FindModelTransform(objectNames[i]); | ||
if (newTransform != null) | ||
{ | ||
objectTransforms.Add(newTransform); | ||
//Debug.Log("FSmeshSwitch: added object to list: " + objectNames[i]); | ||
} | ||
else | ||
{ | ||
Debug.Log("FSmeshSwitch: could not find object " + objectNames[i]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void switchToObject(int objectNumber) | ||
{ | ||
if (objectNumber >= objectTransforms.Count) return; | ||
|
||
for (int i = 0; i < objectTransforms.Count; i++) | ||
{ | ||
objectTransforms[i].gameObject.renderer.enabled = false; | ||
} | ||
|
||
// 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. | ||
objectTransforms[objectNumber].gameObject.renderer.enabled = true; | ||
} | ||
|
||
public override void OnStart(PartModule.StartState state) | ||
{ | ||
parseObjectNames(); | ||
switchToObject(selectedObject); | ||
Events["nextObjectEvent"].guiName = buttonName; | ||
} | ||
} | ||
} |
This file contains 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