-
Notifications
You must be signed in to change notification settings - Fork 2
/
MaterialShowcaseCalculations.cs
63 lines (52 loc) · 3.38 KB
/
MaterialShowcaseCalculations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using CoreGraphics;
using System;
using UIKit;
namespace iOSMaterialShowcase.Xamarin
{
public static class MaterialShowcaseCalculations
{
public static bool IsInGutter(this MaterialShowcase _materialShowcase, CGPoint _center)
{
return _center.Y < MaterialShowcase.OffsetThreshold || _materialShowcase.containerView.Frame.Height - _center.Y < MaterialShowcase.OffsetThreshold;
}
public static CGPoint GetOuterCircleCenterPoint(this MaterialShowcase _materialShowcase, UIView _target)
{
if (_materialShowcase.IsInGutter(_target.Center))
return _target.Center;
var targetRadius = Math.Max(_target.Frame.Width, _target.Frame.Height) * .5f + MaterialShowcase.TargetPadding;
var totalTextHeight = _materialShowcase.instructionView.Frame.Height;
var onTop = _materialShowcase.GetTargetPosition(_materialShowcase.targetView, _materialShowcase.containerView) == MaterialShowcaseExtension.TargetPosition.Below;
var left = Math.Min(_materialShowcase.instructionView.Frame.GetMinX(), _target.Frame.GetMinX() - targetRadius);
var right = Math.Max(_materialShowcase.instructionView.Frame.GetMaxX(), _target.Frame.GetMaxX() + targetRadius);
var titleHeight = _materialShowcase.instructionView.primaryLabel.Frame.Height;
var centerY = onTop ? _target.Center.Y - MaterialShowcase.TargetHolderRadius - MaterialShowcase.TargetPadding - totalTextHeight + titleHeight
: _target.Center.Y + MaterialShowcase.TargetHolderRadius + MaterialShowcase.TargetPadding + titleHeight;
return new CGPoint((left + right) * .5f, centerY);
}
public static float GetOuterCircleRadius(this MaterialShowcase _materialShowcase, CGPoint _center, CGRect _textBounds, CGRect _targetBounds)
{
var targetCenterX = _targetBounds.GetMidX();
var targetCenterY = _targetBounds.GetMidY();
var expandedRadius = new nfloat(1.1 * MaterialShowcase.TargetHolderRadius);
var expandedBounds = new CGRect(targetCenterX, targetCenterY, 0, 0);
expandedBounds.Inset(-expandedRadius, -expandedRadius);
var textRadius = _materialShowcase.MaxDistance(_center, _textBounds);
var targetRadius = _materialShowcase.MaxDistance(_center, expandedBounds);
return Math.Max(textRadius, targetRadius) + 40;
}
public static float MaxDistance(this MaterialShowcase _materialShowcase, CGPoint _fromPoint, CGRect _toRect)
{
var tl = _materialShowcase.Distance(_fromPoint, new CGPoint(_toRect.GetMinX(), _toRect.GetMinY()));
var tr = _materialShowcase.Distance(_fromPoint, new CGPoint(_toRect.GetMaxX(), _toRect.GetMinY()));
var bl = _materialShowcase.Distance(_fromPoint, new CGPoint(_toRect.GetMinX(), _toRect.GetMaxY()));
var br = _materialShowcase.Distance(_fromPoint, new CGPoint(_toRect.GetMaxX(), _toRect.GetMaxY()));
return Math.Max(tl, Math.Max(tr, Math.Max(bl, br)));
}
public static float Distance(this MaterialShowcase _materialShowcase, CGPoint _a, CGPoint _b)
{
var xDist = _a.X - _b.X;
var yDist = _a.Y - _b.Y;
return (float)Math.Sqrt((xDist * xDist) + (yDist * yDist));
}
}
}