-
Notifications
You must be signed in to change notification settings - Fork 8
/
UIGrid.cs
166 lines (145 loc) · 4.42 KB
/
UIGrid.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.UI;
namespace AutoTrash
{
public class UIGrid : UIElement
{
public delegate bool ElementSearchMethod(UIElement element);
private class UIInnerList : UIElement
{
public override bool ContainsPoint(Vector2 point) {
return true;
}
protected override void DrawChildren(SpriteBatch spriteBatch) {
Vector2 position = this.Parent.GetDimensions().Position();
Vector2 dimensions = new Vector2(this.Parent.GetDimensions().Width, this.Parent.GetDimensions().Height);
foreach (UIElement current in this.Elements) {
Vector2 position2 = current.GetDimensions().Position();
Vector2 dimensions2 = new Vector2(current.GetDimensions().Width, current.GetDimensions().Height);
if (Collision.CheckAABBvAABBCollision(position, dimensions, position2, dimensions2)) {
current.Draw(spriteBatch);
}
}
}
}
public List<UIElement> _items = new List<UIElement>();
protected UIScrollbar _scrollbar;
internal UIElement _innerList = new UIGrid.UIInnerList();
private float _innerListHeight;
public float ListPadding = 5f;
public int Count {
get {
return this._items.Count;
}
}
int cols = 1;
public UIGrid(int columns = 1) {
cols = columns;
this._innerList.OverflowHidden = false;
this._innerList.Width.Set(0f, 1f);
this._innerList.Height.Set(0f, 1f);
this.OverflowHidden = true;
base.Append(this._innerList);
}
public float GetTotalHeight() {
return this._innerListHeight;
}
public void Goto(UIGrid.ElementSearchMethod searchMethod, bool center = false) {
for (int i = 0; i < this._items.Count; i++) {
if (searchMethod(this._items[i])) {
this._scrollbar.ViewPosition = this._items[i].Top.Pixels;
if (center) {
this._scrollbar.ViewPosition = this._items[i].Top.Pixels - GetInnerDimensions().Height / 2 + _items[i].GetOuterDimensions().Height / 2;
}
return;
}
}
}
public virtual void Add(UIElement item) {
this._items.Add(item);
this._innerList.Append(item);
this.UpdateOrder();
this._innerList.Recalculate();
}
public virtual bool Remove(UIElement item) {
this._innerList.RemoveChild(item);
this.UpdateOrder();
return this._items.Remove(item);
}
public virtual void Clear() {
this._innerList.RemoveAllChildren();
this._items.Clear();
}
public override void Recalculate() {
base.Recalculate();
this.UpdateScrollbar();
}
public override void ScrollWheel(UIScrollWheelEvent evt) {
base.ScrollWheel(evt);
if (this._scrollbar != null) {
this._scrollbar.ViewPosition -= (float)evt.ScrollWheelValue;
}
}
public override void RecalculateChildren() {
base.RecalculateChildren();
float top = 0f;
float left = 0f;
for (int i = 0; i < this._items.Count; i++) {
this._items[i].Top.Set(top, 0f);
this._items[i].Left.Set(left, 0f);
this._items[i].Recalculate();
if (i % cols == cols - 1) {
top += this._items[i].GetOuterDimensions().Height + this.ListPadding;
left = 0;
}
else {
left += this._items[i].GetOuterDimensions().Width + this.ListPadding;
}
//num += this._items[i].GetOuterDimensions().Height + this.ListPadding;
}
if (_items.Count > 0) {
top += ListPadding + _items[0].GetOuterDimensions().Height;
}
this._innerListHeight = top;
}
private void UpdateScrollbar() {
if (this._scrollbar == null) {
return;
}
this._scrollbar.SetView(base.GetInnerDimensions().Height, this._innerListHeight);
}
public void SetScrollbar(UIScrollbar scrollbar) {
this._scrollbar = scrollbar;
this.UpdateScrollbar();
}
public void UpdateOrder() {
this._items.Sort(new Comparison<UIElement>(this.SortMethod));
this.UpdateScrollbar();
}
public int SortMethod(UIElement item1, UIElement item2) {
return item1.CompareTo(item2);
}
public override List<SnapPoint> GetSnapPoints() {
List<SnapPoint> list = new List<SnapPoint>();
SnapPoint item;
if (base.GetSnapPoint(out item)) {
list.Add(item);
}
foreach (UIElement current in this._items) {
list.AddRange(current.GetSnapPoints());
}
return list;
}
protected override void DrawSelf(SpriteBatch spriteBatch) {
if (this._scrollbar != null) {
this._innerList.Top.Set(-this._scrollbar.GetValue(), 0f);
}
this.Recalculate();
}
}
}