OSDN Git Service

GripperViewがControllerに依存しているのは望ましくないので依存しないようにした
authorgdkhd812 <test@nnn.co.jp>
Fri, 23 Oct 2015 13:01:14 +0000 (18:31 +0530)
committergdkhd812 <test@nnn.co.jp>
Fri, 23 Oct 2015 13:01:14 +0000 (18:31 +0530)
Core/Controller.cs
Core/EditView.cs
Core/GripperView.cs
Metro/FooEditEngine/FooTextBox.cs

index a0b85d3..bb5a88c 100644 (file)
@@ -824,6 +824,17 @@ namespace FooEditEngine
             return new TextPoint(row, col);
         }
 
+        Rectangle GetGripperRect(GripperPostion type, int width, int height)
+        {
+            double radius = width / 2;
+            if (type == GripperPostion.BottomRight)
+                return this.View.GetRectFromIndex(this.SelectionStart + this.SelectionLength,width,height);
+            else if (type == GripperPostion.BottomLeft)
+                return this.View.GetRectFromIndex(this.SelectionStart,width,height);
+            else
+                throw new ArgumentOutOfRangeException();
+        }
+
         /// <summary>
         /// 選択文字列のインデントを一つ増やす
         /// </summary>
index f28313d..0a216d1 100644 (file)
@@ -31,17 +31,18 @@ namespace FooEditEngine
         Point _CaretLocation = new Point();
         TextPoint _CaretPostion = new TextPoint();
         long tickCount;
-        bool _CaretBlink,_HideRuler = true;
+        bool _CaretBlink, _HideRuler = true;
         internal const int LineNumberLength = 6;
         const int UpdateAreaPaddingWidth = 2;
         const int UpdateAreaWidth = 4;
         const int UpdateAreaTotalWidth = UpdateAreaWidth + UpdateAreaPaddingWidth;
+        GripperView[] selectionGrippers = new GripperView[2];
 
         /// <summary>
         /// コンストラクター
         /// </summary>
-        public EditView(Document doc, IEditorRender r,int MarginLeftAndRight = 5)
-            : this(doc,r,new Padding(MarginLeftAndRight, 0, MarginLeftAndRight, 0))
+        public EditView(Document doc, IEditorRender r, int MarginLeftAndRight = 5)
+            : this(doc, r, new Padding(MarginLeftAndRight, 0, MarginLeftAndRight, 0))
         {
         }
 
@@ -52,7 +53,7 @@ namespace FooEditEngine
         /// <param name="r">レンダー</param>
         /// <param name="margin">マージン(1番目:左、2番目:上、3番目:右、4番目:下)</param>
         public EditView(Document doc, IEditorRender r, Padding margin)
-            : base(doc,r,margin)
+            : base(doc, r, margin)
         {
             this.CaretBlinkTime = 500;
             this.CaretWidthOnInsertMode = 1;
@@ -64,6 +65,8 @@ namespace FooEditEngine
             this.HideLineMarker = true;
             this.IsFocused = false;
             this.Selections = new SelectCollection();
+            this.selectionGrippers[0] = new GripperView(this, GripperPostion.BottomLeft);
+            this.selectionGrippers[1] = new GripperView(this, GripperPostion.BottomRight);
         }
 
         /// <summary>
@@ -76,6 +79,17 @@ namespace FooEditEngine
         }
 
         /// <summary>
+        /// 選択範囲にあるグリッパーのリスト
+        /// </summary>
+        public IList<GripperView> SelectGrippers
+        {
+            get
+            {
+                return this.selectionGrippers;
+            }
+        }
+
+        /// <summary>
         /// ラインマーカーを描くなら偽。そうでなければ真
         /// </summary>
         public bool HideLineMarker
@@ -313,6 +327,9 @@ namespace FooEditEngine
             }
 
             this.DrawCaret();
+
+            foreach (GripperView gripper in this.selectionGrippers)
+                gripper.Draw();
         }
 
         void DrawUpdateArea(int row,double ypos)
