using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MdxWpfInteroperability;
namespace MdxWpfInteroperability.Example
{
/// <summary>
/// Interaction logic for InteroperabilityExample.xaml
/// </summary>
public partial class InteroperabilityExample : Window
{
private double surfaceWidth = 2000;
private double surfaceHeight = 1500;
public InteroperabilityExample()
{
InitializeComponent();
mdxHost.D3dSurfaceWidth = surfaceWidth;
mdxHost.D3dSurfaceHeight = surfaceHeight;
mdxHost.Width = surfaceWidth * zoomSlider.Value;
mdxHost.Height = surfaceHeight * zoomSlider.Value;
}
private void RenderScene()
{
float circleCenterX = 200;
float circleCenterY = 300;
float circleRadius = 50;
// Clear the surface.
mdxHost.D3dDevice.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target, ColorToInt(Colors.DarkGray), 1.0f, 0);
// Draw a circle.
Render2dCircle(circleCenterX, circleCenterY, circleRadius, Colors.Red, mdxHost.D3dDevice); // Stroke
Render2dCircle(circleCenterX, circleCenterY, circleRadius - 3, Colors.DarkRed, mdxHost.D3dDevice); // Fill
Render2dCircle(circleCenterX, circleCenterY, 5, Colors.Salmon, mdxHost.D3dDevice); // Center indication
// Draw a text that presents the circle's center position.
Render2dText(string.Format("Circle center: ({0},{1})", circleCenterX, circleCenterY),
(int)(circleCenterX - circleRadius), (int)(circleCenterY + circleRadius + 10),
36f, Colors.White, mdxHost.D3dDevice);
// Present the scene on the D3dHost control.
mdxHost.InvalidateD3dRegion();
}
public void Render2dCircle(float centerX, float centerY, float radius, Color color,
Microsoft.DirectX.Direct3D.Device device)
{
int convertedColor = ColorToInt(color);
int numOfPoints = (int)radius;
if (numOfPoints < 10)
{
numOfPoints = 10;
}
Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[] vertices =
new Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[numOfPoints + 2];
vertices[0].Position = new Microsoft.DirectX.Vector4(centerX, centerY, 0, 1.0f);
vertices[0].Color = convertedColor;
double partAngle = Math.PI * 2 / numOfPoints;
for (int vertexInx = 0; vertexInx <= numOfPoints; vertexInx++)
{
double currAngle = vertexInx * partAngle;
float currX = (float)(centerX + radius * Math.Cos(currAngle));
float currY = (float)(centerY + radius * Math.Sin(currAngle));
vertices[vertexInx + 1].Position =
new Microsoft.DirectX.Vector4(currX, currY, 0, 1.0f);
vertices[vertexInx + 1].Color = convertedColor;
}
device.BeginScene();
device.VertexFormat = Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored.Format;
device.DrawUserPrimitives(Microsoft.DirectX.Direct3D.PrimitiveType.TriangleFan, numOfPoints, vertices);
device.EndScene();
}
public void Render2dText(string text, int x, int y, float fontSize, Color color, Microsoft.DirectX.Direct3D.Device device)
{
System.Drawing.Font systemfont = new System.Drawing.Font("Arial", fontSize, System.Drawing.FontStyle.Regular);
Microsoft.DirectX.Direct3D.Font d3dFont = new Microsoft.DirectX.Direct3D.Font(mdxHost.D3dDevice, systemfont);
device.BeginScene();
d3dFont.DrawText(null, text, new System.Drawing.Point(x, y),
System.Drawing.Color.FromArgb(ColorToInt(color)));
device.EndScene();
d3dFont.Dispose();
}
private int ColorToInt(Color color)
{
return (int)color.A << 24 | (int)color.R << 16 | (int)color.G << 8 | (int)color.B;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
RenderScene();
}
private void zoomSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!IsLoaded)
{
return;
}
mdxHost.Width = surfaceWidth * zoomSlider.Value;
mdxHost.Height = surfaceHeight * zoomSlider.Value;
}
private void mdxHost_D3dSurfaceMouseLeave(object sender, D3dSurfaceMouseEventArgs e)
{
surfaceMousePosition.Text = "Out of surface";
}
private void mdxHost_D3dSurfaceMouseMove(object sender, D3dSurfaceMouseEventArgs e)
{
surfaceMousePosition.Text = string.Format("({0},{1})",
e.D3dSurfaceMousePosition.X, e.D3dSurfaceMousePosition.Y);
}
}
}