2 * Copyright (C) 2013 FooProject
3 * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
6 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
12 using System.Collections.Generic;
13 using Windows.Graphics.Display;
14 using Windows.UI.Xaml.Media;
15 using Windows.UI.Xaml.Media.Imaging;
17 using DXGI = SharpDX.DXGI;
18 using D2D = SharpDX.Direct2D1;
19 using D3D = SharpDX.Direct3D;
20 using D3D11 = SharpDX.Direct3D11;
21 using FooEditEngine.UWP;
22 using Windows.UI.Text.Core;
23 using Windows.UI.Text;
25 namespace FooEditEngine
27 sealed class D2DRender : D2DRenderBase, IEditorRender
29 SurfaceImageSource SurfaceImage;
30 DXGI.ISurfaceImageSourceNative SurfaceImageNative;
32 D2D.DeviceContext D2DContext;
34 Size Size = new Size();
39 public D2DRender(FooTextBox textbox, Windows.UI.Xaml.Shapes.Rectangle rect)
42 this.ConstructDeviceResource(rect.ActualWidth, rect.ActualHeight);
43 this.InitTextFormat(textbox.FontFamily, textbox.FontSize);
45 this.Size.Width = rect.ActualWidth;
46 this.Size.Height = rect.ActualHeight;
48 this.CreateSurface(rect, rect.ActualWidth, rect.ActualHeight);
50 this.Foreground = D2DRenderBase.ToColor4(textbox.Foreground);
51 this.Background = D2DRenderBase.ToColor4(textbox.Background);
52 this.HilightForeground = D2DRenderBase.ToColor4(textbox.HilightForeground);
53 this.Hilight = D2DRenderBase.ToColor4(textbox.Hilight);
54 this.Keyword1 = D2DRenderBase.ToColor4(textbox.Keyword1);
55 this.Keyword2 = D2DRenderBase.ToColor4(textbox.Keyword2);
56 this.Literal = D2DRenderBase.ToColor4(textbox.Literal);
57 this.Url = D2DRenderBase.ToColor4(textbox.URL);
58 this.ControlChar = D2DRenderBase.ToColor4(textbox.ControlChar);
59 this.Comment = D2DRenderBase.ToColor4(textbox.Comment);
60 this.InsertCaret = D2DRenderBase.ToColor4(textbox.InsertCaret);
61 this.OverwriteCaret = D2DRenderBase.ToColor4(textbox.OverwriteCaret);
62 this.LineMarker = D2DRenderBase.ToColor4(textbox.LineMarker);
63 this.UpdateArea = D2DRenderBase.ToColor4(textbox.UpdateArea);
64 this.LineNumber = D2DRenderBase.ToColor4(textbox.LineNumber);
67 public override void GetDpi(out float dpix, out float dpiy)
69 Util.GetDpi(out dpix, out dpiy);
72 public bool Resize(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
74 if (this.Size.Width == width && this.Size.Height == height)
76 this.ReConstructDeviceResource(width, height);
77 this.CreateSurface(rect, width, height);
81 public bool IsCanDraw()
83 return this.Size.Height != 0 && this.Size.Width != 0;
86 public void DrawContent(EditView view,bool IsEnabled,Rectangle updateRect)
88 SharpDX.Mathematics.Interop.RawPoint offset;
89 this.Surface = this.SurfaceImageNative.BeginDraw(
90 new SharpDX.Rectangle(0, 0, (int)this.Size.Width, (int)this.Size.Height), out offset);
91 this.Bitmap = new D2D.Bitmap1(this.D2DContext, this.Surface, null);
92 this.D2DContext.Target = this.Bitmap;
93 this.D2DContext.Transform = Matrix3x2.Translation(offset.X, offset.Y);
97 view.Draw(updateRect);
99 this.FillBackground(updateRect);
102 this.Surface.Dispose();
103 this.Bitmap.Dispose();
104 this.SurfaceImageNative.EndDraw();
107 void CreateSurface(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
109 if (this.SurfaceImageNative != null)
110 this.SurfaceImageNative.Dispose();
111 this.SurfaceImage = new SurfaceImageSource((int)width, (int)height);
112 this.SurfaceImageNative = ComObject.As<DXGI.ISurfaceImageSourceNative>(this.SurfaceImage);
113 this.SurfaceImageNative.Device = this.DXGIDevice;
114 this.Size.Width = width;
115 this.Size.Height = height;
116 ImageBrush brush = new ImageBrush();
117 brush.ImageSource = this.SurfaceImage;
121 protected override D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
123 this.D2DDevice = new D2D.Device(factory,this.DXGIDevice);
124 this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
125 return this.D2DContext;
128 protected override void ConstrctedResource()
132 protected override void DestructRender()
134 if (this.D2DDevice != null)
135 this.D2DDevice.Dispose();
136 if (this.D2DContext != null)
137 this.D2DContext.Dispose();
140 protected override void ReCreateTarget()