227 lines
6.0 KiB
C#
227 lines
6.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using HK.Keyboard;
|
|
// using TalesFromTheRift;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class DrawOnTexture : MonoBehaviour
|
|
{
|
|
public GameObject[] boardObject;
|
|
|
|
public void OpenDraw()
|
|
{
|
|
foreach (GameObject obj in boardObject)
|
|
obj.SetActive(true);
|
|
}
|
|
|
|
public void CloseDraw()
|
|
{
|
|
foreach (GameObject obj in boardObject)
|
|
obj.SetActive(false);
|
|
}
|
|
|
|
|
|
public Keyboard keyboard;
|
|
private LineRenderer lineRender;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
//lineRender = GetComponent<LineRenderer>();
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
canvas = GetComponentInParent<Canvas>();
|
|
}
|
|
|
|
List<Vector3> currentPos = new List<Vector3>();
|
|
List<LineRenderer> strokes = new List<LineRenderer>();
|
|
|
|
List<Vector3> fingerPos = new List<Vector3>();
|
|
|
|
//public TextMeshProUGUI text;
|
|
|
|
private void Update()
|
|
{
|
|
var go = GetUI(canvas.gameObject);
|
|
if (go == null || go.name != "ShowStrokes")
|
|
{
|
|
isCanStartDrawing = false;
|
|
return;
|
|
}
|
|
|
|
isCanStartDrawing = true;
|
|
|
|
if (IsDrawing)
|
|
{
|
|
Vector3 currentFingerPos = Input.mousePosition;
|
|
if (Input.touchCount > 0)
|
|
{
|
|
Touch touch = Input.GetTouch(0);
|
|
//text.text = touch.position.ToString();
|
|
currentFingerPos = touch.position;
|
|
}
|
|
|
|
currentFingerPos.z = 10;
|
|
|
|
if (fingerPos.Count > 0)
|
|
{
|
|
Vector3 previousPos = fingerPos[fingerPos.Count - 1];
|
|
if (Vector3.Distance(currentFingerPos, previousPos) > 5)
|
|
{
|
|
Vector3 worldPos = Camera.main.ScreenToWorldPoint(currentFingerPos);
|
|
worldPos.z = 0;
|
|
currentPos.Add(worldPos);
|
|
fingerPos.Add(currentFingerPos);
|
|
lineRender.positionCount = currentPos.Count;
|
|
lineRender.SetPositions(currentPos.ToArray());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Vector3 worldPos = Camera.main.ScreenToWorldPoint(currentFingerPos);
|
|
worldPos.z = 0;
|
|
currentPos.Add(worldPos);
|
|
fingerPos.Add(currentFingerPos);
|
|
lineRender.positionCount = currentPos.Count;
|
|
lineRender.SetPositions(currentPos.ToArray());
|
|
}
|
|
|
|
resetTimer = TIMEBEFORERESET;
|
|
}
|
|
else
|
|
{
|
|
resetTimer -= Time.deltaTime;
|
|
if (resetTimer <= 0 && fingerPos.Count > 0)
|
|
{
|
|
//Texture2D tex = ToTexture2D(renderTex);
|
|
//SaveTex(tex);
|
|
MyIMEApi.instance.SendApi(ToJsonData(), this);
|
|
|
|
|
|
//currentPos.Clear();
|
|
fingerPos.Clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void DoneLoadingDestroyStrokes(string str)
|
|
{
|
|
Debug.Log("{str}");
|
|
keyboard.AddHandWritingBoardKey(str);
|
|
foreach (LineRenderer stroke in strokes)
|
|
Destroy(stroke.gameObject);
|
|
strokes.Clear();
|
|
}
|
|
|
|
private const float TIMEBEFORERESET = 1f;
|
|
private float resetTimer = 1f;
|
|
|
|
|
|
private bool IsDrawing = false;
|
|
public LineRenderer strokePrefab;
|
|
public Transform strokePrefabParent;
|
|
|
|
public void StartDrawing()
|
|
{
|
|
if (IsDrawing)
|
|
return;
|
|
if (!isCanStartDrawing)
|
|
return;
|
|
|
|
lineRender = Instantiate(strokePrefab, strokePrefabParent);
|
|
currentPos.Clear();
|
|
strokes.Add(lineRender);
|
|
IsDrawing = true;
|
|
}
|
|
|
|
bool isCanStartDrawing = false;
|
|
Canvas canvas;
|
|
|
|
/// <summary>
|
|
/// 获取鼠标停留处UI
|
|
/// </summary>
|
|
public static GameObject GetUI(GameObject canvas)
|
|
{
|
|
if (canvas.GetComponent<GraphicRaycaster>() == null) return null;
|
|
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
|
|
pointerEventData.position = Input.mousePosition;
|
|
GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
|
|
List<RaycastResult> results = new List<RaycastResult>();
|
|
gr.Raycast(pointerEventData, results);
|
|
if (results.Count != 0)
|
|
{
|
|
return results[0].gameObject;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void StopDrawing()
|
|
{
|
|
if (!IsDrawing)
|
|
return;
|
|
IsDrawing = false;
|
|
}
|
|
|
|
string ToJsonData()
|
|
{
|
|
string x = "";
|
|
string y = "";
|
|
for (int i = 0; i < fingerPos.Count; i++)
|
|
{
|
|
Vector3 offsetPos = fingerPos[i] - new Vector3(0, 199);
|
|
offsetPos.y = 460 - offsetPos.y;
|
|
x += ((int)offsetPos.x).ToString();
|
|
if (i < fingerPos.Count - 1)
|
|
x += ",";
|
|
y += ((int)offsetPos.y).ToString();
|
|
if (i < fingerPos.Count - 1)
|
|
y += ",";
|
|
}
|
|
|
|
|
|
string json = "[";
|
|
json += x;
|
|
json += "],";
|
|
json += "[";
|
|
json += y;
|
|
json += "],";
|
|
|
|
Debug.Log(json);
|
|
return json;
|
|
}
|
|
|
|
|
|
public RenderTexture renderTex;
|
|
|
|
void SaveTex(Texture2D texture)
|
|
{
|
|
//then Save To Disk as PNG
|
|
byte[] bytes = texture.EncodeToPNG();
|
|
var dirPath = Application.dataPath + "SaveImages/";
|
|
if (!Directory.Exists(dirPath))
|
|
{
|
|
Directory.CreateDirectory(dirPath);
|
|
}
|
|
|
|
File.WriteAllBytes(dirPath + "Image" + ".png", bytes);
|
|
}
|
|
|
|
Texture2D ToTexture2D(RenderTexture rTex)
|
|
{
|
|
RenderTexture currentActiveRT = RenderTexture.active;
|
|
Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.ARGB32, false);
|
|
RenderTexture.active = rTex;
|
|
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
|
|
tex.Apply();
|
|
RenderTexture.active = currentActiveRT;
|
|
return tex;
|
|
}
|
|
} |