-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathFlyleafHost.cs
309 lines (250 loc) · 9.97 KB
/
FlyleafHost.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using FlyleafLib.MediaPlayer;
namespace FlyleafLib.Controls.WinForms;
public partial class FlyleafHost : UserControl, IHostPlayer, INotifyPropertyChanged
{
/* TODO
*
* Attached: (UserControl) Host = Surface
* Detached: (Form) Surface
* (Form) Overlay
*
*/
#region Properties / Variables
Player _Player;
public Player Player {
get => _Player;
set {
if (_Player == value)
return;
var oldPlayer = _Player;
_Player = value;
SetPlayer(oldPlayer);
Raise(nameof(Player));
}
}
bool _IsFullScreen;
public bool IsFullScreen {
get => _IsFullScreen;
set
{
if (_IsFullScreen == value)
return;
if (value)
FullScreen();
else
NormalScreen();
}
}
bool _ToggleFullScreenOnDoubleClick = true;
public bool ToggleFullScreenOnDoubleClick
{ get => _ToggleFullScreenOnDoubleClick; set => Set(ref _ToggleFullScreenOnDoubleClick, value); }
public int UniqueId { get; private set; } = -1;
bool _KeyBindings = true;
public bool KeyBindings { get => _KeyBindings; set => Set(ref _KeyBindings, value); }
bool _PanMoveOnCtrl = true;
public bool PanMoveOnCtrl { get => _PanMoveOnCtrl; set => Set(ref _PanMoveOnCtrl, value); }
bool _PanZoomOnCtrlWheel = true;
public bool PanZoomOnCtrlWheel { get => _PanZoomOnCtrlWheel; set => Set(ref _PanZoomOnCtrlWheel, value); }
bool _PanRotateOnShiftWheel = true;
public bool PanRotateOnShiftWheel { get => _PanRotateOnShiftWheel; set => Set(ref _PanRotateOnShiftWheel, value); }
bool _DragMove = true;
public bool DragMove { get => _DragMove; set => Set(ref _DragMove, value); }
bool _OpenOnDrop;
public bool OpenOnDrop { get => _OpenOnDrop; set { Set(ref _OpenOnDrop, value); AllowDrop = _SwapOnDrop || _OpenOnDrop; } }
bool _SwapOnDrop = true;
public bool SwapOnDrop { get => _SwapOnDrop; set { Set(ref _SwapOnDrop, value); AllowDrop = _SwapOnDrop || _OpenOnDrop; } }
bool _SwapDragEnterOnShift = true;
public bool SwapDragEnterOnShift { get => _SwapDragEnterOnShift; set => Set(ref _SwapDragEnterOnShift, value); }
int panPrevX, panPrevY;
Point mouseLeftDownPoint = new(0, 0);
Point mouseMoveLastPoint;
Point oldLocation = Point.Empty;
Size oldSize = Size.Empty;
FormBorderStyle oldStyle = FormBorderStyle.None;
Control oldParent = null;
LogHandler Log;
bool designMode = LicenseManager.UsageMode == LicenseUsageMode.Designtime;
static int idGenerator;
private class FlyleafHostDropWrap { public FlyleafHost FlyleafHost; } // To allow non FlyleafHosts to drag & drop
#endregion
public FlyleafHost()
{
UniqueId = idGenerator++;
AllowDrop = _SwapOnDrop || _OpenOnDrop;
BackColor = Color.Black;
if (designMode)
return;
Log = new LogHandler(("[#" + UniqueId + "]").PadRight(8, ' ') + $" [FlyleafHost NP] ");
KeyUp += Host_KeyUp;
KeyDown += Host_KeyDown;
DoubleClick += Host_DoubleClick;
MouseDown += Host_MouseDown;
MouseMove += Host_MouseMove;
MouseWheel += Host_MouseWheel;
DragEnter += Host_DragEnter;
DragDrop += Host_DragDrop;
}
private void Host_DragDrop(object sender, DragEventArgs e)
{
if (Player == null)
return;
FlyleafHostDropWrap hostWrap = (FlyleafHostDropWrap) e.Data.GetData(typeof(FlyleafHostDropWrap));
if (hostWrap != null)
{
(hostWrap.FlyleafHost.Player, Player) = (Player, hostWrap.FlyleafHost.Player);
return;
}
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string filename = ((string[])e.Data.GetData(DataFormats.FileDrop, false))[0];
Player.OpenAsync(filename);
}
else if (e.Data.GetDataPresent(DataFormats.Text))
{
string text = e.Data.GetData(DataFormats.Text, false).ToString();
if (text.Length > 0)
Player.OpenAsync(text);
}
}
private void Host_DragEnter(object sender, DragEventArgs e) { if (Player != null) e.Effect = DragDropEffects.All; }
private void Host_MouseWheel(object sender, MouseEventArgs e)
{
if (Player == null || e.Delta == 0)
return;
if (PanZoomOnCtrlWheel && ModifierKeys.HasFlag(Keys.Control))
{
System.Windows.Point curDpi = new(e.Location.X, e.Location.Y);
if (e.Delta > 0)
Player.ZoomIn(curDpi);
else
Player.ZoomOut(curDpi);
}
else if (PanRotateOnShiftWheel && ModifierKeys.HasFlag(Keys.Shift))
{
if (e.Delta > 0)
Player.RotateRight();
else
Player.RotateLeft();
}
}
private void Host_MouseMove(object sender, MouseEventArgs e)
{
if (Player == null)
return;
if (e.Location != mouseMoveLastPoint)
{
Player.Activity.RefreshFullActive();
mouseMoveLastPoint = e.Location;
}
if (e.Button != MouseButtons.Left)
return;
if (PanMoveOnCtrl && ModifierKeys.HasFlag(Keys.Control))
{
Player.PanXOffset = panPrevX + e.X - mouseLeftDownPoint.X;
Player.PanYOffset = panPrevY + e.Y - mouseLeftDownPoint.Y;
}
else if (DragMove && Capture && ParentForm != null && !IsFullScreen)
{
ParentForm.Location = new Point(ParentForm.Location.X + e.X - mouseLeftDownPoint.X, ParentForm.Location.Y + e.Y - mouseLeftDownPoint.Y);
}
}
private void Host_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
mouseLeftDownPoint = new Point(e.X, e.Y);
if (Player != null)
{
Player.Activity.RefreshFullActive();
panPrevX = Player.PanXOffset;
panPrevY = Player.PanYOffset;
if (ModifierKeys.HasFlag(Keys.Shift))
{
DoDragDrop(new FlyleafHostDropWrap() { FlyleafHost = this }, DragDropEffects.Move);
}
}
}
private void Host_DoubleClick(object sender, EventArgs e) { if (!ToggleFullScreenOnDoubleClick) return; IsFullScreen = !IsFullScreen; }
private void Host_KeyDown(object sender, KeyEventArgs e) { if (KeyBindings) Player.KeyDown(Player, e); }
private void Host_KeyUp(object sender, KeyEventArgs e) { if (KeyBindings) Player.KeyUp(Player, e); }
public void SetPlayer(Player oldPlayer)
{
// De-assign old Player's Handle/FlyleafHost
if (oldPlayer != null)
{
Log.Debug($"De-assign Player #{oldPlayer.PlayerId}");
oldPlayer.VideoDecoder.DestroySwapChain();
oldPlayer.Host = null;
}
if (Player == null)
return;
Log.Prefix = ("[#" + UniqueId + "]").PadRight(8, ' ') + $" [FlyleafHost #{Player.PlayerId}] ";
// De-assign new Player's Handle/FlyleafHost
Player.Host?.Player_Disposed();
// Assign new Player's (Handle/FlyleafHost)
Log.Debug($"Assign Player #{Player.PlayerId}");
Player.Host = this;
Player.VideoDecoder.CreateSwapChain(Handle);
BackColor = Utils.WPFToWinFormsColor(Player.Config.Video.BackgroundColor);
}
public void FullScreen()
{
if (ParentForm == null)
return;
oldStyle = ParentForm.FormBorderStyle;
oldLocation = Location;
oldSize = Size;
oldParent = Parent;
ParentForm.FormBorderStyle = FormBorderStyle.None;
ParentForm.WindowState = FormWindowState.Maximized;
Parent = ParentForm;
Location = new Point(0, 0);
Size = ParentForm.ClientSize;
BringToFront();
Focus();
_IsFullScreen = true;
Raise(nameof(IsFullScreen));
}
public void NormalScreen()
{
if (ParentForm == null)
return;
ParentForm.FormBorderStyle = oldStyle;
ParentForm.WindowState = FormWindowState.Normal;
Parent = oldParent;
Location = oldLocation;
Size = oldSize;
Focus();
_IsFullScreen = false;
Raise(nameof(IsFullScreen));
}
public bool Player_CanHideCursor() => Focused;
public bool Player_GetFullScreen() => IsFullScreen;
public void Player_SetFullScreen(bool value) => IsFullScreen = value;
public void Player_Disposed() => Player = null;
protected override bool IsInputKey(Keys keyData) => Player != null && Player.Host != null; // Required to allow keybindings such as arrows etc.
// TBR: Related to Renderer's WndProc
protected override void OnPaintBackground(PaintEventArgs pe)
{
if (Player == null || (Player != null && !Player.WFPresent()))
base.OnPaintBackground(pe);
}
protected override void OnPaint(PaintEventArgs pe) { }
public event PropertyChangedEventHandler PropertyChanged;
protected bool Set<T>(ref T field, T value, bool check = true, [CallerMemberName] string propertyName = "")
{
if (!check || (field == null && value != null) || (field != null && !field.Equals(value)))
{
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
return false;
}
protected void Raise([CallerMemberName] string propertyName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}