Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Last active April 22, 2020 07:16
Show Gist options
  • Save DongguemYoo/acfc7df611abf86d017dde2e0e47fa26 to your computer and use it in GitHub Desktop.
Save DongguemYoo/acfc7df611abf86d017dde2e0e47fa26 to your computer and use it in GitHub Desktop.
그림그리기 테스트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
public class DrawLines : MonoBehaviour
{
[Serializable]
public class Category
{
public string category;
public GameObject LinePrefab;
private GameObject Cat_Line;
private GameObject go_Info;
private LineRenderer Cat_LineLender;
private RectTransform imageRectTransform;
private GameObject InfoPrefab;
private Transform InfoParent;
//[HideInInspector]
public float myYValue;
public float yOffset;
[HideInInspector]
public List<Vector3> AllPoints = new List<Vector3>();
void CreateLine()
{
if (Cat_Line != null)
return;
Cat_Line = Instantiate(LinePrefab, imageRectTransform);
go_Info = Instantiate(InfoPrefab, InfoParent);
go_Info.GetComponentInChildren<Text>().text = category;
Cat_LineLender = Cat_Line.GetComponent<LineRenderer>();
go_Info.GetComponentInChildren<Image>().color = Cat_LineLender.material.color;
//Cat_Line.transform.localPosition = new Vector3(-imageRectTransform.sizeDelta.x / 2, -imageRectTransform.sizeDelta.y / 2, 0); //imageRectTransform.localPosition.x
Cat_Line.transform.localPosition = new Vector3(-imageRectTransform.sizeDelta.x / 2, 0, 0); //imageRectTransform.localPosition.x
Cat_Line.transform.localRotation = new Quaternion(0, 0, 0, 0);
Cat_Line.transform.localScale = new Vector3(1, 1, 1);
Cat_Line.name = category;
//if (txt_Cat.text != category)
// txt_Cat.text = category;
initCatToggle();
}
public float GetMaxPoint()
{
if (AllPoints.Count == 0)
return 1;
return AllPoints.Max(x => x.y) / yOffset;
}
public float GetMinPoint()
{
if (AllPoints.Count == 0)
return 0.1f;
return AllPoints.Min(x => x.y) / yOffset;
}
void SpawnLineGenerator()
{
Cat_LineLender.positionCount = AllPoints.Count;
Cat_LineLender.SetPositions(AllPoints.ToArray());
}
public void GenerateNewLine()
{
if (AllPoints.Count == 1) //if (AllPoints.Count == 1)
{
CreateLine();
}
else if (AllPoints.Count >= 2)
{
// 포인트 두점 이상인 경우.
SpawnLineGenerator();
}
}
void initCatToggle()
{
//CatToggle.onValueChanged.AddListener(delegate { CatOnOff(CatToggle); } );
}
public void CatOnOff(Toggle toggle)
{
Cat_Line.SetActive(toggle.isOn);
}
public void SetImageCanvas(RectTransform rectTransform, GameObject _InfoPrefab, Transform _InfoParent)
{
if (imageRectTransform == null)
imageRectTransform = rectTransform;
if (InfoPrefab == null)
InfoPrefab = _InfoPrefab;
if (InfoParent == null)
InfoParent = _InfoParent;
}
public GameObject GetCatLine()
{
return Cat_Line;
}
}
[Serializable]
public class GraphState
{
public float drawOffset;
public int MaxCount;
public Text txt_Top1;
public Text txt_Top2;
public Text txt_Top3;
public Text txt_Top4;
public Text txt_Top5;
public RectTransform imageRectTransform;
public GameObject InfoPrefab;
public Transform InfoParent;
public List<Category> categories;
//원래 private이 였음
public float yMax = 1;
public float yMin = 0;
private float tempMax;
private float tempMin;
private float xOffset;
private float yOffset;
private float newYOffset;
public void SetMinMax()
{
if (yMax < 0)
{
yMax = 1;
}
if (yMin >= -0.5f)
{
yMin = -0.5f;
}
}
void TextUpdate()
{
if (txt_Top1.text != yMax.ToString("N2"))
txt_Top1.text = yMax.ToString("N2");
if (txt_Top2.text != (yMax * 0.75f + yMin * 0.25f).ToString("N2"))
txt_Top2.text = (yMax * 0.75f + yMin * 0.25f).ToString("N2");
if (txt_Top3.text != (yMax * 0.5f + yMin * 0.5f).ToString("N2"))
txt_Top3.text = (yMax * 0.5f + yMin * 0.5f).ToString("N2");
if (txt_Top4.text != (yMax * 0.25f + yMin * 0.75f).ToString("N2"))
txt_Top4.text = (yMax * 0.25f + yMin * 0.75f).ToString("N2");
if (txt_Top5.text != yMin.ToString("N2"))
txt_Top5.text = yMin.ToString("N2");
}
/// <summary>
/// 기존 min max값 보다 큰 값이 들어왔을때 Yoffset 값을 변경하여 적용한다.
/// </summary>
public void ReCalueY()
{
if ((Mathf.Abs(yMax) + Mathf.Abs(yMin)) == 0)
return;
newYOffset = imageRectTransform.sizeDelta.y / (Mathf.Abs(yMax) + Mathf.Abs(yMin));
TextUpdate();
for (int i = 0; i < categories.Count; i++)
{
for (int index = 0; index < categories[i].AllPoints.Count; index++)
{
categories[i].AllPoints[index] = new Vector3(categories[i].AllPoints[index].x, categories[i].AllPoints[index].y / yOffset * newYOffset, categories[i].AllPoints[index].z);
}
//LinePositionSet(categories[i].GetCatLine());
}
yOffset = newYOffset;
}
Category GetCat(string categoryName)
{
return categories.Find(x => x.category == categoryName);
}
/// <summary>
/// 사이즈 변경을 위한
/// </summary>
public void Init(bool isNonShifting = false)
{
if (!isNonShifting)
xOffset = imageRectTransform.sizeDelta.x / (MaxCount * (MaxCount * 0.0835f)); //*9.1f
else
xOffset = imageRectTransform.sizeDelta.x / (MaxCount); //*9.1f
yOffset = imageRectTransform.sizeDelta.y / (Mathf.Abs(yMax) + Mathf.Abs(yMin));
}
/// <summary>
///
/// </summary>
/// <param name="pos">점을 찍을 pos Vector3</param>
/// <param name="Cat_Line">위치를 설정하기 위한 게임오브젝트를 가져온다</param>
public void Addpoint(string categoryName, Vector3 pos, GameObject Cat_Line, bool isNonShifting = false)
{
Category tmp = GetCat(categoryName);
tmp.yOffset = yOffset;
if (tmp.AllPoints.Count == 0)
Init(isNonShifting);
tmp.myYValue = pos.y;
if (!isNonShifting)
tmp.AllPoints.Add(GetRealPosition(pos));
else
{
if (tmp.AllPoints.Count == 0)
{
Debug.Log("tmp.AllPoints.Add(GetRealPosition(pos));!!!!");
tmp.AllPoints.Add(GetRealPosition(pos));
}
if (pos.x != 0 && pos.y != 0)
{
///shifting이 되지 않을때는 중복은 찍지 않아야 한다.
if (tmp.AllPoints.Exists(x => GetRealPosition(x) != GetRealPosition(pos)))
{
///존재하지 않는 점만 체크해서 찍는다
tmp.AllPoints.Add(GetRealPosition(pos));
}
}
}
if (!isNonShifting)
ShiftPoints();
if (tmp.AllPoints.Count != 0)
{
//CheckLowHigh(tmp.GetMaxPoint() / yOffset, tmp.GetMinPoint() / yOffset);
ReCalueY();
resize(Cat_Line);
CheckLowHigh(yOffset);
}
}
/// <summary>
/// 카테고리중 실시간으로 최대 최소값을 그래프에 적용한다
/// </summary>
/// <param name="newOffset"></param>
void CheckLowHigh(float newOffset)
{
categories[0].yOffset = newOffset;
tempMax = categories[0].GetMaxPoint();
tempMin = categories[0].GetMinPoint();
foreach (var x in categories)
{
if (x.yOffset != newOffset)
x.yOffset = newOffset;
if (tempMax < x.GetMaxPoint())
tempMax = x.GetMaxPoint();
if (tempMin > x.GetMinPoint())
tempMin = x.GetMinPoint();
}
if (tempMax != yMax)
{
yMax = tempMax + (tempMax * drawOffset);
}
if (tempMin != yMin)
{
if (tempMin < 0)
{
yMin = tempMin + (tempMin * drawOffset);
}
else
{
yMin = tempMin - (tempMin * drawOffset);
}
}
}
/// <summary>
/// 라인의 위치를 offset값에 따라 조절한다
/// </summary>
/// <param name="Cat_Line"></param>
void resize(GameObject Cat_Line)
{
LinePositionSet(Cat_Line);
}
/// <summary>
/// 오른쪽으로 흘러가는 shift효과
/// </summary>
void ShiftPoints()
{
for (int i = 0; i < categories.Count; i++)
{
if (categories[i].AllPoints.Count <= MaxCount)
continue;
for (int index = 0; index < categories[i].AllPoints.Count; index++)
{
categories[i].AllPoints[index] = new Vector3(categories[i].AllPoints[index].x, categories[i].AllPoints[index + 1].y, 0);
if (index == MaxCount - 1)
{
categories[i].AllPoints.RemoveRange(MaxCount, 1);
ReCalueY();
}
}
}
}
Vector3 GetRealPosition(Vector3 pos)
{
//return new Vector3(xOffset * pos.x, yOffset * pos.y, 0);
return new Vector3(float.Parse(string.Format("{0:0.##}", xOffset * pos.x)), float.Parse(string.Format("{0:0.##}", yOffset * pos.y)), 0);
}
/// <summary>
/// 마이너스 값일때만 옵셋을 조정해주는 기능이 필요해서 만듬
/// </summary>
/// <param name="LineObj"></param>
public void LinePositionSet(GameObject LineObj)
{
if (LineObj == null)
return;
LineObj.transform.localPosition = new Vector3(LineObj.transform.localPosition.x, -imageRectTransform.sizeDelta.y / 2 + (-yOffset) * (yMin), LineObj.transform.localPosition.z);
}
}
[Serializable]
public class GraphInfo
{
public GameObject MyOBJ;
public GraphState mystate;
public bool isActive = true;
public void AddPointByCat(string categoryName, Vector3 pos, int _MaxCount = 0, float _drawOffset = 0, bool _isNonShifting = false)
{
if (!mystate.categories.Exists(x => x.category == categoryName))
{
Debug.Log("해당하는 카테고리가 없습니다.");
return;
}
Category tmp = mystate.categories.Find(x => x.category == categoryName);
if (mystate.MaxCount != _MaxCount)
mystate.MaxCount = _MaxCount;
if (mystate.drawOffset != _drawOffset)
mystate.drawOffset = _drawOffset;
tmp.SetImageCanvas(mystate.imageRectTransform, mystate.InfoPrefab, mystate.InfoParent);
mystate.Addpoint(categoryName, pos, tmp.GetCatLine(), _isNonShifting);
tmp.GenerateNewLine();
}
}
#region Object Variables
//[SerializeField]
//private GameObject LineGeneratorPrefab;
//[SerializeField]
//private GameObject LinePointPrefab;
[SerializeField]
[Header("리플레이 그래프")]
public GraphInfo GrpahSpeed;
public GraphInfo GrpahForce;
public GraphInfo GrpahThickness;
[Header("AGC 그래프")]
public GraphInfo GrpahRF;
public GraphInfo GrpahGauge;
public GraphInfo GrpahManual;
public GraphInfo GrpahFB;
public GraphInfo GrpahFF;
public GraphInfo GrpahMF;
public GraphInfo GrpahPositionForce;
[Header("AutoZeroing그래프")]
public GraphInfo GrpahAutoZeroing; //Force
public GraphInfo GrpahAutoZeroing2; //GAP
public GraphInfo GrpahAutoZeroing3; //POstion
[Space]
public JogToggle m_jogToggle;
[Header("캔버스")]
public GameObject go_RePlay;
public GameObject go_AGC;
public GameObject go_AutoZeroing;
[Header("Custom Toggle Group")]
public Toggle[] Group_AGC_Toggle;
public Toggle GroupChecker;
public ToggleGroup toggleGroup;
public Toggle ReplayToggle;
public Toggle AGCToggle;
public int totalCount;
private IEnumerator cor_Speed;
private IEnumerator cor_Force;
private IEnumerator cor_Thickness;
private IEnumerator cor_RF;
private IEnumerator cor_Gauge;
private IEnumerator cor_Manual;
private IEnumerator cor_FB;
private IEnumerator cor_FF;
private IEnumerator cor_MF;
private IEnumerator cor_positionForce;
private IEnumerator cor_AutoZeroing;
private IEnumerator cor_AutoZeroing2;
private IEnumerator cor_AutoZeroing3;
#endregion
public delegate void GraphInit();
public static GraphInit del_InItGraph;
public float TestValue;
private int x = 0;
private bool isPause = false;
public int MaxXCount;
public float drawOffset;
public float textValueX;
public float textValueY;
///그래프그릴떄 AGC
///800빼기
private void Start()
{
del_InItGraph += OffAllChart;
x = 0;
textValueX = 0;
textValueY = 0;
isPause = false;
MaxXCount = 120;
drawOffset = 0.5f;
}
public void OnOffGraph(GameObject obj)
{
if (obj.activeSelf)
obj.SetActive(false);
else
obj.SetActive(true);
}
public void OffAllChart()
{
AllStopGraph();
toggleGroup.SetAllTogglesOff();
GroupChecker.isOn = false;
ReplayToggle.isOn = true;
}
public void OnOffGrahp_Mode()
{
//GroupChecker.SetActive(false);
toggleGroup.SetAllTogglesOff();
ReplayToggle.isOn = true;
if (ModeManager.Instance.mode == 1 || ModeManager.Instance.mode == 0 || ModeManager.Instance.mode == 4)
{
go_AutoZeroing.SetActive(false);
Group_AGC_Toggle[0].isOn = true;
Group_AGC_Toggle[1].isOn = true;
Group_AGC_Toggle[2].isOn = true;
Group_AGC_Toggle[3].isOn = false;
Group_AGC_Toggle[4].isOn = false;
Group_AGC_Toggle[5].isOn = false;
}
else if (ModeManager.Instance.mode == 5)
{
go_AGC.SetActive(false);
go_RePlay.SetActive(false);
go_AutoZeroing.SetActive(true);
}
}
public void GraphOnOffGroupChecker_AGC(Toggle toggle)
{
totalCount = 0;
foreach (var x in Group_AGC_Toggle)
if (x.isOn)
totalCount++;
if (totalCount > 3)
foreach (var x in Group_AGC_Toggle)
{
if (x == toggle)
continue;
if (x.isOn)
{
x.isOn = false;
--totalCount;
return;
}
}
}
private void Update()
{
DataManager.reciveFloat[5] = textValueX;
DataManager.reciveFloat[93] = textValueY;
}
void AllStopGraph()
{
StopGraph(ref cor_Speed, GrpahSpeed);
StopGraph(ref cor_Force, GrpahForce);
StopGraph(ref cor_Thickness, GrpahThickness);
StopGraph(ref cor_RF, GrpahRF);
StopGraph(ref cor_Gauge, GrpahGauge);
StopGraph(ref cor_Manual, GrpahManual);
StopGraph(ref cor_FB, GrpahFB);
StopGraph(ref cor_FF, GrpahFF);
StopGraph(ref cor_MF, GrpahMF);
StopGraph(ref cor_positionForce, GrpahPositionForce);
StopGraph(ref cor_AutoZeroing, GrpahAutoZeroing);
StopGraph(ref cor_AutoZeroing2, GrpahAutoZeroing2);
StopGraph(ref cor_AutoZeroing3, GrpahAutoZeroing3);
}
public void OnPauseGraph()
{
if (isPause)
{
PlayGraphByMode();
isPause = false;
return;
}
else
{
AllStopGraph();
isPause = true;
return;
}
}
public void OnGraphStart(Toggle toggle)
{
if (toggle.isOn)
{
AllStopGraph();
if (ModeManager.Instance.mode == 1 || ModeManager.Instance.mode == 0 || ModeManager.Instance.mode == 4)
{
StartGraph(ref cor_Speed, GrpahSpeed);
StartGraph(ref cor_Force, GrpahForce);
StartGraph(ref cor_Thickness, GrpahThickness);
StartGraph(ref cor_RF, GrpahRF);
StartGraph(ref cor_Gauge, GrpahGauge);
StartGraph(ref cor_Manual, GrpahManual);
StartGraph(ref cor_FB, GrpahFB);
StartGraph(ref cor_FF, GrpahFF);
StartGraph(ref cor_MF, GrpahMF);
StartGraph(ref cor_positionForce, GrpahPositionForce);
}
if (ModeManager.Instance.mode == 5)
{
StartGraph(ref cor_AutoZeroing, GrpahAutoZeroing);
StartGraph(ref cor_AutoZeroing2, GrpahAutoZeroing2);
StartGraph(ref cor_AutoZeroing3, GrpahAutoZeroing3);
}
}
}
public void PlayGraphByMode()
{
if (ModeManager.Instance.mode == 1 || ModeManager.Instance.mode == 0 || ModeManager.Instance.mode == 4)
{
StartGraph(ref cor_Speed, GrpahSpeed);
StartGraph(ref cor_Force, GrpahForce);
StartGraph(ref cor_Thickness, GrpahThickness);
StartGraph(ref cor_RF, GrpahRF);
StartGraph(ref cor_Gauge, GrpahGauge);
StartGraph(ref cor_Manual, GrpahManual);
StartGraph(ref cor_FB, GrpahFB);
StartGraph(ref cor_FF, GrpahFF);
StartGraph(ref cor_MF, GrpahMF);
StartGraph(ref cor_positionForce, GrpahPositionForce);
}
if (ModeManager.Instance.mode == 5)
{
StartGraph(ref cor_AutoZeroing, GrpahAutoZeroing);
StartGraph(ref cor_AutoZeroing2, GrpahAutoZeroing2);
StartGraph(ref cor_AutoZeroing3, GrpahAutoZeroing3);
}
}
void StartGraph(ref IEnumerator enumerator, GraphInfo info)
{
if (enumerator != null)
{
StopCoroutine(enumerator);
enumerator = null;
}
enumerator = OnDrawGraph(info);
StartCoroutine(enumerator);
}
void StopGraph(ref IEnumerator enumerator, GraphInfo info)
{
if (enumerator != null)
{
StopCoroutine(enumerator);
enumerator = null;
}
}
private void RandGenerate()
{
DataManager.reciveFloat[0] = UnityEngine.Random.Range(-1000, 81);
DataManager.reciveFloat[37] = UnityEngine.Random.Range(0, 1000);
DataManager.reciveFloat[1] = UnityEngine.Random.Range(0, 1000);
DataManager.reciveFloat[93] = UnityEngine.Random.Range(-1545, 2032);
DataManager.reciveFloat[78] = UnityEngine.Random.Range(-1000, 7000);
DataManager.reciveFloat[79] = UnityEngine.Random.Range(-1040, 1000);
DataManager.reciveFloat[80] = UnityEngine.Random.Range(-1000, 1000);
DataManager.reciveFloat[70] = UnityEngine.Random.Range(-220, 1000);
DataManager.reciveFloat[71] = UnityEngine.Random.Range(-100, 1000);
DataManager.reciveFloat[51] = UnityEngine.Random.Range(-10, 1000);
DataManager.reciveFloat[50] = UnityEngine.Random.Range(-230, 1000);
DataManager.reciveFloat[9] = UnityEngine.Random.Range(-100, 1000);
DataManager.reciveFloat[10] = UnityEngine.Random.Range(-1400, 5000);
}
IEnumerator OnDrawGraph(GraphInfo info)
{
WaitForSeconds second = new WaitForSeconds(0.1f);
//시간 x
while (true)
{
if (info.isActive)
{
for (int i = 0; i < info.mystate.categories.Count; i++)
{
//randValueY = UnityEngine.Random.Range(-20, 20);
//CurrentPoint = new Vector3(x, randValueY, 0);
switch (info.mystate.categories[i].category)
{
///RePlay
case "Entry Speed":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[0], 0), MaxXCount, drawOffset);
break;
case "Mill Speed":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[37], 0), MaxXCount, drawOffset);
break;
case "Delivery Speed":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[1], 0), MaxXCount, drawOffset);
break;
case "Total Roll Dia Force":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[93], 0), MaxXCount, drawOffset);
break;
case "Work Side Roll Force":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[78], 0), MaxXCount, drawOffset);
break;
case "Drive Side Roll Force":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[79], 0), MaxXCount, drawOffset);
break;
case "Difference Roll Force":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[80], 0), MaxXCount, drawOffset);
break;
case "Entry Thickness":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[70], 0), MaxXCount, drawOffset);
break;
case "Delivery Thickness":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[71], 0), MaxXCount, drawOffset);
break;
///RePlay
///AGC
case "RF Set":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[51], 0), MaxXCount, drawOffset);
break;
case "GAP Set":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[50], 0), MaxXCount, drawOffset);
break;
case "Gap Actual Drive":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[9], 0), MaxXCount, drawOffset);
break;
case "Gap Actual Work":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[10], 0), MaxXCount, drawOffset);
break;
///AGC
///
///AutoZeroing
case "WS Roll Gap":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[9], 0), MaxXCount, drawOffset);
break;
case "DS Roll Gap":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[10], 0), MaxXCount, drawOffset);
break;
case "WS Roll Position":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[5], 0), MaxXCount, drawOffset);
break;
case "DS Roll Position":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, DataManager.reciveFloat[6], 0), MaxXCount, drawOffset);
break;
///AutoZeroing
///position,force그래프
/// shiting이 없어야한다
case "WS Pos,WS Force":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(DataManager.reciveFloat[5], DataManager.reciveFloat[78], 0), 60, drawOffset, true);
break;
case "DS Pos,DS Force":
info.AddPointByCat(info.mystate.categories[i].category, new Vector3(DataManager.reciveFloat[6], DataManager.reciveFloat[79], 0), 60, drawOffset, true);
break;
///position,force그래프
}
//info.AddPointByCat(info.mystate.categories[i].category, new Vector3(x, randValueY, 0));
//RandGenerate();
}
//if(x< MaxXCount)
x++;
}
yield return second;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment