Skip to content

Commit 4b95dc2

Browse files
committed
DTN GUI ScrollBar Refactor: Add inflation modes and progress setter API
1 parent 45e5af6 commit 4b95dc2

File tree

1 file changed

+59
-13
lines changed
  • src/main/java/doggytalents/client/screen/framework/widget

1 file changed

+59
-13
lines changed

src/main/java/doggytalents/client/screen/framework/widget/ScrollBar.java

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class ScrollBar extends AbstractWidget {
1313
private int barSize;
1414
private double barOffset;
1515
private Direction dir;
16+
private AlignMode alignMode = AlignMode.RIGHT_BOTTOM;
1617
private Screen screen;
1718
private boolean holdInflate;
1819
private long inflateAnim = 0;
@@ -30,6 +31,12 @@ public ScrollBar(int x, int y, int w, int h, Direction dir, int barsize, Screen
3031
}
3132

3233
public static enum Direction { VERTICAL, HORIZONTAL }
34+
public static enum AlignMode { CENTER, LEFT_TOP, RIGHT_BOTTOM }
35+
36+
public ScrollBar alignMode(AlignMode mode) {
37+
this.alignMode = mode;
38+
return this;
39+
}
3340

3441
@Override
3542
protected void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float pTicks) {
@@ -42,11 +49,9 @@ protected void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float
4249
if (passedAnimMillis > 0) {
4350
prevAnimUpdateMillis = currentTimeMillis;
4451
}
45-
int barOffset = Mth.floor(this.barOffset);
4652
if (this.holdInflate) {
4753
this.holdInflate = screen.isDragging();
4854
}
49-
final int BASE_THICK = 3;
5055
if (passedAnimMillis > 0) {
5156
if (!this.isHovered && !holdInflate) {
5257
this.inflateAnim -= passedAnimMillis;
@@ -61,20 +66,56 @@ protected void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float
6166
this.inflateAnim = MAX_INFLATE_ANIM;
6267
}
6368

69+
final int BASE_THICK = 3;
6470
float inflateAnimProgress = ((float) this.inflateAnim) / MAX_INFLATE_ANIM;
65-
int maxBarThick = this.dir == Direction.VERTICAL ?
66-
this.getWidth() : this.getHeight();
67-
int barThickAfterAnim = BASE_THICK + Mth.ceil(inflateAnimProgress*(maxBarThick));
68-
if (this.dir == Direction.VERTICAL)
69-
graphics.fill(this.getX() + this.width - barThickAfterAnim, this.getY(), this.getX()+this.width, this.getY()+this.height, 0x87363636);
70-
else
71-
graphics.fill( this.getX(), this.getY() + this.height - barThickAfterAnim, this.getX()+this.width, this.getY()+this.height, 0x87363636);
71+
72+
int maxThickness = (this.dir == Direction.VERTICAL) ? this.getWidth() : this.getHeight();
73+
74+
int currentThickness = (int) Mth.lerp(inflateAnimProgress, BASE_THICK, maxThickness);
75+
76+
int fill_x = this.getX();
77+
int fill_y = this.getY();
78+
int fill_w = (this.dir == Direction.VERTICAL) ? currentThickness : this.getWidth();
79+
int fill_h = (this.dir == Direction.VERTICAL) ? this.getHeight() : currentThickness;
80+
81+
if (this.dir == Direction.VERTICAL) {
82+
switch (this.alignMode) {
83+
case RIGHT_BOTTOM:
84+
fill_x = this.getX() + this.getWidth() - currentThickness;
85+
break;
86+
case CENTER:
87+
fill_x = this.getX() + this.getWidth()/2 - currentThickness/2;
88+
break;
89+
default:
90+
fill_x = this.getX();
91+
break;
92+
}
93+
} else {
94+
switch (this.alignMode) {
95+
case RIGHT_BOTTOM:
96+
fill_y = this.getY() + this.getHeight() - currentThickness;
97+
break;
98+
case CENTER:
99+
fill_y = this.getY() + (this.getHeight() - currentThickness) / 2;
100+
break;
101+
default:
102+
fill_y = this.getY();
103+
break;
104+
}
105+
}
106+
107+
int barOffset = Mth.floor(this.barOffset);
108+
109+
//Track
110+
graphics.fill(fill_x, fill_y, fill_x + fill_w, fill_y + fill_h, 0x87363636);
111+
112+
//Handle
72113
if (this.dir == Direction.VERTICAL) {
73-
graphics.fill(this.getX() + this.getWidth() - barThickAfterAnim, this.getY() + barOffset,
74-
this.getX()+this.getWidth(), this.getY() + barOffset+this.barSize, 0xffffffff);
114+
graphics.fill(fill_x, fill_y + barOffset,
115+
fill_x + fill_w, fill_y + barOffset + this.barSize, 0xffffffff);
75116
} else {
76-
graphics.fill(this.getX() + barOffset, this.getY() + this.getHeight() - barThickAfterAnim,
77-
this.getX() + barOffset + this.barSize, this.getY() + this.getHeight(), 0xffffffff);
117+
graphics.fill(fill_x + barOffset, fill_y,
118+
fill_x + barOffset + this.barSize, fill_y + fill_h, 0xffffffff);
78119
}
79120
}
80121

@@ -133,6 +174,11 @@ public double getProgressValue() {
133174
return barOffset/ (double)maxOffset;
134175
}
135176

177+
public void setProgressValue(float progress) {
178+
progress = Mth.clamp(progress, 0.0f, 1.0f);
179+
this.setBarOffset(progress * this.getMaxOffsetValue());
180+
}
181+
136182
public void onValueUpdated() {
137183

138184
}

0 commit comments

Comments
 (0)