Skip to content

Commit 76915d7

Browse files
brachy84miozune
authored andcommitted
Cyclebutton rework (CleanroomMC#70)
* texture rework * "minor" changes - abstract cycle & toggle button - helper method for drawable arrays - add widget property for widget theme override - and some minor things * minor stuff (cherry picked from commit f26323b)
1 parent 8057233 commit 76915d7

29 files changed

+526
-295
lines changed

src/main/java/com/cleanroommc/modularui/api/ITheme.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.cleanroommc.modularui.theme.WidgetSlotTheme;
55
import com.cleanroommc.modularui.theme.WidgetTextFieldTheme;
66
import com.cleanroommc.modularui.theme.WidgetTheme;
7-
import com.cleanroommc.modularui.theme.WidgetToggleButtonTheme;
7+
import com.cleanroommc.modularui.theme.WidgetThemeSelectable;
88

99
/**
1010
* A theme is parsed from json and contains style information like color or background texture.
@@ -48,10 +48,18 @@ static ITheme get(String id) {
4848

4949
WidgetTextFieldTheme getTextFieldTheme();
5050

51-
WidgetToggleButtonTheme getToggleButtonTheme();
51+
WidgetThemeSelectable getToggleButtonTheme();
5252

5353
WidgetTheme getWidgetTheme(String id);
5454

55+
default <T extends WidgetTheme> T getWidgetTheme(Class<T> clazz, String id) {
56+
WidgetTheme theme = getWidgetTheme(id);
57+
if (clazz.isInstance(theme)) {
58+
return (T) theme;
59+
}
60+
return null;
61+
}
62+
5563
int getOpenCloseAnimationOverride();
5664

5765
boolean getSmoothProgressBarOverride();

src/main/java/com/cleanroommc/modularui/api/IThemeApi.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ static IThemeApi get() {
4444
*/
4545
boolean hasTheme(String id);
4646

47+
/**
48+
* @param id id of the widget theme
49+
* @return if a widget theme with the id is registered
50+
*/
51+
boolean hasWidgetTheme(String id);
52+
4753
/**
4854
* Registers a theme json object. Themes from resource packs always have greater priority.
4955
*

src/main/java/com/cleanroommc/modularui/api/drawable/IDrawable.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
*/
2020
public interface IDrawable {
2121

22+
static IDrawable of(IDrawable... drawables) {
23+
if (drawables == null || drawables.length == 0) {
24+
return null;
25+
} else if (drawables.length == 1) {
26+
return drawables[0];
27+
} else {
28+
return new DrawableArray(drawables);
29+
}
30+
}
31+
2232
/**
2333
* Draws this drawable at the given position with the given size.
2434
*
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package com.cleanroommc.modularui.api.value;
22

3-
public interface IBoolValue<T> extends IValue<T> {
3+
public interface IBoolValue<T> extends IValue<T>, IIntValue<T> {
44

55
boolean getBoolValue();
66

77
void setBoolValue(boolean val);
8+
9+
@Override
10+
default int getIntValue() {
11+
return getBoolValue() ? 1 : 0;
12+
}
13+
14+
@Override
15+
default void setIntValue(int val) {
16+
setBoolValue(val == 1);
17+
}
818
}

src/main/java/com/cleanroommc/modularui/api/value/sync/IBoolSyncValue.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @param <T> value type
99
*/
10-
public interface IBoolSyncValue<T> extends IValueSyncHandler<T>, IBoolValue<T> {
10+
public interface IBoolSyncValue<T> extends IValueSyncHandler<T>, IBoolValue<T>, IIntSyncValue<T> {
1111

1212
@Override
1313
default void setBoolValue(boolean val) {
@@ -19,4 +19,19 @@ default void setBoolValue(boolean val, boolean setSource) {
1919
}
2020

2121
void setBoolValue(boolean value, boolean setSource, boolean sync);
22+
23+
@Override
24+
default void setIntValue(int value, boolean setSource, boolean sync) {
25+
setBoolValue(value == 1, setSource, sync);
26+
}
27+
28+
@Override
29+
default int getIntValue() {
30+
return IBoolValue.super.getIntValue();
31+
}
32+
33+
@Override
34+
default void setIntValue(int val) {
35+
IBoolValue.super.setIntValue(val);
36+
}
2237
}

src/main/java/com/cleanroommc/modularui/api/widget/ITooltip.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,40 @@ default W getThis() {
5151
* @return this
5252
*/
5353
default W tooltip(Consumer<Tooltip> tooltipConsumer) {
54+
return tooltipStatic(tooltipConsumer);
55+
}
56+
57+
/**
58+
* Helper method to call tooltip setters within a widget tree initialisation.
59+
* Only called once.
60+
*
61+
* @param tooltipConsumer tooltip function
62+
* @return this
63+
*/
64+
default W tooltipStatic(Consumer<Tooltip> tooltipConsumer) {
5465
tooltipConsumer.accept(tooltip());
5566
return getThis();
5667
}
5768

5869
/**
5970
* Sets a tooltip builder. The builder will be called every time the tooltip is marked dirty.
60-
* Should be used for dynamic tooltips
71+
* Should be used for dynamic tooltips.
6172
*
6273
* @param tooltipBuilder tooltip function
6374
* @return this
6475
*/
6576
default W tooltipBuilder(Consumer<Tooltip> tooltipBuilder) {
77+
return tooltipDynamic(tooltipBuilder);
78+
}
79+
80+
/**
81+
* Sets a tooltip builder. The builder will be called every time the tooltip is marked dirty.
82+
* Should be used for dynamic tooltips.
83+
*
84+
* @param tooltipBuilder tooltip function
85+
* @return this
86+
*/
87+
default W tooltipDynamic(Consumer<Tooltip> tooltipBuilder) {
6688
tooltip().tooltipBuilder(tooltipBuilder);
6789
return getThis();
6890
}

src/main/java/com/cleanroommc/modularui/drawable/AdaptableUITexture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class AdaptableUITexture extends UITexture {
2525

2626
@Override
2727
public AdaptableUITexture getSubArea(float uStart, float vStart, float uEnd, float vEnd) {
28-
return new AdaptableUITexture(this.location, calcU(uStart), calcV(vStart), calcU(uEnd), calcV(vEnd), this.canApplyTheme, this.imageWidth, this.imageHeight, this.bl, this.bt, this.br, this.bb, this.tiled);
28+
return new AdaptableUITexture(this.location, lerpU(uStart), lerpV(vStart), lerpU(uEnd), lerpV(vEnd), this.canApplyTheme, this.imageWidth, this.imageHeight, this.bl, this.bt, this.br, this.bb, this.tiled);
2929
}
3030

3131
@Override

src/main/java/com/cleanroommc/modularui/drawable/UITexture.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.cleanroommc.modularui.drawable;
22

33
import com.cleanroommc.modularui.ModularUI;
4-
import com.cleanroommc.modularui.api.ITheme;
54
import com.cleanroommc.modularui.api.drawable.IDrawable;
65
import com.cleanroommc.modularui.screen.viewport.GuiContext;
76
import com.cleanroommc.modularui.theme.WidgetTheme;
87
import com.cleanroommc.modularui.utils.Color;
8+
import com.cleanroommc.modularui.utils.Interpolations;
99
import com.cleanroommc.modularui.utils.JsonHelper;
1010
import com.cleanroommc.modularui.widget.sizer.Area;
1111
import com.google.gson.JsonObject;
@@ -109,19 +109,19 @@ public UITexture getSubArea(Area bounds) {
109109
* @return relative sub area
110110
*/
111111
public UITexture getSubArea(float uStart, float vStart, float uEnd, float vEnd) {
112-
return new UITexture(this.location, calcU(uStart), calcV(vStart), calcU(uEnd), calcV(vEnd), this.canApplyTheme);
112+
return new UITexture(this.location, lerpU(uStart), lerpV(vStart), lerpU(uEnd), lerpV(vEnd), this.canApplyTheme);
113113
}
114114

115115
public ResourceLocation getLocation() {
116116
return this.location;
117117
}
118118

119-
protected final float calcU(float uNew) {
120-
return (this.u1 - this.u0) * uNew + this.u0;
119+
protected final float lerpU(float u) {
120+
return Interpolations.lerp(this.u0, this.u1, u);
121121
}
122122

123-
protected final float calcV(float vNew) {
124-
return (this.v1 - this.v0) * vNew + this.v0;
123+
protected final float lerpV(float v) {
124+
return Interpolations.lerp(this.v0, this.v1, v);
125125
}
126126

127127
@SideOnly(Side.CLIENT)
@@ -140,7 +140,7 @@ public void draw(float x, float y, float width, float height) {
140140
}
141141

142142
public void drawSubArea(float x, float y, float width, float height, float uStart, float vStart, float uEnd, float vEnd) {
143-
GuiDraw.drawTexture(this.location, x, y, x + width, y + height, calcU(uStart), calcV(vStart), calcU(uEnd), calcV(vEnd));
143+
GuiDraw.drawTexture(this.location, x, y, x + width, y + height, lerpU(uStart), lerpV(vStart), lerpU(uEnd), lerpV(vEnd));
144144
}
145145

146146
@Override

src/main/java/com/cleanroommc/modularui/screen/ModularPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public boolean hasParent() {
133133
}
134134

135135
@Override
136-
public WidgetTheme getWidgetTheme(ITheme theme) {
136+
public WidgetTheme getWidgetThemeInternal(ITheme theme) {
137137
return theme.getPanelTheme();
138138
}
139139

src/main/java/com/cleanroommc/modularui/test/TestTile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
256256
.coverChildrenHeight()
257257
.child(new CycleButtonWidget()
258258
.size(14, 14)
259-
.length(3)
260-
.texture(GuiTextures.CYCLE_BUTTON_DEMO)
259+
.stateCount(3)
260+
.stateBackground(GuiTextures.CYCLE_BUTTON_DEMO)
261261
.value(new IntSyncValue(() -> this.val2, val -> this.val2 = val))
262262
.margin(8, 0))
263263
.child(IKey.str("Hello World").asWidget().height(18)))

0 commit comments

Comments
 (0)