RemoteClickAction.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public interface IVideoCallSession
  9. {
  10. void OnAnswer();
  11. void OnDecline();
  12. void OnHangup();
  13. void OnDraw();
  14. void OnEndDraw(byte[] image);
  15. }
  16. public class RemoteClickAction : MonoBehaviour, IVideoCallSession
  17. {
  18. uint SessionCallerId;
  19. const int SCREENSHOT_SUPER_SIZE = 2;
  20. private enum STATE
  21. {
  22. RELEASED,
  23. SHOWING_REMOTE,
  24. DRAWING,
  25. CAPTURING_SCREENSHOT,
  26. SCREENSHOT_CAPTURING_COROUTINE
  27. };
  28. private STATE state_ = STATE.RELEASED;
  29. //bool pointerDown_ = false;
  30. private IVideoCallSession session_;
  31. private RectTransform remoteImageRt_;
  32. private GameObject callCanvas_;
  33. private Button answerButton_;
  34. private Button declineButton_;
  35. private Text callerName_;
  36. private GameObject onConnectCanvas_;
  37. private Button drawButton_;
  38. private Button hangupButton_;
  39. private GameObject background_;
  40. private GameObject remoteCanvas_;
  41. private GameObject drawCanvas_;
  42. private RawImage drawImage_;
  43. private RectTransform curDrawTextRt_;
  44. private GameObject drawTextPattern_;
  45. private GameObject drawToolsCanvas_;
  46. private Button sendImageButton_;
  47. private Button cancelDrawButton_;
  48. DrawControl drawControl_;
  49. private GameObject toolsCanvas;
  50. private GameObject loginCanvas;
  51. private AspectRatioFitter fit;
  52. public RawImage remoteCamImage;
  53. GameObject RemoteCameraView_;
  54. // Use this for initialization
  55. void Start ()
  56. {
  57. Client.instance.RemoteAssistCanvas = GameObject.Find("RemoteAssistCanvas");
  58. var lst = Client.instance.RemoteAssistCanvas.GetComponentsInChildren<RectTransform>(true);
  59. drawControl_ = FindObjectOfType<DrawControl>();
  60. foreach (var l in lst)
  61. {
  62. switch (l.name)
  63. {
  64. case "background": fit = l.gameObject.GetComponent<AspectRatioFitter>();
  65. background_ = l.gameObject;
  66. break;
  67. case "RemoteCameraView": remoteCamImage = l.gameObject.GetComponent<RawImage>();
  68. remoteImageRt_ = l.gameObject.GetComponent<RectTransform>();
  69. RemoteCameraView_ = l.gameObject;
  70. break;
  71. case "ToolsCanvas": toolsCanvas = l.gameObject; break;
  72. case "RemoteCanvas": remoteCanvas_ = l.gameObject; break;
  73. case "LoginCanvas": loginCanvas = l.gameObject; break;
  74. case "CallCanvas": callCanvas_ = l.gameObject; break;
  75. case "AnswerButton": answerButton_ = l.gameObject.GetComponent<Button>(); break;
  76. case "DeclineButton": declineButton_ = l.gameObject.GetComponent<Button>(); break;
  77. case "CallerName": callerName_ = l.gameObject.GetComponent<Text>(); break;
  78. case "OnConnectCanvas": onConnectCanvas_ = l.gameObject; break;
  79. case "DrawButton": drawButton_ = l.gameObject.GetComponent<Button>(); break;
  80. case "HangupButton": hangupButton_ = l.gameObject.GetComponent<Button>(); break;
  81. case "DrawCanvas": drawCanvas_ = l.gameObject; break;
  82. case "DrawImage": drawImage_ = l.gameObject.GetComponent<RawImage>(); break;
  83. case "DrawTextPattern": drawTextPattern_ = l.gameObject; break;
  84. case "DrawControlCanvas": drawToolsCanvas_ = l.gameObject; break;
  85. case "SendImageButton": sendImageButton_ = l.gameObject.GetComponent<Button>(); break;
  86. case "CancelDrawButton": cancelDrawButton_ = l.gameObject.GetComponent<Button>(); break;
  87. case "ArrowToggle":
  88. drawControl_.arrowToggle_ = l.gameObject.GetComponent<Toggle>(); break;
  89. case "TextToggle":
  90. drawControl_.textToggle_ = l.gameObject.GetComponent<Toggle>(); break;
  91. case "BrushToggle":
  92. drawControl_.brushToggle_ = l.gameObject.GetComponent<Toggle>(); break;
  93. case "TargetToggle":
  94. drawControl_.targetToggle_ = l.gameObject.GetComponent<Toggle>(); break;
  95. }
  96. // Debug.Log("asdsa " + l.name);
  97. }
  98. drawControl_.Init();
  99. answerButton_.onClick.AddListener(this.OnAnswer);
  100. declineButton_.onClick.AddListener(this.OnDecline);
  101. callCanvas_.SetActive(false);
  102. drawButton_.onClick.AddListener(OnDraw);
  103. hangupButton_.onClick.AddListener(this.OnHangup);
  104. onConnectCanvas_.SetActive(false);
  105. drawCanvas_.SetActive(false); //TODO
  106. sendImageButton_.onClick.AddListener(this.OnSendImage);
  107. cancelDrawButton_.onClick.AddListener(this.OnCancelDraw);
  108. drawToolsCanvas_.SetActive(false); //TODO
  109. RemoteTextureTestFill();
  110. Client.instance.RemoteAssistCanvas.SetActive(false);
  111. }
  112. public uint GetCallerID()
  113. {
  114. return SessionCallerId;
  115. }
  116. private void RemoteTextureTestFill()
  117. {
  118. //RawImage image = GameObject.Find("RemoteCameraView").GetComponent<RawImage>();
  119. /*int WIDTH = (int)remoteCanvasRt_.sizeDelta.x;
  120. int HEIGHT = (int)remoteCanvasRt_.sizeDelta.y;
  121. Texture2D texture = new Texture2D(WIDTH, HEIGHT);
  122. for (int i = 0; i < HEIGHT; ++i)
  123. {
  124. texture.SetPixel(HEIGHT - i, i, Color.blue);
  125. }
  126. texture.Apply();
  127. image.texture = texture;*/
  128. remoteCamImage.texture = Texture2D.whiteTexture;
  129. //image.texture = Texture2D.whiteTexture;
  130. //image.texture = Texture2D.blackTexture;
  131. }
  132. private void Update()
  133. {
  134. switch (state_)
  135. {
  136. case STATE.CAPTURING_SCREENSHOT:
  137. CheckScreenshot();
  138. break;
  139. }
  140. }
  141. public void StartSession(uint callerID)
  142. {
  143. session_ = this;
  144. SessionCallerId = callerID;
  145. remoteCanvas_.SetActive(true);
  146. RemoteCameraView_.SetActive(true);
  147. background_.SetActive(true);
  148. drawCanvas_.SetActive(true);
  149. callCanvas_.SetActive(true);
  150. callerName_.text = "call from " + callerID + "...";
  151. }
  152. public void OnEndDraw(byte[] image)
  153. {
  154. //if (image != null)
  155. //{
  156. // videoCalled_.SendImage(image);
  157. //}
  158. //else
  159. //{
  160. // videoCalled_.Resume();
  161. //}
  162. }
  163. public void StopSession()
  164. {
  165. SessionCallerId = 0;
  166. callCanvas_.SetActive(false);
  167. onConnectCanvas_.SetActive(false);
  168. drawToolsCanvas_.SetActive(false);
  169. drawCanvas_.SetActive(false);
  170. RemoteTextureTestFill();
  171. session_ = null;
  172. }
  173. public void OnAnswer()
  174. {
  175. if (session_ != null)
  176. {
  177. Debug.Log("OnAnswer");
  178. callCanvas_.SetActive(false);
  179. onConnectCanvas_.SetActive(true);
  180. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.videoCallRequest);
  181. }
  182. }
  183. public void OnDraw()
  184. {
  185. drawControl_.Clear();
  186. onConnectCanvas_.SetActive(false);
  187. drawToolsCanvas_.SetActive(true);
  188. drawCanvas_.SetActive(true);
  189. state_ = STATE.DRAWING;
  190. Debug.Log("OnDraw");
  191. }
  192. public void OnHangup()
  193. {
  194. onConnectCanvas_.SetActive(false);
  195. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
  196. if (session_ != null)
  197. {
  198. StopSession();
  199. }
  200. Client.instance.RemoteAssistCanvas.SetActive(false);
  201. }
  202. public void OnDecline()
  203. {
  204. Debug.Log("OnDecline " + SessionCallerId);
  205. callCanvas_.SetActive(false);
  206. Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
  207. if (session_ != null)
  208. {
  209. StopSession();
  210. }
  211. Client.instance.RemoteAssistCanvas.SetActive(false);
  212. }
  213. private void OnSendImage()
  214. {
  215. drawToolsCanvas_.SetActive(false);
  216. //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
  217. state_ = STATE.CAPTURING_SCREENSHOT;
  218. }
  219. public void OnCancelDraw()
  220. {
  221. drawToolsCanvas_.SetActive(false);
  222. //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
  223. state_ = STATE.CAPTURING_SCREENSHOT;
  224. }
  225. private void CheckScreenshot()
  226. {
  227. if (state_ == STATE.CAPTURING_SCREENSHOT)
  228. {
  229. StartCoroutine(RecordFrame());
  230. state_ = STATE.SCREENSHOT_CAPTURING_COROUTINE;
  231. }
  232. }
  233. IEnumerator RecordFrame()
  234. {
  235. yield return new WaitForEndOfFrame();
  236. Texture2D tex = null;
  237. try
  238. {
  239. tex = ScreenCapture.CaptureScreenshotAsTexture(SCREENSHOT_SUPER_SIZE);
  240. }
  241. catch (Exception ex)
  242. {
  243. Debug.Log(ex.Message);
  244. }
  245. drawCanvas_.SetActive(false);
  246. onConnectCanvas_.SetActive(true);
  247. int width = (int)remoteImageRt_.rect.width * SCREENSHOT_SUPER_SIZE;
  248. int height = (int)remoteImageRt_.rect.height * SCREENSHOT_SUPER_SIZE;
  249. var srceenPos = remoteImageRt_.TransformPoint(new Vector2(0, 0));
  250. int x = (int)srceenPos.x * SCREENSHOT_SUPER_SIZE;
  251. int y = (int)srceenPos.y * SCREENSHOT_SUPER_SIZE;
  252. width = x + width > tex.width ? tex.width - x : width;
  253. height = y + height > tex.height ? tex.height - y : height;
  254. Color[] col = tex.GetPixels(x, y, width, height);
  255. Texture2D m2Texture = new Texture2D(width, height);
  256. m2Texture.SetPixels(col);
  257. m2Texture.Apply();
  258. byte[] compress = m2Texture.EncodeToJPG();
  259. File.WriteAllBytes("test.jpg", compress);
  260. if (session_ != null)
  261. {
  262. session_.OnEndDraw(compress);
  263. }
  264. state_ = STATE.SHOWING_REMOTE;
  265. }
  266. }