OSDN Git Service

グリッパーの部分を書き直した
authorgdkhd812 <jbh03215@htmil.co.jp>
Tue, 1 Oct 2013 11:40:42 +0000 (20:40 +0900)
committergdkhd812 <jbh03215@htmil.co.jp>
Tue, 1 Oct 2013 11:40:42 +0000 (20:40 +0900)
Metro/FooEditEngnine/FooTextBox.cs
Metro/FooEditEngnine/GripperManager.cs

index 0fc9f98..303c43c 100644 (file)
@@ -46,7 +46,6 @@ namespace FooEditEngine.Metro
         Windows.UI.Xaml.Shapes.Rectangle rectangle;\r
         GestureRecognizer gestureRecongnizer = new GestureRecognizer();\r
         TextStore2 textStore;\r
-        GripperManager gripper;\r
         FooTextBoxAutomationPeer peer;\r
 \r
         /// <summary>\r
@@ -90,6 +89,9 @@ namespace FooEditEngine.Metro
             this._Controller = new Controller(this.Document, this.View);\r
             this._Controller.CaretMoved += Controller_CaretMoved;\r
 \r
+            this.FirstGripper = new Gripper(this._Controller, this.View, this.Render, GripperType.First);\r
+            this.SecondGripper = new Gripper(this._Controller, this.View, this.Render, GripperType.Second);\r
+\r
             this.gestureRecongnizer.GestureSettings = GestureSettings.Drag | \r
                 GestureSettings.RightTap | \r
                 GestureSettings.Tap | \r
@@ -110,8 +112,6 @@ namespace FooEditEngine.Metro
 \r
             this.SizeChanged += FooTextBox_SizeChanged;\r
 \r
-            this.gripper = new GripperManager(this._Controller, this.View, this.Render);\r
-\r
             this.Loaded += FooTextBox_Loaded;\r
         }\r
 \r
@@ -743,60 +743,54 @@ namespace FooEditEngine.Metro
                 this.textStore.NotifySelectionChanged();\r
         }\r
 \r
-        bool isHitCaret;\r
-        Rectangle hitedSecondGripperRect = Rectangle.Empty,hitedFirstGripperRect = Rectangle.Empty;\r
+        Gripper FirstGripper, SecondGripper;\r
         void gestureRecongnizer_ManipulationStarted(GestureRecognizer sender, ManipulationStartedEventArgs e)\r
         {\r
+        }\r
+\r
+        void gestureRecongnizer_ManipulationUpdated(GestureRecognizer sender, ManipulationUpdatedEventArgs e)\r
+        {\r
             if (this.Document.State == AsyncState.Loading)\r
                 return;\r
+            bool HittedCaret = false;\r
+\r
             TextPoint tp = this.View.GetTextPointFromPostion(e.Position);\r
+            Point p = e.Position;\r
+\r
             if (tp == this.View.CaretPostion)\r
             {\r
-                this.isHitCaret = true;\r
-                this.hitedSecondGripperRect = Rectangle.Empty;\r
+                HittedCaret = true;\r
             }\r
-            Rectangle gripperRect = this.gripper.HitTest(GripperType.Second,e.Position);\r
-            if (gripperRect != Rectangle.Empty)\r
+\r
+            Gripper hittedGripper = null;\r
+            if (this.FirstGripper.IsHit(p))\r
             {\r
-                this.isHitCaret = true;\r
-                this.hitedSecondGripperRect = gripperRect;\r
+                p = this.FirstGripper.AdjustPoint(p);\r
+                hittedGripper = this.FirstGripper;\r
+                HittedCaret = true;\r
             }\r
-            gripperRect = this.gripper.HitTest(GripperType.First,e.Position);\r
-            if (gripperRect != Rectangle.Empty)\r
+            else if (this.SecondGripper.IsHit(p))\r
             {\r
-                this.isHitCaret = true;\r
-                this.hitedFirstGripperRect = gripperRect;\r
+                p = this.SecondGripper.AdjustPoint(p);\r
+                hittedGripper = this.SecondGripper;\r
+                HittedCaret = true;\r
             }\r
-        }\r
 \r
