A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System.Collections.Generic;
5using osu.Framework.Extensions.IEnumerableExtensions;
6using osu.Framework.Input.StateChanges.Events;
7using osu.Framework.Input.States;
8
9namespace osu.Framework.Input.StateChanges
10{
11 /// <summary>
12 /// Denotes a change of a touch input.
13 /// </summary>
14 public class TouchInput : IInput
15 {
16 /// <summary>
17 /// The list of touch structures each providing the source and position to move to.
18 /// </summary>
19 public readonly IEnumerable<Touch> Touches;
20
21 /// <summary>
22 /// Whether to activate the provided <see cref="Touches"/>.
23 /// </summary>
24 public readonly bool Activate;
25
26 /// <summary>
27 /// Constructs a new <see cref="TouchInput"/>.
28 /// </summary>
29 /// <param name="touch">The <see cref="Touch"/>.</param>
30 /// <param name="activate">Whether to activate the provided <param ref="touch"/>, must be true if changing position only.</param>
31 public TouchInput(Touch touch, bool activate)
32 : this(touch.Yield(), activate)
33 {
34 }
35
36 /// <summary>
37 /// Constructs a new <see cref="TouchInput"/>.
38 /// </summary>
39 /// <param name="touches">The list of <see cref="Touch"/>es.</param>
40 /// <param name="activate">Whether to activate the provided <param ref="touches"/>, must be true if changing position only.</param>
41 public TouchInput(IEnumerable<Touch> touches, bool activate)
42 {
43 Touches = touches;
44 Activate = activate;
45 }
46
47 public void Apply(InputState state, IInputStateChangeHandler handler)
48 {
49 var touches = state.Touch;
50
51 foreach (var touch in Touches)
52 {
53 var lastPosition = touches.GetTouchPosition(touch.Source);
54 touches.TouchPositions[(int)touch.Source] = touch.Position;
55
56 bool activityChanged = touches.ActiveSources.SetPressed(touch.Source, Activate);
57 var positionChanged = lastPosition != null && touch.Position != lastPosition;
58
59 if (activityChanged || positionChanged)
60 {
61 handler.HandleInputStateChange(new TouchStateChangeEvent(state, this, touch,
62 !activityChanged ? (bool?)null : Activate,
63 !positionChanged ? null : lastPosition
64 ));
65 }
66 }
67 }
68 }
69}