diff --git a/CustomWidgets/DataViewGraph.cs b/CustomWidgets/DataViewGraph.cs index 4c16ae9c9..644be72ef 100644 --- a/CustomWidgets/DataViewGraph.cs +++ b/CustomWidgets/DataViewGraph.cs @@ -1,4 +1,6 @@ using MatterHackers.Agg; +using MatterHackers.Agg.Transform; +using MatterHackers.Agg.UI; using MatterHackers.Agg.VertexSource; using MatterHackers.VectorMath; using System; @@ -6,35 +8,34 @@ using System.Collections.Generic; namespace Gaming.Game { - public class DataViewGraph + public class DataViewGraph : WindowWidget { private RGBA_Floats SentDataLineColor = new RGBA_Floats(200, 200, 0); private RGBA_Floats ReceivedDataLineColor = new RGBA_Floats(0, 200, 20); private RGBA_Floats BoxColor = new RGBA_Floats(10, 25, 240); - private double m_DataViewMinY; - private double m_DataViewMaxY; - private bool m_DynamiclyScaleYRange; - private Vector2 m_Position; - private uint m_Width; - private uint m_Height; - private Dictionary m_DataHistoryArray; - private int m_ColorIndex; - private PathStorage m_LinesToDraw; + private double valueMin; + private double valueMax; + private bool dynamiclyScaleRange; + private uint graphWidth; + private uint graphHeight; + private Dictionary dataHistoryArray; + private int nextLineColorIndex; + private PathStorage linesToDrawStorage; internal class HistoryData { - private int m_Capacity; - private List m_Data; + private int capacity; + private List data; - internal double m_TotalValue; - internal RGBA_Bytes m_Color; + internal double currentDataSum; + internal RGBA_Bytes lineColor; - internal HistoryData(int Capacity, IColorType Color) + internal HistoryData(int capacity, IColorType lineColor) { - m_Color = Color.GetAsRGBA_Bytes(); - m_Capacity = Capacity; - m_Data = new List(); + this.lineColor = lineColor.GetAsRGBA_Bytes(); + this.capacity = capacity; + data = new List(); Reset(); } @@ -42,33 +43,33 @@ namespace Gaming.Game { get { - return m_Data.Count; + return data.Count; } } internal void Add(double Value) { - if (m_Data.Count == m_Capacity) + if (data.Count == capacity) { - m_TotalValue -= m_Data[0]; - m_Data.RemoveAt(0); + currentDataSum -= data[0]; + data.RemoveAt(0); } - m_Data.Add(Value); + data.Add(Value); - m_TotalValue += Value; + currentDataSum += Value; } internal void Reset() { - m_TotalValue = 0; - m_Data.Clear(); + currentDataSum = 0; + data.Clear(); } internal double GetItem(int ItemIndex) { - if (ItemIndex < m_Data.Count) + if (ItemIndex < data.Count) { - return m_Data[ItemIndex]; + return data[ItemIndex]; } else { @@ -79,11 +80,11 @@ namespace Gaming.Game internal double GetMaxValue() { double Max = -9999999999; - for (int i = 0; i < m_Data.Count; i++) + for (int i = 0; i < data.Count; i++) { - if (m_Data[i] > Max) + if (data[i] > Max) { - Max = m_Data[i]; + Max = data[i]; } } @@ -93,11 +94,11 @@ namespace Gaming.Game internal double GetMinValue() { double Min = 9999999999; - for (int i = 0; i < m_Data.Count; i++) + for (int i = 0; i < data.Count; i++) { - if (m_Data[i] < Min) + if (data[i] < Min) { - Min = m_Data[i]; + Min = data[i]; } } @@ -106,44 +107,44 @@ namespace Gaming.Game internal double GetAverageValue() { - return m_TotalValue / m_Data.Count; + return currentDataSum / data.Count; } }; - public DataViewGraph(Vector2 RenderPosition) - : this(RenderPosition, 80, 50, 0, 0) + public DataViewGraph() + : this(80, 50, 0, 0) { - m_DynamiclyScaleYRange = true; + dynamiclyScaleRange = true; } - public DataViewGraph(Vector2 RenderPosition, uint Width, uint Height) - : this(RenderPosition, Width, Height, 0, 0) + public DataViewGraph(uint Width, uint Height) + : this(Width, Height, 0, 0) { - m_DynamiclyScaleYRange = true; + dynamiclyScaleRange = true; } - public DataViewGraph(Vector2 RenderPosition, uint Width, uint Height, double StartMin, double StartMax) + public DataViewGraph(uint width, uint height, double valueMin, double valueMax) + : base(new RectangleDouble(0,0,width + 150,height + 80)) { - m_LinesToDraw = new PathStorage(); - m_DataHistoryArray = new Dictionary(); + linesToDrawStorage = new PathStorage(); + dataHistoryArray = new Dictionary(); - m_Width = Width; - m_Height = Height; - m_DataViewMinY = StartMin; - m_DataViewMaxY = StartMax; - if (StartMin == 0 && StartMax == 0) + graphWidth = width; + graphHeight = height; + this.valueMin = valueMin; + this.valueMax = valueMax; + if (valueMin == 0 && valueMax == 0) { - m_DataViewMaxY = -999999; - m_DataViewMinY = 999999; + this.valueMax = -999999; + this.valueMin = 999999; } - m_Position = RenderPosition; - m_DynamiclyScaleYRange = false; + dynamiclyScaleRange = false; } public double GetAverageValue(String DataType) { HistoryData TrendLine; - m_DataHistoryArray.TryGetValue(DataType, out TrendLine); + dataHistoryArray.TryGetValue(DataType, out TrendLine); if (TrendLine != null) { return TrendLine.GetAverageValue(); @@ -152,84 +153,83 @@ namespace Gaming.Game return 0; } - public void Draw(MatterHackers.Agg.Transform.ITransform Position, Graphics2D renderer) + public override void OnDraw(Graphics2D graphics2D) { - double TextHeight = m_Position.y - 20; - double Range = (m_DataViewMaxY - m_DataViewMinY); + double currentTextHeight = -20; + double Range = (valueMax - valueMin); VertexSourceApplyTransform TransformedLinesToDraw; Stroke StrockedTransformedLinesToDraw; - RoundedRect BackGround = new RoundedRect(m_Position.x, m_Position.y - 1, m_Position.x + m_Width, m_Position.y - 1 + m_Height + 2, 5); - VertexSourceApplyTransform TransformedBackGround = new VertexSourceApplyTransform(BackGround, Position); - renderer.Render(TransformedBackGround, new RGBA_Bytes(0, 0, 0, .5)); + Vector2 renderOffset = new Vector2(1, Height - graphHeight - 22); + RoundedRect backGround = new RoundedRect(renderOffset.x, renderOffset.y - 1, renderOffset.x + graphWidth, renderOffset.y - 1 + graphHeight + 2, 5); + graphics2D.Render(backGround, new RGBA_Bytes(0, 0, 0, .5)); // if the 0 line is within the window than draw it. - if (m_DataViewMinY < 0 && m_DataViewMaxY > 0) + if (valueMin < 0 && valueMax > 0) { - m_LinesToDraw.remove_all(); - m_LinesToDraw.MoveTo(m_Position.x, - m_Position.y + ((0 - m_DataViewMinY) * m_Height / Range)); - m_LinesToDraw.LineTo(m_Position.x + m_Width, - m_Position.y + ((0 - m_DataViewMinY) * m_Height / Range)); - TransformedLinesToDraw = new VertexSourceApplyTransform(m_LinesToDraw, Position); - StrockedTransformedLinesToDraw = new Stroke(TransformedLinesToDraw); - renderer.Render(StrockedTransformedLinesToDraw, new RGBA_Bytes(0, 0, 0, 1)); + linesToDrawStorage.remove_all(); + linesToDrawStorage.MoveTo(renderOffset.x, + renderOffset.y + ((0 - valueMin) * graphHeight / Range)); + linesToDrawStorage.LineTo(renderOffset.x + graphWidth, + renderOffset.y + ((0 - valueMin) * graphHeight / Range)); + StrockedTransformedLinesToDraw = new Stroke(linesToDrawStorage); + graphics2D.Render(StrockedTransformedLinesToDraw, new RGBA_Bytes(0, 0, 0, 1)); } double MaxMax = -999999999; double MinMin = 999999999; double MaxAverage = 0; - foreach (KeyValuePair historyKeyValue in m_DataHistoryArray) + foreach (KeyValuePair historyKeyValue in dataHistoryArray) { HistoryData history = historyKeyValue.Value; - m_LinesToDraw.remove_all(); + linesToDrawStorage.remove_all(); MaxMax = System.Math.Max(MaxMax, history.GetMaxValue()); MinMin = System.Math.Min(MinMin, history.GetMinValue()); MaxAverage = System.Math.Max(MaxAverage, history.GetAverageValue()); - for (int i = 0; i < m_Width - 1; i++) + for (int i = 0; i < graphWidth - 1; i++) { if (i == 0) { - m_LinesToDraw.MoveTo(m_Position.x + i, - m_Position.y + ((history.GetItem(i) - m_DataViewMinY) * m_Height / Range)); + linesToDrawStorage.MoveTo(renderOffset.x + i, + renderOffset.y + ((history.GetItem(i) - valueMin) * graphHeight / Range)); } else { - m_LinesToDraw.LineTo(m_Position.x + i, - m_Position.y + ((history.GetItem(i) - m_DataViewMinY) * m_Height / Range)); + linesToDrawStorage.LineTo(renderOffset.x + i, + renderOffset.y + ((history.GetItem(i) - valueMin) * graphHeight / Range)); } } - TransformedLinesToDraw = new VertexSourceApplyTransform(m_LinesToDraw, Position); - StrockedTransformedLinesToDraw = new Stroke(TransformedLinesToDraw); - renderer.Render(StrockedTransformedLinesToDraw, history.m_Color); + StrockedTransformedLinesToDraw = new Stroke(linesToDrawStorage); + graphics2D.Render(StrockedTransformedLinesToDraw, history.lineColor); String Text = historyKeyValue.Key + ": Min:" + MinMin.ToString("0.0") + " Max:" + MaxMax.ToString("0.0") + " Avg:" + MaxAverage.ToString("0.0"); - renderer.DrawString(Text, m_Position.x, TextHeight, backgroundColor: new RGBA_Bytes(RGBA_Bytes.White, 220)); - TextHeight -= 20; + graphics2D.DrawString(Text, renderOffset.x, renderOffset.y + currentTextHeight, backgroundColor: new RGBA_Bytes(RGBA_Bytes.White, 220), drawFromHintedCach: true); + currentTextHeight -= 20; } - RoundedRect BackGround2 = new RoundedRect(m_Position.x, m_Position.y - 1, m_Position.x + m_Width, m_Position.y - 1 + m_Height + 2, 5); - VertexSourceApplyTransform TransformedBackGround2 = new VertexSourceApplyTransform(BackGround2, Position); - Stroke StrockedTransformedBackGround = new Stroke(TransformedBackGround2); - renderer.Render(StrockedTransformedBackGround, new RGBA_Bytes(0.0, 0, 0, 1)); + RoundedRect BackGround2 = new RoundedRect(renderOffset.x, renderOffset.y - 1, renderOffset.x + graphWidth, renderOffset.y - 1 + graphHeight + 2, 5); + Stroke StrockedTransformedBackGround = new Stroke(BackGround2); + graphics2D.Render(StrockedTransformedBackGround, new RGBA_Bytes(0.0, 0, 0, 1)); //renderer.Color = BoxColor; //renderer.DrawRect(m_Position.x, m_Position.y - 1, m_Width, m_Height + 2); + + base.OnDraw(graphics2D); } public void AddData(String DataType, double NewData) { - if (m_DynamiclyScaleYRange) + if (dynamiclyScaleRange) { - m_DataViewMaxY = System.Math.Max(m_DataViewMaxY, NewData); - m_DataViewMinY = System.Math.Min(m_DataViewMinY, NewData); + valueMax = System.Math.Max(valueMax, NewData); + valueMin = System.Math.Min(valueMin, NewData); } - if (!m_DataHistoryArray.ContainsKey(DataType)) + if (!dataHistoryArray.ContainsKey(DataType)) { RGBA_Bytes LineColor = new RGBA_Bytes(255, 255, 255); - switch (m_ColorIndex++ % 3) + switch (nextLineColorIndex++ % 3) { case 0: LineColor = new RGBA_Bytes(255, 55, 55); @@ -244,17 +244,17 @@ namespace Gaming.Game break; } - m_DataHistoryArray.Add(DataType, new HistoryData((int)m_Width, LineColor)); + dataHistoryArray.Add(DataType, new HistoryData((int)graphWidth, LineColor)); } - m_DataHistoryArray[DataType].Add(NewData); + dataHistoryArray[DataType].Add(NewData); } public void Reset() { - m_DataViewMaxY = 1; - m_DataViewMinY = 99999; - foreach (KeyValuePair historyKeyValue in m_DataHistoryArray) + valueMax = 1; + valueMin = 99999; + foreach (KeyValuePair historyKeyValue in dataHistoryArray) { historyKeyValue.Value.Reset(); } diff --git a/MatterControlApplication.cs b/MatterControlApplication.cs index ffbc9579e..0c48eb334 100644 --- a/MatterControlApplication.cs +++ b/MatterControlApplication.cs @@ -51,6 +51,7 @@ using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using MatterHackers.GCodeVisualizer; +using Gaming.Game; namespace MatterHackers.MatterControl { @@ -67,7 +68,7 @@ namespace MatterHackers.MatterControl private int drawCount = 0; private bool firstDraw = true; private AverageMillisecondTimer millisecondTimer = new AverageMillisecondTimer(); - private Gaming.Game.DataViewGraph msGraph = new Gaming.Game.DataViewGraph(new Vector2(20, 500), 50, 50, 0, 200); + private DataViewGraph msGraph; private string savePartsSheetExitAnywayMessage = "You are currently saving a parts sheet, are you sure you want to exit?".Localize(); private bool ShowMemoryUsed = false; private Stopwatch totalDrawTime = new Stopwatch(); diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index c72cdd191..398c760db 100644 --- a/PrinterCommunication/PrinterConnectionAndCommunication.cs +++ b/PrinterCommunication/PrinterConnectionAndCommunication.cs @@ -175,7 +175,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication private List LinesToWriteQueue = new List(); - DataViewGraph sendTimeAfterOkGraph = new DataViewGraph(new Vector2(320, 500), 150, 150, 0, 30); + DataViewGraph sendTimeAfterOkGraph; private GCodeFile loadedGCode = new GCodeFileLoaded(); @@ -2894,13 +2894,11 @@ namespace MatterHackers.MatterControl.PrinterCommunication timeSinceRecievedOk.Stop(); if (!haveHookedDrawing) { - MatterControlApplication.Instance.DrawAfter += (sender, e) => - { - sendTimeAfterOkGraph.Draw(MatterHackers.Agg.Transform.Affine.NewIdentity(), e.graphics2D); - }; + sendTimeAfterOkGraph = new DataViewGraph(150, 150, 0, 30); + MatterControlApplication.Instance.AddChild(sendTimeAfterOkGraph); haveHookedDrawing = true; } - sendTimeAfterOkGraph.AddData("ms", timeSinceRecievedOk.ElapsedMilliseconds); + sendTimeAfterOkGraph.AddData("ok->send", timeSinceRecievedOk.ElapsedMilliseconds); } timeSinceLastWrite.Restart(); timeHaveBeenWaitingForOK.Restart();