blob: e38a9cbc4270e4ecb1272b7281be090e7ac7fd64 (
plain)
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
|
using System;
using UnityEngine;
public class Scroller : MonoBehaviour
{
public bool AtTop
{
get
{
return this.Inner.localPosition.y <= this.YBounds.min + 0.25f;
}
}
public bool AtBottom
{
get
{
return this.Inner.localPosition.y >= this.YBounds.max - 0.25f;
}
}
public Transform Inner;
public Collider2D HitBox;
private Controller myController = new Controller();
private Vector3 origin;
public bool allowX;
public FloatRange XBounds = new FloatRange(-10f, 10f);
public bool allowY;
public FloatRange YBounds = new FloatRange(-10f, 10f);
public float YBoundPerItem;
public void FixedUpdate()
{
if (!this.Inner)
{
return;
}
Vector2 mouseScrollDelta = Input.mouseScrollDelta;
mouseScrollDelta.y = -mouseScrollDelta.y;
this.DoScroll(this.Inner.transform.localPosition, mouseScrollDelta);
this.myController.Update();
DragState dragState = this.myController.CheckDrag(this.HitBox, false);
if (dragState == DragState.TouchStart)
{
this.origin = this.Inner.transform.localPosition;
return;
}
if (dragState != DragState.Dragging)
{
return;
}
Vector2 del = this.myController.DragPosition - this.myController.DragStartPosition;
this.DoScroll(this.origin, del);
}
private void DoScroll(Vector3 origin, Vector2 del)
{
if (del.magnitude < 0.05f)
{
return;
}
if (!this.allowX)
{
del.x = 0f;
}
if (!this.allowY)
{
del.y = 0f;
}
Vector3 vector = origin + del;
vector.x = this.XBounds.Clamp(vector.x);
int childCount = this.Inner.transform.childCount;
float max = Mathf.Max(this.YBounds.min, this.YBounds.max + this.YBoundPerItem * (float)childCount);
vector.y = Mathf.Clamp(vector.y, this.YBounds.min, max);
this.Inner.transform.localPosition = vector;
}
}
|