Skip to content

Commit faad6f0

Browse files
committed
Commented Code
1 parent ab9af0d commit faad6f0

5 files changed

Lines changed: 55 additions & 28 deletions

File tree

StateControl/Editor/Buttons.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55

66
public class Buttons
77
{
8+
// return delete button
89
public static bool Delete()
910
{
1011
return GUILayout.Button(EditorGUIUtility.IconContent("d_TreeEditor.Trash"), GUILayout.Width(30));
1112
}
1213

14+
//return add button
1315
public static bool Add()
1416
{
1517
return GUILayout.Button("+");
1618
}
1719

20+
// return pack and send button
1821
public static bool PackAndSend()
1922
{
2023
return GUILayout.Button("Pack and Send");

StateControl/Editor/MenuEditor.cs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,20 @@ public class MenuEditor : MonoBehaviour
1111

1212
private const string MENU_ITEM_STATE_EDITOR = GROUP_NAME + "/Open State Editor";
1313
private const string MENU_ITEM_NEW_STATE_LIST = GROUP_NAME + "/Add States";
14-
private const string MENU_ITEM_BUILD_AND_SEND = GROUP_NAME + "/Pack and send";
15-
16-
private void Awake()
17-
{
18-
SerializedProperty tagsProp = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]).FindProperty("tags");
19-
20-
bool found = false;
21-
for (int i = 0; i < tagsProp.arraySize; i++)
22-
{
23-
SerializedProperty t = tagsProp.GetArrayElementAtIndex(i);
24-
if (t.stringValue.Equals("States")) { found = true; break; }
25-
}
26-
27-
if (!found)
28-
{
29-
tagsProp.InsertArrayElementAtIndex(0);
30-
SerializedProperty n = tagsProp.GetArrayElementAtIndex(0);
31-
n.stringValue = "States";
32-
}
33-
}
14+
private const string MENU_ITEM_BUILD_AND_SEND = GROUP_NAME + "/Pack and send";
3415

16+
//Open StateEditor windows
3517
[MenuItem(MENU_ITEM_STATE_EDITOR, false, 0)]
3618
static void OpenStateUI()
3719
{
3820
EditorWindow.GetWindow<StateEditorWindow>("State Editor");
3921
}
4022

23+
// Adding State object to sceene
4124
[MenuItem(MENU_ITEM_NEW_STATE_LIST, false, 20)]
4225
static void AddStateObj()
4326
{
27+
// Getting tags settings
4428
SerializedObject tagsManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
4529
SerializedProperty tagsProp = tagsManager.FindProperty("tags");
4630

@@ -51,6 +35,7 @@ static void AddStateObj()
5135
if (t.stringValue.Equals("States")) { found = true; break; }
5236
}
5337

38+
// Add "States" tag if it isn't there
5439
if (!found)
5540
{
5641
tagsProp.InsertArrayElementAtIndex(0);
@@ -59,20 +44,24 @@ static void AddStateObj()
5944
tagsManager.ApplyModifiedProperties();
6045
}
6146

47+
// Check if there is State object on the sceene already
6248
var temp = GameObject.FindGameObjectWithTag("States");
6349

6450
if (temp == null)
6551
{
52+
// Addinig State object to sceene
6653
GameObject stateObj = new GameObject();
6754
stateObj.name = "States";
6855
stateObj.tag = "States";
6956
stateObj.AddComponent<WebConnection>();
7057
} else
7158
{
59+
// throw warning
7260
EditorUtility.DisplayDialog("Warning", "State List is already on scene.\nOpen State Editor.", "Ok");
7361
}
7462
}
7563

