Skip to content

Commit f26323b

Browse files
authored
Cyclebutton rework (#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
1 parent 47cf883 commit f26323b

26 files changed

+522
-293
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
@@ -45,6 +45,12 @@ static IThemeApi get() {
4545
*/
4646
boolean hasTheme(String id);
4747

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

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
@@ -52,18 +52,40 @@ default W getThis() {
5252
* @return this
5353
*/
5454
default W tooltip(Consumer<Tooltip> tooltipConsumer) {
55+
return tooltipStatic(tooltipConsumer);
56+
}
57+
58+
/**
59+
* Helper method to call tooltip setters within a widget tree initialisation.
60+
* Only called once.
61+
*
62+
* @param tooltipConsumer tooltip function
63+
* @return this
64+
*/
65+
default W tooltipStatic(Consumer<Tooltip> tooltipConsumer) {
5566
tooltipConsumer.accept(tooltip());
5667
return getThis();
5768
}
5869

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

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

@@ -111,19 +111,19 @@ public UITexture getSubArea(Area bounds) {
111111
* @return relative sub area
112112
*/
113113
public UITexture getSubArea(float uStart, float vStart, float uEnd, float vEnd) {
114-
return new UITexture(this.location, calcU(uStart), calcV(vStart), calcU(uEnd), calcV(vEnd), this.canApplyTheme);
114+
return new UITexture(this.location, lerpU(uStart), lerpV(vStart), lerpU(uEnd), lerpV(vEnd), this.canApplyTheme);
115115
}
116116

117117
public ResourceLocation getLocation() {
118118
return this.location;
119119
}
120120

121-
protected final float calcU(float uNew) {
122-
return (this.u1 - this.u0) * uNew + this.u0;
121+
protected final float lerpU(float u) {
122+
return Interpolations.lerp(this.u0, this.u1, u);
123123
}
124124

125-
protected final float calcV(float vNew) {
126-
return (this.v1 - this.v0) * vNew + this.v0;
125+
protected final float lerpV(float v) {
126+
return Interpolations.lerp(this.v0, this.v1, v);
127127
}
128128

129129
@SideOnly(Side.CLIENT)
@@ -142,7 +142,7 @@ public void draw(float x, float y, float width, float height) {
142142
}
143143

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

148148
@Override

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

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

131131
@Override
132-
public WidgetTheme getWidgetTheme(ITheme theme) {
132+
public WidgetTheme getWidgetThemeInternal(ITheme theme) {
133133
return theme.getPanelTheme();
134134
}
135135

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
230230
.coverChildrenHeight()
231231
.child(new CycleButtonWidget()
232232
.size(14, 14)
233-
.length(3)
234-
.texture(GuiTextures.CYCLE_BUTTON_DEMO)
233+
.stateCount(3)
234+
.stateBackground(GuiTextures.CYCLE_BUTTON_DEMO)
235235
.value(new IntSyncValue(() -> this.val2, val -> this.val2 = val))
236236
.margin(8, 0))
237237
.child(IKey.str("Hello World").asWidget().height(18)))

0 commit comments

Comments
 (0)