Skip to content

Instantly share code, notes, and snippets.

@MickaelCruzDB
Created June 2, 2017 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MickaelCruzDB/ca6f50e566af5dac7be4c2747e9884f8 to your computer and use it in GitHub Desktop.
Save MickaelCruzDB/ca6f50e566af5dac7be4c2747e9884f8 to your computer and use it in GitHub Desktop.
using UnityEngine;
using Vectrosity;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
using System.Collections;
using UnityEngine.SceneManagement;
public class DrawingView : MonoBehaviour {
public Text _Console;
public Texture2D lineTex;
public float lineWidth = 4.0f;
public Texture2D capTex;
public Transform _monCanvas;
public Transform _myPanel;
public InputField textfieldPrefab;
public Image takenPhoto;
public Animator PanelCommandsDrawingAnimator;
public Canvas UICanvas;
public Canvas UICanvasColors;
public Canvas DrawCanvas;
public Canvas UICanvasHamburger;
private VectorLine newCircle;
private VectorLine obliqueLine;
private Vector2 previousPosition;
private int sqrMinPixelMove;
private bool canDraw = false;
private Touch touch;
private int _circleIndex;
private int segments = 128;
private Vector3 origin;
private Vector2 startPoint;
private Vector2 endPoint;
private Transform _actualTransform;
private List<VectorLine> circleList;
private List<VectorLine> obliqueLineList;
private List<InputField> dimValueList;
private RectTransform rect;
private Sprite myPhotoSprite;
private string lineColor = "white";
// Use this for initialization
void Start () {
DrawCanvas.GetComponent<Canvas> ().enabled = false;
UICanvas.GetComponent<Canvas> ().enabled = true;
UICanvasColors.GetComponent<Canvas> ().enabled = true;
UICanvasHamburger.GetComponent<Canvas> ().enabled = false;
// PanelCommandsDrawingAnimator.SetBool("tools_in",true);
//Photo Image background button
var fileName = "/Users/mika/Library/Application Support/Digitalblend/EDF_Rapports/rapport_roue/2/0.jpg";
var photo = File.ReadAllBytes( fileName );
var texture = new Texture2D( 73, 73 );
texture.LoadImage (photo);
myPhotoSprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f);
takenPhoto.sprite = myPhotoSprite;
circleList = new List<VectorLine>();
obliqueLineList = new List<VectorLine>();
dimValueList = new List<InputField>();
_circleIndex = 0;
canDraw = true;
}
// Update is called once per frame
void Update () {
if (phasesContentStatus.DrawingShapeIs == "circle" && canDraw == true && phasesContentStatus.colorSelected == true && phasesContentStatus.toolSelected == true && (Input.touchCount > 0)) {
GameObject drawCanvas = GameObject.FindWithTag("DrawCanvas");
drawCanvas.gameObject.SetActive (true);
DrawCanvas.GetComponent<Canvas> ().enabled = true;
GameObject uiCanvasHamburger = GameObject.FindWithTag("UICanvasHamburger");
uiCanvasHamburger.gameObject.SetActive (true);
UICanvasHamburger.GetComponent<Canvas> ().enabled = true;
UICanvasHamburger.GetComponent<Canvas> ().sortingOrder = 32767;
if (Input.touchCount > 0) {
touch = Input.GetTouch (0);
if (touch.phase == TouchPhase.Began) {
Debug.Log ("Inside " + touch.position.x + " " + touch.position.y);
SetCircle ();
circleList.Add (newCircle);
obliqueLineList.Add (obliqueLine);
startPoint = touch.position;
newCircle.points2.Add (touch.position);
if (newCircle.points2.Count == 1) {
newCircle.points2.Add (Vector2.zero);
_Console.text = newCircle.points2.Count.ToString ();
}
}
if (newCircle.points2.Count >= 2 && touch.phase == TouchPhase.Moved) {
_Console.text = "Moved ?";
newCircle.points2 [newCircle.points2.Count - 1] = touch.position;
newCircle.MakeCircle (startPoint, Vector2.Distance (startPoint, touch.position), segments);
newCircle.Draw ();
}
if (touch.phase == TouchPhase.Ended) {
canDraw = false;
endPoint = Input.GetTouch (0).position;
Vector2 obliqueStartPoint = new Vector2 (startPoint.x - Vector2.Distance (startPoint, endPoint), startPoint.y);
obliqueLine.points2.Add (obliqueStartPoint);
Vector2 obliqueEndPoint = new Vector2 (startPoint.x + Vector2.Distance (startPoint, endPoint) + 30f, startPoint.y);
obliqueLine.points2.Add (obliqueEndPoint);
//New Start Point
obliqueLine.points2.Add (obliqueEndPoint);
Vector2 obliqueOffsetPoint2 = new Vector2 (obliqueEndPoint.x + 30f, obliqueEndPoint.y - 30f);
obliqueLine.points2.Add (obliqueOffsetPoint2);
InputField dimValue = (InputField)Instantiate (textfieldPrefab, obliqueOffsetPoint2, Quaternion.identity);
dimValueList.Add (dimValue);
//Rotation du transform contenant la ligne
transform.RotateAround (startPoint, Vector3.forward, 45f);
//Rotation du dimValue par rapport au centre du cercle
dimValue.transform.RotateAround (startPoint, Vector3.forward, 45f);
//Rotation du dimValue par rapport à son centre (pour le redresser)
dimValue.transform.rotation = Quaternion.Euler (45f, 0f, 0f);
//Tracé de la ligne
obliqueLine.Draw ();
//Reset de la rotation du transform contenant la ligne
transform.RotateAround (startPoint, Vector3.forward, -45f);
dimValue.onEndEdit.AddListener (delegate {
checkValue (dimValue);
});
dimValue.transform.SetParent (_myPanel, false);
// canDraw = true;
}
} else if (phasesContentStatus.DrawingShapeIs == "line") {
}
}
}
public void DrawCircle() {
phasesContentStatus.DrawingShapeIs = "circle";
phasesContentStatus.toolSelected = true;
StartCoroutine(test ());
}
IEnumerator test() {
if (phasesContentStatus.toolSelected == true && phasesContentStatus.colorSelected == true) {
// DrawCanvas.GetComponent<Canvas> ().enabled = true;
UICanvasColors.GetComponent<Canvas> ().enabled = false;
UICanvas.GetComponent<Canvas> ().enabled = false;
yield return new WaitForSeconds(1);
SetCircle ();
}
}
void SetCircle() {
var linePoints = new List<Vector2>(segments*2);
newCircle = new VectorLine("cercle_"+_circleIndex.ToString(), linePoints, lineTex, lineWidth, LineType.Discrete, Joins.Weld);
obliqueLine = new VectorLine("obliqueLine_"+_circleIndex.ToString(), new List<Vector2>(),lineTex, lineWidth, LineType.Discrete, Joins.Weld);
obliqueLine.drawTransform = transform;
switch (lineColor)
{
case "white":
newCircle.color = Color.white;
obliqueLine.color = Color.white;
break;
case "black":
newCircle.color = Color.black;
obliqueLine.color = Color.black;
break;
case "blue":
newCircle.color = Color.blue;
obliqueLine.color = Color.blue;
break;
case "red":
newCircle.color = Color.red;
obliqueLine.color = Color.red;
break;
case "yellow":
newCircle.color = Color.yellow;
obliqueLine.color = Color.yellow;
break;
case "green":
newCircle.color = Color.green;
obliqueLine.color = Color.green;
break;
default:
newCircle.color = Color.white;
obliqueLine.color = Color.white;
break;
}
_circleIndex++;
}
void checkValue(InputField input)
{
if (input.text != "") {
string actualValue = input.text;
actualValue = "Ø " + actualValue;
input.text = actualValue;
canDraw = true;
}
}
public void Undo() {
VectorLine circleToDelete = circleList [circleList.Count-1];
VectorLine.Destroy (ref circleToDelete);
VectorLine obliqueLineToDelete = obliqueLineList [obliqueLineList.Count-1];
VectorLine.Destroy (ref obliqueLineToDelete);
InputField dimValueToDelete = dimValueList [dimValueList.Count-1];
if (dimValueToDelete != null) {
GameObject.Destroy(dimValueToDelete.gameObject);
}
_circleIndex--;
Debug.Log (circleList.Count);
}
public void ClearCanvas () {
Destroy (GameObject.Find ("DrawCanvas"));
Destroy (GameObject.Find ("VectorCanvas"));
}
public void ChangeColorToWhite() {
Debug.Log ("White selected");
lineColor = "white";
phasesContentStatus.colorSelected = true;
if (phasesContentStatus.toolSelected == true && phasesContentStatus.colorSelected == true) {
DrawCircle ();
}
}
public void ChangeColorToBlack() {
Debug.Log ("Black selected");
lineColor = "black";
phasesContentStatus.colorSelected = true;
if (phasesContentStatus.toolSelected == true && phasesContentStatus.colorSelected == true) {
DrawCircle ();
}
}
public void ChangeColorToBlue() {
Debug.Log ("Blue selected");
lineColor = "blue";
phasesContentStatus.colorSelected = true;
if (phasesContentStatus.toolSelected == true && phasesContentStatus.colorSelected == true) {
DrawCircle ();
}
}
public void ChangeColorToRed() {
Debug.Log ("Red selected");
lineColor = "red";
phasesContentStatus.colorSelected = true;
if (phasesContentStatus.toolSelected == true && phasesContentStatus.colorSelected == true) {
DrawCircle ();
}
}
public void ChangeColorToYellow() {
Debug.Log ("Yellow selected");
lineColor = "yellow";
phasesContentStatus.colorSelected = true;
if (phasesContentStatus.toolSelected == true && phasesContentStatus.colorSelected == true) {
DrawCircle ();
}
}
public void ChangeColorToGreen() {
Debug.Log ("Green selected");
lineColor = "green";
phasesContentStatus.colorSelected = true;
if (phasesContentStatus.toolSelected == true && phasesContentStatus.colorSelected == true) {
DrawCircle ();
}
}
public void OpenMenusTools() {
Debug.Log ("on ouvre les menus");
UICanvasColors.GetComponent<Canvas> ().enabled = true;
UICanvas.GetComponent<Canvas> ().enabled = true;
canDraw = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment