forked from oxyplot/oxyplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerBase.cs
More file actions
525 lines (473 loc) · 20.5 KB
/
ControllerBase.cs
File metadata and controls
525 lines (473 loc) · 20.5 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ControllerBase.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Provides functionality to handle input events.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot
{
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Provides functionality to handle input events.
/// </summary>
public abstract class ControllerBase : IController
{
/// <summary>
/// A synchronization object that is used when the actual model in the current view is <c>null</c>.
/// </summary>
private readonly object syncRoot = new object();
/// <summary>
/// Initializes a new instance of the <see cref="ControllerBase" /> class.
/// </summary>
protected ControllerBase()
{
this.InputCommandBindings = new List<InputCommandBinding>();
this.MouseDownManipulators = new List<ManipulatorBase<OxyMouseEventArgs>>();
this.MouseHoverManipulators = new List<ManipulatorBase<OxyMouseEventArgs>>();
this.TouchManipulators = new List<ManipulatorBase<OxyTouchEventArgs>>();
}
/// <summary>
/// Gets the input bindings.
/// </summary>
/// <remarks>This collection is used to specify the customized input gestures (both key, mouse and touch).</remarks>
public List<InputCommandBinding> InputCommandBindings { get; private set; }
/// <summary>
/// Gets the manipulators that are created by mouse down events. These manipulators are removed when the mouse button is released.
/// </summary>
protected IList<ManipulatorBase<OxyMouseEventArgs>> MouseDownManipulators { get; private set; }
/// <summary>
/// Gets the manipulators that are created by mouse enter events. These manipulators are removed when the mouse leaves the control.
/// </summary>
protected IList<ManipulatorBase<OxyMouseEventArgs>> MouseHoverManipulators { get; private set; }
/// <summary>
/// Gets the manipulators that are created by touch events. These manipulators are removed when the touch gesture is completed.
/// </summary>
protected IList<ManipulatorBase<OxyTouchEventArgs>> TouchManipulators { get; private set; }
/// <summary>
/// Handles the specified gesture.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="gesture">The gesture.</param>
/// <param name="args">The <see cref="OxyInputEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleGesture(IView view, OxyInputGesture gesture, OxyInputEventArgs args)
{
var command = this.GetCommand(gesture);
return this.HandleCommand(command, view, args);
}
/// <summary>
/// Handles mouse down events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyMouseEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleMouseDown(IView view, OxyMouseDownEventArgs args)
{
lock (this.GetSyncRoot(view))
{
if (view.ActualModel != null)
{
view.ActualModel.HandleMouseDown(this, args);
if (args.Handled)
{
return true;
}
}
var command = this.GetCommand(new OxyMouseDownGesture(args.ChangedButton, args.ModifierKeys, args.ClickCount));
return this.HandleCommand(command, view, args);
}
}
/// <summary>
/// Handles mouse enter events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyMouseEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleMouseEnter(IView view, OxyMouseEventArgs args)
{
lock (this.GetSyncRoot(view))
{
if (view.ActualModel != null)
{
view.ActualModel.HandleMouseEnter(this, args);
if (args.Handled)
{
return true;
}
}
var command = this.GetCommand(new OxyMouseEnterGesture(args.ModifierKeys));
return this.HandleCommand(command, view, args);
}
}
/// <summary>
/// Handles mouse leave events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyMouseEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleMouseLeave(IView view, OxyMouseEventArgs args)
{
lock (this.GetSyncRoot(view))
{
if (view.ActualModel != null)
{
view.ActualModel.HandleMouseLeave(this, args);
if (args.Handled)
{
return true;
}
}
foreach (var m in this.MouseHoverManipulators.ToArray())
{
m.Completed(args);
this.MouseHoverManipulators.Remove(m);
}
return args.Handled;
}
}
/// <summary>
/// Handles mouse move events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyMouseEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleMouseMove(IView view, OxyMouseEventArgs args)
{
lock (this.GetSyncRoot(view))
{
if (view.ActualModel != null)
{
view.ActualModel.HandleMouseMove(this, args);
if (args.Handled)
{
return true;
}
}
foreach (var m in this.MouseDownManipulators)
{
m.Delta(args);
}
foreach (var m in this.MouseHoverManipulators)
{
m.Delta(args);
}
return args.Handled;
}
}
/// <summary>
/// Handles mouse up events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyMouseEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleMouseUp(IView view, OxyMouseEventArgs args)
{
lock (this.GetSyncRoot(view))
{
if (view.ActualModel != null)
{
view.ActualModel.HandleMouseUp(this, args);
if (args.Handled)
{
return true;
}
}
foreach (var m in this.MouseDownManipulators.ToArray())
{
m.Completed(args);
this.MouseDownManipulators.Remove(m);
}
return args.Handled;
}
}
/// <summary>
/// Handles mouse wheel events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyMouseWheelEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleMouseWheel(IView view, OxyMouseWheelEventArgs args)
{
lock (this.GetSyncRoot(view))
{
var command = this.GetCommand(new OxyMouseWheelGesture(args.ModifierKeys));
return this.HandleCommand(command, view, args);
}
}
/// <summary>
/// Handles touch started events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyTouchEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleTouchStarted(IView view, OxyTouchEventArgs args)
{
lock (this.GetSyncRoot(view))
{
if (view.ActualModel != null)
{
view.ActualModel.HandleTouchStarted(this, args);
if (args.Handled)
{
return true;
}
}
var command = this.GetCommand(new OxyTouchGesture());
return this.HandleCommand(command, view, args);
}
}
/// <summary>
/// Handles touch delta events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyTouchEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleTouchDelta(IView view, OxyTouchEventArgs args)
{
lock (this.GetSyncRoot(view))
{
if (view.ActualModel != null)
{
view.ActualModel.HandleTouchDelta(this, args);
if (args.Handled)
{
return true;
}
}
foreach (var m in this.TouchManipulators)
{
m.Delta(args);
}
return args.Handled;
}
}
/// <summary>
/// Handles touch completed events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyTouchEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleTouchCompleted(IView view, OxyTouchEventArgs args)
{
lock (this.GetSyncRoot(view))
{
if (view.ActualModel != null)
{
view.ActualModel.HandleTouchCompleted(this, args);
if (args.Handled)
{
return true;
}
}
foreach (var m in this.TouchManipulators.ToArray())
{
m.Completed(args);
this.TouchManipulators.Remove(m);
}
return args.Handled;
}
}
/// <summary>
/// Handles key down events.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyKeyEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the event was handled.</returns>
public virtual bool HandleKeyDown(IView view, OxyKeyEventArgs args)
{
lock (this.GetSyncRoot(view))
{
if (view.ActualModel == null)
{
return false;
}
view.ActualModel.HandleKeyDown(this, args);
if (args.Handled)
{
return true;
}
var command = this.GetCommand(new OxyKeyGesture(args.Key, args.ModifierKeys));
return this.HandleCommand(command, view, args);
}
}
/// <summary>
/// Adds the specified mouse manipulator and invokes the <see cref="MouseManipulator.Started" /> method with the specified mouse down event arguments.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="manipulator">The manipulator to add.</param>
/// <param name="args">The <see cref="OxyMouseDownEventArgs" /> instance containing the event data.</param>
public virtual void AddMouseManipulator(
IView view,
ManipulatorBase<OxyMouseEventArgs> manipulator,
OxyMouseDownEventArgs args)
{
this.MouseDownManipulators.Add(manipulator);
manipulator.Started(args);
}
/// <summary>
/// Adds the specified mouse hover manipulator and invokes the <see cref="MouseManipulator.Started" /> method with the specified mouse event arguments.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="manipulator">The manipulator.</param>
/// <param name="args">The <see cref="OxyMouseEventArgs" /> instance containing the event data.</param>
public virtual void AddHoverManipulator(
IView view,
ManipulatorBase<OxyMouseEventArgs> manipulator,
OxyMouseEventArgs args)
{
this.MouseHoverManipulators.Add(manipulator);
manipulator.Started(args);
}
/// <summary>
/// Adds the specified mouse hover manipulator and invokes the <see cref="TouchManipulator.Started" /> method with the specified mouse event arguments.
/// </summary>
/// <param name="view">The plot view.</param>
/// <param name="manipulator">The manipulator.</param>
/// <param name="args">The <see cref="OxyTouchEventArgs" /> instance containing the event data.</param>
public virtual void AddTouchManipulator(
IView view,
ManipulatorBase<OxyTouchEventArgs> manipulator,
OxyTouchEventArgs args)
{
this.TouchManipulators.Add(manipulator);
manipulator.Started(args);
}
/// <summary>
/// Binds the specified command to the specified mouse gesture. Removes old bindings to the gesture.
/// </summary>
/// <param name="gesture">The gesture.</param>
/// <param name="command">The command. If <c>null</c>, the binding will be removed.</param>
public virtual void Bind(OxyMouseDownGesture gesture, IViewCommand<OxyMouseDownEventArgs> command)
{
this.BindCore(gesture, command);
}
/// <summary>
/// Binds the specified command to the specified mouse enter gesture. Removes old bindings to the gesture.
/// </summary>
/// <param name="gesture">The gesture.</param>
/// <param name="command">The command. If <c>null</c>, the binding will be removed.</param>
public virtual void Bind(OxyMouseEnterGesture gesture, IViewCommand<OxyMouseEventArgs> command)
{
this.BindCore(gesture, command);
}
/// <summary>
/// Binds the specified command to the specified mouse wheel gesture. Removes old bindings to the gesture.
/// </summary>
/// <param name="gesture">The gesture.</param>
/// <param name="command">The command. If <c>null</c>, the binding will be removed.</param>
public virtual void Bind(OxyMouseWheelGesture gesture, IViewCommand<OxyMouseWheelEventArgs> command)
{
this.BindCore(gesture, command);
}
/// <summary>
/// Binds the specified command to the specified touch gesture. Removes old bindings to the gesture.
/// </summary>
/// <param name="gesture">The gesture.</param>
/// <param name="command">The command. If <c>null</c>, the binding will be removed.</param>
public virtual void Bind(OxyTouchGesture gesture, IViewCommand<OxyTouchEventArgs> command)
{
this.BindCore(gesture, command);
}
/// <summary>
/// Binds the specified command to the specified key gesture. Removes old bindings to the gesture.
/// </summary>
/// <param name="gesture">The gesture.</param>
/// <param name="command">The command. If <c>null</c>, the binding will be removed.</param>
public virtual void Bind(OxyKeyGesture gesture, IViewCommand<OxyKeyEventArgs> command)
{
this.BindCore(gesture, command);
}
/// <summary>
/// Unbinds the specified gesture.
/// </summary>
/// <param name="gesture">The gesture to unbind.</param>
public virtual void Unbind(OxyInputGesture gesture)
{
// ReSharper disable once RedundantNameQualifier
foreach (var icb in this.InputCommandBindings.Where(icb => icb.Gesture.Equals(gesture)).ToArray())
{
this.InputCommandBindings.Remove(icb);
}
}
/// <summary>
/// Unbinds the specified command from all gestures.
/// </summary>
/// <param name="command">The command to unbind.</param>
public virtual void Unbind(IViewCommand command)
{
// ReSharper disable once RedundantNameQualifier
foreach (var icb in this.InputCommandBindings.Where(icb => object.ReferenceEquals(icb.Command, command)).ToArray())
{
this.InputCommandBindings.Remove(icb);
}
}
/// <summary>
/// Unbinds all commands.
/// </summary>
public virtual void UnbindAll()
{
this.InputCommandBindings.Clear();
}
/// <summary>
/// Binds the specified command to the specified gesture. Removes old bindings to the gesture.
/// </summary>
/// <param name="gesture">The gesture.</param>
/// <param name="command">The command. If <c>null</c>, the binding will be removed.</param>
/// <remarks>This method was created to avoid calling a virtual method in the constructor.</remarks>
protected void BindCore(OxyInputGesture gesture, IViewCommand command)
{
var current = this.InputCommandBindings.FirstOrDefault(icb => icb.Gesture.Equals(gesture));
if (current != null)
{
this.InputCommandBindings.Remove(current);
}
if (command != null)
{
this.InputCommandBindings.Add(new InputCommandBinding(gesture, command));
}
}
/// <summary>
/// Gets the command for the specified <see cref="OxyInputGesture" />.
/// </summary>
/// <param name="gesture">The input gesture.</param>
/// <returns>A command.</returns>
protected virtual IViewCommand GetCommand(OxyInputGesture gesture)
{
var binding = this.InputCommandBindings.FirstOrDefault(b => b.Gesture.Equals(gesture));
if (binding == null)
{
return null;
}
return binding.Command;
}
/// <summary>
/// Handles a command triggered by an input gesture.
/// </summary>
/// <param name="command">The command.</param>
/// <param name="view">The plot view.</param>
/// <param name="args">The <see cref="OxyInputEventArgs" /> instance containing the event data.</param>
/// <returns><c>true</c> if the command was handled.</returns>
protected virtual bool HandleCommand(IViewCommand command, IView view, OxyInputEventArgs args)
{
if (command == null)
{
return false;
}
command.Execute(view, this, args);
return args.Handled;
}
/// <summary>
/// Gets the synchronization object for the specified view.
/// </summary>
/// <param name="view">The view.</param>
/// <returns>An object that can be used to synchronize access to the actual model of the view.</returns>
/// <remarks>This object is used to ensure that events are not handled when the model is being updated.</remarks>
protected object GetSyncRoot(IView view)
{
return view.ActualModel != null ? view.ActualModel.SyncRoot : this.syncRoot;
}
}
}