-        void gestureRecongnizer_ManipulationUpdated(GestureRecognizer sender, ManipulationUpdatedEventArgs e)\r
-        {\r
-            if (this.Document.State == AsyncState.Loading)\r
-                return;\r
-            if (this.isHitCaret)\r
+            if (HittedCaret)\r
             {\r
-                Point p = e.Position;\r
-                if (this.hitedFirstGripperRect != Rectangle.Empty)\r
-                    p = this.gripper.GetPointOnGripper(GripperType.First,this.hitedFirstGripperRect, p);\r
-                else if (this.hitedSecondGripperRect != Rectangle.Empty)\r
-                    p = this.gripper.GetPointOnGripper(GripperType.Second,this.hitedSecondGripperRect, p);\r
-                if (this.View.HitTextArea(p.X, p.Y))\r
+                if (hittedGripper != null && this.View.HitTextArea(p.X, p.Y))\r
                 {\r
-                    TextPoint tp = this.View.GetTextPointFromPostion(p);\r
+                    tp = this.View.GetTextPointFromPostion(p);\r
                     if (this._Controller.IsReverseSelect())\r
                     {\r
-                        if (this.hitedFirstGripperRect == this.hitedSecondGripperRect)\r
-                            this._Controller.MoveCaretAndSelect(tp);\r
-                        else if (this.hitedSecondGripperRect != Rectangle.Empty)\r
+                        if (Object.ReferenceEquals(hittedGripper,this.SecondGripper))\r
                             this._Controller.MoveSelectBefore(tp);\r
                         else\r
                             this._Controller.MoveCaretAndSelect(tp);\r
                     }\r
                     else\r
                     {\r
-                        if(this.hitedFirstGripperRect == this.hitedSecondGripperRect)\r
-                            this._Controller.MoveCaretAndSelect(tp);\r
-                        else if (this.hitedFirstGripperRect != Rectangle.Empty)\r
+                        if (Object.ReferenceEquals(hittedGripper,this.FirstGripper))\r
                             this._Controller.MoveSelectBefore(tp);\r
                         else\r
                             this._Controller.MoveCaretAndSelect(tp);\r
@@ -840,7 +834,8 @@ namespace FooEditEngine.Metro
                     this._Controller.Scroll(ScrollDirection.Up, scrollCount, false, false);\r
                 else\r
                     this._Controller.Scroll(ScrollDirection.Down, scrollCount, false, false);\r
-                this.gripper.Enabled = false;\r
+                this.FirstGripper.Enabled = false;\r
+                this.SecondGripper.Enabled = false;\r
                 this.Refresh();\r
                 return;\r
             }\r
@@ -852,16 +847,14 @@ namespace FooEditEngine.Metro
                     this._Controller.Scroll(ScrollDirection.Left, deltax, false, false);\r
                 else\r
                     this._Controller.Scroll(ScrollDirection.Right, deltax, false, false);\r
-                this.gripper.Enabled = false;\r
+                this.FirstGripper.Enabled = false;\r
+                this.SecondGripper.Enabled = false;\r
                 this.Refresh();\r
             }\r
         }\r
 \r
         void gestureRecongnizer_ManipulationCompleted(GestureRecognizer sender, ManipulationCompletedEventArgs e)\r
         {\r
-            this.isHitCaret = false;\r
-            this.hitedFirstGripperRect = Rectangle.Empty;\r
-            this.hitedSecondGripperRect = Rectangle.Empty;\r
         }\r
 \r
         async void gestureRecongnizer_RightTapped(GestureRecognizer sender, RightTappedEventArgs e)\r
@@ -908,7 +901,9 @@ namespace FooEditEngine.Metro
 \r
         void gestureRecongnizer_Tapped(GestureRecognizer sender, TappedEventArgs e)\r
         {\r
-            this.gripper.Enabled = e.PointerDeviceType == PointerDeviceType.Touch;\r
+            bool touched = e.PointerDeviceType == PointerDeviceType.Touch;\r
+            this.FirstGripper.Enabled = touched;\r
+            this.SecondGripper.Enabled = touched;\r
             this.JumpCaret(e.Position);\r
         }\r
 \r
@@ -972,7 +967,8 @@ namespace FooEditEngine.Metro
                 this.View.Draw(updateRect);\r
             else\r
                 this.Render.FillBackground(updateRect);\r
-            this.gripper.DrawGrippers();\r
+            this.FirstGripper.Draw();\r
+            this.SecondGripper.Draw();\r
             this.Render.EndDraw();\r
         }\r
 \r
@@ -1037,7 +1033,8 @@ namespace FooEditEngine.Metro
             else\r
                 toX = -this.horizontalScrollBar.Value;\r
             this._Controller.Scroll(toX, this.View.Src.Row, false, false);\r
-            this.gripper.Enabled = false;\r
+            this.FirstGripper.Enabled = false;\r
+            this.SecondGripper.Enabled = false;\r
             this.Refresh();\r
         }\r
 \r
@@ -1051,7 +1048,8 @@ namespace FooEditEngine.Metro
             if (newRow >= this.View.LayoutLines.Count)\r
                 return;\r
             this._Controller.Scroll(this.View.Src.X, newRow, false, false);\r
-            this.gripper.Enabled = false;\r
+            this.FirstGripper.Enabled = false;\r
+            this.SecondGripper.Enabled = false;\r
             this.Refresh();\r
         }\r
 \r
index 77c0a32..645097d 100644 (file)
@@ -21,95 +21,104 @@ namespace FooEditEngine
         First,
         Second,
     }
