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;
15 using System.Threading.Tasks;
17 namespace FooEditEngine
24 class Gripper : IEquatable<Gripper>
26 Controller Controller;
30 public Gripper(Controller controller, EditView view,D2DRender render,GripperType type)
32 this.Controller = controller;
43 public Rectangle Rectangle
47 return this.GetGripperPoint(this.type);
50 public bool IsHit(Point p)
52 Rectangle gripperRect = this.GetGripperPoint(type);
53 return this.Enabled && gripperRect.IsHit(p);
56 public Point AdjustPoint(Point p)
58 if (this.type == GripperType.First)
60 if (this.Rectangle.IsHit(p))
61 p.Y = this.Rectangle.Bottom + 1;
63 p.Y += this.Rectangle.Height;
67 if (this.Rectangle.IsHit(p))
68 p.Y = this.Rectangle.Y - 1;
70 p.Y -= this.Rectangle.Height;
72 if (p.Y < this.Render.TextArea.Y)
73 p.Y = this.Render.TextArea.Y;
80 this.DrawGripper(this.type);
83 void DrawGripper(GripperType type)
85 Rectangle gripperRect = GetGripperPoint(type);
86 double radius = gripperRect.Width / 2;
88 if (type == GripperType.First)
89 point = new Point(gripperRect.X + radius, gripperRect.Y + radius);
91 point = new Point(gripperRect.X + radius, gripperRect.Y + radius);
92 this.Render.DrawGripper(point, radius);
95 Rectangle GetGripperPoint(GripperType type)
99 double radius = D2DRender.GripperWidth / 2;
100 if (type == GripperType.Second)
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);
107 else if (type == GripperType.First)
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);
115 throw new ArgumentOutOfRangeException();
119 public bool Equals(Gripper other)
121 return this.Rectangle == other.Rectangle;