-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathVolumeControlViewModel.cs
More file actions
162 lines (141 loc) · 3.59 KB
/
VolumeControlViewModel.cs
File metadata and controls
162 lines (141 loc) · 3.59 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
namespace Bivrost.Bivrost360Player
{
public class VolumeControlViewModel : Screen
{
private VolumeControlView view;
private IInputElement _element;
private bool isDragging = false;
private DateTime lastMouse;
private BackgroundWorker visibilityWorker;
private bool visible = false;
private bool mute = false;
public event Action<double> OnVolumeChange = delegate { };
public VolumeControlViewModel()
{
Volume = 1;
lastMouse = DateTime.Now;
}
protected override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
this.view = view as VolumeControlView;
this.view.Opacity = 0;
this.view.IsHitTestVisible = false;
}
private double _volume = 0.5;
public double Volume {
get
{
return mute ? 0 : this._volume;
}
set
{
this._volume = Math.Max(0, Math.Min(value, 1));
NotifyOfPropertyChange(() => Volume);
OnVolumeChange(Volume);
}
}
public bool IsMuted { get { return this.mute; } }
public void Show()
{
lastMouse = DateTime.Now;
if (!visible)
{
visible = true;
Execute.OnUIThread(() => view.IsHitTestVisible = true);
Animate(true);
visibilityWorker = new BackgroundWorker();
visibilityWorker.DoWork += (sender, parameters) =>
{
while (visible)
{
if ((DateTime.Now - lastMouse).TotalSeconds > 2)
{
Execute.OnUIThread(() => Hide());
}
else
Thread.Sleep(100);
}
};
visibilityWorker.RunWorkerAsync();
}
}
public void Hide()
{
if (visible)
{
visible = false;
view.IsHitTestVisible = false;
Animate(false);
}
}
private void Animate(bool visible)
{
Storyboard storyboard = new Storyboard();
DoubleAnimation opacityAnimation = new DoubleAnimation() { From = view.Opacity, To = visible ? 1 : 0, Duration = TimeSpan.FromSeconds(0.5) };
Storyboard.SetTarget(opacityAnimation, view);
Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
storyboard.Children.Add(opacityAnimation);
storyboard.Begin();
}
public void MouseWheel(MouseWheelEventArgs eventArgs)
{
double vol = Volume + Math.Sign(eventArgs.Delta) * 0.05f;
vol = Math.Max(0, Math.Min(1, vol));
if(!IsMuted)
Volume = vol;
lastMouse = DateTime.Now;
}
public void MouseDown(object sender, MouseButtonEventArgs eventArgs)
{
PositionToVolume(eventArgs.GetPosition(view).Y);
_element = (IInputElement)eventArgs.Source;
_element.CaptureMouse();
_element.MouseMove += MouseMove;
isDragging = true;
eventArgs.Handled = true;
}
public void ToggleMute()
{
mute = !mute;
OnVolumeChange(Volume);
NotifyOfPropertyChange(() => Volume);
}
private void PositionToVolume(double position)
{
//54 / 86
double ratio = view.ActualHeight / 86;
double margin = (86 - 54) * ratio * 0.5;
double vol = ((86 * ratio - position - margin) / (54 * ratio));
vol = Math.Max(0, Math.Min(1, vol));
Volume = vol;
}
public void MouseMove(object sender, MouseEventArgs eventArgs)
{
if (isDragging)
{
PositionToVolume(eventArgs.GetPosition(view).Y);
}
lastMouse = DateTime.Now;
}
public void MouseUp(object sender, MouseButtonEventArgs eventArgs)
{
if (isDragging && _element != null)
{
_element.ReleaseMouseCapture();
isDragging = false;
}
}
}
}