private static bool ContainsH(IssoBinding binding, IssoPoint2D pt, ModelViewSurface surface) { float leftX = Math.Min(binding.Source.Location.X, binding.Target.Location.X); float rightX = Math.Max(binding.Source.Location.X, binding.Target.Location.X); // Прямоугольник вогруг одной из размерных линий SKRect r = new SKRect() { Left = binding.Source.Location.X - 5, Right = binding.Source.Location.X + 5, Top = Math.Min(binding.Source.Location.Y, binding.LinePlace.Y), Bottom = Math.Max(binding.Source.Location.Y, binding.LinePlace.Y) }; if (r.Contains(pt.X, pt.Y)) { return(true); } // Прямоугольник вокруг горизонтальной линии r.Top = binding.LinePlace.Y - 5; r.Bottom = binding.LinePlace.Y + 5; r.Left = leftX - 5; r.Right = rightX + 5; if (r.Contains(pt.X, pt.Y)) { return(true); } // Прямоугольник вогруг второй размерной линии r.Left = binding.Target.Location.X - 5; r.Right = binding.Target.Location.X + 5; r.Top = Math.Min(binding.Target.Location.Y, binding.LinePlace.Y); r.Bottom = Math.Max(binding.Target.Location.Y, binding.LinePlace.Y); if (r.Contains(pt.X, pt.Y)) { return(true); } // Прямоугольник вогруг текста размера float middle = (leftX + rightX) / 2; string dim = binding.Value.ToString("G0"); float tsize = dim.Length * 16; r.Left = middle - tsize / 2 - 5; r.Right = middle + tsize / 2 + 5; r.Top = binding.LinePlace.Y; r.Bottom = binding.LinePlace.Y + 20; if (r.Contains(pt.X, pt.Y)) { return(true); } return(false); }
/// <summary> /// Touch Event handler on the Console Canvas /// </summary> /// <param name="e"></param> void TouchConsole(SKTouchEventArgs e) { if (e.ActionType == SKTouchAction.Pressed) { if (_powerOnButtonRect.Contains(e.Location)) { if (!_powerOn) { _powerOn = true; _framesRendered = 0; _emu.Start(); _measureFpsTask = Task.Factory.StartNew(MeasureFPS); } else { _emu.Stop(); _powerOn = false; _renderStaticTask = Task.Factory.StartNew(RenderStatic); MessagingCenter.Send(this, "InvalidateEmulatorSurface"); } } MessagingCenter.Send(this, "InvalidateConsoleSurface"); } }
public void HitTest(SKPoint location) { var scalar = canvasView.CanvasSize.Height / 200f; var halfWidth = canvasView.CanvasSize.Width / scalar / 2; var height = canvasView.CanvasSize.Height / scalar; float newX = (float)((location.X / scalar) - (halfWidth)); float newY = (float)((location.Y / scalar) - height - (scrollHeight / scalar)); SKPoint hitPoint = new SKPoint(newX, newY); if (hitBounds.Contains(hitPoint)) { foreach (SKRect rect in hitRects) { if (rect.Contains(hitPoint)) { var hitIndex = hitRects.IndexOf(rect) + 1; if (hitIndex != animationIndex) { animationIndex = hitIndex; animateClick = true; } } } } }
private void ImageControls_TouchAction(object sender, TouchActionEventArgs args) { // https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/transforms/touch var point = new SKPoint((float)(CanvasView.CanvasSize.Width * args.Location.X / CanvasView.Width), (float)(CanvasView.CanvasSize.Height * args.Location.Y / CanvasView.Height)); switch (args.Type) { case TouchActionType.Pressed: var rect = new SKRect(0, 0, Design.Bitmap.Width, Design.Bitmap.Height); rect = Matrix.MapRect(rect); if (rect.Contains(point) && !Touches.ContainsKey(args.Id)) { Touches.Add(args.Id, point); } break; case TouchActionType.Moved: MoveOrScaleCanvas(args.Id, point); break; case TouchActionType.Released: case TouchActionType.Exited: case TouchActionType.Cancelled: if (Touches.ContainsKey(args.Id)) { Touches.Remove(args.Id); } return; } }
private bool TouchInControlArea(float inX, float inY) { if (_realm == null || _loginIconTouchRect.Contains(inX, inY)) // treat entire screen as control area { InvalidateCachedPaths(); CredentialsEditor.Invoke(); // TODO only invalidate if changed server?? return(true); } if (inY < _pencilsTop) { return(false); } // see opposite calc in DrawBackground var pencilIndex = (int)(inX / (_pencilWidth + PENCIL_MARGIN)); var selectecColor = SwatchColor.Colors[pencilIndex]; if (!selectecColor.Name.Equals(CurrentColor.Name)) { CurrentColor = selectecColor; // will update saved settings } InvalidateCachedPaths(); return(true); // if in this area even if didn't actually change }
public virtual Drawable?HitTest(SKPoint skPoint) { if (TransformedBounds.Contains(skPoint)) { return(this); } return(null); }
public override bool OnHitTest(SKPoint location, long fingerId) { SKPoint hitPoint = GetNewCoordinatedPoint(location); //左上角 SKRect rect = SKRect.Create(_cropRect.Left - _cornerTouchRadius, _cropRect.Top - _cornerTouchRadius, _cornerTouchRadius * 2, _cornerTouchRadius * 2); if (rect.Contains(hitPoint)) { _hittedCorner = CornerType.LeftTop; return(true); } //右上角 rect = SKRect.Create(_cropRect.Right - _cornerTouchRadius, _cropRect.Top - _cornerTouchRadius, _cornerTouchRadius * 2, _cornerTouchRadius * 2); if (rect.Contains(hitPoint)) { _hittedCorner = CornerType.RightTop; return(true); } //左下角 rect = SKRect.Create(_cropRect.Left - _cornerTouchRadius, _cropRect.Bottom - _cornerTouchRadius, _cornerTouchRadius * 2, _cornerTouchRadius * 2); if (rect.Contains(hitPoint)) { _hittedCorner = CornerType.LeftBottom; return(true); } //右下角 rect = SKRect.Create(_cropRect.Right - _cornerTouchRadius, _cropRect.Bottom - _cornerTouchRadius, _cornerTouchRadius * 2, _cornerTouchRadius * 2); if (rect.Contains(hitPoint)) { _hittedCorner = CornerType.RightBottom; return(true); } _hittedCorner = CornerType.None; return(false); }
public virtual bool IsPointInside(SKPoint point, SKMatrix transfromStack) { var mat = transfromStack;//.Concat(this.VisualTransform); if (mat.TryInvert(out var invert)) { point = invert.MapPoint(point); } return(boundsMinusMargin.Contains(point)); }
public bool Contains(float x, float y, ContainsMode mode) { if (mode == ContainsMode.Geometry) { return(Geometry.Contains(x, y)); } else if (mode == ContainsMode.Bounds) { return(Bounds.Contains(x, y)); } return(false); }
public Plot PlotAtPoint(SKSize figureSize, SKPoint point) { foreach (Plot subplot in subplots) { SKRect rect = SubplotRect(figureSize, subplot); if (rect.Contains(point)) { return(subplot); } } return(null); }
public Plot GetPlotUnderCursor(SKCanvas canvas, SKPoint mouseLocation) { if (canvas != null) { foreach (Plot plt in subplots) { SKRect subplotRect = FigureSettings.Layout.GetRectangle(canvas, plt.subplotPosition, padding); if (subplotRect.Contains(mouseLocation)) { return(plt); } } } return(null); }
public bool HitTest(SKPoint location) { // Invert the matrix SKMatrix inverseMatrix; if (Matrix.TryInvert(out inverseMatrix)) { // Transform the point using the inverted matrix SKPoint transformedPoint = inverseMatrix.MapPoint(location); // Check if it's in the untransformed bitmap rectangle SKRect rect = new SKRect(0, 0, bitmap.Width, bitmap.Height); return(rect.Contains(transformedPoint)); } return(false); }
protected override void OnTouch(SKTouchEventArgs e) { switch (e.ActionType) { case SKTouchAction.Pressed: if (skBallBounds.Contains(e.Location)) { touching = true; skTouchForce = new Vector2(0); oldTouchLocation = e.Location; // Snap ball to touch position skPosition.X = e.Location.X; skPosition.Y = e.Location.Y; skVelocity.X = 0; skVelocity.Y = 0; } break; case SKTouchAction.Moved: if (touching) { diffLocation = e.Location - oldTouchLocation; oldTouchLocation = e.Location; // Drag the ball around the screen skPosition.X = e.Location.X; skPosition.Y = e.Location.Y; } break; case SKTouchAction.Released: if (touching) { // Compute force based on lat movement skTouchForce.X = diffLocation.X * ForceScale; skTouchForce.Y = diffLocation.Y * ForceScale; } touching = false; break; } e.Handled = true; base.OnTouch(e); }
public override HitTestResults HitTest(SKPointI point) { if (!ScreenRect.Contains(point)) { return(null); } for (int i = 0; i < MaxNumberOfSkills; ++i) { SKRect buttonRect = RectForSkill(i); buttonRect.Offset(Position.X, Position.Y); if (buttonRect.Contains(point)) { return(new HitTestResults(this, i)); } } return(null); }
/// <summary> /// Проверка при нажатии на схему /// </summary> /// <param name="location">Точка прикосновения</param> /// <returns>Помещение, если в месте нажатия было помещение</returns> public Room HitTest(SKPoint location) { SKMatrix inverseMatrix; if (Matrix.TryInvert(out inverseMatrix)) { SKPoint transformedPoint = inverseMatrix.MapPoint(location); foreach (var r in PointData.CurrentFloorRoomPoints) { SKRect rect = SKRect.Create((float)r.X - 25, (float)r.Y - 25, 50, 50); if (rect.Contains(transformedPoint)) { return(r.Room); } } } return(null); }
public override Drawable?HitTest(SKPoint skPoint) { if (Image != null) { if (DestRect.Contains(skPoint)) { return(this); } } if (FragmentDrawable != null) { var result = FragmentDrawable?.HitTest(skPoint); if (result != null) { return(result); } } return(base.HitTest(skPoint)); }
public override bool Contains(SKPoint c) { var tolerance = 5f; //5-pixel tolerance foreach (var path in _completedPaths) { for (int i = 1; i < path.PointCount; i++) { var a = path[i - 1]; var b = path[i]; //Box with tolerance padding to ensure we are near the line segment var box = new SKRect { Left = Math.Min(a.X, b.X) - tolerance, Right = Math.Max(a.X, b.X) + tolerance, Top = Math.Min(a.Y, b.Y) - tolerance, Bottom = Math.Max(a.Y, b.Y) + tolerance }; if (!box.Contains(c)) { continue;//outside of the bounding box + tolerance of this line segment } //https://stackoverflow.com/questions/910882/how-can-i-tell-if-a-point-is-nearby-a-certain-line var quotient = (b.X - a.X) * (a.Y - c.Y) - (a.X - c.X) * (b.Y - a.Y); var divisor = Math.Sqrt(Math.Pow(b.X - a.X, 2) + Math.Pow(b.Y - a.Y, 2)); var distanceFromLine = quotient / divisor; if (Math.Abs(distanceFromLine) > tolerance) { continue; } return(true); } } return(false); }
/* TOUCH SENISITIVIY ********************************************************************** *********************************************************************/ private void OnTouch(object sender, SKTouchEventArgs e) { switch (e.ActionType) { case SKTouchAction.Pressed: //Debug.WriteLine(this.Id); if (rect_CP0.Contains(e.Location)) { Deadlock.CPx_Clicked(ref this.skiaview, ref this.touchable, 0, this.vectorA); } else if (rect_CP1.Contains(e.Location)) { Deadlock.CPx_Clicked(ref this.skiaview, ref this.touchable, 1, this.vectorA); } else if (rect_CP2.Contains(e.Location)) { Deadlock.CPx_Clicked(ref this.skiaview, ref this.touchable, 2, this.vectorA); } else if (rect_CP3.Contains(e.Location)) { Deadlock.CPx_Clicked(ref this.skiaview, ref this.touchable, 3, this.vectorA); } else if (rect_CP4.Contains(e.Location)) { Deadlock.CPx_Clicked(ref this.skiaview, ref this.touchable, 4, this.vectorA); } break; } e.Handled = true; }
internal int HitTest(SKPoint location, SKRect info) { if (Matrix.TryInvert(out SKMatrix inverseMatrix)) { SKRect rect = new SKRect(0, 0, Bitmap.Width, Bitmap.Height); SKPoint transformedPoint = inverseMatrix.MapPoint(location); if (Device.RuntimePlatform == Device.UWP) { float max = info.Width > info.Height ? info.Width : info.Height; float radius = max * 0.025f; var corners = new SKPoint[] { new SKPoint(rect.Left, rect.Top), new SKPoint(rect.Right, rect.Top), new SKPoint(rect.Right, rect.Bottom), new SKPoint(rect.Left, rect.Bottom) }; for (int index = 0; index < corners.Length; index++) { SKPoint diff = transformedPoint - corners[index]; float delta = (float)Math.Sqrt(diff.X * diff.X + diff.Y * diff.Y); if (delta < radius) { return(index); } } } return(rect.Contains(transformedPoint) ? 4 : -1); } return(-1); }
private void OnTouchBitmapEffectAction(TouchActionEventArgs args, SKPoint point) { if (args.Type != TouchActionType.Moved) { TrashEnabled?.Invoke(false, false, false); } if (bitmapDictionary == null) { bitmapDictionary = new Dictionary <long, TouchManipulationBitmap>(); } if (bitmapCollection == null) { bitmapCollection = new List <TouchManipulationBitmap>(); } switch (args.Type) { case TouchActionType.Pressed: if (!bitmapDictionary.ContainsKey(args.Id)) { bool isFindBitmap = false; if (bitmapCollection?.Count > 0) { for (int i = bitmapCollection.Count - 1; i >= 0; i--) { TouchManipulationBitmap bitmap = bitmapCollection[i]; int testResult = bitmap.HitTest(point, rectInfo); if (testResult != -1) { if (Device.RuntimePlatform == Device.UWP) { bitmap.TouchManager.Mode = testResult == 4 ? TouchManipulationMode.ScaleRotate : TouchManipulationMode.ScaleDualRotate; } bitmap.TouchAction = TouchActionType.Pressed; bitmapCollection.Remove(bitmap); bitmapCollection.Add(bitmap); // 进行触摸处理 bitmapDictionary.Add(args.Id, bitmap); bitmap.ProcessTouchEvent(args.Id, args.Type, point); isFindBitmap = true; InvalidateSurface(); break; } } } if ((config?.CanTransformMainBitmap ?? false) && !isFindBitmap && mainBitmap.HitTest(point, rectInfo) != -1) { bitmapDictionary.Add(args.Id, mainBitmap); mainBitmap.ProcessTouchEvent(args.Id, args.Type, point); InvalidateSurface(); } } break; case TouchActionType.Moved: if (bitmapDictionary.ContainsKey(args.Id)) { TouchManipulationBitmap bitmap = bitmapDictionary[args.Id]; bitmap.TouchAction = TouchActionType.Moved; bitmap.ProcessTouchEvent(args.Id, args.Type, point); if (bitmap.Type != BitmapType.Main) { if (rectTrash.Contains(point)) { TrashEnabled?.Invoke(false, true, !rectTrash.Contains(previousTouchPoint)); } else { TrashEnabled?.Invoke(true, false, false); } } else { TrashEnabled?.Invoke(false, false, false); } InvalidateSurface(); } else { TrashEnabled?.Invoke(false, false, false); } break; case TouchActionType.Released: case TouchActionType.Cancelled: if (bitmapDictionary.ContainsKey(args.Id)) { TouchManipulationBitmap bitmap = bitmapDictionary[args.Id]; bitmap.ProcessTouchEvent(args.Id, args.Type, point); bitmapDictionary.Remove(args.Id); if (bitmap.TouchAction == TouchActionType.Pressed && bitmap.Type == BitmapType.Text) { bitmap.IsHide = true; TextBitmapClicked?.Invoke(bitmap); } bitmap.TouchAction = null; if (rectTrash.Contains(point)) { bitmapCollection.Remove(bitmap); } InvalidateSurface(); } break; } }
/// <summary> /// Touch Event handler on the Controller Canvas /// </summary> /// <param name="e"></param> void TouchController(SKTouchEventArgs e) { switch (e.ActionType) { case SKTouchAction.Moved: break; case SKTouchAction.Pressed: { //Determine which button is being pressed enumButtons buttonPressed; if (_controllerUpRect.Contains(e.Location)) { buttonPressed = enumButtons.Up; } else if (_controllerDownRect.Contains(e.Location)) { buttonPressed = enumButtons.Down; } else if (_controllerRightRect.Contains(e.Location)) { buttonPressed = enumButtons.Right; } else if (_controllerLeftRect.Contains(e.Location)) { buttonPressed = enumButtons.Left; } else if (_controllerStartRect.Contains(e.Location)) { buttonPressed = enumButtons.Start; } else if (_controllerSelectRect.Contains(e.Location)) { buttonPressed = enumButtons.Select; } else if (_controllerARect.Contains(e.Location)) { buttonPressed = enumButtons.A; } else if (_controllerBRect.Contains(e.Location)) { buttonPressed = enumButtons.B; } else { return; } //Track The Event _controllerEventTracker.Add(e.Id, buttonPressed); //Press It _emu.Controller1.ButtonPress(buttonPressed); } break; case SKTouchAction.Released: { //Find the button this event started pressing _controllerEventTracker.TryGetValue(e.Id, out var buttonReleased); //Release that button _emu.Controller1.ButtonRelease(buttonReleased); //Clear this event _controllerEventTracker.Remove(e.Id); } break; } //Handle Event and bail e.Handled = true; }
void OnTouchEffectAction(object sender, TouchActionEventArgs args) { // Convert Xamarin.Forms point to pixels Point pt = args.Location; SKPoint point = new SKPoint((float)(canvasView.CanvasSize.Width * pt.X / canvasView.Width), (float)(canvasView.CanvasSize.Height * pt.Y / canvasView.Height)); switch (args.Type) { case TouchActionType.Pressed: // Track only one finger if (touchId.HasValue) { return; } // Check if the finger is within the boundaries of the bitmap SKRect rect = new SKRect(0, 0, bitmap.Width, bitmap.Height); rect = currentMatrix.MapRect(rect); if (!rect.Contains(point)) { return; } // First assume there will be no scaling isScaling = false; // If touch is outside interior ellipse, make this a scaling operation if (Math.Pow((point.X - rect.MidX) / (rect.Width / 2), 2) + Math.Pow((point.Y - rect.MidY) / (rect.Height / 2), 2) > 1) { isScaling = true; float xPivot = point.X < rect.MidX ? rect.Right : rect.Left; float yPivot = point.Y < rect.MidY ? rect.Bottom : rect.Top; pivotPoint = new SKPoint(xPivot, yPivot); } // Common for either pan or scale touchId = args.Id; pressedLocation = point; pressedMatrix = currentMatrix; break; case TouchActionType.Moved: if (!touchId.HasValue || args.Id != touchId.Value) { return; } SKMatrix matrix = SKMatrix.MakeIdentity(); // Translating if (!isScaling) { SKPoint delta = point - pressedLocation; matrix = SKMatrix.MakeTranslation(delta.X, delta.Y); } // Scaling else { float scaleX = (point.X - pivotPoint.X) / (pressedLocation.X - pivotPoint.X); float scaleY = (point.Y - pivotPoint.Y) / (pressedLocation.Y - pivotPoint.Y); matrix = SKMatrix.MakeScale(scaleX, scaleY, pivotPoint.X, pivotPoint.Y); } // Concatenate the matrices SKMatrix.PreConcat(ref matrix, pressedMatrix); currentMatrix = matrix; canvasView.InvalidateSurface(); break; case TouchActionType.Released: case TouchActionType.Cancelled: touchId = null; break; } }
void OnTouchEffectAction(object sender, TouchActionEventArgs args) { // Convert Xamarin.Forms point to pixels Point pt = args.Location; SKPoint point = new SKPoint((float)(canvasView.CanvasSize.Width * pt.X / canvasView.Width), (float)(canvasView.CanvasSize.Height * pt.Y / canvasView.Height)); SKPoint offsetpoint = new SKPoint((float)(canvasView.CanvasSize.Width * pt.X / canvasView.Width), (float)((canvasView.CanvasSize.Height * pt.Y / canvasView.Height) + 100)); switch (args.Type) { case TouchActionType.Pressed: for (int i = bitmapCollection.Count - 1; i >= 0; i--) { TouchManipulationBitmap bitmap = bitmapCollection[i]; //350, 179, SKRect rect = new SKRect(0, 0, (float)bitmaptEST.Width + 100, (float)bitmaptEST.Height + 100); rect.Offset(300, 129); // Determine if the touch was within that rectangle if (rect.Contains(point)) { //if (bitmap.HitTest(point)) //{ // Move bitmap to end of collection bitmapCollection.Remove(bitmap); bitmapCollection.Add(bitmap); // Do the touch processing bitmapDictionary.Add(args.Id, bitmap); bitmap.ProcessTouchEvent(args.Id, args.Type, offsetpoint); canvasView.InvalidateSurface(); break; //} } } //testa barras posicionadas for (int i = VisiblebitmapCollection.Count - 1; i >= 0; i--) { TouchManipulationBitmap bitmap = VisiblebitmapCollection[i]; if (bitmap.HitTest(point)) { InvisiblebitmapCollection.Add(bitmap); VisiblebitmapCollection.Remove(bitmap); canvasView.InvalidateSurface(); break; } } break; case TouchActionType.Moved: if (bitmapDictionary.ContainsKey(args.Id)) { TouchManipulationBitmap bitmap = bitmapDictionary[args.Id]; bitmap.ProcessTouchEvent(args.Id, args.Type, point); canvasView.InvalidateSurface(); } break; case TouchActionType.Released: case TouchActionType.Cancelled: if (bitmapDictionary.ContainsKey(args.Id)) { TouchManipulationBitmap bitmap = bitmapDictionary[args.Id]; bitmap.ProcessTouchEvent(args.Id, args.Type, point); foreach (var item in InvisiblebitmapCollection) { if (new Point(bitmap.Matrix.TransX, bitmap.Matrix.TransY).Distance(new Point(item.Matrix.TransX, item.Matrix.TransY)) < 30) { InvisiblebitmapCollection.Remove(item); VisiblebitmapCollection.Add(item); break; } } bitmapCollection.Remove(bitmap); CriaBarraHorizontal(); bitmapDictionary.Remove(args.Id); canvasView.InvalidateSurface(); } break; } }
/********************************************************************** *********************************************************************/ private async void OnTouch(object sender, SKTouchEventArgs e) { switch (e.ActionType) { case SKTouchAction.Pressed: if (rect_wUV.Contains(e.Location)) { await dS.OpenPickerPopUp(); SetWeight(ref weightUV, action); } else if (rect_wUX.Contains(e.Location)) { await dS.OpenPickerPopUp(); SetWeight(ref weightUX, action); } else if (rect_wUW.Contains(e.Location) && (this.id == 2 || this.id == 4)) { await dS.OpenPickerPopUp(); SetWeight(ref weightUW, action); } else if (rect_wUY.Contains(e.Location) && (this.id == 3 || this.id == 4)) { await dS.OpenPickerPopUp(); SetWeight(ref weightUY, action); } else if (rect_wZW.Contains(e.Location)) { await dS.OpenPickerPopUp(); SetWeight(ref weightZW, action); } else if (rect_wZY.Contains(e.Location)) { await dS.OpenPickerPopUp(); SetWeight(ref weightZY, action); } else if (rect_wZV.Contains(e.Location) && this.id == 4) { await dS.OpenPickerPopUp(); SetWeight(ref weightZV, action); } else if (rect_wZX.Contains(e.Location) && this.id == 4) { await dS.OpenPickerPopUp(); SetWeight(ref weightZX, action); } else if (rect_wVX.Contains(e.Location) && (this.id == 2 || this.id == 3 || this.id == 4)) { await dS.OpenPickerPopUp(); SetWeight(ref weightVX, action); } else if (rect_wVY.Contains(e.Location) && this.id == 1) { await dS.OpenPickerPopUp(); SetWeight(ref weightVY, action); } else if (rect_wVW.Contains(e.Location)) { await dS.OpenPickerPopUp(); SetWeight(ref weightVW, action); } else if (rect_wXW.Contains(e.Location) && (this.id == 1 || this.id == 2 || this.id == 3)) { await dS.OpenPickerPopUp(); SetWeight(ref weightXW, action); } else if (rect_wXY.Contains(e.Location)) { await dS.OpenPickerPopUp(); SetWeight(ref weightXY, action); } else if (rect_wYW.Contains(e.Location) && (this.id == 2 || this.id == 3 || this.id == 4)) { await dS.OpenPickerPopUp(); SetWeight(ref weightYW, action); } break; } e.Handled = true; }
public static IList <ChartValueItemParam> GetChartValueItemFromX(this IList <ChartItem> chartItems, float xPosition, SKRect frame, float itemWidth) { var items = new List <ChartValueItemParam>(); if (chartItems?.Any(x => x.IsVisible) != true) { return(items); } foreach (var chartEntry in chartItems.Where(x => x.Items?.Any() == true && x.IsVisible)) { var valueItems = chartEntry.Items; ChartValueItem item = null; if (true) { // Order list and takes the first that's lower then X value item = valueItems.OrderByDescending(c => c.Point.X).FirstOrDefault(c => c.Point.X <= xPosition); } else { // Create a bound and get item inside this bound foreach (var valueItem in valueItems) { SKRect rect = new SKRect((xPosition - (itemWidth / 2)), 0, (xPosition + (itemWidth / 2)), frame.Bottom + 2); if (rect.Contains(valueItem.Point)) { item = valueItem; } } } // No item found so check if we touched left of the frame if (item == null) { item = valueItems.FirstOrDefault(); if ((xPosition <= frame.Left && item.Point.X == frame.Left)) { items.Add(new ChartValueItemParam(item, null, chartEntry)); } continue; } // Get the index of the found item int index = valueItems.ToList().IndexOf(item); // It's the last item in the list so add just the this entry if (index + 1 >= valueItems.Count()) { if ((xPosition >= frame.Right && item.Point.X == frame.Right) || item.Point.X >= (xPosition - (itemWidth / 2))) { items.Add(new ChartValueItemParam(item, null, chartEntry)); } continue; } // Takes the next item in the list var nextItem = valueItems.ToList()[index + 1]; if (item != null) { // Add current item, next item and parents line color items.Add(new ChartValueItemParam(item, nextItem, chartEntry)); } } return(items); }
void OnTouchEffectAction(object sender, TouchActionEventArgs args) { // Convert Xamarin.Forms point to pixels var pt = args.Location; SKPoint point = new SKPoint((float)(canvasView.CanvasSize.Width * pt.X / canvasView.Width), (float)(canvasView.CanvasSize.Height * pt.Y / canvasView.Height)); switch (args.Type) { case TouchActionType.Pressed: // Find transformed bitmap rectangle SKRect rect = new SKRect(0, 0, bitmap.Width, bitmap.Height); rect = matrix.MapRect(rect); // Determine if the touch was within that rectangle //if (rect.Contains(point)) //{ // // touchId = args.Id; // // previousPoint = point; // touchDictionary.Add(args.Id, point); //} if (rect.Contains(point) && !touchDictionary.ContainsKey(args.Id)) { touchDictionary.Add(args.Id, point); } break; case TouchActionType.Moved: if (touchDictionary.ContainsKey(args.Id)) { //if (touchId == args.Id) if (touchDictionary.Count == 1) { // Adjust the matrix for the new position //matrix.TransX += point.X - previousPoint.X; //matrix.TransY += point.Y - previousPoint.Y; //previousPoint = point; SKPoint prevPoint = touchDictionary[args.Id]; matrix.TransX += point.X - prevPoint.X; matrix.TransY += point.Y - prevPoint.Y; // Move within canvas limits //if (matrix.TransX > 0) matrix.TransX = 0; //if (matrix.TransX < - bitmap.Width + (int)canvasView.CanvasSize.Width) matrix.TransX = - bitmap.Width + (int)canvasView.CanvasSize.Width; //if (matrix.TransY > 0) matrix.TransY = 0; //if (matrix.TransY < -bitmap.Height + (int)canvasView.CanvasSize.Height) matrix.TransY = -bitmap.Height + (int)canvasView.CanvasSize.Height; canvasView.InvalidateSurface(); } else if (touchDictionary.Count >= 2) { // Copy two dictionary keys into array long[] keys = new long[touchDictionary.Count]; touchDictionary.Keys.CopyTo(keys, 0); // Find index of non-moving (pivot) finger int pivotIndex = (keys[0] == args.Id) ? 1 : 0; // Get the three points involved in the transform SKPoint pivotPoint = touchDictionary[keys[pivotIndex]]; SKPoint prevPoint = touchDictionary[args.Id]; SKPoint newPoint = point; // Calculate two vectors SKPoint oldVector = prevPoint - pivotPoint; SKPoint newVector = newPoint - pivotPoint; // Scaling factors are ratios of those scaleX = newVector.X / oldVector.X; scaleY = newVector.Y / oldVector.Y; //if (!float.IsNaN(scaleX) && !float.IsInfinity(scaleX) && // !float.IsNaN(scaleY) && !float.IsInfinity(scaleY)) if (!float.IsNaN(scaleX) && !float.IsInfinity(scaleX)) { // If something bad hasn't happened, calculate a scale and translation matrix SKMatrix scaleMatrix = SKMatrix.MakeScale(scaleX, scaleX, pivotPoint.X, pivotPoint.Y); SKMatrix.PostConcat(ref matrix, scaleMatrix); canvasView.InvalidateSurface(); } } // Store the new point in the dictionary touchDictionary[args.Id] = point; } break; case TouchActionType.Released: case TouchActionType.Cancelled: // touchId = -1; if (touchDictionary.ContainsKey(args.Id)) { touchDictionary.Remove(args.Id); } break; } }
/*Controlla se un punto appartiene ad un rettangolo*/ private bool CheckLocation(SKPoint point, SKRect rect) { return(rect.Contains(point)); }