OSDN Git Service

正常に選択できないバグを修正した
[fooeditengine/FooEditEngine.git] / Metro / FooEditEngine / GripperManager.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 System.Linq;
14 using System.Text;
15 using System.Threading.Tasks;
16
17 namespace FooEditEngine
18 {
19     enum GripperType
20     {
21         First,
22         Second,
23     }
24     class Gripper : IEquatable<Gripper>
25     {
26         Controller Controller;
27         EditView View;
28         D2DRender Render;
29         GripperType type;
30         public Gripper(Controller controller, EditView view,D2DRender render,GripperType type)
31         {
32             this.Controller = controller;
33             this.View = view;
34             this.Render = render;
35             this.Enabled = false;
36             this.type = type;
37         }
38         public bool Enabled
39         {
40             get;
41             set;
42         }
43         public Rectangle Rectangle
44         {
45             get
46             {
47                 return this.GetGripperPoint(this.type);
48             }
49         }
50         public bool IsHit(Point p)
51         {
52             Rectangle gripperRect = this.GetGripperPoint(type);
53             return this.Enabled && gripperRect.IsHit(p);
54         }
55
56         public Point AdjustPoint(Point p)
57         {
58             if (this.type == GripperType.First)
59             {
60                 if (this.Rectangle.IsHit(p))
61                     p.Y = this.Rectangle.Bottom + 1;
62                 else
63                     p.Y += this.Rectangle.Height;
64             }
65             else
66             {
67                 if (this.Rectangle.IsHit(p))
68                     p.Y = this.Rectangle.Y - 1;
69                 else
70                     p.Y -= this.Rectangle.Height;
71             }
72             if (p.Y < this.Render.TextArea.Y)
73                 p.Y = this.Render.TextArea.Y;
74             return p;
75         }
76
77         public void Draw()
78         {
79             if(this.Enabled)
80                 this.DrawGripper(this.type);
81         }
82
83         void DrawGripper(GripperType type)
84         {
85             Rectangle gripperRect = GetGripperPoint(type);
86             double radius = gripperRect.Width / 2;
87             Point point;
88             if (type == GripperType.First)
89                 point = new Point(gripperRect.X + radius, gripperRect.Y + radius);
90             else
91                 point = new Point(gripperRect.X + radius, gripperRect.Y + radius);
92             this.Render.DrawGripper(point, radius);
93         }
94
95         Rectangle GetGripperPoint(GripperType type)
96         {
97             TextPoint tp;
98             Point point;
99             double radius = D2DRender.GripperWidth / 2;
100             if (type == GripperType.Second)
101             {
102                 tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart + this.Controller.SelectionLength);
103                 point = this.View.GetPostionFromTextPoint(tp);
104                 double lineHeight = this.View.LayoutLines.GetLayout(tp.row).Height;
105                 return new Rectangle(point.X - radius, point.Y + lineHeight, D2DRender.GripperWidth, D2DRender.GripperWidth);
106             }
107             else if (type == GripperType.First)
108             {
109                 tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart);
110                 point = this.View.GetPostionFromTextPoint(tp);
111                 return new Rectangle(point.X - radius, point.Y - D2DRender.GripperWidth, D2DRender.GripperWidth, D2DRender.GripperWidth);
112             }
113             else
114             {
115                 throw new ArgumentOutOfRangeException();
116             }
117         }
118
119         public bool Equals(Gripper other)
120         {
121             return this.Rectangle == other.Rectangle;
122         }
123     }
124 }