Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cyclebutton rework #70

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main/java/com/cleanroommc/modularui/api/ITheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.cleanroommc.modularui.theme.WidgetSlotTheme;
import com.cleanroommc.modularui.theme.WidgetTextFieldTheme;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.theme.WidgetToggleButtonTheme;
import com.cleanroommc.modularui.theme.WidgetThemeSelectable;

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

WidgetTextFieldTheme getTextFieldTheme();

WidgetToggleButtonTheme getToggleButtonTheme();
WidgetThemeSelectable getToggleButtonTheme();

WidgetTheme getWidgetTheme(String id);

default <T extends WidgetTheme> T getWidgetTheme(Class<T> clazz, String id) {
WidgetTheme theme = getWidgetTheme(id);
if (clazz.isInstance(theme)) {
return (T) theme;
}
return null;
}

int getOpenCloseAnimationOverride();

boolean getSmoothProgressBarOverride();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/cleanroommc/modularui/api/IThemeApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ static IThemeApi get() {
*/
boolean hasTheme(String id);

/**
* @param id id of the widget theme
* @return if a widget theme with the id is registered
*/
boolean hasWidgetTheme(String id);

/**
* Registers a theme json object. Themes from resource packs always have greater priority.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.modularui.api.drawable;

import com.cleanroommc.modularui.drawable.DrawableArray;
import com.cleanroommc.modularui.drawable.Icon;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
Expand All @@ -17,6 +18,16 @@
*/
public interface IDrawable {

static IDrawable of(IDrawable... drawables) {
if (drawables == null || drawables.length == 0) {
return null;
} else if (drawables.length == 1) {
return drawables[0];
} else {
return new DrawableArray(drawables);
}
}

/**
* Draws this drawable at the given position with the given size.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package com.cleanroommc.modularui.api.value;

public interface IBoolValue<T> extends IValue<T> {
public interface IBoolValue<T> extends IValue<T>, IIntValue<T> {

boolean getBoolValue();

void setBoolValue(boolean val);

@Override
default int getIntValue() {
return getBoolValue() ? 1 : 0;
}

@Override
default void setIntValue(int val) {
setBoolValue(val == 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @param <T> value type
*/
public interface IBoolSyncValue<T> extends IValueSyncHandler<T>, IBoolValue<T> {
public interface IBoolSyncValue<T> extends IValueSyncHandler<T>, IBoolValue<T>, IIntSyncValue<T> {

@Override
default void setBoolValue(boolean val) {
Expand All @@ -19,4 +19,19 @@ default void setBoolValue(boolean val, boolean setSource) {
}

void setBoolValue(boolean value, boolean setSource, boolean sync);

@Override
default void setIntValue(int value, boolean setSource, boolean sync) {
setBoolValue(value == 1, setSource, sync);
}

@Override
default int getIntValue() {
return IBoolValue.super.getIntValue();
}

@Override
default void setIntValue(int val) {
IBoolValue.super.setIntValue(val);
}
}
24 changes: 23 additions & 1 deletion src/main/java/com/cleanroommc/modularui/api/widget/ITooltip.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,40 @@ default W getThis() {
* @return this
*/
default W tooltip(Consumer<Tooltip> tooltipConsumer) {
return tooltipStatic(tooltipConsumer);
}

/**
* Helper method to call tooltip setters within a widget tree initialisation.
* Only called once.
*
* @param tooltipConsumer tooltip function
* @return this
*/
default W tooltipStatic(Consumer<Tooltip> tooltipConsumer) {
tooltipConsumer.accept(tooltip());
return getThis();
}

/**
* Sets a tooltip builder. The builder will be called every time the tooltip is marked dirty.
* Should be used for dynamic tooltips
* Should be used for dynamic tooltips.
*
* @param tooltipBuilder tooltip function
* @return this
*/
default W tooltipBuilder(Consumer<Tooltip> tooltipBuilder) {
return tooltipDynamic(tooltipBuilder);
}

/**
* Sets a tooltip builder. The builder will be called every time the tooltip is marked dirty.
* Should be used for dynamic tooltips.
*
* @param tooltipBuilder tooltip function
* @return this
*/
default W tooltipDynamic(Consumer<Tooltip> tooltipBuilder) {
tooltip().tooltipBuilder(tooltipBuilder);
return getThis();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class AdaptableUITexture extends UITexture {

@Override
public AdaptableUITexture getSubArea(float uStart, float vStart, float uEnd, float vEnd) {
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);
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);
}

@Override
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/cleanroommc/modularui/drawable/UITexture.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.cleanroommc.modularui.drawable;

import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.api.ITheme;
import com.cleanroommc.modularui.api.drawable.IDrawable;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.Color;
import com.cleanroommc.modularui.utils.Interpolations;
import com.cleanroommc.modularui.utils.JsonHelper;
import com.cleanroommc.modularui.widget.sizer.Area;

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

public ResourceLocation getLocation() {
return this.location;
}

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

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

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public boolean hasParent() {
}

@Override
public WidgetTheme getWidgetTheme(ITheme theme) {
public WidgetTheme getWidgetThemeInternal(ITheme theme) {
return theme.getPanelTheme();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cleanroommc/modularui/test/TestTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
.coverChildrenHeight()
.child(new CycleButtonWidget()
.size(14, 14)
.length(3)
.texture(GuiTextures.CYCLE_BUTTON_DEMO)
.stateCount(3)
.stateBackground(GuiTextures.CYCLE_BUTTON_DEMO)
.value(new IntSyncValue(() -> this.val2, val -> this.val2 = val))
.margin(8, 0))
.child(IKey.str("Hello World").asWidget().height(18)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class AbstractDefaultTheme implements ITheme {
private WidgetSlotTheme itemSlot;
private WidgetSlotTheme fluidSlot;
private WidgetTextFieldTheme textField;
private WidgetToggleButtonTheme toggleButtonTheme;
private WidgetThemeSelectable toggleButtonTheme;

@Override
public ITheme getParentTheme() {
Expand Down Expand Up @@ -59,9 +59,9 @@ public WidgetTextFieldTheme getTextFieldTheme() {
}

@Override
public WidgetToggleButtonTheme getToggleButtonTheme() {
public WidgetThemeSelectable getToggleButtonTheme() {
if (this.toggleButtonTheme == null) {
this.toggleButtonTheme = (WidgetToggleButtonTheme) getWidgetTheme(Theme.TOGGLE_BUTTON);
this.toggleButtonTheme = (WidgetThemeSelectable) getWidgetTheme(Theme.TOGGLE_BUTTON);
}
return this.toggleButtonTheme;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/cleanroommc/modularui/theme/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Theme implements ITheme {
private final WidgetSlotTheme itemSlotTheme;
private final WidgetSlotTheme fluidSlotTheme;
private final WidgetTextFieldTheme textFieldTheme;
private final WidgetToggleButtonTheme toggleButtonTheme;
private final WidgetThemeSelectable toggleButtonTheme;

private int openCloseAnimationOverride = -1;
private Boolean smoothProgressBarOverride = null;
Expand Down Expand Up @@ -61,7 +61,7 @@ public class Theme implements ITheme {
this.itemSlotTheme = (WidgetSlotTheme) this.widgetThemes.get(ITEM_SLOT);
this.fluidSlotTheme = (WidgetSlotTheme) this.widgetThemes.get(FLUID_SLOT);
this.textFieldTheme = (WidgetTextFieldTheme) this.widgetThemes.get(TEXT_FIELD);
this.toggleButtonTheme = (WidgetToggleButtonTheme) this.widgetThemes.get(TOGGLE_BUTTON);
this.toggleButtonTheme = (WidgetThemeSelectable) this.widgetThemes.get(TOGGLE_BUTTON);
}

void setOpenCloseAnimationOverride(int override) {
Expand Down Expand Up @@ -111,7 +111,7 @@ public WidgetTextFieldTheme getTextFieldTheme() {
}

@Override
public WidgetToggleButtonTheme getToggleButtonTheme() {
public WidgetThemeSelectable getToggleButtonTheme() {
return this.toggleButtonTheme;
}

Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/cleanroommc/modularui/theme/ThemeAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ThemeAPI implements IThemeApi {
public static final String DEFAULT = "DEFAULT";
public static final ITheme DEFAULT_DEFAULT = new DefaultTheme();

private final Object2ObjectMap<String, ITheme> THEMES = new Object2ObjectOpenHashMap<>();
private final Object2ObjectMap<String, ITheme> themes = new Object2ObjectOpenHashMap<>();
protected final Object2ObjectMap<String, List<JsonBuilder>> defaultThemes = new Object2ObjectOpenHashMap<>();
protected final Object2ObjectMap<String, WidgetTheme> defaultWidgetThemes = new Object2ObjectOpenHashMap<>();
protected final Object2ObjectMap<String, WidgetThemeParser> widgetThemeFunctions = new Object2ObjectOpenHashMap<>();
Expand All @@ -36,8 +36,8 @@ private ThemeAPI() {
registerWidgetTheme(Theme.ITEM_SLOT, new WidgetSlotTheme(GuiTextures.SLOT_ITEM, Color.withAlpha(Color.WHITE.main, 0x60)), WidgetSlotTheme::new);
registerWidgetTheme(Theme.FLUID_SLOT, new WidgetSlotTheme(GuiTextures.SLOT_FLUID, Color.withAlpha(Color.WHITE.main, 0x60)), WidgetSlotTheme::new);
registerWidgetTheme(Theme.TEXT_FIELD, new WidgetTextFieldTheme(0xFF2F72A8), (parent, json, fallback) -> new WidgetTextFieldTheme(parent, fallback, json));
registerWidgetTheme(Theme.TOGGLE_BUTTON, new WidgetToggleButtonTheme(GuiTextures.MC_BUTTON, GuiTextures.MC_BUTTON_HOVERED, Color.WHITE.main, Color.WHITE.main, true,
GuiTextures.MC_BUTTON_DISABLED, IDrawable.NONE, Color.WHITE.main, Color.WHITE.main, true), WidgetToggleButtonTheme::new);
registerWidgetTheme(Theme.TOGGLE_BUTTON, new WidgetThemeSelectable(GuiTextures.MC_BUTTON, GuiTextures.MC_BUTTON_HOVERED, Color.WHITE.main, Color.WHITE.main, true,
GuiTextures.MC_BUTTON_DISABLED, IDrawable.NONE, Color.WHITE.main, Color.WHITE.main, true), WidgetThemeSelectable::new);
}

@Override
Expand All @@ -47,12 +47,17 @@ public ITheme getDefaultTheme() {

@Override
public @NotNull ITheme getTheme(String id) {
return this.THEMES.getOrDefault(id, getDefaultTheme());
return this.themes.getOrDefault(id, getDefaultTheme());
}

@Override
public boolean hasTheme(String id) {
return this.THEMES.containsKey(id);
return this.themes.containsKey(id);
}

@Override
public boolean hasWidgetTheme(String id) {
return this.widgetThemeFunctions.containsKey(id);
}

@Override
Expand Down Expand Up @@ -105,14 +110,14 @@ public void registerWidgetTheme(String id, WidgetTheme defaultTheme, WidgetTheme
// Internals

void registerTheme(ITheme theme) {
if (this.THEMES.containsKey(theme.getId())) {
if (this.themes.containsKey(theme.getId())) {
throw new IllegalArgumentException("Theme with id " + theme.getId() + " already exists!");
}
this.THEMES.put(theme.getId(), theme);
this.themes.put(theme.getId(), theme);
}

void onReload() {
this.THEMES.clear();
this.themes.clear();
this.jsonScreenThemes.clear();
registerTheme(DEFAULT_DEFAULT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ private Theme deserialize() {
widgetThemes.put(entry.getKey(), entry.getValue().parse(parentWidgetTheme, widgetThemeJson, jsonBuilder.getJson()));
}
Theme theme = new Theme(this.id, parent, widgetThemes);
// TODO: bad implementation
if (jsonBuilder.getJson().has("openCloseAnimation")) {
theme.setOpenCloseAnimationOverride(jsonBuilder.getJson().get("openCloseAnimation").getAsInt());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
import com.google.gson.JsonObject;
import org.jetbrains.annotations.Nullable;

public class WidgetToggleButtonTheme extends WidgetTheme {
public class WidgetThemeSelectable extends WidgetTheme {

private final WidgetTheme selected;

public WidgetToggleButtonTheme(@Nullable IDrawable background, @Nullable IDrawable hoverBackground,
int color, int textColor, boolean textShadow,
@Nullable IDrawable selectedBackground, @Nullable IDrawable selectedHoverBackground,
int selectedColor, int selectedTextColor, boolean selectedTextShadow) {
public WidgetThemeSelectable(@Nullable IDrawable background, @Nullable IDrawable hoverBackground,
int color, int textColor, boolean textShadow,
@Nullable IDrawable selectedBackground, @Nullable IDrawable selectedHoverBackground,
int selectedColor, int selectedTextColor, boolean selectedTextShadow) {
super(background, hoverBackground, color, textColor, textShadow);
this.selected = new WidgetTheme(selectedBackground, selectedHoverBackground, selectedColor, selectedTextColor, selectedTextShadow);
}

public WidgetToggleButtonTheme(WidgetTheme parent, JsonObject json, JsonObject fallback) {
public WidgetThemeSelectable(WidgetTheme parent, JsonObject json, JsonObject fallback) {
super(parent, json, fallback);
WidgetToggleButtonTheme parentWTBT = (WidgetToggleButtonTheme) parent;
WidgetThemeSelectable parentWTBT = (WidgetThemeSelectable) parent;
IDrawable selectedBackground = JsonHelper.deserializeWithFallback(json, fallback, IDrawable.class, parentWTBT.getSelected().getBackground(), "selectedBackground");
IDrawable selectedHoverBackground = JsonHelper.deserializeWithFallback(json, fallback, IDrawable.class, parentWTBT.getSelected().getHoverBackground(), "selectedHoverBackground");
int selectedColor = JsonHelper.getColorWithFallback(json, fallback, parentWTBT.getSelected().getColor(), "selectedColor");
Expand Down
Loading