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
|
/*
* Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
* Copyright (C) 2021 cyoung06
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package kr.syeyoung.dungeonsguide.gui.elements;
import kr.syeyoung.dungeonsguide.config.types.AColor;
import kr.syeyoung.dungeonsguide.gui.MPanel;
import kr.syeyoung.dungeonsguide.utils.RenderUtils;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.gui.Gui;
import net.minecraft.util.MathHelper;
import java.awt.*;
public class MScrollBar extends MPanel {
private final Axis axis;
@Setter
@Getter
private int thumbSize, max, min;
@Setter
@Getter
private int current;
public void addToCurrent(int dv) {
int current2 = current + dv;
current = MathHelper.clamp_int(current2, min, max - thumbSize);
if (max - min < thumbSize) current = min;
if (onUpdate != null) onUpdate.run();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(axis == Axis.X ? -1 : 20, axis == Axis.Y ? -1 : 20);
}
@Getter
@Setter
private int background = RenderUtils.blendAlpha(0xFF141414, 0.04f),
thumb = RenderUtils.blendAlpha(0xFF141414, 0.08f),
thumbHover = RenderUtils.blendAlpha(0xFF141414, 0.09f),
thumbClick = RenderUtils.blendAlpha(0xFF141414, 0.14f);
private Runnable onUpdate;
public MScrollBar(int min, int max, int thumbSize, int current, Axis axis, Runnable onUpdate) {
this.min = min; this.min = max; this.thumbSize = thumbSize; this.current = current; this.axis = axis;
this.current = MathHelper.clamp_int(current, min, max - thumbSize);
if (max - min < thumbSize) this.current = min;
this.onUpdate = onUpdate;
}
@Override
public void render(int absMousex, int absMousey, int relMousex0, int relMousey0, float partialTicks, Rectangle scissor) {
// RENDER SUPER NICE SCROLL BAR
Gui.drawRect(0,0,getBounds().width, getBounds().height, background);
double startPerc, endPerc;
if (max - min == 0) {
startPerc =0; endPerc = 1;
} else {
startPerc = (current - min)/((double)max - min);
endPerc = (current+thumbSize - min)/((double)max - min);
}
int color = thumbHover;
if (getBounds().contains(relMousex0, relMousey0)) color = thumbHover;
if (grabbed) color = thumbClick;
if (axis == Axis.X) {
int startX = (int) (startPerc * getBounds().width);
int endX = (int) (endPerc * getBounds().width);
endX = Math.max(endX, startX + 20);
Gui.drawRect(startX,0,endX,getBounds().height, color);
} else if (axis == Axis.Y) {
int startY = (int) (startPerc * getBounds().height);
int endY = (int) (endPerc * getBounds().height);
endY = Math.max(endY, startY + 20);
Gui.drawRect(0,startY,getBounds().width,endY, color);
}
}
private int lastX;
private int lastY;
private int actualValue;
private boolean grabbed;
@Override
public void mouseClicked(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int mouseButton) {
if (!lastAbsClip.contains(absMouseX, absMouseY)) return;
if (getTooltipsOpen() > 0) return;
grabbed = true;
actualValue = current;
lastX = absMouseX;
lastY = absMouseY;
}
@Override
public void mouseClickMove(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int clickedMouseButton, long timeSinceLastClick) {
if (!grabbed) return;
int dx = absMouseX - lastX, dy = absMouseY - lastY;
lastX = absMouseX;
lastY = absMouseY;
int prevVal = current;
if (axis == Axis.Y) {
actualValue += dy * (max - min) / getBounds().height;
} else if (axis == Axis.X) {
actualValue += dx * (max - min) / getBounds().width;
}
current = MathHelper.clamp_int(actualValue, min, max - thumbSize);
if (max - min < thumbSize) current = min;
if (onUpdate != null && prevVal != current) onUpdate.run();
}
@Override
public void mouseReleased(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int state) {
grabbed= false;
}
public static enum Axis {
X, Y
}
}
|