-    class GripperManager
+    class Gripper : IEquatable<Gripper>
     {
         Controller Controller;
         EditView View;
         D2DRender Render;
-        public GripperManager(Controller controller, EditView view,D2DRender render)
+        GripperType type;
+        public Gripper(Controller controller, EditView view,D2DRender render,GripperType type)
         {
             this.Controller = controller;
             this.View = view;
             this.Render = render;
             this.Enabled = false;
+            this.type = type;
         }
-
         public bool Enabled
         {
             get;
             set;
         }
-
-        public Rectangle HitTest(GripperType type,Point p)
+        public Rectangle Rectangle
         {
-            Rectangle gripperRect = this.GetGripperPoint(type);
-            if (this.Enabled && gripperRect.IsHit(p))
-                return gripperRect;
-            return Rectangle.Empty;
-        }
-
-        public Rectangle GetGripperPoint(GripperType type)
-        {
-            TextPoint tp;
-            Point point;
-            double radius = D2DRender.GripperWidth / 2;
-            if (type == GripperType.Second)
-            {
-                tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart + this.Controller.SelectionLength);
-                point = this.View.GetPostionFromTextPoint(tp);
-                double lineHeight = this.View.LayoutLines.GetLayout(tp.row).Height;
-                return new Rectangle(point.X - radius, point.Y + lineHeight, D2DRender.GripperWidth, D2DRender.GripperWidth);
-            }
-            else if (type == GripperType.First)
-            {
-                tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart);
-                point = this.View.GetPostionFromTextPoint(tp);
-                return new Rectangle(point.X - radius, point.Y - D2DRender.GripperWidth, D2DRender.GripperWidth, D2DRender.GripperWidth);
-            }
-            else
+            get
             {
-                throw new ArgumentOutOfRangeException();
+                return this.GetGripperPoint(this.type);
             }
         }
+        public bool IsHit(Point p)
+        {
+            Rectangle gripperRect = this.GetGripperPoint(type);
+            return this.Enabled && gripperRect.IsHit(p);
+        }
 
-        public Point GetPointOnGripper(GripperType type,Rectangle gripperRect, Point p)
+        public Point AdjustPoint(Point p)
         {
-            if (type == GripperType.First)
+            if (this.type == GripperType.First)
             {
-                if (gripperRect.IsHit(p))
-                    p.Y = gripperRect.Bottom + 1;
+                if (this.Rectangle.IsHit(p))
+                    p.Y = this.Rectangle.Bottom + 1;
                 else
-                    p.Y += gripperRect.Height;
+                    p.Y += this.Rectangle.Height;
             }
             else
             {
-                if (gripperRect.IsHit(p))
-                    p.Y = gripperRect.Y - 1;
+                if (this.Rectangle.IsHit(p))
+                    p.Y = this.Rectangle.Y - 1;
                 else
-                    p.Y -= gripperRect.Height;
+                    p.Y -= this.Rectangle.Height;
             }
             if (p.Y < this.Render.TextArea.Y)
                 p.Y = this.Render.TextArea.Y;
             return p;
         }
 
-        public void DrawGrippers()
+        public void Draw()
         {
-            if (!this.Enabled)
-                return;
-            DrawGripper(GripperType.First);
-            DrawGripper(GripperType.Second);
+            if(this.Enabled)
+                this.DrawGripper(this.type);
         }
+
         void DrawGripper(GripperType type)
         {
             Rectangle gripperRect = GetGripperPoint(type);
             double radius = gripperRect.Width / 2;
             Point point;
-            if(type == GripperType.First)
+            if (type == GripperType.First)
                 point = new Point(gripperRect.X + radius, gripperRect.Y + radius);
             else
                 point = new Point(gripperRect.X + radius, gripperRect.Y + radius);
             this.Render.DrawGripper(point, radius);
         }
+
+        Rectangle GetGripperPoint(GripperType type)
+        {
+            TextPoint tp;
+            Point point;
+            double radius = D2DRender.GripperWidth / 2;
+            if (type == GripperType.Second)
+            {
+                tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart + this.Controller.SelectionLength);
+                point = this.View.GetPostionFromTextPoint(tp);
+                double lineHeight = this.View.LayoutLines.GetLayout(tp.row).Height;
+                return new Rectangle(point.X - radius, point.Y + lineHeight, D2DRender.GripperWidth, D2DRender.GripperWidth);
+            }
+            else if (type == GripperType.First)
+            {
+                tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart);
+                point = this.View.GetPostionFromTextPoint(tp);
+                return new Rectangle(point.X - radius, point.Y - D2DRender.GripperWidth, D2DRender.GripperWidth, D2DRender.GripperWidth);
+            }
+            else
+            {
+                throw new ArgumentOutOfRangeException();
+            }
+        }
+
+        public bool Equals(Gripper other)
+        {
+            return this.Rectangle == other.Rectangle;
+        }
     }
 }