Skip to content

Commit

Permalink
fix: Fixed crash + animated checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
cvs0 committed Jul 7, 2024
1 parent 209406b commit 472af56
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/main/java/net/aoba/gui/colors/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ public Color(int r, int g, int b, int alpha) {
this.alpha = alpha;
}

/**
* Interpolates between two colors.
*
* @param color1 The first color.
* @param color2 The second color.
* @param factor The interpolation factor. 0.0 will return color1, 1.0 will return color2.
* @return The interpolated color.
*/
public static Color interpolate(Color color1, Color color2, float factor) {
int r = (int) (color1.r + (color2.r - color1.r) * factor);
int g = (int) (color1.g + (color2.g - color1.g) * factor);
int b = (int) (color1.b + (color2.b - color1.b) * factor);
int alpha = (int) (color1.alpha + (color2.alpha - color1.alpha) * factor);
return new Color(r, g, b, alpha);
}

private void HSVFromRGB(int r, int g, int b) {
// Calculate HSV value
float rPrime = r / 255.0f;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/aoba/gui/tabs/GoToTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ public void OnRender(RenderEvent event) {
public void OnUpdate(TickEvent event) {
MinecraftClient MC = MinecraftClient.getInstance();

if (currentNodeIndex < nodes.size() - 1) {
// Check next position
if (nodes != null && currentNodeIndex < nodes.size() - 1) {
PathNode next = nodes.get(currentNodeIndex + 1);
BlockPos playerPos = MC.player.getBlockPos();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ public void setBackgroundColor(Color color) {

/**
* Draws the button to the screen.
* @param offset The offset (Y location relative to parent) of the Component.
* @param drawContext The current draw context of the game.
* @param partialTicks The partial ticks used for interpolation.
* @param color The current Color of the UI.
*/
@Override
public void draw(DrawContext drawContext, float partialTicks) {
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/net/aoba/gui/tabs/components/CheckboxComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public class CheckboxComponent extends Component implements MouseClickListener {
private String text;
private BooleanSetting checkbox;
private Runnable onClick;
private boolean isHovered = false;
private float animationProgress = 0.0f;
private Color hoverBorderColor = new Color(255, 255, 255);
private Color clickAnimationColor = new Color(255, 255, 0);

public CheckboxComponent(IGuiElement parent, BooleanSetting checkbox) {
super(parent);
Expand All @@ -48,31 +52,32 @@ public CheckboxComponent(IGuiElement parent, BooleanSetting checkbox) {

/**
* Draws the checkbox to the screen.
* @param offset The offset (Y location relative to parent) of the Component.
* @param drawContext The current draw context of the game.
* @param partialTicks The partial ticks used for interpolation.
* @param color The current Color of the UI.
*/
@Override
public void draw(DrawContext drawContext, float partialTicks) {
super.draw(drawContext, partialTicks);

MatrixStack matrixStack = drawContext.getMatrices();
Matrix4f matrix4f = matrixStack.peek().getPositionMatrix();

RenderUtils.drawString(drawContext, this.text, actualX + 6, actualY + 8, 0xFFFFFF);
if (this.checkbox.getValue()) {
RenderUtils.drawOutlinedBox(matrix4f, actualX + actualWidth - 24, actualY + 5, 20, 20,
new Color(0, 154, 0, 200));
} else {
RenderUtils.drawOutlinedBox(matrix4f, actualX + actualWidth - 24, actualY + 5, 20, 20,
new Color(154, 0, 0, 200));

// Determine border color based on hover and click state
Color borderColor = isHovered ? hoverBorderColor : new Color(128, 128, 128);
if (animationProgress > 0) {
borderColor = clickAnimationColor;
animationProgress -= partialTicks; // Decrease animation progress
}

// Determine fill color based on checkbox state
Color fillColor = this.checkbox.getValue() ? new Color(0, 154, 0, 200) : new Color(154, 0, 0, 200);

RenderUtils.drawString(drawContext, this.text, actualX + 6, actualY + 8, 0xFFFFFF);
RenderUtils.drawOutlinedBox(matrix4f, actualX + actualWidth - 24, actualY + 5, 20, 20, borderColor, fillColor);
}

/**
* Handles updating the Checkbox component.
* @param offset The offset (Y position relative to parent) of the Checkbox.
*/
@Override
public void update() {
Expand All @@ -93,6 +98,7 @@ public void OnMouseClick(MouseClickEvent event) {
if(event.button == MouseButton.LEFT && event.action == MouseAction.DOWN) {
if (hovered && Aoba.getInstance().hudManager.isClickGuiOpen()) {
checkbox.toggle();
animationProgress = 1.0f; // Reset animation progress on click
if(onClick != null) {
onClick.run();
}
Expand Down

0 comments on commit 472af56

Please sign in to comment.