This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
/
BeatmapBeat.xaml.cs
124 lines (107 loc) · 3 KB
/
BeatmapBeat.xaml.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Windows.Controls;
using System.Windows.Input;
namespace ShadowBBR_Editor
{
/// <summary>
/// Interaction logic for BeatmapBeat.xaml
/// </summary>
public partial class BeatmapBeat : UserControl
{
private BeatmapTimeline timeline;
private int targetBeat;
private double canvasLeft;
private double mouseXInitial;
private static BeatmapBeat hovered;
public int TargetBeat
{
get
{
return targetBeat;
}
set
{
targetBeat = value;
canvas.ToolTip = value.ToString();
RefreshPosition();
}
}
public BeatmapBeat(BeatmapTimeline timeline, int height, int targetBeat)
{
InitializeComponent();
this.targetBeat = targetBeat;
this.timeline = timeline;
outerRectangle.Height = height;
innerRectangle.Height = height;
canvas.ToolTip = targetBeat.ToString();
MouseEnter += BeatmapBeat_MouseEnter;
MouseLeave += BeatmapBeat_MouseLeave;
MouseLeftButtonDown += BeatmapBeat_MouseLeftButtonDown;
}
internal void RefreshPosition()
{
Canvas.SetLeft(this, (timeline.BeatPixelDistance * TargetBeat) - 2);
Canvas.SetTop(this, timeline.elementTop);
}
/// <summary>
/// Sets the selection state of the beat.
/// </summary>
/// <param name="state">State to set the beat as</param>
private void SetBeatState(BeatState state)
{
switch (state)
{
case BeatState.None:
hovered = hovered == this ? null : hovered;
outerRectangle.Opacity = 0.3;
break;
case BeatState.Hovering:
hovered = this;
outerRectangle.Opacity = 0.5;
break;
case BeatState.Grab:
outerRectangle.Opacity = 0.6;
break;
}
}
#region Events
private void BeatmapBeat_MouseEnter(object sender, MouseEventArgs e)
{
SetBeatState(BeatState.Hovering);
}
private void BeatmapBeat_MouseLeave(object sender, MouseEventArgs e)
{
SetBeatState(BeatState.None);
}
private void BeatmapBeat_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (hovered == this)
{
SetBeatState(BeatState.Grab);
canvasLeft = Canvas.GetLeft(this);
mouseXInitial = Mouse.GetPosition(timeline).X;
ToolTip = "";
timeline.MouseMove += Parent_MouseMove;
timeline.MouseLeftButtonUp += Parent_MouseLeftButtonUp;
}
}
private void Parent_MouseMove(object sender, MouseEventArgs e)
{
Canvas.SetLeft(this, canvasLeft - (mouseXInitial - Mouse.GetPosition(timeline).X));
//Clamp the position to the edges of the canvas
if (Canvas.GetLeft(this) > timeline.canvasPixelDistance - 2)
Canvas.SetLeft(this, timeline.canvasPixelDistance - 2);
if (Canvas.GetLeft(this) < -2)
Canvas.SetLeft(this, -2);
}
private void Parent_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
timeline.MouseMove -= Parent_MouseMove;
timeline.MouseLeftButtonUp -= Parent_MouseLeftButtonUp;
SetBeatState(BeatState.None);
TargetBeat = (int)Math.Round((Canvas.GetLeft(this) + 2) / timeline.BeatPixelDistance);
}
#endregion
private enum BeatState { None, Hovering, Grab }
}
}