Skip to content

Commit

Permalink
Added Color Mode to ColorPickerComponent.
Browse files Browse the repository at this point in the history
WIP but works well for now.
  • Loading branch information
coltonk9043 committed Sep 7, 2024
1 parent 54c3deb commit 23824b5
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 50 deletions.
17 changes: 17 additions & 0 deletions src/main/java/net/aoba/gui/colors/AnimatedColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.aoba.gui.colors;

import net.aoba.Aoba;
import net.aoba.event.events.TickEvent;
import net.aoba.event.listeners.TickListener;

public class AnimatedColor extends Color implements TickListener {
public AnimatedColor() {
super(255, 0, 0);
Aoba.getInstance().eventManager.AddListener(TickListener.class, this);
}

@Override
public void OnUpdate(TickEvent event) {
this.setHue(((this.getHue() + 1f) % 360));
}
}
7 changes: 0 additions & 7 deletions src/main/java/net/aoba/gui/colors/ColorMode.java

This file was deleted.

7 changes: 2 additions & 5 deletions src/main/java/net/aoba/gui/colors/RainbowColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@
*/
package net.aoba.gui.colors;

import net.aoba.Aoba;
import net.aoba.event.events.TickEvent;
import net.aoba.event.listeners.TickListener;

public class RainbowColor extends Color implements TickListener {
public class RainbowColor extends AnimatedColor {
public RainbowColor() {
super(255, 0, 0);
Aoba.getInstance().eventManager.AddListener(TickListener.class, this);
super();
}

@Override
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/net/aoba/gui/colors/RandomColor.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package net.aoba.gui.colors;

import net.aoba.Aoba;
import net.aoba.event.events.TickEvent;
import net.aoba.event.listeners.TickListener;

public class RandomColor extends Color implements TickListener {
public class RandomColor extends AnimatedColor{
public RandomColor() {
super(0, 0, 0);
Aoba.getInstance().eventManager.AddListener(TickListener.class, this);
super();
}

@Override
Expand Down
78 changes: 55 additions & 23 deletions src/main/java/net/aoba/gui/components/ColorPickerComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.aoba.gui.colors.Color;
import net.aoba.gui.colors.Colors;
import net.aoba.settings.types.ColorSetting;
import net.aoba.settings.types.ColorSetting.ColorMode;
import net.aoba.utils.render.Render2D;
import net.aoba.utils.types.MouseAction;
import net.aoba.utils.types.MouseButton;
Expand Down Expand Up @@ -71,19 +72,41 @@ public void onMouseClick(MouseClickEvent event) {
if (event.button == MouseButton.LEFT) {
if (event.action == MouseAction.DOWN) {
if (hovered) {
double mouseY = event.mouseY;
float mouseX = (float)event.mouseX;
float mouseY = (float)event.mouseY;
float actualY = actualSize.getY();
if (mouseY < actualY + 29) {
collapsed = !collapsed;
if (collapsed)
this.setHeight(30f);
else
this.setHeight(145f);
this.setHeight(175f);
event.cancel();
} else {
if (!collapsed)
isSliding = true;

}else if(!collapsed) {
if (mouseY > actualY + 29 && mouseY <= actualY + 59) {
float actualX = this.getActualSize().getX();
float actualWidth = this.getActualSize().getWidth();

Rectangle leftButton = new Rectangle(actualX + actualWidth - 128, actualY + 34, 16.0f, 16.0f);
Rectangle rightButton = new Rectangle(actualX + actualWidth - 16, actualY + 34, 16.0f, 16.0f);

ColorMode[] enumConstants = color.getMode().getDeclaringClass().getEnumConstants();
int currentIndex = java.util.Arrays.asList(enumConstants).indexOf(color.getMode());
int enumCount = enumConstants.length;
if(leftButton.intersects(mouseX, mouseY)) {
currentIndex = (currentIndex - 1 + enumCount) % enumCount;
}else if (rightButton.intersects(mouseX, mouseY)) {
currentIndex = (currentIndex + 1) % enumCount;
}

color.setMode(enumConstants[currentIndex]);
} else if (mouseY > actualY + 59) {
if (!collapsed)
isSliding = true;
}
}

event.cancel();
}
} else if (event.action == MouseAction.UP) {
Expand All @@ -105,8 +128,7 @@ public void onMouseMove(MouseMoveEvent event) {
double mouseY = event.getY();
if (Aoba.getInstance().hudManager.isClickGuiOpen() && this.isSliding) {
Color colorToModify = color.getValue();
float vertical = (float) Math
.min(Math.max(1.0f - (((mouseY - (actualY + 29)) - 1) / (actualHeight - 33)), 0.0f), 1.0f);
float vertical = (float) Math.min(Math.max(1.0f - (((mouseY - (actualY + 59)) - 1) / (actualHeight - 63)), 0.0f), 1.0f);

// If inside of saturation/lightness box.
if (mouseX >= actualX && mouseX <= actualX + actualWidth - 76) {
Expand Down Expand Up @@ -143,45 +165,55 @@ public void draw(DrawContext drawContext, float partialTicks) {
GuiManager.foregroundColor.getValue().getColorAsInt());

if (!collapsed) {
// Mode
Render2D.drawString(drawContext, "Mode", actualX, actualY + 34, 0xFFFFFF);
Render2D.drawString(drawContext, "<", actualX + actualWidth - 128, actualY + 34, 0xFFFFFF);
Render2D.drawString(drawContext, ">", actualX + actualWidth - 16, actualY + 34, 0xFFFFFF);

String enumText = color.getMode().name();
float stringLength = Render2D.getStringWidth(enumText);
Render2D.drawString(drawContext, enumText, actualX + actualWidth - 70 - stringLength, actualY + 34, 0xFFFFFF);

// Gradients
Color newColor = new Color(255, 0, 0);
Color colorSetting = this.color.getValue();
newColor.setHSV(colorSetting.getHue(), 1.0f, 1.0f);
Render2D.drawHorizontalGradient(matrix4f, actualX, actualY + 29, actualWidth - 76, actualHeight - 33,
Render2D.drawHorizontalGradient(matrix4f, actualX, actualY + 59, actualWidth - 76, actualHeight - 63,
new Color(255, 255, 255), newColor);
Render2D.drawVerticalGradient(matrix4f, actualX, actualY + 29, actualWidth - 76, actualHeight - 33,
Render2D.drawVerticalGradient(matrix4f, actualX, actualY + 59, actualWidth - 76, actualHeight - 63,
new Color(0, 0, 0, 0), new Color(0, 0, 0));

// Draw Hue Rectangle
float increment = ((actualHeight - 33) / 6.0f);
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 29, 30, increment,
float increment = ((actualHeight - 63) / 6.0f);
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 59, 30, increment,
new Color(255, 0, 0), new Color(255, 255, 0));
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 29 + increment, 30,
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 59 + increment, 30,
increment, new Color(255, 255, 0), new Color(0, 255, 0));
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 29 + (2 * increment), 30,
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 59 + (2 * increment), 30,
increment, new Color(0, 255, 0), new Color(0, 255, 255));
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 29 + (3 * increment), 30,
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 59 + (3 * increment), 30,
increment, new Color(0, 255, 255), new Color(0, 0, 255));
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 29 + (4 * increment), 30,
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 59 + (4 * increment), 30,
increment, new Color(0, 0, 255), new Color(255, 0, 255));
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 29 + (5 * increment), 30,
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 68, actualY + 59 + (5 * increment), 30,
increment, new Color(255, 0, 255), new Color(255, 0, 0));

// Draw Alpha Rectangle
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 30, actualY + 29, 30, actualHeight - 33,
Render2D.drawVerticalGradient(matrix4f, actualX + actualWidth - 30, actualY + 59, 30, actualHeight - 63,
new Color(255, 255, 255), new Color(0, 0, 0));

// Draw Outlines
Render2D.drawBoxOutline(matrix4f, actualX, actualY + 29, actualWidth - 76, actualHeight - 33, Colors.Black);
Render2D.drawBoxOutline(matrix4f, actualX + actualWidth - 68, actualY + 29, 30, actualHeight - 33, Colors.Black);
Render2D.drawBoxOutline(matrix4f, actualX + actualWidth - 30, actualY + 29, 30, actualHeight - 33, Colors.Black);
Render2D.drawBoxOutline(matrix4f, actualX, actualY + 59, actualWidth - 76, actualHeight - 63, Colors.Black);
Render2D.drawBoxOutline(matrix4f, actualX + actualWidth - 68, actualY + 59, 30, actualHeight - 63, Colors.Black);
Render2D.drawBoxOutline(matrix4f, actualX + actualWidth - 30, actualY + 59, 30, actualHeight - 63, Colors.Black);

// Draw Indicators
Render2D.drawCircle(matrix4f, actualX + (colorSetting.getSaturation() * (actualWidth - 76)),
actualY + 29 + ((1.0f - colorSetting.getLuminance()) * (actualHeight - 33)), 3, new Color(255, 255, 255, 255));
actualY + 59 + ((1.0f - colorSetting.getLuminance()) * (actualHeight - 63)), 3, new Color(255, 255, 255, 255));
Render2D.drawOutlinedBox(matrix4f, actualX + actualWidth - 68,
actualY + 29 + ((colorSetting.getHue() / 360.0f) * (actualHeight - 33)), 30, 3, Colors.Black,new Color(255, 255, 255, 255));
actualY + 59 + ((colorSetting.getHue() / 360.0f) * (actualHeight - 63)), 30, 3, Colors.Black,new Color(255, 255, 255, 255));
Render2D.drawOutlinedBox(matrix4f, actualX + actualWidth - 30,
actualY + 29 + (((255.0f - (colorSetting.getAlpha() * 255)) / 255.0f) * (actualHeight - 33)), 30, 3, Colors.Black,
actualY + 59 + (((255.0f - (colorSetting.getAlpha() * 255)) / 255.0f) * (actualHeight - 63)), 30, 3, Colors.Black,
new Color(255, 255, 255, 255));
}
}
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/net/aoba/settings/SettingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.mojang.logging.LogUtils;
import net.aoba.gui.Rectangle;
import net.aoba.gui.colors.Color;
import net.aoba.settings.types.ColorSetting;
import net.aoba.settings.types.ColorSetting.ColorMode;
import net.aoba.settings.types.EnumSetting;
import net.aoba.settings.types.FloatSetting;
import net.aoba.settings.types.IntegerSetting;
Expand Down Expand Up @@ -98,7 +100,8 @@ public static void saveSettings(SettingsContainer container)
+ ((Rectangle) setting.getValue()).getHeight());
}
case COLOR -> {
String s = ((Color) setting.getValue()).getColorAsHex();
ColorSetting cSetting = (ColorSetting)setting;
String s = cSetting.getMode().name() + "," + ((Color) setting.getValue()).getColorAsHex();
config.setProperty(setting.ID, s);
}
case BLOCKS -> {
Expand Down Expand Up @@ -185,12 +188,18 @@ public static void loadSettings(SettingsContainer container) {
}
}
case COLOR -> {
long hexValue = Long.parseLong(value.replace("#", ""), 16);
int Alpha = (int) ((hexValue) >> 24) & 0xFF;
int R = (int) ((hexValue) >> 16) & 0xFF;
int G = (int) ((hexValue) >> 8) & 0xFF;
int B = (int) (hexValue) & 0xFF;
setting.setValue(new Color(R, G, B, Alpha));
String[] splits = value.split(",");
ColorSetting cSetting = (ColorSetting)setting;
if(splits.length == 2) {
ColorMode enumValue = Enum.valueOf(((ColorSetting) setting).getMode().getDeclaringClass(), splits[0]);
long hexValue = Long.parseLong(splits[1].replace("#", ""), 16);
int Alpha = (int) ((hexValue) >> 24) & 0xFF;
int R = (int) ((hexValue) >> 16) & 0xFF;
int G = (int) ((hexValue) >> 8) & 0xFF;
int B = (int) (hexValue) & 0xFF;
cSetting.setMode(enumValue);
setting.setValue(new Color(R, G, B, Alpha));
}
}
case BLOCKS -> {
String[] ids = value.split(",");
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/net/aoba/settings/types/ColorSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@

import net.aoba.gui.GuiManager;
import net.aoba.gui.colors.Color;
import net.aoba.gui.colors.ColorMode;
import net.aoba.settings.Setting;

public class ColorSetting extends Setting<Color> {
public enum ColorMode {
Solid,
Rainbow,
Random,
}

private ColorMode mode;
private ColorMode mode = ColorMode.Solid;

public ColorSetting(String ID, String description, Color default_value) {
super(ID, description, default_value);
Expand All @@ -42,10 +46,14 @@ protected boolean isValueValid(Color value) {
return (value.getRed() <= 255 && value.getGreen() <= 255 && value.getBlue() <= 255);
}

public ColorMode getMode() {
return mode;
}

public void setMode(ColorMode color) {
mode = color;
switch (mode) {
case Normal:
case Solid:
this.setValue(default_value);
break;
case Rainbow:
Expand Down

0 comments on commit 23824b5

Please sign in to comment.