From 3a47b5883c9b7e9f6720a40a57982f8eddafb14e Mon Sep 17 00:00:00 2001 From: brandon3055 Date: Sun, 21 Apr 2024 18:45:06 +1000 Subject: [PATCH] Fixed colour bug and improved colour picker. --- .../java/codechicken/lib/colour/Colour.java | 8 ++-- .../gui/modular/elements/GuiColourPicker.java | 45 +++++++++++++++---- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/main/java/codechicken/lib/colour/Colour.java b/src/main/java/codechicken/lib/colour/Colour.java index 61b22b05..ca083a8a 100644 --- a/src/main/java/codechicken/lib/colour/Colour.java +++ b/src/main/java/codechicken/lib/colour/Colour.java @@ -175,19 +175,19 @@ public Colour aF(int a) { } public float rF() { - return r / 255F; + return r() / 255F; } public float gF() { - return g / 255F; + return g() / 255F; } public float bF() { - return b / 255F; + return b() / 255F; } public float aF() { - return a / 255F; + return a() / 255F; } public int r() { diff --git a/src/main/java/codechicken/lib/gui/modular/elements/GuiColourPicker.java b/src/main/java/codechicken/lib/gui/modular/elements/GuiColourPicker.java index c576a7e8..1d4d6b71 100644 --- a/src/main/java/codechicken/lib/gui/modular/elements/GuiColourPicker.java +++ b/src/main/java/codechicken/lib/gui/modular/elements/GuiColourPicker.java @@ -19,11 +19,15 @@ public class GuiColourPicker extends GuiManipulable { private ColourState colourState = ColourState.create(); + private final Colour initialColour; + private boolean blockOutsideClicks = false; + private boolean closeOnOutsideClick = false; private GuiButton okButton; private GuiButton cancelButton; - public GuiColourPicker(@NotNull GuiParent parent) { + public GuiColourPicker(@NotNull GuiParent parent, Colour initialColour) { super(parent); + this.initialColour = initialColour; } public static GuiColourPicker create(GuiParent guiParent, ColourState colourState) { @@ -31,11 +35,10 @@ public static GuiColourPicker create(GuiParent guiParent, ColourState colourS } public static GuiColourPicker create(GuiParent guiParent, ColourState colourState, boolean hasAlpha) { - Colour initialColour = colourState.getColour(); - GuiColourPicker picker = new GuiColourPicker(guiParent.getModularGui().getRoot()); + GuiColourPicker picker = new GuiColourPicker(guiParent.getModularGui().getRoot(), colourState.getColour()); picker.setOpaque(true); picker.setColourState(colourState); - Constraints.size(picker, 80, hasAlpha ? 80 : 68); + Constraints.size(picker, 80, hasAlpha ? 80 : 70); GuiRectangle background = GuiRectangle.toolTipBackground(picker.getContentElement()); Constraints.bind(background, picker.getContentElement()); @@ -77,10 +80,7 @@ public static GuiColourPicker create(GuiParent guiParent, ColourState colourS picker.cancelButton = GuiButton.flatColourButton(background, () -> Component.translatable("gui.cancel"), e -> 0xFF000000, e -> e ? 0xFF777777 : 0xFF555555) .setOpaque(true) - .onPress(() -> { - colourState.set(initialColour); - picker.getParent().removeChild(picker); - }) + .onPress(picker::cancel) .constrain(HEIGHT, literal(10)) .constrain(TOP, relative(preview.get(BOTTOM), 2)) .constrain(LEFT, midPoint(background.get(LEFT), background.get(RIGHT), -4)) @@ -121,6 +121,35 @@ public static SliderBG makeSlider(GuiElement background, int colour, SliderSt return slideBG; } + public GuiColourPicker setBlockOutsideClicks(boolean blockOutsideClicks) { + this.blockOutsideClicks = blockOutsideClicks; + return this; + } + + public GuiColourPicker setCancelOnOutsideClick(boolean closeOnOutsideClick) { + this.closeOnOutsideClick = closeOnOutsideClick; + return this; + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button, boolean consumed) { + if (closeOnOutsideClick && !getContentElement().getRectangle().contains(mouseX, mouseY)) { + cancel(); + } + return super.mouseClicked(mouseX, mouseY, button, consumed) || blockOutsideClicks; + } + + public void cancel() { + if (cancelButton != null && cancelButton.isEnabled()){ + colourState.set(initialColour); + } + close(); + } + + public void close() { + getParent().removeChild(this); + } + private static boolean validHex(String value) { try { Integer.parseUnsignedInt(value, 16);