@@ -552,6 +569,34 @@ namespace FooEditEngine
             return p;
         }
 
+        public GripperView HitGripperFromPoint(Point p)
+        {
+            for(int i = 0; i < this.selectionGrippers.Length; i++)
+            {
+                if (this.selectionGrippers[i].IsHit(p))
+                {
+                    System.Diagnostics.Debug.WriteLine("gripper hitted number:{0}", i);
+                    return selectionGrippers[i];
+                }
+            }
+            return null;
+        }
+
+        public Rectangle GetRectFromIndex(int index,int width,int height)
+        {
+            TextPoint tp = this.LayoutLines.GetTextPointFromIndex(index);
+            return this.GetRectFromTextPoint(tp, width, height);
+        }
+
+        public Rectangle GetRectFromTextPoint(TextPoint tp, int width, int height)
+        {
+            double radius = width / 2;
+            Point point = this.GetPostionFromTextPoint(tp);
+            double lineHeight = this.LayoutLines.GetLayout(tp.row).Height;
+
+            return new Rectangle(point.X - radius, point.Y + lineHeight, width, height);
+        }
+
         /// <summary>
         /// キャレットを指定した位置に移動させる
         /// </summary>
index 1e26ed6..4ab867a 100644 (file)
@@ -14,14 +14,10 @@ namespace FooEditEngine
         public const int GripperWidth = 10;
         public const int HitAreaWidth = 48;
 
-        Controller Controller;
-        EditView View;
         ITextRender Render;
         GripperPostion type;
-        public GripperView(Controller controller, EditView view, GripperPostion type)
+        public GripperView(EditView view, GripperPostion type)
         {
-            this.Controller = controller;
-            this.View = view;
             this.Render = view.render;
             this.Enabled = false;
             this.type = type;
@@ -33,20 +29,34 @@ namespace FooEditEngine
         }
         public Rectangle Rectangle
         {
-            get
-            {
-                return this.GetGripperRect(this.type, GripperWidth, GripperWidth);
-            }
+            get;
+            set;
+        }
+        public Rectangle HitArea
+        {
+            get;
+            set;
         }
         public bool IsHit(Point p)
         {
-            Rectangle gripperRect = this.GetGripperRect(type, HitAreaWidth, HitAreaWidth);
-            return this.Enabled && gripperRect.IsHit(p);
+            return this.Enabled && this.HitArea.IsHit(p);
+        }
+
+        public void Move(EditView view,TextPoint tp)
+        {
+            this.Rectangle = view.GetRectFromTextPoint(tp, GripperView.GripperWidth, GripperView.GripperWidth);
+            this.HitArea = view.GetRectFromTextPoint(tp, GripperView.HitAreaWidth, GripperView.HitAreaWidth);
+        }
+
+        public void MoveByIndex(EditView view, int index)
+        {
+            this.Rectangle = view.GetRectFromIndex(index, GripperView.GripperWidth, GripperView.GripperWidth);
+            this.HitArea = view.GetRectFromIndex(index, GripperView.HitAreaWidth, GripperView.HitAreaWidth);
         }
 
         public Point AdjustPoint(Point p)
         {
-            Rectangle gripperRect = this.GetGripperRect(type, HitAreaWidth, HitAreaWidth);
+            Rectangle gripperRect = this.HitArea;
 
             if (gripperRect.IsHit(p))
                 p.Y = gripperRect.Y - 1;
@@ -66,31 +76,13 @@ namespace FooEditEngine
 
         void DrawGripper(GripperPostion type)
         {
-            Rectangle gripperRect = GetGripperRect(type, GripperWidth, GripperWidth);
+            Rectangle gripperRect = this.Rectangle;
             double radius = gripperRect.Width / 2;
             Point point;
             point = new Point(gripperRect.X + radius, gripperRect.Y + radius);
             this.Render.DrawGripper(point, radius);
         }
 
-        Rectangle GetGripperRect(GripperPostion type, int width, int height)
-        {
-            TextPoint tp;
-            Point point;
-            double radius = width / 2;
-            if (type == GripperPostion.BottomRight)
-                tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart + this.Controller.SelectionLength);
-            else if (type == GripperPostion.BottomLeft)
-                tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart);
-            else
-                throw new ArgumentOutOfRangeException();
-
-            point = this.View.GetPostionFromTextPoint(tp);
-            double lineHeight = this.View.LayoutLines.GetLayout(tp.row).Height;
-
-            return new Rectangle(point.X - radius, point.Y + lineHeight, width, height);
-        }
-
         public bool Equals(GripperView other)
         {
             return this.Rectangle == other.Rectangle;
index 5d506a1..a77eafc 100644 (file)
@@ -92,9 +92,6 @@ namespace FooEditEngine.Metro
             this._Controller = new Controller(this.Document, this.View);
             this._Controller.SelectionChanged += Controller_SelectionChanged;
 
-            this.FirstGripper = new GripperView(this._Controller, this.View, GripperPostion.BottomLeft);
-            this.SecondGripper = new GripperView(this._Controller, this.View, GripperPostion.BottomRight);
-
             this.gestureRecongnizer.GestureSettings = GestureSettings.Drag | 
                 GestureSettings.RightTap | 
                 GestureSettings.Tap | 
@@ -777,11 +774,12 @@ namespace FooEditEngine.Metro
             SetValue(SelectionProperty, new TextRange(this._Controller.SelectionStart, this._Controller.SelectionLength));
             SetValue(CaretPostionPropertyKey, this.View.CaretPostion);
             this.nowCaretMove = false;
+            this.View.SelectGrippers[0].MoveByIndex(this.View, this.Controller.SelectionStart);
+            this.View.SelectGrippers[1].MoveByIndex(this.View, this.Controller.SelectionStart + this.Controller.SelectionLength);
             if (this.textStore.IsLocked() == false)
                 this.textStore.NotifySelectionChanged();
         }
 
