/// <summary>Hit test this order</summary> public override ChartControl.HitTestResult.Hit HitTest(PointF chart_point, Point client_point, Keys modifier_keys, View3d.CameraControls cam) { // Chart elements can only be modified when selected // Chart elements can only be selected when control is held down // Only visible to hit if selected if (!Selected) { return(null); } // This Order graphic is hit if the mouse is over one of the control lines. // Hit testing is done in client space so we can use pixel tolerance Func <EHitSite, ChartControl.HitTestResult.Hit> Hit = site => { var elem_point = m4x4.Invert(Position) * new v4(chart_point, 0f, 1f); return(new ChartControl.HitTestResult.Hit(this, new PointF(elem_point.x, elem_point.y), site)); }; const int tolerance = 5; // Test the price levels if (Order.State.CanMoveEntryPrice()) { var client_ep = Chart.ChartToClient(new PointF((float)Chart.XAxis.Min, (float)Order.EntryPrice)); if (Math.Abs(client_ep.Y - client_point.Y) < tolerance) { return(Hit(EHitSite.EntryPrice)); } } if (Order.State.CanMoveStopLoss()) { var client_sl = Chart.ChartToClient(new PointF((float)Chart.XAxis.Min, (float)Order.StopLossAbs)); if (Math.Abs(client_sl.Y - client_point.Y) < tolerance) { return(Hit(EHitSite.StopLoss)); } } if (Order.State.CanMoveTakeProfit()) { var client_tp = Chart.ChartToClient(new PointF((float)Chart.XAxis.Min, (float)Order.TakeProfitAbs)); if (Math.Abs(client_tp.Y - client_point.Y) < tolerance) { return(Hit(EHitSite.TakeProfit)); } } // If the order is still modifiable, test the entry/exit times if (Order.State.CanMoveEntryTime()) { var x = Instrument.ChartXValue(new TFTime(Order.EntryTimeUTC, Instrument.TimeFrame)); var client_x = Chart.ChartToClient(new PointF((float)x, (float)Chart.YAxis.Min)); if (Math.Abs(client_x.X - client_point.X) < tolerance) { return(Hit(EHitSite.EntryTime)); } } if (Order.State.CanMoveExpiry()) { var x = Instrument.ChartXValue(new TFTime(Order.ExitTimeUTC, Instrument.TimeFrame)); var client_x = Chart.ChartToClient(new PointF((float)x, (float)Chart.YAxis.Min)); if (Math.Abs(client_x.X - client_point.X) < tolerance) { return(Hit(EHitSite.Expiry)); } } // No hit... return(null); }
/// <summary>Perform a hit test on this object. Returns null for no hit. 'point' is in client space because typically hit testing uses pixel tolerances</summary> public override ChartControl.HitTestResult.Hit HitTest(PointF chart_point, Point client_point, Keys modifier_keys, View3d.CameraControls cam) { // Find the nearest point to 'client_point' on the line var chart_pt = chart_point; var client_pt = Chart.ChartToClient(new PointF(chart_pt.X, (float)Settings.Price)); // If clicked within 'tolerance' of the line const int tolerance = 3; if (Math.Abs(client_pt.Y - client_point.Y) < tolerance) { return(new ChartControl.HitTestResult.Hit(this, new PointF(chart_pt.X, (float)Settings.Price), null)); } return(null); }