123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- public interface IVideoCallSession
- {
- void OnAnswer();
- void OnDecline();
- void OnHangup();
- void OnDraw();
- void OnEndDraw(byte[] image);
- }
- public class RemoteClickAction : MonoBehaviour, IVideoCallSession
- {
- public uint SessionCallerId;
- const int SCREENSHOT_SUPER_SIZE = 1;
- private enum STATE
- {
- RELEASED,
- SHOWING_REMOTE,
- DRAWING,
- CAPTURING_SCREENSHOT,
- SCREENSHOT_CAPTURING_COROUTINE
- };
- private STATE state_ = STATE.RELEASED;
- //bool pointerDown_ = false;
- private IVideoCallSession session_;
- private RectTransform remoteImageRt_;
- private GameObject callCanvas_;
- private Button answerButton_;
- private Button declineButton_;
- public Text callerName_;
- private GameObject onConnectCanvas_;
- private Button drawButton_;
- private Button hangupButton_;
- private GameObject background_;
- private GameObject remoteCanvas_;
- private GameObject drawCanvas_;
- private RawImage drawImage_;
- private RectTransform curDrawTextRt_;
- private GameObject drawTextPattern_;
- private GameObject drawToolsCanvas_;
- private Button sendImageButton_;
- private Button cancelDrawButton_;
- private Button undoButton_;
- private Button redoButton_;
- DrawControl drawControl_;
- private GameObject toolsCanvas;
- private GameObject loginCanvas;
- private AspectRatioFitter fit;
- public RawImage remoteCamImage;
- GameObject RemoteCameraView_;
- // Use this for initialization
- void Start ()
- {
- Client.instance.RemoteAssistCanvas = GameObject.Find("RemoteAssistCanvas");
- var lst = Client.instance.RemoteAssistCanvas.GetComponentsInChildren<RectTransform>(true);
- drawControl_ = FindObjectOfType<DrawControl>();
- foreach (var l in lst)
- {
- switch (l.name)
- {
- case "background": fit = l.gameObject.GetComponent<AspectRatioFitter>();
- background_ = l.gameObject;
- break;
- case "RemoteCameraView": remoteCamImage = l.gameObject.GetComponent<RawImage>();
- remoteImageRt_ = l.gameObject.GetComponent<RectTransform>();
- RemoteCameraView_ = l.gameObject;
- break;
- case "ToolsCanvas": toolsCanvas = l.gameObject; break;
- case "RemoteCanvas": remoteCanvas_ = l.gameObject; break;
- case "LoginCanvas": loginCanvas = l.gameObject; break;
- case "CallCanvas": callCanvas_ = l.gameObject; break;
- case "AnswerButton": answerButton_ = l.gameObject.GetComponent<Button>(); break;
- case "DeclineButton": declineButton_ = l.gameObject.GetComponent<Button>(); break;
- case "CallerName": callerName_ = l.gameObject.GetComponent<Text>(); break;
- case "OnConnectCanvas": onConnectCanvas_ = l.gameObject; break;
- case "DrawButton": drawButton_ = l.gameObject.GetComponent<Button>(); break;
- case "HangupButton": hangupButton_ = l.gameObject.GetComponent<Button>(); break;
- case "UndoButton": undoButton_ = l.gameObject.GetComponent<Button>(); break;
-
- case "DrawCanvas": drawCanvas_ = l.gameObject;
- DrawControl.drawScript_ = drawCanvas_.GetComponent<DrawScript>();
- break;
- case "DrawImage": drawImage_ = l.gameObject.GetComponent<RawImage>(); break;
- case "DrawTextPattern": drawTextPattern_ = l.gameObject; break;
- case "DrawControlCanvas": drawToolsCanvas_ = l.gameObject; break;
- case "SendImageButton": sendImageButton_ = l.gameObject.GetComponent<Button>(); break;
- case "CancelDrawButton": cancelDrawButton_ = l.gameObject.GetComponent<Button>(); break;
- case "ArrowToggle":
- drawControl_.arrowToggle_ = l.gameObject.GetComponent<Toggle>(); break;
- case "TextToggle":
- drawControl_.textToggle_ = l.gameObject.GetComponent<Toggle>(); break;
- case "BrushToggle":
- drawControl_.brushToggle_ = l.gameObject.GetComponent<Toggle>(); break;
- case "TargetToggle":
- drawControl_.targetToggle_ = l.gameObject.GetComponent<Toggle>(); break;
- }
- // Debug.Log("asdsa " + l.name);
- }
- drawControl_.Init();
- undoButton_.onClick.AddListener(DrawControl.drawScript_.Undo);
- answerButton_.onClick.AddListener(this.OnAnswer);
- declineButton_.onClick.AddListener(this.OnDecline);
- drawButton_.onClick.AddListener(OnDraw);
- hangupButton_.onClick.AddListener(this.OnHangup);
- callCanvas_.SetActive(false);
- onConnectCanvas_.SetActive(false);
- drawCanvas_.SetActive(false); //TODO
-
- sendImageButton_.onClick.AddListener(this.OnSendImage);
- cancelDrawButton_.onClick.AddListener(this.OnCancelDraw);
- drawToolsCanvas_.SetActive(false); //TODO
- RemoteTextureTestFill();
- Client.instance.RemoteAssistCanvas.SetActive(false);
- }
- public uint GetCallerID()
- {
- return SessionCallerId;
- }
- private void RemoteTextureTestFill()
- {
- //RawImage image = GameObject.Find("RemoteCameraView").GetComponent<RawImage>();
- /*int WIDTH = (int)remoteCanvasRt_.sizeDelta.x;
- int HEIGHT = (int)remoteCanvasRt_.sizeDelta.y;
- Texture2D texture = new Texture2D(WIDTH, HEIGHT);
- for (int i = 0; i < HEIGHT; ++i)
- {
- texture.SetPixel(HEIGHT - i, i, Color.blue);
- }
- texture.Apply();
- image.texture = texture;*/
- remoteCamImage.texture = Texture2D.whiteTexture;
- //image.texture = Texture2D.whiteTexture;
- //image.texture = Texture2D.blackTexture;
- }
- private void Update()
- {
- switch (state_)
- {
- case STATE.CAPTURING_SCREENSHOT:
- CheckScreenshot();
- break;
- }
- }
- public void StartSession(uint callerID)
- {
- session_ = this;
- SessionCallerId = callerID;
- remoteCanvas_.SetActive(true);
- RemoteCameraView_.SetActive(true);
- background_.SetActive(true);
- drawCanvas_.SetActive(true);
- callCanvas_.SetActive(true);
- var user = User.Find(callerID);
- if (user != null)
- callerName_.text = "Звонок от [" + callerID + "] "+ user.name_user;
- else
- callerName_.text = "Звонок от сотрудника [ID: " + callerID + "]";
- }
- public void OnEndDraw(byte[] image)
- {
- Client.instance.ImageSend(image, SessionCallerId);
-
- //if (image != null)
- //{
- // videoCalled_.SendImage(image);
- //}
- //else
- //{
- // videoCalled_.Resume();
- //}
- }
- public void StopSession()
- {
- SessionCallerId = 0;
- callCanvas_.SetActive(false);
- onConnectCanvas_.SetActive(false);
- drawToolsCanvas_.SetActive(false);
- drawCanvas_.SetActive(false);
- RemoteTextureTestFill();
- session_ = null;
- }
- public void OnAnswer()
- {
- if (session_ != null)
- {
- callCanvas_.SetActive(false);
- onConnectCanvas_.SetActive(true);
- Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.videoCallRequest);
- }
- }
- public void OnDraw()
- {
- drawControl_.Clear();
- onConnectCanvas_.SetActive(false);
- toolsCanvas.SetActive(true);
- drawToolsCanvas_.SetActive(true);
- drawCanvas_.SetActive(true);
- state_ = STATE.DRAWING;
- Client.ImageStreamControlCommandSend(SessionCallerId, ImageCMD.pause);
- }
- public void OnHangup()
- {
- onConnectCanvas_.SetActive(false);
- Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
- if (session_ != null)
- {
- StopSession();
- }
- Client.instance.RemoteAssistCanvas.SetActive(false);
- }
- public void OnDecline()
- {
- Debug.Log("OnDecline " + SessionCallerId);
- callCanvas_.SetActive(false);
- Client.instance.RemoteAssistRequestVideoCall(SessionCallerId, RemoteAssistCMD.callStop);
- if (session_ != null)
- {
- StopSession();
- }
- Client.instance.RemoteAssistCanvas.SetActive(false);
- }
- private void OnSendImage()
- {
- drawToolsCanvas_.SetActive(false);
- //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
- state_ = STATE.CAPTURING_SCREENSHOT;
- }
- public void OnCancelDraw()
- {
- drawToolsCanvas_.SetActive(false);
- //ScreenCapture.CaptureScreenshot(SCREENSHOT_FILE_NAME, SCREENSHOT_SUPER_SIZE);
- state_ = STATE.CAPTURING_SCREENSHOT;
- }
- private void CheckScreenshot()
- {
- if (state_ == STATE.CAPTURING_SCREENSHOT)
- {
- StartCoroutine(RecordFrame());
- state_ = STATE.SCREENSHOT_CAPTURING_COROUTINE;
- }
- }
- IEnumerator RecordFrame()
- {
- yield return new WaitForEndOfFrame();
- Texture2D tex = null;
- try
- {
- tex = ScreenCapture.CaptureScreenshotAsTexture(SCREENSHOT_SUPER_SIZE);
- }
- catch (Exception ex)
- {
- Debug.Log(ex.Message);
- }
- drawCanvas_.SetActive(false);
- onConnectCanvas_.SetActive(true);
- int width = (int)remoteImageRt_.rect.width * SCREENSHOT_SUPER_SIZE;
- int height = (int)remoteImageRt_.rect.height * SCREENSHOT_SUPER_SIZE;
-
- var srceenPos = remoteImageRt_.TransformPoint(new Vector2(0, 0));
- int x = (int)srceenPos.x * SCREENSHOT_SUPER_SIZE;
- int y = (int)srceenPos.y * SCREENSHOT_SUPER_SIZE;
- Debug.Log($"RecordFrame w {width} h {height} x {x} y {y}");
- width = x + width > tex.width ? tex.width - x : width;
- height = y + height > tex.height ? tex.height - y : height;
- Debug.Log($"RecordFrame2 w {width} h {height} x {x} y {y}");
- Color[] col = tex.GetPixels(x, y, width, height);
- Texture2D m2Texture = new Texture2D(width, height);
- m2Texture.SetPixels(col);
- m2Texture.Apply();
- byte[] compress = m2Texture.EncodeToJPG(90);
- //File.WriteAllBytes("test.jpg", compress);
- if (session_ != null)
- {
- session_.OnEndDraw(compress);
- }
- state_ = STATE.SHOWING_REMOTE;
- }
- }
|