GuiSimpleScrollBar.java
3.7 KB
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
package com.mumfrey.liteloader.client.gui;
import net.minecraft.client.gui.Gui;
/**
* Extremely simple scrollbar implementation
*
* @author Adam Mummery-Smith
*/
public class GuiSimpleScrollBar extends Gui
{
/**
* Current value
*/
private int value = 0;
/**
* Current maximum value
*/
private int maxValue = 100;
private int backColour = 0x44FFFFFF;
private int foreColour = 0xFFFFFFFF;
/**
* True if mouse was over the drag bar when last drawn
*/
private boolean mouseOver = false;
/**
* True if currently dragging the scroll bar
*/
private boolean dragging = false;
/**
* Value prior to starting to drag
*/
private int mouseDownValue = 0;
/**
* mouse Y coordinate prior to starting to drag
*/
private int mouseDownY = 0;
/**
* Get the current scroll value
*/
public int getValue()
{
return this.value;
}
/**
* Set the scroll value, the value is clamped between 0 and the current max
* value.
*/
public void setValue(int value)
{
this.value = Math.min(Math.max(value, 0), this.maxValue);
}
/**
* Offset the scroll value by the specified amount, the value is clamped
* between 0 and the current max value.
*/
public void offsetValue(int offset)
{
this.setValue(this.value + offset);
}
/**
* Get the current max value
*/
public int getMaxValue()
{
return this.maxValue;
}
/**
* Sets the current max value
*/
public void setMaxValue(int maxValue)
{
this.maxValue = Math.max(0, maxValue);
this.value = Math.min(this.value, this.maxValue);
}
/**
* Returns true if the mouse was over the drag bar on the last render
*/
public boolean wasMouseOver()
{
return this.mouseOver;
}
/**
* Set the current dragging state
*/
public void setDragging(boolean dragging)
{
this.dragging = dragging;
}
/**
* Draw the scroll bar
*
* @param mouseX
* @param mouseY
* @param partialTicks
* @param xPosition
* @param yPosition
* @param width
* @param height
* @param totalHeight
*/
public void drawScrollBar(int mouseX, int mouseY, float partialTicks, int xPosition, int yPosition, int width, int height, int totalHeight)
{
drawRect(xPosition, yPosition, xPosition + width, yPosition + height, this.backColour);
if (totalHeight > 0)
{
int slideHeight = height - 2;
float pct = Math.min(1.0F, (float)slideHeight / (float)totalHeight);
int barHeight = (int)(pct * slideHeight);
int barTravel = slideHeight - barHeight;
int barPosition = yPosition + 1 + (this.maxValue > 0 ? (int)((this.value / (float)this.maxValue) * barTravel) : 0);
drawRect(xPosition + 1, barPosition, xPosition + width - 1, barPosition + barHeight, this.foreColour);
this.mouseOver = mouseX > xPosition && mouseX < xPosition + width && mouseY > barPosition && mouseY < barPosition + barHeight;
this.handleDrag(mouseY, barTravel);
}
}
/**
* @param mouseY
* @param barTravel
*/
public void handleDrag(int mouseY, int barTravel)
{
if (this.dragging)
{
// Convert pixel delta to value delta
float valuePerPixel = (float)this.maxValue / barTravel;
this.setValue((int)(this.mouseDownValue + ((mouseY - this.mouseDownY) * valuePerPixel)));
}
else
{
this.mouseDownY = mouseY;
this.mouseDownValue = this.value;
}
}
}