GuiScrollPanel.java
5.04 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.client.gui;
import static com.mumfrey.liteloader.gl.GL.*;
import static com.mumfrey.liteloader.gl.GLClippingPlanes.*;
import org.lwjgl.input.Keyboard;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
/**
* Basic non-interactive scrollable panel using OpenGL clipping planes
*
* TODO handle interaction
*
* @author Adam Mummery-Smith
*/
class GuiScrollPanel extends GuiPanel
{
private ScrollPanelContent content;
/**
* Scroll bar for the panel
*/
private GuiSimpleScrollBar scrollBar = new GuiSimpleScrollBar();
/**
* Left edge coord - specified
*/
private int left;
/**
* Top edge coord - specified
*/
private int top;
/**
*
*/
private int contentHeight;
public GuiScrollPanel(Minecraft minecraft, ScrollPanelContent content, int left, int top, int width, int height)
{
super(minecraft);
this.setContent(content);
}
public void setContent(ScrollPanelContent content)
{
if (content == null)
{
throw new IllegalArgumentException("Scroll pane content can not be null");
}
this.content = content;
}
@Override
void setSize(int width, int height)
{
this.width = width;
this.height = height;
this.updateHeight();
}
public void setSizeAndPosition(int left, int top, int width, int height)
{
this.left = left;
this.top = top;
this.setSize(width, height);
}
public void updateHeight()
{
this.contentHeight = this.content.getScrollPanelContentHeight(this);
this.scrollBar.setMaxValue(this.contentHeight - this.height);
}
public void scrollToBottom()
{
this.scrollBar.setValue(this.contentHeight);
}
public void scrollToTop()
{
this.scrollBar.setValue(0);
}
public void scrollBy(int amount)
{
this.scrollBar.offsetValue(amount);
}
public GuiButton addControl(GuiButton control)
{
this.controls.add(control);
return control;
}
/**
* Draw the panel and chrome
*
* @param mouseX
* @param mouseY
* @param partialTicks
*/
@Override
public void draw(int mouseX, int mouseY, float partialTicks)
{
int scrollPosition = this.scrollBar.getValue();
// Clip rect
glEnableClipping(this.left, this.left + this.width - 6, this.top, this.top + this.height);
// Offset by scroll
glPushMatrix();
glTranslatef(this.left, this.top - scrollPosition, 0.0F);
this.content.drawScrollPanelContent(this, mouseX, mouseY, partialTicks, scrollPosition, this.height);
super.draw(mouseX - this.left, mouseY + scrollPosition - this.top, partialTicks);
// Disable clip rect
glDisableClipping();
// Restore transform
glPopMatrix();
// Update and draw scroll bar
this.scrollBar.drawScrollBar(mouseX, mouseY, partialTicks, this.left + this.width - 5, this.top, 5, this.height,
Math.max(this.height, this.contentHeight));
}
@Override
public void mouseWheelScrolled(int mouseWheelDelta)
{
this.scrollBy(-mouseWheelDelta / 8);
}
@Override
public void mousePressed(int mouseX, int mouseY, int mouseButton)
{
mouseY += this.scrollBar.getValue() - this.top;
mouseX -= this.left;
super.mousePressed(mouseX, mouseY, mouseButton);
if (mouseX > 0 && mouseX < this.width && mouseY > 0 && mouseY < this.contentHeight)
{
this.content.scrollPanelMousePressed(this, mouseX, mouseY, mouseButton);
}
if (mouseButton == 0)
{
if (this.scrollBar.wasMouseOver())
{
this.scrollBar.setDragging(true);
}
}
}
@Override
public void mouseReleased(int mouseX, int mouseY, int mouseButton)
{
if (mouseButton == 0)
{
this.scrollBar.setDragging(false);
}
}
@Override
public void keyPressed(char keyChar, int keyCode)
{
if (keyCode == Keyboard.KEY_UP) this.scrollBar.offsetValue(-10);
if (keyCode == Keyboard.KEY_DOWN) this.scrollBar.offsetValue(10);
if (keyCode == Keyboard.KEY_PRIOR) this.scrollBar.offsetValue(-this.height + 10);
if (keyCode == Keyboard.KEY_NEXT) this.scrollBar.offsetValue(this.height - 10);
if (keyCode == Keyboard.KEY_HOME) this.scrollBar.setValue(0);
if (keyCode == Keyboard.KEY_END) this.scrollBar.setValue(this.contentHeight);
}
@Override
void onTick()
{
}
@Override
void onHidden()
{
}
@Override
void onShown()
{
}
@Override
void mouseMoved(int mouseX, int mouseY)
{
}
@Override
void actionPerformed(GuiButton control)
{
this.content.scrollPanelActionPerformed(this, control);
}
}