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 Size Size = new Size();
37 public D2DRender(FooTextBox textbox, Windows.UI.Xaml.Shapes.Rectangle rect)
40 this.ConstructRenderAndResoruce(rect.ActualWidth, rect.ActualHeight);
41 this.InitTextFormat(textbox.FontFamily, textbox.FontSize);
43 this.Size.Width = rect.ActualWidth;
44 this.Size.Height = rect.ActualHeight;
46 this.CreateSurface(rect, rect.ActualWidth, rect.ActualHeight);
48 this.Foreground = D2DRenderBase.ToColor4(textbox.Foreground);
49 this.Background = D2DRenderBase.ToColor4(textbox.Background);
50 this.HilightForeground = D2DRenderBase.ToColor4(textbox.HilightForeground);
51 this.Hilight = D2DRenderBase.ToColor4(textbox.Hilight);
52 this.Keyword1 = D2DRenderBase.ToColor4(textbox.Keyword1);
53 this.Keyword2 = D2DRenderBase.ToColor4(textbox.Keyword2);
54 this.Literal = D2DRenderBase.ToColor4(textbox.Literal);
55 this.Url = D2DRenderBase.ToColor4(textbox.URL);
56 this.ControlChar = D2DRenderBase.ToColor4(textbox.ControlChar);
57 this.Comment = D2DRenderBase.ToColor4(textbox.Comment);
58 this.InsertCaret = D2DRenderBase.ToColor4(textbox.InsertCaret);
59 this.OverwriteCaret = D2DRenderBase.ToColor4(textbox.OverwriteCaret);
60 this.LineMarker = D2DRenderBase.ToColor4(textbox.LineMarker);
61 this.UpdateArea = D2DRenderBase.ToColor4(textbox.UpdateArea);
62 this.LineNumber = D2DRenderBase.ToColor4(textbox.LineNumber);
65 public override void GetDpi(out float dpix, out float dpiy)
67 Util.GetDpi(out dpix, out dpiy);
70 public bool Resize(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
72 if (this.Size.Width == width && this.Size.Height == height)
74 this.ReConstructRenderAndResource(width, height);
75 this.CreateSurface(rect, width, height);
79 public bool IsCanDraw()
81 return this.Size.Height != 0 && this.Size.Width != 0;
84 public void DrawContent(EditView view,bool IsEnabled,Rectangle updateRect)
86 SharpDX.Mathematics.Interop.RawPoint offset;
88 this.Surface = this.SurfaceImageNative.BeginDraw(
89 new SharpDX.Rectangle(0, 0, (int)(this.Size.Width * this.GetScale()), (int)(this.Size.Height * this.GetScale())), out offset);
90 this.Bitmap = new D2D.Bitmap1(this.D2DContext, this.Surface, null);
91 this.D2DContext.Target = this.Bitmap;
92 //こっちはDirect2Dなのでデバイス非依存の座標を渡す
93 float dipOffsetX = (float)(offset.X / this.GetScale());
94 float dipOffsetY = (float)(offset.Y / this.GetScale());
95 this.D2DContext.Transform = Matrix3x2.Translation(dipOffsetX, dipOffsetY);
99 view.Draw(updateRect);
101 this.FillBackground(updateRect);
104 this.Surface.Dispose();
105 this.Bitmap.Dispose();
106 this.SurfaceImageNative.EndDraw();
109 void CreateSurface(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
111 if (this.SurfaceImageNative != null)
112 this.SurfaceImageNative.Dispose();
113 //デバイス依存の座標を渡さないといけない
114 this.SurfaceImage = new SurfaceImageSource((int)(width * this.GetScale()), (int)(height * this.GetScale()), false);
115 this.SurfaceImageNative = ComObject.As<DXGI.ISurfaceImageSourceNative>(this.SurfaceImage);
116 this.SurfaceImageNative.Device = this.DXGIDevice;
117 this.Size.Width = width;
118 this.Size.Height = height;
119 ImageBrush brush = new ImageBrush();
120 brush.ImageSource = this.SurfaceImage;
124 public void ReConstructRenderAndResource(double width, double height)
126 this.DestructRenderAndResource();
127 this.ConstructRenderAndResoruce(width, height);