You are on page 1of 6

using UnityEngine; using System.

Collections; public class stateDiagram : MonoBehaviour { //too many variables public GUISkin newSkin; public GUISkin otherSkin; public static bool showStateDiagram = false; public static bool ssd = false; //contains next state depending on input public static Vector2[] stateInformation = new Vector2[9]; //ligh position at astate public static bool[,] lightsAtState = new bool[9,6]; //state in boolean values string[,] stateInBool = new string[4,9]; string tempStr = "0"; Vector2[] statePositions; public static int numberOfStates = 5; int tempN = 5; //windowRect public static Rect windowRect = new Rect(10,10,Screen.width-20,Screen.height-20); Rect boxRect; string nos = "5"; float stateWidth = 10.0F; bool showStateOptions = false; int selectedState = -1; int bitsForStates = 0; Color saveColor; // Use this for initialization void Start () { boxRect = new Rect(windowRect.width*(1/6.0f),windowRect.height*(1/6.0f),windowRect.width*(2.0f/3),windowRect .height*(2.0f/3)); //calculate initial positions and sizes of states statePositions = calculatePositions(numberOfStates,windowRect.width,windowRect.height); stateWidth = Mathf.Abs(statePositions[0].x-statePositions[1].x)/3.0F; //find bits for state bitsForStates = calculateBits(numberOfStates); //calculate stateInBool stateInBool = new string[9,4]{{"0","0","0","0"},{"0","0","0","1"},{"0","0","1","0"}, {"0","0","1","1"},{"0","1","0","0"},{"0","1","0","1"},{"0","1","1","0"},{"0","1","1","1"}, {"1","0","0","0"}}; tempN = numberOfStates; nos = numberOfStates.ToString(); }

void Update(){ if(showStateDiagram){ showStateDiagram = false; tempN = numberOfStates; nos = numberOfStates.ToString(); //calculate initial positions and sizes of states statePositions = calculatePositions(numberOfStates,windowRect.width,windowRect.height); stateWidth = Mathf.Abs(statePositions[0].x-statePositions[1].x)/3.0F; //find bits for state bitsForStates = calculateBits(numberOfStates); ssd = !ssd; boxRect = new Rect(windowRect.width*(1/6.0f),windowRect.height*(1/6.0f),windowRect.width*(2.0f/3),windowRect .height*(2.0f/3)); } } void OnGUI () { if(ssd){ GUI.skin = newSkin; windowRect = GUI.Window(0,windowRect,createWindow,"State Diagram"); } } void createWindow(int windowID) { //Find the number of states GUI.Label(new Rect(10,20,150,50),"Number Of States: "); nos = GUI.TextField(new Rect(155,20,20,30),nos,1); //check new number of states if(int.TryParse(nos,out tempN)) { if(tempN != numberOfStates && tempN >= 3) { //set new number and calculate new positions and sizes numberOfStates = tempN; statePositions = calculatePositions(numberOfStates,windowRect.width,windowRect.height); stateWidth = Mathf.Abs((statePositions[0]statePositions[1]).magnitude)/3.0F; //find bits for state bitsForStates = calculateBits(numberOfStates); } else if(tempN < 3) nos = "3";

} //show states and lines if(!showStateOptions){ for(int i = 0;i<numberOfStates;i++) { //state buttons and labels if(GUI.Button(new Rect(statePositions[i].xstateWidth/2.0F,statePositions[i].y-stateWidth/2.0F,stateWidth,stateWidth),"")) { //show state options for specific state selectedState = i; showStateOptions = true; } GUI.Label(new Rect(statePositions[i].x-10,statePositions[i].y10,20,30),"S"+i.ToString()); //lines should be here } } //state options if(showStateOptions) { //use different skin GUI.skin = otherSkin; GUI.Box(new Rect(boxRect.x-3,boxRect.y23,boxRect.width+6,boxRect.height+26),"State "+selectedState.ToString()+" Options"); GUILayout.BeginArea(boxRect); //state for input determines the next states given an input of zero or one GUILayout.BeginHorizontal(); GUILayout.Label("Next State with Input 0"); GUILayout.Label("Next State with Input 1"); GUILayout.EndHorizontal(); //input 1 GUILayout.BeginHorizontal(); for(int p = 0;p<numberOfStates;p++){ if(stateInformation[selectedState].x == p){ saveColor = GUI.backgroundColor; GUI.backgroundColor = Color.green; } if(GUILayout.Button("S"+p.ToString())) stateInformation[selectedState].x = p; if(stateInformation[selectedState].x == p) GUI.backgroundColor = saveColor; } //input 0 GUILayout.Label(""); for(int h = 0;h<numberOfStates;h++){ if(stateInformation[selectedState].y == h){ saveColor = GUI.backgroundColor;

GUI.backgroundColor = Color.red; } if(GUILayout.Button("S"+h.ToString())) stateInformation[selectedState].y = h; if(stateInformation[selectedState].y == h) GUI.backgroundColor = saveColor; } GUILayout.EndHorizontal(); //state variables - shows the boolean value of the states selected as next states GUILayout.BeginHorizontal(); GUILayout.Label(""); for(int k = bitsForStates-1;k>=0;k--){ GUILayout.BeginVertical(); GUILayout.Label("Q"+k.ToString()); GUILayout.Label(stateInBool[(int)stateInformation[selectedState].x,(k3)*-1]); GUILayout.EndVertical(); } GUILayout.Label(""); for(int l = bitsForStates-1;l>=0;l--){ GUILayout.BeginVertical(); GUILayout.Label("D"+l.ToString()); GUILayout.Label(stateInBool[(int)stateInformation[selectedState].y,(l3)*-1]); GUILayout.EndVertical(); } GUILayout.EndHorizontal(); //space GUILayout.Label(""); //outputs at state - what the outputs should be doing at the current state GUILayout.Label("Output Lights at state"); //light labels GUILayout.BeginHorizontal(); GUILayout.Label(" R1"); GUILayout.Label(" Y1"); GUILayout.Label(" G1"); GUILayout.Label(" R2"); GUILayout.Label(" Y2"); GUILayout.Label(" G2"); GUILayout.EndHorizontal(); //light states GUILayout.BeginHorizontal(); for(int j = 0;j<lightsAtState.GetLength(1);j++){ if(lightsAtState[selectedState,j]) tempStr = "1"; else tempStr = "0";

if(GUILayout.Button(tempStr)) lightsAtState[selectedState,j] = !lightsAtState[selectedState,j]; } GUILayout.EndHorizontal(); //done if(GUILayout.Button("Done")){ showStateOptions = false; //return to first skin GUI.skin = newSkin; } GUILayout.EndArea(); } } //determines the positins of objects in a circulat pattern given the number of objects, and square area the circle takes up public Vector2[] calculatePositions(int n,float x, float y) { Vector2[] positions = new Vector2[n]; //center Vector2 center = new Vector2(x/2,y/2); //find radius float r; if(x > y) r = center.y*0.8F; else r = center.x*0.8F; //angle between states float a = 360.0F/n; float ta = 0.0F; for(int i = 0;i < n;i++) { ta = a/2+a*i; ta *= Mathf.Deg2Rad; positions[i].x = center.x+r*Mathf.Cos(ta); positions[i].y = center.y+r*Mathf.Sin(ta); } return positions; } //determines the number of bits required to store an integer < 16 public static int calculateBits(int n){ int b = 0; if(n > 0){ b++; if(n > 2){

b++; if(n>4){ b++; if(n>8) b++; } } } return b; } }

You might also like