-        GripperView FirstGripper, SecondGripper;
         bool HittedCaret;
         GripperView hittedGripper;
         private void gestureRecongnizer_ManipulationInertiaStarting(GestureRecognizer sender, ManipulationInertiaStartingEventArgs e)
@@ -794,7 +792,6 @@ namespace FooEditEngine.Metro
         void gestureRecongnizer_ManipulationStarted(GestureRecognizer sender, ManipulationStartedEventArgs e)
         {
             this.HittedCaret = false;
-            this.hittedGripper = null;
 
             Point p = e.Position;
             TextPoint tp = this.View.GetTextPointFromPostion(p);
@@ -802,19 +799,11 @@ namespace FooEditEngine.Metro
             {
                 HittedCaret = true;
             }
-            
-            if (this.FirstGripper.IsHit(p))
-            {
-                hittedGripper = this.FirstGripper;
-                HittedCaret = true;
-                System.Diagnostics.Debug.WriteLine("first gripper hitted");
-            }
-            
-            else if (this.SecondGripper.IsHit(p))
+
+            this.hittedGripper = this.View.HitGripperFromPoint(e.Position);
+            if (this.hittedGripper != null)
             {
-                hittedGripper = this.SecondGripper;
-                HittedCaret = true;
-                System.Diagnostics.Debug.WriteLine("second gripper hitted");
+                this.HittedCaret = true;
             }
         }
 
@@ -837,18 +826,19 @@ namespace FooEditEngine.Metro
                     TextPoint tp = this.View.GetTextPointFromPostion(p);
                     if (this._Controller.IsReverseSelect())
                     {
-                        if (Object.ReferenceEquals(hittedGripper,this.SecondGripper))
+                        if (Object.ReferenceEquals(hittedGripper,this.View.SelectGrippers[1]))
                             this._Controller.MoveSelectBefore(tp);
                         else
                             this._Controller.MoveCaretAndSelect(tp);
                     }
                     else
                     {
-                        if (Object.ReferenceEquals(hittedGripper,this.FirstGripper))
+                        if (Object.ReferenceEquals(hittedGripper, this.View.SelectGrippers[0]))
                             this._Controller.MoveSelectBefore(tp);
                         else
                             this._Controller.MoveCaretAndSelect(tp);
                     }
