Files
SAMKWANG/Assets/Scripts/Branch/UI/UI_MachineStatusItem.cs
2025-12-15 19:21:34 +09:00

380 lines
14 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Samkwang
{
public class UI_MachineStatusItem : MonoBehaviour
{
public CompleteInfo completeInfo;
public UI_ModelCodeTooltip modelCodeTooltip;
private EventTrigger eventTrigger;
public TextMeshProUGUI worknm;
public TextMeshProUGUI status;
public TextMeshProUGUI pjtcd;
public TextMeshProUGUI itemdesc;
public TextMeshProUGUI goalqty;
public TextMeshProUGUI workqty;
public TextMeshProUGUI goodqty;
public TextMeshProUGUI badqty;
public TextMeshProUGUI cumulative;
public TextMeshProUGUI Text_H1;
public TextMeshProUGUI H1WORKQTY;
public TextMeshProUGUI Text_good;
public TextMeshProUGUI H1BADQTY;
public Image Image_Status;
public Image[] Line;
public float fontSize;
public float blankSize;
Dictionary<string, string> prevValues = new Dictionary<string, string>();
Dictionary<TMP_Text, Coroutine> runningHighlight = new Dictionary<TMP_Text, Coroutine>();
public float highlightDuration = 1.0f;
public Transform parent;
public void Init()
{
modelCodeTooltip = transform.GetComponentInChildren<UI_ModelCodeTooltip>(true);
modelCodeTooltip.Init();
eventTrigger = transform.GetComponentInChildren<EventTrigger>(true);
SetEventTrigger();
var textDict = transform.GetChildComponentsByName<TextMeshProUGUI>(transform);
var imageDict = transform.GetChildComponentsByName<Image>(transform);
worknm = textDict.GetOrNull(nameof(worknm));
status = textDict.GetOrNull(nameof(status));
pjtcd = textDict.GetOrNull(nameof(pjtcd));
itemdesc = textDict.GetOrNull(nameof(itemdesc));
goalqty = textDict.GetOrNull(nameof(goalqty));
workqty = textDict.GetOrNull(nameof(workqty));
goodqty = textDict.GetOrNull(nameof(goodqty));
badqty = textDict.GetOrNull(nameof(badqty));
cumulative = textDict.GetOrNull(nameof(cumulative));
Text_H1 = textDict.GetOrNull(nameof(Text_H1));
H1WORKQTY = textDict.GetOrNull(nameof(H1WORKQTY));
Text_good = textDict.GetOrNull(nameof(Text_good));
H1BADQTY = textDict.GetOrNull(nameof(H1BADQTY));
Image_Status = imageDict.GetOrNull(nameof(Image_Status));
var allImages = GetComponentsInChildren<Image>(true);
var lineList = new List<Image>();
foreach (var img in allImages)
{
if (img.name == "Line")
{
lineList.Add(img);
}
}
Line = lineList.ToArray();
parent = GetParentUnderRoot(this);
}
private void SetEventTrigger()
{
EventTrigger.Entry enterentry = new EventTrigger.Entry();
enterentry.eventID = EventTriggerType.PointerEnter;
enterentry.callback.AddListener((data) => { MouseEnter(); });
EventTrigger.Entry exitentry = new EventTrigger.Entry();
exitentry.eventID = EventTriggerType.PointerExit;
exitentry.callback.AddListener((data) => { MouseExit(); });
eventTrigger.triggers.Add(enterentry);
eventTrigger.triggers.Add(exitentry);
}
public Transform GetParentUnderRoot(UI_MachineStatusItem item)
{
if (item == null) return null;
// 현재에서 위로 올라가며 조상들을 모두 저장
List<Transform> ancestors = new List<Transform>();
Transform current = item.transform;
while (current != null)
{
ancestors.Add(current);
current = current.parent;
}
if (ancestors.Count >= 2)
return ancestors[ancestors.Count - 2];
else
return null;
}
public void SetStatusData(CompleteInfo completeInfo)
{
this.completeInfo = completeInfo;
worknm.SetText(SetTextData(completeInfo.worknm));
status.SetText(SetTextData(completeInfo.statusnm));
SetStatusColor(completeInfo.statusnm);
SetActiveOneHour(completeInfo.statusnm);
if (completeInfo.statusnm == "가동중")
{
UpdateAndHighlight(pjtcd, SetTextData(completeInfo.pjtcd), "pjtcd");
UpdateAndHighlight(itemdesc, SetTextData(completeInfo.itemdesc), "itemdesc");
UpdateAndHighlight(goalqty, RoundToIntData(completeInfo.goalqty), "goalqty");
UpdateAndHighlight(workqty, RoundToIntData(completeInfo.workqty), "workqty");
UpdateAndHighlight(goodqty, RoundToIntData(completeInfo.goodqty), "goodqty");
UpdateAndHighlight(badqty, RoundToIntData(completeInfo.badqty), "badqty");
SetStatusImageResize();
}
else
{
SetNotWorkStatusData(completeInfo);
}
}
public void SetStatusData(OneHourInfo oneHourInfo)
{
// wordno가 없는 경우 체크
if (oneHourInfo == null)
{
H1WORKQTY.SetText("--");
Text_good.SetText("--");
H1BADQTY.SetText("--");
return;
}
var caculateData = CalculateGoodData(oneHourInfo.workqty, oneHourInfo.badqty);
H1WORKQTY.SetText(SetTextData(oneHourInfo.workqty));
Text_good.SetText(RoundToIntData(caculateData));
H1BADQTY.SetText(SetTextData(oneHourInfo.badqty));
}
public void SetNotWorkStatusData(CompleteInfo completeInfo)
{
ColorUtility.TryParseHtmlString("#6D6D6D", out var newColor);
SetTextColor(worknm, completeInfo.worknm, newColor);
SetTextColor(pjtcd, SetTextData(completeInfo.pjtcd), newColor);
SetTextColor(itemdesc, SetTextData(completeInfo.itemdesc), newColor);
SetTextColor(goalqty, RoundToIntData(completeInfo.goalqty), newColor);
SetTextColor(workqty, RoundToIntData(completeInfo.workqty), newColor);
SetTextColor(goodqty, RoundToIntData(completeInfo.goodqty), newColor);
SetTextColor(badqty, RoundToIntData(completeInfo.badqty), newColor);
cumulative.gameObject.SetActive(false);
Text_H1.gameObject.SetActive(false);
H1WORKQTY.gameObject.SetActive(false);
Text_good.gameObject.SetActive(false);
H1BADQTY.gameObject.SetActive(false);
if (Line != null)
{
for (int i = 0; i < Line.Length; i++)
{
if (Line[i] != null) Line[i].gameObject.SetActive(false);
}
}
}
private void SetTextColor(TMP_Text comp, string value, Color color)
{
comp.SetText(value);
comp.color = color;
}
private string SetTextData(string data)
{
if (data == string.Empty || data == null)
{
return "--";
}
return data;
}
public string RoundToIntData(string data)
{
// 데이터가 null이거나 빈 문자열이면 "--" 반환
if (string.IsNullOrEmpty(data))
return "--";
float.TryParse(data, out var floatData);
var intData = Mathf.RoundToInt(floatData);
return intData.ToString("N0");
}
public string RoundToFloatData(string data)
{
// 데이터가 null이거나 빈 문자열이면 "--" 반환
if (string.IsNullOrEmpty(data))
return "--";
float.TryParse(data, out var floatData);
var truncateData = Math.Truncate(floatData * 10) / 10;
return truncateData.ToString("N0");
}
public string CalculateGoodData(string data_work, string data_bad)
{
// 데이터가 null이거나 빈 문자열이면 "--" 반환
if (string.IsNullOrEmpty(data_work) || string.IsNullOrEmpty(data_bad))
return "--";
float.TryParse(data_work, out var floatData_work);
float.TryParse(data_bad, out var floatData_bad);
var intdata_work = Mathf.RoundToInt(floatData_work);
var intdata_bad = Mathf.RoundToInt(floatData_bad);
var calculatedData = intdata_work - intdata_bad;
return calculatedData.ToString("N0");
}
void SetStatusColor(string stat)
{
var newColor = new Color();
switch (stat)
{
case "가동중":
ColorUtility.TryParseHtmlString("#00C314", out newColor);
Image_Status.color = newColor;
status.color = Color.white;
break;
case "비가동중":
ColorUtility.TryParseHtmlString("#6D6D6D", out newColor);
Image_Status.color = newColor;
status.color = Color.white;
break;
default:
Image_Status.color = Color.white;
status.color = Color.black;
break;
}
}
void SetActiveOneHour(string stat)
{
switch (stat)
{
case "가동중":
cumulative.gameObject.SetActive(true);
Text_H1.gameObject.SetActive(true);
H1WORKQTY.gameObject.SetActive(true);
Text_good.gameObject.SetActive(true);
H1BADQTY.gameObject.SetActive(true);
if (Line != null)
{
for (int i = 0; i < Line.Length; i++)
{
if (Line[i] != null) Line[i].gameObject.SetActive(true);
}
}
break;
default:
cumulative.gameObject.SetActive(false);
Text_H1.gameObject.SetActive(false);
H1WORKQTY.gameObject.SetActive(false);
Text_good.gameObject.SetActive(false);
H1BADQTY.gameObject.SetActive(false);
if (Line != null)
{
for (int i = 0; i < Line.Length; i++)
{
if (Line[i] != null) Line[i].gameObject.SetActive(false);
}
}
break;
}
}
private void SetStatusImageResize()
{
var newWidth = status.text.Length * fontSize + blankSize;
var height = Image_Status.rectTransform.sizeDelta.y;
Image_Status.rectTransform.sizeDelta = new Vector2(newWidth, height);
}
void UpdateAndHighlight(TMP_Text comp, string newText, string key)
{
if (comp == null) return;
if (newText == null) newText = string.Empty;
string old = prevValues.ContainsKey(key) ? prevValues[key] : comp.text ?? string.Empty;
if (!string.Equals(old, newText))
{
comp.SetText(newText);
prevValues[key] = newText;
if (runningHighlight.TryGetValue(comp, out Coroutine existing))
{
if (existing != null) StopCoroutine(existing);
runningHighlight.Remove(comp);
}
if (parent.gameObject.activeSelf)
{
Coroutine cr = StartCoroutine(FlashHighlight(comp));
runningHighlight[comp] = cr;
}
}
else
{
comp.SetText(newText);
}
}
IEnumerator FlashHighlight(TMP_Text comp)
{
if (comp == null) yield break;
Image img = comp.GetComponentInParent<Image>();
Color c = img.color;
c.a = 0f;
img.color = c;
// fade in 0 -> 1
float t = 0f;
while (t < highlightDuration / 2f)
{
t += Time.deltaTime;
float p = highlightDuration / 2f <= 0f ? 1f : Mathf.Clamp01(t / (highlightDuration / 2f));
c.a = Mathf.Lerp(0f, 1f, p);
img.color = c;
yield return null;
}
// fade out 1 -> 0
t = 0f;
while (t < highlightDuration / 2f)
{
t += Time.deltaTime;
float p = highlightDuration / 2f <= 0f ? 1f : Mathf.Clamp01(t / (highlightDuration / 2f));
c.a = Mathf.Lerp(1f, 0f, p);
img.color = c;
yield return null;
}
c.a = 0f;
img.color = c;
// 정리
if (runningHighlight.ContainsKey(comp))
runningHighlight.Remove(comp);
}
public void MouseEnter()
{
if (completeInfo.pjtcd == null)
return;
modelCodeTooltip.SetData(completeInfo.itemcd, eventTrigger.transform.position);
modelCodeTooltip.gameObject.SetActive(true);
}
public void MouseExit()
{
if (completeInfo.pjtcd == null)
return;
modelCodeTooltip.gameObject.SetActive(false);
}
}
}