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;
5using osu.Framework.Input.Events;
6using osuTK;
7
8namespace osu.Framework.Graphics.Containers
9{
10 /// <summary>
11 /// An item of a <see cref="RearrangeableListContainer{TModel}"/>.
12 /// </summary>
13 /// <typeparam name="TModel"></typeparam>
14 public abstract class RearrangeableListItem<TModel> : CompositeDrawable
15 {
16 /// <summary>
17 /// Invoked on drag start, if an arrangement should be started.
18 /// </summary>
19 internal Action<RearrangeableListItem<TModel>, DragStartEvent> StartArrangement;
20
21 /// <summary>
22 /// Invoked on drag, if this item is being arranged.
23 /// </summary>
24 internal Action<RearrangeableListItem<TModel>, DragEvent> Arrange;
25
26 /// <summary>
27 /// Invoked on drag end, if this item is being arranged.
28 /// </summary>
29 internal Action<RearrangeableListItem<TModel>, DragEndEvent> EndArrangement;
30
31 /// <summary>
32 /// The item this <see cref="RearrangeableListItem{TModel}"/> represents.
33 /// </summary>
34 public readonly TModel Model;
35
36 /// <summary>
37 /// Creates a new <see cref="RearrangeableListItem{TModel}"/>.
38 /// </summary>
39 /// <param name="item">The item to represent.</param>
40 protected RearrangeableListItem(TModel item)
41 {
42 Model = item;
43 }
44
45 /// <summary>
46 /// Whether the item is able to be dragged at the given screen-space position.
47 /// </summary>
48 protected virtual bool IsDraggableAt(Vector2 screenSpacePos) => true;
49
50 protected override bool OnDragStart(DragStartEvent e)
51 {
52 if (IsDraggableAt(e.ScreenSpaceMouseDownPosition))
53 {
54 StartArrangement?.Invoke(this, e);
55 return true;
56 }
57
58 return false;
59 }
60
61 protected override void OnDrag(DragEvent e) => Arrange?.Invoke(this, e);
62
63 protected override void OnDragEnd(DragEndEvent e) => EndArrangement?.Invoke(this, e);
64 }
65}