+                    this.hittedGripper.Move(this.View, tp);
                 }
                 else
                 {
@@ -858,10 +848,7 @@ namespace FooEditEngine.Metro
                 }
                 if (this.peer != null)
                     this.peer.OnNotifyCaretChanged();
-                if (this._Controller.SelectionLength != 0)
-                    this.FirstGripper.Enabled = true;
-                else
-                    this.FirstGripper.Enabled = false;
+                this.View.SelectGrippers[0].Enabled = this._Controller.SelectionLength != 0;
 
                 this.Refresh();
                 
@@ -916,8 +903,8 @@ namespace FooEditEngine.Metro
                     this._Controller.Scroll(ScrollDirection.Up, scrollCount, false, false);
                 else
                     this._Controller.Scroll(ScrollDirection.Down, scrollCount, false, false);
-                this.FirstGripper.Enabled = false;
-                this.SecondGripper.Enabled = false;
+                this.View.SelectGrippers[0].Enabled = false;
+                this.View.SelectGrippers[1].Enabled = false;
                 this.Refresh();
                 return;
             }
@@ -929,8 +916,8 @@ namespace FooEditEngine.Metro
                     this._Controller.Scroll(ScrollDirection.Left, deltax, false, false);
                 else
                     this._Controller.Scroll(ScrollDirection.Right, deltax, false, false);
-                this.FirstGripper.Enabled = false;
-                this.SecondGripper.Enabled = false;
+                this.View.SelectGrippers[0].Enabled = false;
+                this.View.SelectGrippers[1].Enabled = false;
                 this.Refresh();
             }
         }
@@ -985,13 +972,13 @@ namespace FooEditEngine.Metro
         void gestureRecongnizer_Tapped(GestureRecognizer sender, TappedEventArgs e)
         {
             bool touched = e.PointerDeviceType == PointerDeviceType.Touch;
-            this.FirstGripper.Enabled = false;
-            this.SecondGripper.Enabled = touched;
+            this.View.SelectGrippers[0].Enabled = false;
+            this.View.SelectGrippers[1].Enabled = touched;
             this.JumpCaret(e.Position);
             System.Diagnostics.Debug.WriteLine(e.TapCount);
             if (e.TapCount == 2)
             {
-                this.FirstGripper.Enabled = touched;
+                this.View.SelectGrippers[0].Enabled = touched;
                 //タッチスクリーンでダブルタップした場合、アンカーインデックスを単語の先頭にしないとバグる
                 this.Controller.SelectWord(this.Controller.SelectionStart, touched);
             }
@@ -1053,8 +1040,6 @@ namespace FooEditEngine.Metro
                 this.View.Draw(updateRect);
             else
                 this.Render.FillBackground(updateRect);
-            this.FirstGripper.Draw();
-            this.SecondGripper.Draw();
             this.Render.EndDraw();
         }
 
@@ -1117,8 +1102,8 @@ namespace FooEditEngine.Metro
             else
                 toX = -this.horizontalScrollBar.Value;
             this._Controller.Scroll(toX, this.View.Src.Row, false, false);
-            this.FirstGripper.Enabled = false;
-            this.SecondGripper.Enabled = false;
+            this.View.SelectGrippers[0].Enabled = false;
+            this.View.SelectGrippers[1].Enabled = false;
             this.Refresh();
         }
 
@@ -1130,8 +1115,8 @@ namespace FooEditEngine.Metro
             if (newRow >= this.View.LayoutLines.Count)
                 return;
             this._Controller.Scroll(this.View.Src.X, newRow, false, false);
-            this.FirstGripper.Enabled = false;
-            this.SecondGripper.Enabled = false;
+            this.View.SelectGrippers[0].Enabled = false;
+            this.View.SelectGrippers[1].Enabled = false;
             this.Refresh();
         }