Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UIScrollbar.cs
Go to the documentation of this file.
5using Terraria.UI;
6
8
9public class UIScrollbar : UIElement
10{
11 private float _viewPosition;
12
13 private float _viewSize = 1f;
14
15 private float _maxViewSize = 20f;
16
17 private bool _isDragging;
18
20
21 private float _dragYOffset;
22
24
26
27 public float ViewPosition
28 {
29 get
30 {
31 return _viewPosition;
32 }
33 set
34 {
36 }
37 }
38
39 public bool CanScroll => _maxViewSize != _viewSize;
40
41 public void GoToBottom()
42 {
44 }
45
46 public UIScrollbar()
47 {
48 Width.Set(20f, 0f);
49 MaxWidth.Set(20f, 0f);
50 _texture = Main.Assets.Request<Texture2D>("Images/UI/Scrollbar", (AssetRequestMode)1);
51 _innerTexture = Main.Assets.Request<Texture2D>("Images/UI/ScrollbarInner", (AssetRequestMode)1);
52 PaddingTop = 5f;
53 PaddingBottom = 5f;
54 }
55
56 public void SetView(float viewSize, float maxViewSize)
57 {
58 viewSize = MathHelper.Clamp(viewSize, 0f, maxViewSize);
59 _viewPosition = MathHelper.Clamp(_viewPosition, 0f, maxViewSize - viewSize);
60 _viewSize = viewSize;
61 _maxViewSize = maxViewSize;
62 }
63
64 public float GetValue()
65 {
66 return _viewPosition;
67 }
68
70 {
71 CalculatedStyle innerDimensions = GetInnerDimensions();
72 if (_maxViewSize == 0f && _viewSize == 0f)
73 {
74 _viewSize = 1f;
75 _maxViewSize = 1f;
76 }
77 return new Rectangle((int)innerDimensions.X, (int)(innerDimensions.Y + innerDimensions.Height * (_viewPosition / _maxViewSize)) - 3, 20, (int)(innerDimensions.Height * (_viewSize / _maxViewSize)) + 7);
78 }
79
80 private void DrawBar(SpriteBatch spriteBatch, Texture2D texture, Rectangle dimensions, Color color)
81 {
82 spriteBatch.Draw(texture, new Rectangle(dimensions.X, dimensions.Y - 6, dimensions.Width, 6), new Rectangle(0, 0, texture.Width, 6), color);
83 spriteBatch.Draw(texture, new Rectangle(dimensions.X, dimensions.Y, dimensions.Width, dimensions.Height), new Rectangle(0, 6, texture.Width, 4), color);
84 spriteBatch.Draw(texture, new Rectangle(dimensions.X, dimensions.Y + dimensions.Height, dimensions.Width, 6), new Rectangle(0, texture.Height - 6, texture.Width, 6), color);
85 }
86
87 protected override void DrawSelf(SpriteBatch spriteBatch)
88 {
89 CalculatedStyle dimensions = GetDimensions();
90 CalculatedStyle innerDimensions = GetInnerDimensions();
91 if (_isDragging)
92 {
93 float num = UserInterface.ActiveInstance.MousePosition.Y - innerDimensions.Y - _dragYOffset;
94 _viewPosition = MathHelper.Clamp(num / innerDimensions.Height * _maxViewSize, 0f, _maxViewSize - _viewSize);
95 }
96 Rectangle handleRectangle = GetHandleRectangle();
97 Vector2 mousePosition = UserInterface.ActiveInstance.MousePosition;
98 bool isHoveringOverHandle = _isHoveringOverHandle;
99 _isHoveringOverHandle = handleRectangle.Contains(new Point((int)mousePosition.X, (int)mousePosition.Y));
100 if (!isHoveringOverHandle && _isHoveringOverHandle && Main.hasFocus)
101 {
103 }
104 DrawBar(spriteBatch, _texture.Value, dimensions.ToRectangle(), Color.White);
105 DrawBar(spriteBatch, _innerTexture.Value, handleRectangle, Color.White * ((_isDragging || _isHoveringOverHandle) ? 1f : 0.85f));
106 }
107
108 public override void LeftMouseDown(UIMouseEvent evt)
109 {
110 base.LeftMouseDown(evt);
111 if (evt.Target == this)
112 {
113 Rectangle handleRectangle = GetHandleRectangle();
114 if (handleRectangle.Contains(new Point((int)evt.MousePosition.X, (int)evt.MousePosition.Y)))
115 {
116 _isDragging = true;
117 _dragYOffset = evt.MousePosition.Y - (float)handleRectangle.Y;
118 }
119 else
120 {
121 CalculatedStyle innerDimensions = GetInnerDimensions();
122 float num = UserInterface.ActiveInstance.MousePosition.Y - innerDimensions.Y - (float)(handleRectangle.Height >> 1);
123 _viewPosition = MathHelper.Clamp(num / innerDimensions.Height * _maxViewSize, 0f, _maxViewSize - _viewSize);
124 }
125 }
126 }
127
128 public override void LeftMouseUp(UIMouseEvent evt)
129 {
130 base.LeftMouseUp(evt);
131 _isDragging = false;
132 }
133}
void Draw(Texture2D texture, Vector2 position, Color color)
static float Clamp(float value, float min, float max)
Definition MathHelper.cs:46
static void PlaySound(int type, Vector2 position, int style=1)
override void DrawSelf(SpriteBatch spriteBatch)
override void LeftMouseDown(UIMouseEvent evt)
void DrawBar(SpriteBatch spriteBatch, Texture2D texture, Rectangle dimensions, Color color)
override void LeftMouseUp(UIMouseEvent evt)
void SetView(float viewSize, float maxViewSize)
static bool hasFocus
Definition Main.cs:1781
static IAssetRepository Assets
Definition Main.cs:209
StyleDimension MaxWidth
Definition UIElement.cs:31
CalculatedStyle GetInnerDimensions()
Definition UIElement.cs:377
StyleDimension Width
Definition UIElement.cs:27
CalculatedStyle GetDimensions()
Definition UIElement.cs:382
readonly UIElement Target
Definition UIEvent.cs:5
readonly Vector2 MousePosition
static UserInterface ActiveInstance
bool Contains(int x, int y)
Definition Rectangle.cs:92
void Set(float pixels, float precent)