void IRenderLoopHost.OnRenderLoop_PrepareRendering(EngineDevice engineDevice) { if ((m_targetPanel != null) && (m_renderLoop != null) && (m_renderLoop.Camera != null) && (m_swapChain != null)) { // Update swap chain scaling (only relevant for SwapChainPanel targets) // see https://www.packtpub.com/books/content/integrating-direct3d-xaml-and-windows-81 if (m_compositionScaleChanged && m_targetPanel.CompositionRescalingNeeded) { m_compositionScaleChanged = false; DXGI.SwapChain2 swapChain2 = m_swapChain.QueryInterfaceOrNull <DXGI.SwapChain2>(); if (swapChain2 != null) { try { SharpDX.Matrix3x2 inverseScale = new SharpDX.Matrix3x2(); inverseScale.M11 = 1.0f / (float)m_targetPanel.CompositionScaleX; inverseScale.M22 = 1.0f / (float)m_targetPanel.CompositionScaleY; swapChain2.MatrixTransform = inverseScale; } finally { swapChain2.Dispose(); } } } } }
public void Begin(Microsoft.Xna.Framework.Matrix transform) { var mt3x2 = new SharpDX.Matrix3x2(transform.M11, transform.M12, transform.M21, transform.M22, transform.M41, transform.M42); this.Begin(mt3x2); }
public static Matrix FromSharpDX(this Matrix3x2 matrix) { return(new Matrix( (float)matrix.M11, (float)matrix.M12, (float)matrix.M21, (float)matrix.M22, (float)matrix.M31, (float)matrix.M32)); }
private void Begin(SharpDX.Matrix3x2 transform) { this.context = D2DFactory.Instance.GetContext(this.GraphicsDevice); if (this.context == null) { throw new Exception("Create D2D context failed."); } this.context.D2DRenderTarget.Transform = transform; this.context.D2DRenderTarget.BeginDraw(); this.IsBeginEndPair = true; }
public static System.Drawing.Drawing2D.Matrix ConvertMatrix3x2ToMatrix(ref SharpDX.Matrix3x2 m) { return(new System.Drawing.Drawing2D.Matrix(m.M11, m.M12, m.M21, m.M22, m.M31, m.M32)); }
public override void OnRender(ChartControl chartControl, ChartScale chartScale) { if (firstTime && DrawingState == DrawingState.Normal) { firstTime = false; Cbi.License.Log("Path"); } RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive; Stroke outlineStroke = OutlineStroke; outlineStroke.RenderTarget = RenderTarget; ChartPanel chartPanel = chartControl.ChartPanels[PanelIndex]; double strokePixAdjust = outlineStroke.Width % 2 == 0 ? 0.5d : 0d; Vector pixelAdjustVec = new Vector(strokePixAdjust, strokePixAdjust); SharpDX.Direct2D1.PathGeometry polyGeo = CreatePathGeometry(chartControl, chartPanel, chartScale, strokePixAdjust); SharpDX.Direct2D1.Brush tmpBrush = IsInHitTest ? chartControl.SelectionBrush : outlineStroke.BrushDX; RenderTarget.DrawGeometry(polyGeo, tmpBrush, outlineStroke.Width, outlineStroke.StrokeStyle); polyGeo.Dispose(); if (PathBegin == PathToolCapMode.Arrow || PathEnd == PathToolCapMode.Arrow) { Point[] points = GetPathAnchorPoints(chartControl, chartScale); if (points.Length > 1) { if (arrowPathGeometry == null) { arrowPathGeometry = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFactory); SharpDX.Direct2D1.GeometrySink geometrySink = arrowPathGeometry.Open(); float arrowWidth = 6f; SharpDX.Vector2 top = new SharpDX.Vector2(0, outlineStroke.Width * 0.5f); geometrySink.BeginFigure(top, SharpDX.Direct2D1.FigureBegin.Filled); geometrySink.AddLine(new SharpDX.Vector2(arrowWidth, -arrowWidth)); geometrySink.AddLine(new SharpDX.Vector2(-arrowWidth, -arrowWidth)); geometrySink.AddLine(top); // cap off figure geometrySink.EndFigure(SharpDX.Direct2D1.FigureEnd.Closed); geometrySink.Close(); } if (PathBegin == PathToolCapMode.Arrow) { Vector lineVector = points[0] - points[1]; lineVector.Normalize(); Point pointAdjusted = points[0] + pixelAdjustVec; SharpDX.Vector2 pointVec = pointAdjusted.ToVector2(); float vectorAngle = -(float)Math.Atan2(lineVector.X, lineVector.Y); Vector adjustVector = lineVector * 5; SharpDX.Vector2 arrowPointVec = new SharpDX.Vector2((float)(pointVec.X + adjustVector.X), (float)(pointVec.Y + adjustVector.Y)); SharpDX.Matrix3x2 transformMatrix2 = SharpDX.Matrix3x2.Rotation(vectorAngle, SharpDX.Vector2.Zero) * SharpDX.Matrix3x2.Scaling((float)Math.Max(1.0f, outlineStroke.Width * .45) + 0.25f) * SharpDX.Matrix3x2.Translation(arrowPointVec); RenderTarget.Transform = transformMatrix2; RenderTarget.FillGeometry(arrowPathGeometry, tmpBrush); RenderTarget.Transform = SharpDX.Matrix3x2.Identity; } if (PathEnd == PathToolCapMode.Arrow) { Vector lineVector = points[points.Length - 1] - points[points.Length - 2]; lineVector.Normalize(); Point pointAdjusted = points[points.Length - 1] + pixelAdjustVec; SharpDX.Vector2 pointVec = pointAdjusted.ToVector2(); float vectorAngle = -(float)Math.Atan2(lineVector.X, lineVector.Y); Vector adjustVector = lineVector * 5; SharpDX.Vector2 arrowPointVec = new SharpDX.Vector2((float)(pointVec.X + adjustVector.X), (float)(pointVec.Y + adjustVector.Y)); SharpDX.Matrix3x2 transformMatrix2 = SharpDX.Matrix3x2.Rotation(vectorAngle, SharpDX.Vector2.Zero) * SharpDX.Matrix3x2.Scaling((float)Math.Max(1.0f, outlineStroke.Width * .45) + 0.25f) * SharpDX.Matrix3x2.Translation(arrowPointVec); RenderTarget.Transform = transformMatrix2; RenderTarget.FillGeometry(arrowPathGeometry, tmpBrush); RenderTarget.Transform = SharpDX.Matrix3x2.Identity; } } } if (ShowCount) { SimpleFont wpfFont = chartControl.Properties.LabelFont ?? new SimpleFont(); SharpDX.DirectWrite.TextFormat textFormat = wpfFont.ToDirectWriteTextFormat(); textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading; textFormat.WordWrapping = SharpDX.DirectWrite.WordWrapping.NoWrap; for (int i = 1; i < ChartAnchors.Count; i++) { Point p = ChartAnchors[i - 1].GetPoint(chartControl, chartPanel, chartScale); Point p1 = ChartAnchors[i].GetPoint(chartControl, chartPanel, chartScale); if (i + 1 < ChartAnchors.Count) { Point p2 = ChartAnchors[i + 1].GetPoint(chartControl, chartPanel, chartScale); Vector v1 = p - p1; v1.Normalize(); Vector v2 = p2 - p1; v2.Normalize(); Vector vector = v1 + v2; vector.Normalize(); SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, i.ToString(), textFormat, 250, textFormat.FontSize); Point textPoint = p1 - vector * textFormat.FontSize; textPoint.X -= textLayout.Metrics.Width / 2f; textPoint.Y -= textLayout.Metrics.Height / 2f; RenderTarget.DrawTextLayout((textPoint + pixelAdjustVec).ToVector2(), textLayout, outlineStroke.BrushDX, SharpDX.Direct2D1.DrawTextOptions.NoSnap); textLayout.Dispose(); } else { SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, i.ToString(), textFormat, 250, textFormat.FontSize); Vector vector = (p - p1); vector.Normalize(); Point textPoint = p1 - vector * textFormat.FontSize; textPoint.X -= textLayout.Metrics.Width / 2f; textPoint.Y -= textLayout.Metrics.Height / 2f; RenderTarget.DrawTextLayout((textPoint + pixelAdjustVec).ToVector2(), textLayout, outlineStroke.BrushDX, SharpDX.Direct2D1.DrawTextOptions.NoSnap); textLayout.Dispose(); } } textFormat.Dispose(); } }
public override void OnRender(ChartControl chartControl, ChartScale chartScale) { RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive; ChartPanel panel = chartControl.ChartPanels[PanelIndex]; Point anchorPoint = Anchor.GetPoint(chartControl, panel, chartScale); Point lastEndPoint = new Point(0, 0); SharpDX.Direct2D1.Brush lastBrush = null; foreach (GannAngle gannAngle in GannAngles.Where(ga => ga.IsVisible && ga.Stroke != null).OrderBy(ga => (ga.RatioX / ga.RatioY))) { gannAngle.Stroke.RenderTarget = RenderTarget; double dx = gannAngle.RatioX * chartControl.Properties.BarDistance; double dVal = gannAngle.RatioY * PointsPerBar; //NT7, just multiple directly this is price not pixels //chartScale.GetPixelsForDistance(gannAngle.RatioY * PointsPerBar); Vector gannDataVector = GetGannStepDataVector(dx, dVal); Point extendedEndPoint = CalculateExtendedDataPoint(panel, chartScale, Convert.ToInt32(anchorPoint.X), Anchor.Price, gannDataVector); // align to full pixel to avoid unneeded aliasing double strokePixAdj = ((double)(gannAngle.Stroke.Width % 2)).ApproxCompare(0) == 0 ? 0.5d : 0d; Vector pixelAdjustVec = new Vector(0, strokePixAdj); SharpDX.Direct2D1.Brush tmpBrush = IsInHitTest ? chartControl.SelectionBrush : gannAngle.Stroke.BrushDX; RenderTarget.DrawLine((anchorPoint + pixelAdjustVec).ToVector2(), (extendedEndPoint + pixelAdjustVec).ToVector2(), tmpBrush, gannAngle.Stroke.Width, gannAngle.Stroke.StrokeStyle); if (lastBrush != null) { float oldOpacity = lastBrush.Opacity; lastBrush.Opacity = PriceLevelOpacity / 100f; // create geometry SharpDX.Direct2D1.PathGeometry lineGeometry = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFactory); SharpDX.Direct2D1.GeometrySink sink = lineGeometry.Open(); sink.BeginFigure(lastEndPoint.ToVector2(), SharpDX.Direct2D1.FigureBegin.Filled); // Does the fill color need to fill a corner? Check and add a point if (Math.Abs(lastEndPoint.Y - extendedEndPoint.Y) > 0.1 && Math.Abs(lastEndPoint.X - extendedEndPoint.X) > 0.1) { double boundaryX; double boundaryY; if (lastEndPoint.Y <= ChartPanel.Y || lastEndPoint.Y >= ChartPanel.Y + ChartPanel.H) { if (FanDirection == GannFanDirection.UpLeft || FanDirection == GannFanDirection.UpRight) { boundaryY = extendedEndPoint.Y; boundaryX = lastEndPoint.X; } else { boundaryY = lastEndPoint.Y; boundaryX = extendedEndPoint.X; } } else { if (FanDirection == GannFanDirection.UpLeft || FanDirection == GannFanDirection.UpRight) { boundaryY = lastEndPoint.Y; boundaryX = extendedEndPoint.X; } else { boundaryY = extendedEndPoint.Y; boundaryX = lastEndPoint.X; } } sink.AddLine(new SharpDX.Vector2((float)boundaryX, (float)boundaryY)); } sink.AddLine(extendedEndPoint.ToVector2()); sink.AddLine((anchorPoint + pixelAdjustVec).ToVector2()); sink.AddLine((lastEndPoint).ToVector2()); sink.EndFigure(SharpDX.Direct2D1.FigureEnd.Closed); sink.Close(); RenderTarget.FillGeometry(lineGeometry, lastBrush); lineGeometry.Dispose(); lastBrush.Opacity = oldOpacity; } lastEndPoint = extendedEndPoint + pixelAdjustVec; lastBrush = tmpBrush; } if (!IsTextDisplayed || IsInHitTest) { return; } foreach (GannAngle gannAngle in GannAngles.Where(ga => ga.IsVisible && ga.Stroke != null).OrderBy(ga => (ga.RatioX / ga.RatioY))) { gannAngle.Stroke.RenderTarget = RenderTarget; double dx = gannAngle.RatioX * chartControl.Properties.BarDistance; double dVal = gannAngle.RatioY * PointsPerBar; //NT7, just multiple directly this is price not pixels //chartScale.GetPixelsForDistance(gannAngle.RatioY * PointsPerBar); Vector gannDataVector = GetGannStepDataVector(dx, dVal); Point extendedEndPoint = CalculateExtendedDataPoint(panel, chartScale, Convert.ToInt32(anchorPoint.X), Anchor.Price, gannDataVector); if (!IsTextDisplayed || IsInHitTest) { continue; } SimpleFont wpfFont = chartControl.Properties.LabelFont ?? new SimpleFont(); SharpDX.DirectWrite.TextFormat textFormat = wpfFont.ToDirectWriteTextFormat(); textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading; textFormat.WordWrapping = SharpDX.DirectWrite.WordWrapping.NoWrap; SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(Core.Globals.DirectWriteFactory, gannAngle.Name, textFormat, 100, textFormat.FontSize); // once text is laid out, update used width to calcuated space required float fontHeight = textLayout.Metrics.Height; Point textEndPoint = new Point(extendedEndPoint.X, extendedEndPoint.Y); if (textEndPoint.X > panel.X + panel.W - textLayout.Metrics.Width) { textEndPoint.X = panel.X + panel.W - textLayout.Metrics.Width; textEndPoint.Y += textLayout.Metrics.Width; } if (gannDataVector.Y > 0) { if (textEndPoint.Y < panel.Y + (fontHeight * 0.5)) { textEndPoint.Y = panel.Y + (fontHeight * 0.5); } } else { if (textEndPoint.Y > panel.Y + panel.H - (fontHeight * 1.5)) { textEndPoint.Y = panel.Y + panel.H - (fontHeight * 1.5); } } float?marginResource = Application.Current.FindResource("FontModalTitleMargin") as float?; float margin = 2f + (marginResource.HasValue ? marginResource.Value : 3f); // Allow for changes in X position based on whether text is aligned to left or right edge of screen float marginX = FanDirection == GannFanDirection.DownLeft || FanDirection == GannFanDirection.UpLeft ? margin : -2 * margin; SharpDX.Vector2 endVec = new SharpDX.Vector2((float)textEndPoint.X, (float)textEndPoint.Y); SharpDX.Matrix3x2 transformMatrix = SharpDX.Matrix3x2.Translation(endVec); RenderTarget.Transform = transformMatrix; RenderTarget.DrawTextLayout(new SharpDX.Vector2(marginX + margin, margin), textLayout, gannAngle.Stroke.BrushDX, SharpDX.Direct2D1.DrawTextOptions.NoSnap); RenderTarget.Transform = SharpDX.Matrix3x2.Identity; textFormat.Dispose(); textLayout.Dispose(); } }
protected override void SetTransform(Matrix matrix) { _transformMatrix = new SharpDX.Matrix3x2(matrix.Elements); _renderTarget.Transform = _transformMatrix * _pageUnitMatrix; }