64+
// Open Pack and send window
7665
[MenuItem(MENU_ITEM_BUILD_AND_SEND, false, 40)]
7766
static void Build_And_Send()
7867
{

StateControl/Editor/PackAndSendWindow.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class PackAndSendWindow : EditorWindow
2323

2424
private void OnFocus()
2525
{
26+
// Get info of States status on server when window got focus
2627
var states = GameObject.FindGameObjectWithTag("States");
2728
if (states != null)
2829
{
@@ -32,6 +33,7 @@ private void OnFocus()
3233
}
3334
}
3435

36+
// Get iinfo from server about State status
3537
private async void reloadServerInfo()
3638
{
3739
HttpClient client = new HttpClient();
@@ -54,6 +56,7 @@ private async void reloadServerInfo()
5456

5557
private void OnGUI()
5658
{
59+
// Find Status object
5760
if (_webConnection != null)
5861
{
5962
DrawPackAndSend();
@@ -71,16 +74,19 @@ private void DrawStateListError()
7174

7275
private async void DrawPackAndSend()
7376
{
77+
// Scroll zone if content would be higher than window hight
7478
_statesScroll = EditorGUILayout.BeginScrollView(_statesScroll, GUILayout.ExpandHeight(false));
7579

7680
GUILayout.Label("Version on the server:", EditorStyles.boldLabel);
7781
EditorGUI.indentLevel++;
7882

83+
// Field of presentation States for this App on Server
7984
EditorGUILayout.BeginHorizontal();
8085
GUILayout.Space(EditorGUI.indentLevel * 30);
8186
GUILayout.Label("Already loaded on the server: " + (serverStatus ? "Yes" : "No"), GUILayout.ExpandWidth(false));
8287
EditorGUILayout.EndHorizontal();
8388

89+
// Show States on server if it has them for this app
8490
if (serverStatus) {
8591
EditorGUILayout.BeginHorizontal();
8692
GUILayout.Space(EditorGUI.indentLevel * 15);
@@ -104,19 +110,23 @@ private async void DrawPackAndSend()
104110

105111
GUILayout.Space(40);
106112

113+
// Block with current local information fron State object
107114
GUILayout.Label("Pack and Send to the server:", EditorStyles.boldLabel);
108115
EditorGUI.indentLevel = 1;
109116

117+
// App name field
110118
EditorGUILayout.BeginHorizontal();
111119
GUILayout.Space(EditorGUI.indentLevel * 30);
112120
GUILayout.Label("Application name: " + _webConnection.GameName);
113121
EditorGUILayout.EndHorizontal();
114122

123+
// Server URL field
115124
EditorGUILayout.BeginHorizontal();
116125
GUILayout.Space(EditorGUI.indentLevel * 30);
117126
GUILayout.Label("Send to: http://" + _webConnection.BaseURL + "/");
118127
EditorGUILayout.EndHorizontal();
119128

129+
// Local States field
120130
EditorGUILayout.BeginHorizontal();
121131
GUILayout.Space(EditorGUI.indentLevel * 15);
122132
statesOpend = EditorGUILayout.Foldout(statesOpend, "States:", true);
@@ -139,9 +149,13 @@ private async void DrawPackAndSend()
139149
EditorGUILayout.EndScrollView();
140150
GUILayout.Space(20);
141151

152+
// Check that local and server data are different
142153
if (!CheckUniq()) {
154+
155+
//if yes, draw Pack and send button
143156
if (Buttons.PackAndSend())
144157
{
158+
// Pack States and other info to JSON
145159
List<string> states = new List<string>();
146160
for (int i = 0; i < _webConnection.GetLenght(); i++)
147161
{
@@ -154,6 +168,7 @@ private async void DrawPackAndSend()
154168
statesList = states
155169
});
156170

171+
// And send to server
157172
HttpClient client = new HttpClient();
158173

159174
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://" + _webConnection.BaseURL + "/api/add_states/add");
@@ -166,6 +181,7 @@ private async void DrawPackAndSend()
166181
}
167182
} else
168183
{
184+
// if not, draw "All up to date!" phrase
169185
GUIStyle style = new GUIStyle();
170186
style.normal.textColor = Color.green;
171187
style.fontSize = 17;
@@ -175,6 +191,7 @@ private async void DrawPackAndSend()
175191
}
176192
}
177193

194+
// check that States on server are differ from States on plugin
178195
private bool CheckUniq()
179196
{
180197
List<string> states = new List<string>();

StateControl/Editor/StateEditorWindow.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ public class StateEditorWindow : EditorWindow
1717

1818
private void OnGUI()
1919
{
20+
// Find State object
2021
var states = GameObject.FindGameObjectWithTag("States");
2122

2223

2324
if (states != null)
2425
{
26+
// Open editor window if found
2527
_webConnection = states.GetComponent<WebConnection>();
2628
DrawStateEditor();
2729

@@ -31,6 +33,7 @@ private void OnGUI()
3133
}
3234
} else
3335
{
36+
// open warning window if not
3437
DrawStateListError();
3538
}
3639
}
@@ -42,34 +45,40 @@ private void DrawStateListError()
4245

4346
private void DrawStateEditor()
4447
{
45-
//if (EditorApplication.isPlaying) return;
48+
if (EditorApplication.isPlaying) return;
4649

47-
// Draw App Name
50+
// Field for App Name
4851
GUILayout.Label("Application name:", EditorStyles.boldLabel);
4952
_webConnection.GameName = EditorGUILayout.TextField(_webConnection.GameName);
5053

5154
EditorGUILayout.Space();
5255

56+
// Field for server URL
5357
GUILayout.Label("Server URL (without protocol):");
5458
_webConnection.BaseURL = EditorGUILayout.TextField(_webConnection.BaseURL);
5559

5660
EditorGUILayout.Space();
5761

62+
// States list
5863
statesOpend = EditorGUILayout.Foldout(statesOpend, "States:", true);
5964
EditorGUI.indentLevel++;
6065
if (statesOpend)
6166
{
67+
// Scroll part if it gets higher, than window hight
6268
_statesScroll = EditorGUILayout.BeginScrollView(_statesScroll);
6369

6470
for (int i = 0; i < _webConnection.GetLenght(); i++)
6571
{
6672
EditorGUILayout.BeginHorizontal();
6773
GUILayout.Space(EditorGUI.indentLevel * 30);
6874

75+
// State number
6976
GUILayout.Label((i + 1).ToString() + ".", GUILayout.Width(15));
7077

78+
// State name with renaming logic
7179
string key = _webConnection.GetKey(i);
7280
var _newKey = EditorGUILayout.TextField(key);
81+
// Check that object doesn't have State with this name
7382
if (_newKey != _webConnection.GetKey(i))
7483
{
7584
if (!_webConnection.ContainsKey(_newKey)) {
@@ -81,6 +90,7 @@ private void DrawStateEditor()
8190
}
8291
}
8392

93+
// delete button
8494
if (Buttons.Delete())
8595
{
8696
_webConnection.Remove(key);
@@ -91,8 +101,11 @@ private void DrawStateEditor()
91101

92102
EditorGUILayout.BeginHorizontal();
93103
GUILayout.Space(EditorGUI.indentLevel * 30);
104+
105+
// Add button
94106
if (Buttons.Add())
95107
{
108+
// Check that object doesn't have State with empty name
96109
if (!_webConnection.ContainsKey("")) {
97110
_webConnection.Add("");
98111
_errorMessage = "";
@@ -105,6 +118,7 @@ private void DrawStateEditor()
105118

106119
if (!string.IsNullOrEmpty(_errorMessage))
107120
{
121+
// throw warning
108122
EditorGUILayout.HelpBox(_errorMessage, MessageType.Error);
109123
}
110124

StateControl/Runtime/WebConnection.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class WebConnection : MonoBehaviour
1919

2020
private void Awake()
2121
{
22+
// create empty delegates to list of States names
2223
Values.Clear();
2324
foreach (var key in Keys)
2425
{
@@ -29,11 +30,13 @@ private void Awake()
2930
// Start is called before the first frame update
3031
void Start()
3132
{
33+
// Connect to server via WebSocket
3234
if (!string.IsNullOrWhiteSpace(BaseURL)) {
3335
ws = new WebSocket("ws://" + BaseURL + "/GameControl");
3436
ws.OnMessage += Ws_OnMessage;
3537
ws.Connect();
3638

39+
// and send started app name
3740
ws.Send(JsonConvert.SerializeObject(new
3841
{
3942
Game = GameName
@@ -43,27 +46,33 @@ void Start()
4346

4447
private void OnDestroy()
4548
{
49+
// Close connection when game stops
4650
ws.Close();
4751
}
4852

53+
// Get and check messages from WebSocket
4954
private void Ws_OnMessage(object sender, MessageEventArgs e)
5055
{
5156
var json = JObject.Parse(e.Data.ToString());
57+
// Check that message has at least one of needed types
5258
if (json.ContainsKey("DeviceSession") && json.ContainsKey("GameName") && json.ContainsKey("State"))
5359
{
60+
// if it is message with state, start invoke this delegate
5461
if (DeviceSession == json["DeviceSession"].ToString() && GameName == json["GameName"].ToString())
5562
{
5663
InvokeDelegate(json["State"].ToString());
5764
}
5865
} else if (json.ContainsKey("DeviceSession"))
5966
{
67+
// if it is deviceSession info, save it
6068
DeviceSession = json["DeviceSession"].ToString();
6169
}
6270
}
6371

6472
// Update is called once per frame
6573
void Update()
6674
{
75+
// invoke delegates in their queue in main thread
6776
lock (_executionQueue)
6877
{
6978
while (_executionQueue.Count > 0)
@@ -73,15 +82,10 @@ void Update()
7382
}
7483
}
7584

76-
private void OnOpenHandler(object sender, EventArgs e)
77-
{
78-
Debug.Log("WebSocket connected!");
79-
}
80-
81-
8285

8386

8487

88+
// Custom Map/Dictionary structure for saving States name and their delegates
8589
[SerializeField]
8690
private List<string> Keys = new List<string>();
8791
private List<StateDelegate> Values = new List<StateDelegate>();

0 commit comments

Comments
 (0)