From ca8e728dc541856f157917b4615679b812932d65 Mon Sep 17 00:00:00 2001 From: Josh Shifman Date: Wed, 29 Mar 2017 23:35:26 -0400 Subject: [PATCH] add chat features --- MediVision_Unity/Assets/Scenes/HUD.unity | 72 +++++++++++++++-- .../Assets/Scripts/ChatBoxMain.cs | 77 +++++++++++++++---- MediVision_Unity/Assets/Scripts/HUD.cs | 6 +- .../Assets/Scripts/JSONResponses.cs | 1 + 4 files changed, 131 insertions(+), 25 deletions(-) diff --git a/MediVision_Unity/Assets/Scenes/HUD.unity b/MediVision_Unity/Assets/Scenes/HUD.unity index 8acc821..c78ff4a 100644 --- a/MediVision_Unity/Assets/Scenes/HUD.unity +++ b/MediVision_Unity/Assets/Scenes/HUD.unity @@ -287,6 +287,12 @@ MonoBehaviour: debug_capture_on: 0 disconnectionMessage: Stream Disconnected ___________________________: 0 +--- !u!114 &331957774 stripped +MonoBehaviour: + m_PrefabParentObject: {fileID: 114912508763807164, guid: 08bf54483fb250346b11c66b8be528c9, + type: 2} + m_PrefabInternal: {fileID: 471524086} + m_Script: {fileID: 708705254, guid: f70555f144d8491a825f0804e09c671c, type: 3} --- !u!1001 &348723320 Prefab: m_ObjectHideFlags: 0 @@ -346,6 +352,7 @@ GameObject: - component: {fileID: 361102408} - component: {fileID: 361102407} - component: {fileID: 361102406} + - component: {fileID: 361102405} m_Layer: 5 m_Name: Chat_panel (1) m_TagString: Untagged @@ -373,6 +380,26 @@ RectTransform: m_AnchoredPosition: {x: 5.89, y: -5.37} m_SizeDelta: {x: 255.80005, y: 0} m_Pivot: {x: 0.5, y: 0} +--- !u!114 &361102405 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 361102403} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b6e4a2c4eb860a74fae66269c1bcca00, type: 3} + m_Name: + m_EditorClassIdentifier: + serverURL: http://34.198.160.73/api/stream/query/ + stream_id: + IP: + urlSuffix: socket.io/?EIO=4&transport=websocket + checkConnectionDelay: 5 + msgPrefab: {fileID: 696921162} + currentMesage: {fileID: 331957774} + msgParentPanel: {fileID: 361102404} + titleText: {fileID: 1581492273} --- !u!114 &361102406 MonoBehaviour: m_ObjectHideFlags: 0 @@ -496,7 +523,7 @@ Prefab: - target: {fileID: 224118964369250636, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} propertyPath: m_AnchoredPosition.y - value: 0 + value: -28.85 objectReference: {fileID: 0} - target: {fileID: 224118964369250636, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} @@ -516,7 +543,7 @@ Prefab: - target: {fileID: 224118964369250636, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} propertyPath: m_AnchorMin.y - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 224118964369250636, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} @@ -526,7 +553,7 @@ Prefab: - target: {fileID: 224118964369250636, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} propertyPath: m_AnchorMax.y - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 224118964369250636, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} @@ -546,17 +573,17 @@ Prefab: - target: {fileID: 224946664677755714, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} propertyPath: m_AnchorMin.y - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 224946664677755714, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} propertyPath: m_AnchorMax.y - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 224946664677755714, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} propertyPath: m_AnchoredPosition.y - value: 0 + value: -9.85 objectReference: {fileID: 0} - target: {fileID: 224946664677755714, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} @@ -568,6 +595,16 @@ Prefab: propertyPath: m_Material value: objectReference: {fileID: 2100000, guid: 7187377b157675449ad892634f194ca6, type: 2} + - target: {fileID: 114912508763807164, guid: 08bf54483fb250346b11c66b8be528c9, + type: 2} + propertyPath: m_Text + value: + objectReference: {fileID: 0} + - target: {fileID: 114584777811179402, guid: 08bf54483fb250346b11c66b8be528c9, + type: 2} + propertyPath: m_Enabled + value: 0 + objectReference: {fileID: 0} m_RemovedComponents: [] m_ParentPrefab: {fileID: 100100000, guid: 08bf54483fb250346b11c66b8be528c9, type: 2} m_IsPrefabParent: 0 @@ -586,6 +623,7 @@ GameObject: - component: {fileID: 633141678} - component: {fileID: 633141677} - component: {fileID: 633141680} + - component: {fileID: 633141681} m_Layer: 0 m_Name: HUD m_TagString: Untagged @@ -729,6 +767,28 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: PivotAxis: 1 +--- !u!114 &633141681 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 633141672} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: dd4333e6f284c4e4eb1c7257fe866c40, type: 3} + m_Name: + m_EditorClassIdentifier: + IP_box: {fileID: 300801562} + IP_Fade_Delay: 3 + IP_Fade_Speed: 0.01 + debug_capture_on: 0 + disconnectionMessage: Stream Disconnected + ___________________________: 0 +--- !u!1 &696921162 stripped +GameObject: + m_PrefabParentObject: {fileID: 1513393013730664, guid: 08bf54483fb250346b11c66b8be528c9, + type: 2} + m_PrefabInternal: {fileID: 471524086} --- !u!1001 &976442468 Prefab: m_ObjectHideFlags: 0 diff --git a/MediVision_Unity/Assets/Scripts/ChatBoxMain.cs b/MediVision_Unity/Assets/Scripts/ChatBoxMain.cs index de03243..340dc7a 100644 --- a/MediVision_Unity/Assets/Scripts/ChatBoxMain.cs +++ b/MediVision_Unity/Assets/Scripts/ChatBoxMain.cs @@ -13,10 +13,12 @@ public class ChatBoxMain : MonoBehaviour public string urlSuffix = "socket.io/?EIO=4&transport=websocket"; public float checkConnectionDelay = 5; //seconds public GameObject msgPrefab; + public Text currentMesage; public Transform msgParentPanel; public Text titleText; float timeOfLastCheck = 0; + bool alreadyCalled = false; @@ -24,7 +26,9 @@ public class ChatBoxMain : MonoBehaviour void Start() { Debug.Log("I am alive!"); - StartCoroutine(GetText()); + + Debug.Log("CHATBOX MAIN HAS STARTED"); + } @@ -43,12 +47,23 @@ string GetIP() // Update is called once per frame void Update() - { + { + if (HUD.S.captureOn() && !alreadyCalled) { + Debug.Log("CALLED COROUTINE TO GET STREAM ID"); + StartCoroutine(GetText()); + alreadyCalled = true; + } } IEnumerator GetText() { - StreamID stream_info; + Debug.Log("IN GET TEXT"); + + + Debug.Log("WAIING FOR 5 SECONDS..."); + yield return new WaitForSeconds(5f); + + string url = getURL(); Debug.Log(url); @@ -61,24 +76,30 @@ IEnumerator GetText() } else { - stream_info = JsonUtility.FromJson(www.downloadHandler.text); + Debug.Log("PARSING JSON..."); + StreamID stream_info = JsonUtility.FromJson(www.downloadHandler.text); + + Debug.Log("RAW JSON STRING: "); + Debug.Log(www.downloadHandler.text); + Debug.Log("STREAM ID: "); - Debug.Log(stream_info.stream_id); + Debug.Log(stream_info.stream_id.ToString()); - StartCoroutine(getMessage(stream_info)); + StartCoroutine(getMessage(stream_info.stream_id)); } } - public IEnumerator getMessage(StreamID stream_info) { + public IEnumerator getMessage(string id) { while (true) { Debug.Log("IN GETMESSAGE()"); + yield return new WaitForSeconds(5f); - string url = "http://34.198.160.73/api/chat/" + stream_info.stream_id + "?viewer_only=false"; + string url = "http://34.198.160.73/api/chat/" + id + "?viewer_only=false"; Debug.Log(url); ChatResponse chat; @@ -93,25 +114,47 @@ public IEnumerator getMessage(StreamID stream_info) { else { chat = JsonUtility.FromJson(www.downloadHandler.text); - string message = chat.chat_messages[0].chat_content; - Debug.Log(message); - setMessage(message); + + if (chat.chat_messages.Length > 0) + { + string message = chat.chat_messages[chat.chat_messages.Length - 1].chat_content; + + Debug.Log("CHAT MESSAGE: "); + Debug.Log(message); + + + setMessage(message); + } + } } } public void setMessage(string msg) { - if (msg == "") return; - GameObject msgClone = Instantiate(msgPrefab); - msgClone.transform.SetParent(msgParentPanel); - msgClone.transform.SetSiblingIndex(msgParentPanel.transform.childCount - 2); - msgClone.GetComponent().showMessage(msg); + + Debug.Log("IN SET MESSAGE"); + + //if (msg == "") return; + //GameObject msgClone = Instantiate(msgPrefab); + //msgClone.transform.SetParent(msgParentPanel); + //msgClone.transform.SetSiblingIndex(msgParentPanel.transform.childCount - 2); + + //var currentPos = msgClone.transform.position; + //msgClone.transform.position = new Vector3(currentPos.x, currentPos.y, 1.75f); + + //var scale = new Vector3(1, 1, 1); + //msgClone.transform.localScale = scale; + + //msgClone.GetComponent().showMessage(msg); + + currentMesage.text = msg; + } public string getURL() { - return serverURL + "10.0.0.1"; //GetIP(); + return serverURL + GetIP(); } string requestStreamIdentifier() diff --git a/MediVision_Unity/Assets/Scripts/HUD.cs b/MediVision_Unity/Assets/Scripts/HUD.cs index e3efb93..eabcb11 100644 --- a/MediVision_Unity/Assets/Scripts/HUD.cs +++ b/MediVision_Unity/Assets/Scripts/HUD.cs @@ -6,6 +6,8 @@ public class HUD : MonoBehaviour { + static public HUD S; + //inspector variables public Text IP_box; public float IP_Fade_Delay = 3f; @@ -22,7 +24,7 @@ public class HUD : MonoBehaviour private void Awake() { - + S = this; } // Use this for initialization @@ -74,7 +76,7 @@ void ip_fade() } } - bool captureOn() + public bool captureOn() { if (debug_capture_on) { diff --git a/MediVision_Unity/Assets/Scripts/JSONResponses.cs b/MediVision_Unity/Assets/Scripts/JSONResponses.cs index 91958e1..3e316d1 100644 --- a/MediVision_Unity/Assets/Scripts/JSONResponses.cs +++ b/MediVision_Unity/Assets/Scripts/JSONResponses.cs @@ -15,6 +15,7 @@ void Update () { } } +[System.Serializable] public class StreamID { public string stream_id;