Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. public enum TriangularButtonDirection
  2. {
  3. Up = 0,
  4. Down = 1
  5. }
  6.  
  7. class TriangularButton : Button
  8. {
  9. public TriangularButton(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
  10. {
  11. this.Initialize(null);
  12. }
  13.  
  14. public TriangularButton(Context context) : base(context)
  15. {
  16. this.Initialize(null);
  17. }
  18.  
  19. public TriangularButton(Context context, IAttributeSet attrs) : base(context, attrs)
  20. {
  21. this.Initialize(attrs);
  22. }
  23.  
  24. public TriangularButton(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
  25. {
  26. this.Initialize(attrs);
  27. }
  28.  
  29. public TriangularButton(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
  30. {
  31. this.Initialize(attrs);
  32. }
  33.  
  34. private TriangularButtonDirection _direction = TriangularButtonDirection.Down;
  35.  
  36. private void Initialize(IAttributeSet attributeSet)
  37. {
  38. if (attributeSet != null)
  39. {
  40. TypedArray a = this.Context.ObtainStyledAttributes(attributeSet, Resource.Styleable.TriangularButton);
  41.  
  42. int direction = a.GetInt(Resource.Styleable.TriangularButton_direction, -1);
  43. if (direction > -1)
  44. this._direction = (TriangularButtonDirection)direction;
  45. a.Recycle();
  46. }
  47. }
  48.  
  49. public override bool OnTouchEvent(MotionEvent e)
  50. {
  51. float x = e.GetX();
  52. float y = e.GetY();
  53. int width = this.MeasuredWidth;
  54. PointF point1Draw;
  55. PointF point2Draw;
  56. PointF point3Draw;
  57. if (this._direction == TriangularButtonDirection.Up)
  58. {
  59. point1Draw = new PointF(0, 3f * width / 4);
  60. point2Draw = new PointF(width, 3f * width / 4);
  61. point3Draw = new PointF(width / 2f, 0);
  62. }
  63. else
  64. {
  65. point1Draw = new PointF(0, 0);
  66. point2Draw = new PointF(width, 0);
  67. point3Draw = new PointF(width / 2f, 3f * width / 4);
  68. }
  69. bool test = PointInTriangle(new PointF(x, y), point1Draw, point2Draw, point3Draw);
  70. if (test)
  71. base.OnTouchEvent(e);
  72. return (test);
  73. }
  74.  
  75. public static bool PointInTriangle(PointF p, PointF p0, PointF p1, PointF p2)
  76. {
  77. float s = p0.Y * p2.X - p0.X * p2.Y + (p2.Y - p0.Y) * p.X + (p0.X - p2.X) * p.Y;
  78. float t = p0.X * p1.Y - p0.Y * p1.X + (p0.Y - p1.Y) * p.X + (p1.X - p0.X) * p.Y;
  79. if ((s < 0) != (t < 0))
  80. return false;
  81. float a = -p1.Y * p2.X + p0.Y * (p2.X - p1.X) + p0.X * (p1.Y - p2.Y) + p1.X * p2.Y;
  82. if (a < 0.0)
  83. {
  84. s = -s;
  85. t = -t;
  86. a = -a;
  87. }
  88. return s > 0 && t > 0 && (s + t) <= a;
  89. }
  90.  
  91. protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
  92. {
  93. base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
  94. this.SetMeasuredDimension(this.MeasuredWidth, 3 * this.MeasuredWidth / 4);
  95. }
  96.  
  97. public override void Draw(Canvas canvas)
  98. {
  99. int width = this.MeasuredWidth;
  100.  
  101. Paint paintFill = new Paint(PaintFlags.AntiAlias);
  102. paintFill.StrokeWidth = 2;
  103. paintFill.Color = this.Hovered ? Color.Red : new Color(242, 180, 54);
  104. paintFill.SetStyle(Android.Graphics.Paint.Style.Fill);
  105. paintFill.AntiAlias = true;
  106.  
  107. Paint paintStroke = new Paint(PaintFlags.AntiAlias);
  108. paintStroke.StrokeWidth = 2;
  109. paintStroke.Color = Color.White;
  110. paintStroke.SetStyle(Android.Graphics.Paint.Style.Stroke);
  111. paintStroke.AntiAlias = true;
  112.  
  113. PointF point1Draw;
  114. PointF point2Draw;
  115. PointF point3Draw;
  116. if (this._direction == TriangularButtonDirection.Up)
  117. {
  118. point1Draw = new PointF(0, 3f * width / 4);
  119. point2Draw = new PointF(width, 3f * width / 4);
  120. point3Draw = new PointF(width / 2f, 0);
  121. }
  122. else
  123. {
  124. point1Draw = new PointF(0, 0);
  125. point2Draw = new PointF(width, 0);
  126. point3Draw = new PointF(width / 2f, 3f * width / 4);
  127. }
  128.  
  129. Path path = new Path();
  130. path.SetFillType(Path.FillType.EvenOdd);
  131. path.MoveTo(point1Draw.X, point1Draw.Y);
  132. path.LineTo(point2Draw.X, point2Draw.Y);
  133. path.LineTo(point3Draw.X, point3Draw.Y);
  134. path.LineTo(point1Draw.X, point1Draw.Y);
  135. path.Close();
  136.  
  137. canvas.DrawPath(path, paintFill);
  138. canvas.DrawPath(path, paintStroke);
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement