-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cs
89 lines (77 loc) · 2.38 KB
/
Button.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using Android.Graphics;
namespace Segmentus
{
class Button : TouchablePart
{
const bool DebugDraw = false;
const float DiveScale = 0.8f;
const int DiveDuration = 150;
protected DrawablePart face;
public event Action Pressed;
bool disturbed = false;
HandyAnimator diveAnim;
float currentDiveScale;
float currentDiveTime;
float CurrentDiveTime
{
get { return currentDiveTime; }
set
{
currentDiveTime = value;
currentDiveScale = 1 - (1 - DiveScale) * currentDiveTime / DiveDuration;
OnAppearanceChanged();
}
}
public Button(DrawablePart face, Rect localBounds, Pivot parentPivot,
float x = 0, float y = 0) : base(localBounds, parentPivot, x, y) {
CurrentDiveTime = 0;
this.face = face;
}
public override void OnTouchDown(int x, int y)
{
disturbed = true;
AnimateDivingTo(DiveDuration);
}
public override void OnTouchUp(int x, int y)
{
if (disturbed)
{
disturbed = false;
AnimateDivingTo(0);
SoundMaster.PlaySound(SoundMaster.ButtonSound);
Pressed?.Invoke();
}
}
public override void OnTouchCancel(int x, int y)
{
if (disturbed)
{
disturbed = false;
AnimateDivingTo(0);
}
}
void AnimateDivingTo(int divingTimeDest)
{
diveAnim?.core.Cancel();
diveAnim = HandyAnimator.OfFloat(currentDiveTime,
divingTimeDest, (int)Math.Abs(currentDiveTime - divingTimeDest));
diveAnim.Update += (value) => CurrentDiveTime = (float)value;
diveAnim.core.Start();
}
protected override void Draw(Canvas canvas)
{
canvas.Save();
canvas.Scale(currentDiveScale, currentDiveScale);
face.OnDraw(canvas);
canvas.Restore();
if (DebugDraw)
{
Paint p = new Paint();
p.Color = new Color(0, 0, 255);
p.SetStyle(Paint.Style.Stroke);
canvas.DrawRect(localBounds, p);
}
}
}
}