OSDN Git Service

初期化の仕方はプラットフォームによって違うので共通化しないことにした
[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.Bitmap1 Bitmap;
32         Size Size = new Size();
33         DXGI.Surface Surface;
34
35         //変換中の書式
36
37         public D2DRender(FooTextBox textbox, Windows.UI.Xaml.Shapes.Rectangle rect)
38             : base()
39         {
40             this.ConstructRenderAndResoruce(rect.ActualWidth, rect.ActualHeight);
41             this.InitTextFormat(textbox.FontFamily, textbox.FontSize);
42
43             this.Size.Width = rect.ActualWidth;
44             this.Size.Height = rect.ActualHeight;
45
46             this.CreateSurface(rect, rect.ActualWidth, rect.ActualHeight);
47
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);
63         }
64
65         public override void GetDpi(out float dpix, out float dpiy)
66         {
67             Util.GetDpi(out dpix, out dpiy);
68         }
69
70         public bool Resize(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
71         {
72             if (this.Size.Width == width && this.Size.Height == height)
73                 return false;
74             this.ReConstructRenderAndResource(width, height);
75             this.CreateSurface(rect, width, height);
76             return true;
77         }
78
79         public bool IsCanDraw()
80         {
81             return this.Size.Height != 0 && this.Size.Width != 0;
82         }
83
84         public void DrawContent(EditView view,bool IsEnabled,Rectangle updateRect)
85         {
86             SharpDX.Mathematics.Interop.RawPoint offset;
87             //デバイス依存の座標を渡さないといけない
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);
96             base.BegineDraw();
97
98             if (IsEnabled)
99                 view.Draw(updateRect);
100             else
101                 this.FillBackground(updateRect);
102
103             base.EndDraw();
104             this.Surface.Dispose();
105             this.Bitmap.Dispose();
106             this.SurfaceImageNative.EndDraw();
107         }
108
109         void CreateSurface(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
110         {
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;
121             rect.Fill = brush;
122         }
123
124         public void ReConstructRenderAndResource(double width, double height)
125         {
126             this.DestructRenderAndResource();
127             this.ConstructRenderAndResoruce(width, height);
128         }
129     }
130 }