OSDN Git Service

2f2341d8ba420975e43493c920987363bfae38c3
[fooeditengine/FooEditEngine.git] / UWP / FooEditEngine.UWP / Direct2D / D2DRender.cs
1 /*
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.
5
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.
8
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/>.
10  */
11 using System;
12 using System.Collections.Generic;
13 using Windows.Graphics.Display;
14 using Windows.UI.Xaml.Media;
15 using Windows.UI.Xaml.Media.Imaging;
16 using SharpDX;
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;
24
25 namespace FooEditEngine
26 {
27     sealed class D2DRender : D2DRenderBase, IEditorRender
28     {
29         SurfaceImageSource SurfaceImage;
30         DXGI.ISurfaceImageSourceNative SurfaceImageNative;
31         D2D.Device D2DDevice;
32         D2D.DeviceContext D2DContext;
33         D2D.Bitmap1 Bitmap;
34         Size Size = new Size();
35         DXGI.Surface Surface;
36
37         //変換中の書式
38
39         public D2DRender(FooTextBox textbox, Windows.UI.Xaml.Shapes.Rectangle rect)
40             : base()
41         {
42             this.ConstructDeviceResource(rect.ActualWidth, rect.ActualHeight);
43             this.InitTextFormat(textbox.FontFamily, textbox.FontSize);
44
45             this.Size.Width = rect.ActualWidth;
46             this.Size.Height = rect.ActualHeight;
47
48             this.CreateSurface(rect, rect.ActualWidth, rect.ActualHeight);
49
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);
65         }
66
67         public override void GetDpi(out float dpix, out float dpiy)
68         {
69             Util.GetDpi(out dpix, out dpiy);
70         }
71
72         public bool Resize(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
73         {
74             if (this.Size.Width == width && this.Size.Height == height)
75                 return false;
76             this.ReConstructDeviceResource(width, height);
77             this.CreateSurface(rect, width, height);
78             return true;
79         }
80
81         public bool IsCanDraw()
82         {
83             return this.Size.Height != 0 && this.Size.Width != 0;
84         }
85
86         public void DrawContent(EditView view,bool IsEnabled,Rectangle updateRect)
87         {
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);
94             base.BegineDraw();
95
96             if (IsEnabled)
97                 view.Draw(updateRect);
98             else
99                 this.FillBackground(updateRect);
100
101             base.EndDraw();
102             this.Surface.Dispose();
103             this.Bitmap.Dispose();
104             this.SurfaceImageNative.EndDraw();
105         }
106
107         void CreateSurface(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
108         {
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;
118             rect.Fill = brush;
119         }
120
121         protected override D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
122         {
123             this.D2DDevice = new D2D.Device(factory,this.DXGIDevice);
124             this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
125             return this.D2DContext;
126         }
127
128         protected override void ConstrctedResource()
129         {
130         }
131
132         protected override void DestructRender()
133         {
134             if (this.D2DDevice != null)
135                 this.D2DDevice.Dispose();
136             if (this.D2DContext != null)
137                 this.D2DContext.Dispose();
138         }
139
140         protected override void ReCreateTarget()
141         {
142         }
143     }
144 }