Skip to content

Commit

Permalink
hex color displayed when changing color
Browse files Browse the repository at this point in the history
the text field is however not editable
  • Loading branch information
Goby56 committed Dec 29, 2024
1 parent bd717b1 commit 248f82a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ColorIntervalSlider(ColorPickerScreen screenContext, int x, int y, int wi
for (float val : WakesConfig.wakeColorIntervals) {
this.handles.add(new SliderHandle(val));
}
this.colorPicker = new ColorPicker(screenContext, 10, screenContext.height / 2, 100, 100);
this.colorPicker = new ColorPicker(screenContext, 10, screenContext.height / 2 - 64, 128, 128);
colorPicker.registerListener(this::onColorPicked);
}

Expand Down Expand Up @@ -97,7 +97,7 @@ public void onClick(double mouseX, double mouseY) {
colorPicker.setActive(!colorPicker.active);
}
activeSection = clickedSection;
colorPicker.setColor(WakesConfig.getWakeColor(activeSection));
colorPicker.setColor(WakesConfig.getWakeColor(activeSection), false);
}
}
}
Expand Down
40 changes: 30 additions & 10 deletions src/main/java/com/goby56/wakes/config/gui/ColorPicker.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.goby56.wakes.config.gui;

import com.goby56.wakes.WakesClient;
import com.goby56.wakes.config.WakesConfig;
import com.goby56.wakes.config.WakesConfigScreen;
import com.goby56.wakes.render.enums.WakeColor;
import com.mojang.blaze3d.systems.RenderSystem;
Expand Down Expand Up @@ -40,8 +41,8 @@ public ColorPicker(ColorPickerScreen screenContext, int x, int y, int width, int
this.bounds = new AABB(0, 0, 1f, 2f / 3f, x, y, width, height);

this.widgets.put("hueSlider", new GradientSlider(new AABB(0f, 4f / 6f, 1f, 5f / 6f, x, y, width, height), "Hue", this,true));
this.widgets.put("alphaSlider", new GradientSlider(new AABB(5f / 12f, 5f / 6f, 1f, 1f, x, y, width, height), "Opacity", this, false));
this.widgets.put("hexInputField", new HexInputField(new AABB(0f, 5f / 6f, 5f / 12f, 1f, x, y, width, height), this, MinecraftClient.getInstance().textRenderer));
this.widgets.put("alphaSlider", new GradientSlider(new AABB(3f / 6f, 5f / 6f, 1f, 1f, x, y, width, height), "Opacity", this, false));
this.widgets.put("hexInputField", new HexInputField(new AABB(0f, 5f / 6f, 3f / 6f, 1f, x, y, width, height), this, MinecraftClient.getInstance().textRenderer));

screenContext.addWidget(this);
for (var widget : this.widgets.values()) {
Expand All @@ -57,7 +58,11 @@ public void setActive(boolean active) {
}
}

public void setColor(WakeColor currentColor) {
public void setColor(WakeColor currentColor, boolean onlyHexText) {
if (onlyHexText) {
this.widgets.get("hexInputField").setColor(currentColor);
return;
}
float[] hsv = Color.RGBtoHSB(currentColor.r, currentColor.g, currentColor.b, null);
this.pickerPos.set(
this.bounds.x + hsv[1] * this.bounds.width,
Expand Down Expand Up @@ -115,7 +120,9 @@ public void updateColor() {
float saturation = (pickerPos.x - this.bounds.x) / this.bounds.width;
float value = 1f - (pickerPos.y - this.bounds.y) / this.bounds.height;
float opacity = ((GradientSlider) this.widgets.get("alphaSlider").getWidget()).getValue();
this.changedColorListener.accept(new WakeColor(hue, saturation, value, opacity));
WakeColor newColor = new WakeColor(hue, saturation, value, opacity);
this.setColor(newColor, true);
this.changedColorListener.accept(newColor);
}

@Override
Expand Down Expand Up @@ -188,10 +195,23 @@ private static class HexInputField extends TextFieldWidget implements Bounded {
private final ColorPicker colorPicker;

public HexInputField(AABB bounds, ColorPicker colorPicker, TextRenderer textRenderer) {
super(textRenderer, bounds.x, bounds.y, bounds.width, bounds.height, Text.of("HEX"));
super(textRenderer, bounds.x, bounds.y, bounds.width, bounds.height, Text.empty());
this.setMaxLength(9); // #AARRGGBB
this.bounds = bounds;
this.colorPicker = colorPicker;
//setTextPredicate(HexInputField::validHex);
}

private static boolean validHex(String text) {
if (text.charAt(0) != '#' || text.length() == 9) {
return false;
}
for (char c : text.substring(1).toLowerCase().toCharArray()) {
if (Character.digit(c, 16) == -1) {
return false;
}
}
return true;
}

public void setActive(boolean active) {
Expand All @@ -200,13 +220,13 @@ public void setActive(boolean active) {

@Override
public void setColor(WakeColor currentColor) {

setText(currentColor.toHex());
}

@Override
public void onClick(double mouseX, double mouseY) {
super.onClick(mouseX, mouseY);
}
// @Override
// public void onClick(double mouseX, double mouseY) {
// super.onClick(mouseX, mouseY);
// }

@Override
public AABB getBounds() {
Expand Down

0 comments on commit 248f82a

Please sign in to comment.