91 lines
3.4 KiB
Java
91 lines
3.4 KiB
Java
package de.fullrisk.citrusDev.UIHelper;
|
|
|
|
import io.wispforest.owo.ui.component.ButtonComponent;
|
|
import io.wispforest.owo.ui.core.AnimatableProperty;
|
|
import io.wispforest.owo.ui.core.Easing;
|
|
import io.wispforest.owo.ui.core.OwoUIGraphics;
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
public class ScalableButton extends ButtonComponent {
|
|
|
|
protected final AnimatableProperty<ScaleValue> scale = AnimatableProperty.of(new ScaleValue(0f));
|
|
protected final AnimatableProperty<ColorValue> fillColor;
|
|
protected final AnimatableProperty<ColorValue> outlineColor;
|
|
|
|
private final int baseFill;
|
|
private final int hoveredFill;
|
|
private final int baseOutline;
|
|
private final int hoveredOutline;
|
|
private final int outlineWidth;
|
|
|
|
private boolean wasHovered = false;
|
|
|
|
public ScalableButton(Component message, Consumer<ButtonComponent> onPress,
|
|
int baseFill, int hoveredFill,
|
|
int baseOutline, int hoveredOutline,
|
|
int outlineWidth) {
|
|
super(message, onPress);
|
|
this.baseFill = baseFill;
|
|
this.hoveredFill = hoveredFill;
|
|
this.baseOutline = baseOutline;
|
|
this.hoveredOutline = hoveredOutline;
|
|
this.outlineWidth = outlineWidth;
|
|
|
|
this.fillColor = AnimatableProperty.of(new ColorValue(baseFill));
|
|
this.outlineColor = AnimatableProperty.of(new ColorValue(baseOutline));
|
|
|
|
this.renderer((context, button, delta) -> {
|
|
int x = button.getX();
|
|
int y = button.getY();
|
|
int w = button.getWidth();
|
|
int h = button.getHeight();
|
|
|
|
int fill = this.fillColor.get().argb();
|
|
int outline = this.outlineColor.get().argb();
|
|
|
|
context.fill(x, y, x + w, y + h, fill);
|
|
context.fill(x, y, x + w, y + this.outlineWidth, outline); // top
|
|
context.fill(x, y + h - this.outlineWidth, x + w, y + h, outline); // bottom
|
|
context.fill(x, y, x + this.outlineWidth, y + h, outline); // left
|
|
context.fill(x + w - this.outlineWidth, y, x + w, y + h, outline); // right
|
|
});
|
|
}
|
|
|
|
public AnimatableProperty<ScaleValue> scale() {
|
|
return this.scale;
|
|
}
|
|
|
|
@Override
|
|
public void update(float delta, int mouseX, int mouseY) {
|
|
super.update(delta, mouseX, mouseY);
|
|
this.scale.update(delta);
|
|
this.fillColor.update(delta);
|
|
this.outlineColor.update(delta);
|
|
|
|
boolean hovered = this.isHovered();
|
|
if (hovered != this.wasHovered) {
|
|
this.fillColor.animate(150, Easing.CUBIC, new ColorValue(hovered ? hoveredFill : baseFill)).forwards();
|
|
this.outlineColor.animate(150, Easing.CUBIC, new ColorValue(hovered ? hoveredOutline : baseOutline)).forwards();
|
|
this.wasHovered = hovered;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void draw(OwoUIGraphics graphics, int mouseX, int mouseY, float partialTicks, float delta) {
|
|
float s = this.scale.get().value();
|
|
|
|
float centerX = this.x() + this.width() / 2f;
|
|
float centerY = this.y() + this.height() / 2f;
|
|
|
|
graphics.pose().pushMatrix();
|
|
graphics.pose().translate(centerX, centerY);
|
|
graphics.pose().scale(s, s);
|
|
graphics.pose().translate(-centerX, -centerY);
|
|
|
|
super.draw(graphics, mouseX, mouseY, partialTicks, delta);
|
|
|
|
graphics.pose().popMatrix();
|
|
}
|
|
} |