123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- /************************************************/
- /* */
- /* Copyright (c) 2018 - 2021 monitor1394 */
- /* https://github.com/monitor1394 */
- /* */
- /************************************************/
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using XUGL;
- namespace XCharts
- {
- internal class DrawSerieLiquid : IDrawSerie
- {
- public BaseChart chart;
- private bool m_UpdateLabelText = false;
- public DrawSerieLiquid(BaseChart chart)
- {
- this.chart = chart;
- }
- public void InitComponent()
- {
- //UpdateRuntimeData();
- //SerieLabelHelper.UpdateLabelText(chart.series, chart.theme, m_LegendRealShowName);
- }
- public void CheckComponent()
- {
- }
- public void Update()
- {
- if (m_UpdateLabelText)
- {
- m_UpdateLabelText = false;
- foreach (var serie in chart.series.list)
- {
- if (serie.type == SerieType.Liquid)
- {
- var colorIndex = chart.m_LegendRealShowName.IndexOf(serie.name);
- SerieLabelHelper.SetLiquidLabelText(serie, chart.theme, colorIndex);
- }
- }
- }
- }
- public void DrawBase(VertexHelper vh)
- {
- }
- public void DrawSerie(VertexHelper vh, Serie serie)
- {
- if (serie.type != SerieType.Liquid) return;
- UpdateRuntimeData(serie);
- DrawVesselBackground(vh, serie);
- DrawLiquid(vh, serie);
- DrawVessel(vh, serie);
- }
- public void RefreshLabel()
- {
- }
- public bool CheckTootipArea(Vector2 local)
- {
- return false;
- }
- public bool OnLegendButtonClick(int index, string legendName, bool show)
- {
- return false;
- }
- public bool OnLegendButtonEnter(int index, string legendName)
- {
- return false;
- }
- public bool OnLegendButtonExit(int index, string legendName)
- {
- return false;
- }
- public void OnPointerDown(PointerEventData eventData)
- {
- }
- private void UpdateRuntimeData()
- {
- foreach (var vessel in chart.vessels)
- {
- VesselHelper.UpdateVesselCenter(vessel, chart.chartPosition, chart.chartWidth, chart.chartHeight);
- }
- }
- private void UpdateRuntimeData(Serie serie)
- {
- var vessel = chart.GetVessel(serie.vesselIndex);
- if (vessel != null)
- {
- VesselHelper.UpdateVesselCenter(vessel, chart.chartPosition, chart.chartWidth, chart.chartHeight);
- }
- }
- private void DrawVesselBackground(VertexHelper vh, Serie serie)
- {
- var vessel = chart.GetVessel(serie.vesselIndex);
- if (vessel != null)
- {
- if (vessel.backgroundColor.a != 0)
- {
- switch (vessel.shape)
- {
- case Vessel.Shape.Circle:
- var cenPos = vessel.runtimeCenterPos;
- var radius = vessel.runtimeRadius;
- UGL.DrawCricle(vh, cenPos, vessel.runtimeInnerRadius + vessel.gap, vessel.backgroundColor,
- chart.settings.cicleSmoothness);
- UGL.DrawDoughnut(vh, cenPos, vessel.runtimeInnerRadius, vessel.runtimeInnerRadius + vessel.gap,
- vessel.backgroundColor, Color.clear, chart.settings.cicleSmoothness);
- break;
- case Vessel.Shape.Rect:
- UGL.DrawRectangle(vh, vessel.runtimeCenterPos, vessel.runtimeWidth / 2, vessel.runtimeHeight / 2,
- vessel.backgroundColor);
- break;
- default:
- break;
- }
- }
- }
- }
- private void DrawVessel(VertexHelper vh, Serie serie)
- {
- var vessel = chart.GetVessel(serie.vesselIndex);
- if (vessel != null)
- {
- switch (vessel.shape)
- {
- case Vessel.Shape.Circle:
- DrawCirleVessel(vh, vessel);
- break;
- case Vessel.Shape.Rect:
- DrawRectVessel(vh, vessel);
- break;
- default:
- DrawCirleVessel(vh, vessel);
- break;
- }
- }
- }
- private void DrawCirleVessel(VertexHelper vh, Vessel vessel)
- {
- var cenPos = vessel.runtimeCenterPos;
- var radius = vessel.runtimeRadius;
- var serie = SeriesHelper.GetSerieByVesselIndex(chart.series, vessel.index);
- var vesselColor = VesselHelper.GetColor(vessel, serie, chart.theme, chart.m_LegendRealShowName);
- if (vessel.gap != 0)
- {
- UGL.DrawDoughnut(vh, cenPos, vessel.runtimeInnerRadius, vessel.runtimeInnerRadius + vessel.gap,
- vessel.backgroundColor, Color.clear, chart.settings.cicleSmoothness);
- }
- UGL.DrawDoughnut(vh, cenPos, radius - vessel.shapeWidth, radius, vesselColor, Color.clear,
- chart.settings.cicleSmoothness);
- }
- private void DrawRectVessel(VertexHelper vh, Vessel vessel)
- {
- var serie = SeriesHelper.GetSerieByVesselIndex(chart.series, vessel.index);
- var vesselColor = VesselHelper.GetColor(vessel, serie, chart.theme, chart.m_LegendRealShowName);
- if (vessel.gap != 0)
- {
- UGL.DrawBorder(vh, vessel.runtimeCenterPos, vessel.runtimeWidth,
- vessel.runtimeHeight, vessel.gap, vessel.backgroundColor, 0, vessel.cornerRadius);
- }
- UGL.DrawBorder(vh, vessel.runtimeCenterPos, vessel.runtimeWidth + 2 * vessel.gap,
- vessel.runtimeHeight + 2 * vessel.gap, vessel.shapeWidth, vesselColor, 0, vessel.cornerRadius);
- }
- private void DrawLiquid(VertexHelper vh, Serie serie)
- {
- if (!serie.show) return;
- if (serie.animation.HasFadeOut()) return;
- var vessel = chart.GetVessel(serie.vesselIndex);
- if (vessel == null) return;
- switch (vessel.shape)
- {
- case Vessel.Shape.Circle:
- DrawCirleLiquid(vh, serie, vessel);
- break;
- case Vessel.Shape.Rect:
- DrawRectLiquid(vh, serie, vessel);
- break;
- default:
- DrawCirleLiquid(vh, serie, vessel);
- break;
- }
- }
- private void DrawCirleLiquid(VertexHelper vh, Serie serie, Vessel vessel)
- {
- var cenPos = vessel.runtimeCenterPos;
- var radius = vessel.runtimeInnerRadius;
- var serieData = serie.GetSerieData(0);
- if (serieData == null) return;
- var dataChangeDuration = serie.animation.GetUpdateAnimationDuration();
- var value = serieData.GetCurrData(1, dataChangeDuration);
- if (serie.runtimeCheckValue != value)
- {
- serie.runtimeCheckValue = value;
- m_UpdateLabelText = true;
- }
- if (serieData.labelPosition != cenPos)
- {
- serieData.labelPosition = cenPos;
- m_UpdateLabelText = true;
- }
- if (value <= 0) return;
- var colorIndex = chart.m_LegendRealShowName.IndexOf(serie.name);
- var realHig = (float)((value - serie.min) / (serie.max - serie.min) * radius * 2);
- serie.animation.InitProgress(1, 0, realHig);
- var hig = serie.animation.IsFinish() ? realHig : serie.animation.GetCurrDetail();
- var a = Mathf.Abs(radius - hig + (hig > radius ? serie.waveHeight : -serie.waveHeight));
- var diff = Mathf.Sqrt(radius * radius - Mathf.Pow(a, 2));
- var color = SerieHelper.GetItemColor(serie, serieData, chart.theme, colorIndex, false);
- var toColor = SerieHelper.GetItemToColor(serie, serieData, chart.theme, colorIndex, false);
- var isNeedGradient = !ChartHelper.IsValueEqualsColor(color, toColor);
- var isFull = hig >= 2 * radius;
- if (hig >= 2 * radius) hig = 2 * radius;
- if (isFull && !isNeedGradient)
- {
- UGL.DrawCricle(vh, cenPos, radius, toColor, chart.settings.cicleSmoothness);
- }
- else
- {
- var startY = cenPos.y - radius + hig;
- var waveStartPos = new Vector3(cenPos.x - diff, startY);
- var waveEndPos = new Vector3(cenPos.x + diff, startY);
- var startX = hig > radius ? cenPos.x - radius : waveStartPos.x;
- var endX = hig > radius ? cenPos.x + radius : waveEndPos.x;
- var step = vessel.smoothness;
- if (step < 0.5f) step = 0.5f;
- var lup = hig > radius ? new Vector3(cenPos.x - radius, cenPos.y) : waveStartPos;
- var ldp = lup;
- var nup = Vector3.zero;
- var ndp = Vector3.zero;
- var angle = 0f;
- serie.runtimeWaveSpeed += serie.waveSpeed * Time.deltaTime;
- var isStarted = false;
- var isEnded = false;
- var waveHeight = isFull ? 0 : serie.waveHeight;
- while (startX < endX)
- {
- startX += step;
- if (startX > endX) startX = endX;
- if (startX > waveStartPos.x && !isStarted)
- {
- startX = waveStartPos.x;
- isStarted = true;
- }
- if (startX > waveEndPos.x && !isEnded)
- {
- startX = waveEndPos.x;
- isEnded = true;
- }
- var py = Mathf.Sqrt(Mathf.Pow(radius, 2) - Mathf.Pow(Mathf.Abs(cenPos.x - startX), 2));
- if (startX < waveStartPos.x || startX > waveEndPos.x)
- {
- nup = new Vector3(startX, cenPos.y + py);
- }
- else
- {
- var py2 = waveHeight * Mathf.Sin(1 / serie.waveLength * angle + serie.runtimeWaveSpeed + serie.waveOffset);
- var nupY = waveStartPos.y + py2;
- if (nupY > cenPos.y + py) nupY = cenPos.y + py;
- else if (nupY < cenPos.y - py) nupY = cenPos.y - py;
- nup = new Vector3(startX, nupY);
- angle += step;
- }
- ndp = new Vector3(startX, cenPos.y - py);
- if (!ChartHelper.IsValueEqualsColor(color, toColor))
- {
- var colorMin = cenPos.y - radius;
- var colorMax = startY + serie.waveHeight;
- var tcolor1 = Color32.Lerp(color, toColor, 1 - (lup.y - colorMin) / (colorMax - colorMin));
- var tcolor2 = Color32.Lerp(color, toColor, 1 - (ldp.y - colorMin) / (colorMax - colorMin));
- UGL.DrawQuadrilateral(vh, lup, nup, ndp, ldp, tcolor1, tcolor2);
- }
- else
- {
- UGL.DrawQuadrilateral(vh, lup, nup, ndp, ldp, color);
- }
- lup = nup;
- ldp = ndp;
- }
- }
- if (serie.waveSpeed != 0 && Application.isPlaying && !isFull)
- {
- chart.RefreshPainter(serie);
- }
- if (!serie.animation.IsFinish())
- {
- serie.animation.CheckProgress(realHig);
- chart.m_IsPlayingAnimation = true;
- chart.RefreshPainter(serie);
- }
- }
- private void DrawRectLiquid(VertexHelper vh, Serie serie, Vessel vessel)
- {
- var cenPos = vessel.runtimeCenterPos;
- var serieData = serie.GetSerieData(0);
- if (serieData == null) return;
- var dataChangeDuration = serie.animation.GetUpdateAnimationDuration();
- var value = serieData.GetCurrData(1, dataChangeDuration);
- if (serie.runtimeCheckValue != value)
- {
- serie.runtimeCheckValue = value;
- m_UpdateLabelText = true;
- }
- if (serieData.labelPosition != cenPos)
- {
- serieData.labelPosition = cenPos;
- m_UpdateLabelText = true;
- }
- if (value <= 0) return;
- var colorIndex = chart.m_LegendRealShowName.IndexOf(serie.name);
- var realHig = (value - serie.min) / (serie.max - serie.min) * vessel.runtimeHeight;
- serie.animation.InitProgress(1, 0, (float)realHig);
- var hig = serie.animation.IsFinish() ? realHig : serie.animation.GetCurrDetail();
- var color = SerieHelper.GetItemColor(serie, serieData, chart.theme, colorIndex, false);
- var toColor = SerieHelper.GetItemToColor(serie, serieData, chart.theme, colorIndex, false);
- var isNeedGradient = !ChartHelper.IsValueEqualsColor(color, toColor);
- var isFull = hig >= vessel.runtimeHeight;
- if (hig >= vessel.runtimeHeight) hig = vessel.runtimeHeight;
- if (isFull && !isNeedGradient)
- {
- UGL.DrawRectangle(vh, cenPos, vessel.runtimeWidth / 2, vessel.runtimeHeight / 2, toColor);
- }
- else
- {
- var startY = (float)(cenPos.y - vessel.runtimeHeight / 2 + hig);
- var waveStartPos = new Vector3(cenPos.x - vessel.runtimeWidth / 2, startY);
- var waveEndPos = new Vector3(cenPos.x + vessel.runtimeWidth / 2, startY);
- var startX = waveStartPos.x;
- var endX = waveEndPos.x;
- var step = vessel.smoothness;
- if (step < 0.5f) step = 0.5f;
- var lup = waveStartPos;
- var ldp = new Vector3(startX, vessel.runtimeCenterPos.y - vessel.runtimeHeight / 2);
- var nup = Vector3.zero;
- var ndp = Vector3.zero;
- var angle = 0f;
- var isStarted = false;
- var isEnded = false;
- var waveHeight = isFull ? 0 : serie.waveHeight;
- serie.runtimeWaveSpeed += serie.waveSpeed * Time.deltaTime;
- while (startX < endX)
- {
- startX += step;
- if (startX > endX) startX = endX;
- if (startX > waveStartPos.x && !isStarted)
- {
- startX = waveStartPos.x;
- isStarted = true;
- }
- if (startX > waveEndPos.x && !isEnded)
- {
- startX = waveEndPos.x;
- isEnded = true;
- }
- var py = Mathf.Sqrt(Mathf.Pow(vessel.runtimeHeight, 2) - Mathf.Pow(Mathf.Abs(cenPos.x - startX), 2));
- if (startX < waveStartPos.x || startX > waveEndPos.x)
- {
- nup = new Vector3(startX, cenPos.y + py);
- }
- else
- {
- var py2 = waveHeight * Mathf.Sin(1 / serie.waveLength * angle + serie.runtimeWaveSpeed + serie.waveOffset);
- var nupY = waveStartPos.y + py2;
- nup = new Vector3(startX, nupY);
- angle += step;
- }
- ndp = new Vector3(startX, cenPos.y - vessel.runtimeHeight / 2);
- if (nup.y > cenPos.y + vessel.runtimeHeight / 2)
- {
- nup.y = cenPos.y + vessel.runtimeHeight / 2;
- }
- if (nup.y < cenPos.y - vessel.runtimeHeight / 2)
- {
- nup.y = cenPos.y - vessel.runtimeHeight / 2;
- }
- if (!ChartHelper.IsValueEqualsColor(color, toColor))
- {
- var colorMin = cenPos.y - vessel.runtimeHeight;
- var colorMax = startY + serie.waveHeight;
- var tcolor1 = Color32.Lerp(color, toColor, 1 - (lup.y - colorMin) / (colorMax - colorMin));
- var tcolor2 = Color32.Lerp(color, toColor, 1 - (ldp.y - colorMin) / (colorMax - colorMin));
- UGL.DrawQuadrilateral(vh, lup, nup, ndp, ldp, tcolor1, tcolor2);
- }
- else
- {
- UGL.DrawQuadrilateral(vh, lup, nup, ndp, ldp, color);
- }
- lup = nup;
- ldp = ndp;
- }
- }
- if (serie.waveSpeed != 0 && Application.isPlaying && !isFull)
- {
- chart.RefreshPainter(serie);
- }
- if (!serie.animation.IsFinish())
- {
- serie.animation.CheckProgress(realHig);
- chart.m_IsPlayingAnimation = true;
- chart.RefreshPainter(serie);
- }
- }
- }
- }
|