diff --git a/src/main/java/sonar/fluxnetworks/api/FluxTranslate.java b/src/main/java/sonar/fluxnetworks/api/FluxTranslate.java index b263f68e..7e981f49 100644 --- a/src/main/java/sonar/fluxnetworks/api/FluxTranslate.java +++ b/src/main/java/sonar/fluxnetworks/api/FluxTranslate.java @@ -73,6 +73,7 @@ public class FluxTranslate { EDITING_CONNECTIONS = new FluxTranslate("gui.fluxnetworks.label.editingconnections"), CONNECTIONS = new FluxTranslate("gui.fluxnetworks.label.connections"), CUSTOM_COLOR = new FluxTranslate("gui.fluxnetworks.label.customcolor"), + CUSTOM_COLOR_EDIT = new FluxTranslate("gui.fluxnetworks.label.customcolor.edit"), CONNECTING_TO = new FluxTranslate("gui.fluxnetworks.label.connectingto"), TOTAL = new FluxTranslate("gui.fluxnetworks.label.total"), DELETE_NETWORK = new FluxTranslate("gui.fluxnetworks.label.deletenetwork"), diff --git a/src/main/java/sonar/fluxnetworks/api/gui/EnumNetworkColor.java b/src/main/java/sonar/fluxnetworks/api/gui/EnumNetworkColor.java index ec088f2a..c7c59781 100644 --- a/src/main/java/sonar/fluxnetworks/api/gui/EnumNetworkColor.java +++ b/src/main/java/sonar/fluxnetworks/api/gui/EnumNetworkColor.java @@ -27,4 +27,13 @@ public enum EnumNetworkColor { public int getRGB() { return color; } + + public static int getColorIndex(int color) { + for (int i = 0; i < VALUES.length; i++) { + if (VALUES[i].color == color) { + return i; + } + } + return -1; + } } diff --git a/src/main/java/sonar/fluxnetworks/client/gui/button/CustomColorButton.java b/src/main/java/sonar/fluxnetworks/client/gui/button/CustomColorButton.java new file mode 100644 index 00000000..881724cf --- /dev/null +++ b/src/main/java/sonar/fluxnetworks/client/gui/button/CustomColorButton.java @@ -0,0 +1,11 @@ +package sonar.fluxnetworks.client.gui.button; + +import sonar.fluxnetworks.client.gui.basic.GuiFocusable; + +public class CustomColorButton extends ColorButton { + public static final int DEFAULT_COLOR = 0xFFFFFF; + + public CustomColorButton(GuiFocusable screen, int x, int y) { + super(screen, x, y, DEFAULT_COLOR); + } +} \ No newline at end of file diff --git a/src/main/java/sonar/fluxnetworks/client/gui/popup/PopupCustomColor.java b/src/main/java/sonar/fluxnetworks/client/gui/popup/PopupCustomColor.java index 432890d4..bacfcc92 100644 --- a/src/main/java/sonar/fluxnetworks/client/gui/popup/PopupCustomColor.java +++ b/src/main/java/sonar/fluxnetworks/client/gui/popup/PopupCustomColor.java @@ -5,6 +5,7 @@ import sonar.fluxnetworks.api.FluxTranslate; import sonar.fluxnetworks.client.gui.basic.GuiButtonCore; import sonar.fluxnetworks.client.gui.basic.GuiPopupCore; +import sonar.fluxnetworks.client.gui.button.ColorButton; import sonar.fluxnetworks.client.gui.button.FluxEditBox; import sonar.fluxnetworks.client.gui.button.SimpleButton; import sonar.fluxnetworks.client.gui.tab.GuiTabEditAbstract; @@ -18,6 +19,8 @@ public class PopupCustomColor extends GuiPopupCore { public SimpleButton mCancel; public SimpleButton mApply; public int mCurrentColor; + public boolean mCancelled = true; + private ColorButton mColorPreview; public PopupCustomColor(GuiTabEditAbstract host, int currentColor) { super(host); @@ -38,11 +41,41 @@ public void init() { mColor = FluxEditBox.create("0x", font, leftPos + (imageWidth / 2) - 40, topPos + 64, 80, 12) .setHexOnly(); mColor.setMaxLength(6); - mColor.setValue(Integer.toHexString(mCurrentColor).toUpperCase(Locale.ROOT)); - mColor.setResponder(string -> mApply.setClickable(string.length() == 6)); + mColor.setValue(paddedColorHex(mCurrentColor)); + mColor.setResponder(this::onInputChanged); + + mColorPreview = new ColorButton(this, leftPos + 30, topPos + 64, mCurrentColor); + mColorPreview.setSelected(true); + mColorPreview.setClickable(false); + + mButtons.add(mColorPreview); + addRenderableWidget(mColor); } + private String paddedColorHex(int color) { + StringBuilder builder = new StringBuilder(Integer.toHexString(color)); + while (builder.length() < 6) { + builder.insert(0, '0'); + } + return builder.toString().toUpperCase(Locale.ROOT); + } + + private void onInputChanged(String string) { + if (string.length() == 6) { + try { + mColorPreview.mColor = mColor.getIntegerFromHex(); + mApply.setClickable(true); + } catch (NumberFormatException e) { + mApply.setClickable(false); + mColorPreview.mColor = 0; + } + } else { + mApply.setClickable(false); + mColorPreview.mColor = 0; + } + } + @Override public void drawForegroundLayer(@Nonnull GuiGraphics gr, int mouseX, int mouseY, float deltaTicks) { super.drawForegroundLayer(gr, mouseX, mouseY, deltaTicks); @@ -56,9 +89,10 @@ public void onButtonClicked(GuiButtonCore button, int mouseX, int mouseY, int mo if (button == mCancel) { mHost.closePopup(); } else if (button == mApply) { + mCancelled = false; mCurrentColor = mColor.getIntegerFromHex(); mHost.closePopup(); } } } -} +} \ No newline at end of file diff --git a/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabCreate.java b/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabCreate.java index 85bed718..00061806 100644 --- a/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabCreate.java +++ b/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabCreate.java @@ -5,11 +5,9 @@ import org.lwjgl.glfw.GLFW; import sonar.fluxnetworks.api.FluxConstants; import sonar.fluxnetworks.api.FluxTranslate; -import sonar.fluxnetworks.api.gui.EnumNetworkColor; import sonar.fluxnetworks.api.network.SecurityLevel; import sonar.fluxnetworks.client.gui.EnumNavigationTab; import sonar.fluxnetworks.client.gui.basic.GuiButtonCore; -import sonar.fluxnetworks.client.gui.button.ColorButton; import sonar.fluxnetworks.client.gui.button.SimpleButton; import sonar.fluxnetworks.common.connection.FluxMenu; import sonar.fluxnetworks.register.ClientMessages; @@ -35,17 +33,7 @@ public void init() { super.init(); mNetworkName.setValue(FluxTranslate.PLAYERS_NETWORK.format(mPlayer.getGameProfile().getName())); - // two rows - for (int i = 0; i < EnumNetworkColor.VALUES.length; i++) { - final EnumNetworkColor color = EnumNetworkColor.VALUES[i]; - ColorButton button = new ColorButton(this, - leftPos + 48 + (i % 7) * 16, topPos + 87 + (i / 7) * 16, color.getRGB()); - if (i == 0) { - mColorButton = button; - button.setSelected(true); - } - mButtons.add(button); - } + initColorSelector(leftPos + 48, topPos + 87, true); mCreate = new SimpleButton(this, leftPos + (imageWidth / 2) - 24, topPos + 150, 48, 12, FluxTranslate.CREATE.get()); diff --git a/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabEditAbstract.java b/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabEditAbstract.java index ab4d50b4..1f918dc8 100644 --- a/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabEditAbstract.java +++ b/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabEditAbstract.java @@ -5,10 +5,12 @@ import net.minecraft.world.entity.player.Player; import org.lwjgl.glfw.GLFW; import sonar.fluxnetworks.api.FluxTranslate; +import sonar.fluxnetworks.api.gui.EnumNetworkColor; import sonar.fluxnetworks.api.network.SecurityLevel; import sonar.fluxnetworks.client.gui.EnumNavigationTab; import sonar.fluxnetworks.client.gui.basic.*; import sonar.fluxnetworks.client.gui.button.ColorButton; +import sonar.fluxnetworks.client.gui.button.CustomColorButton; import sonar.fluxnetworks.client.gui.button.FluxEditBox; import sonar.fluxnetworks.client.gui.popup.PopupCustomColor; import sonar.fluxnetworks.common.connection.FluxMenu; @@ -24,6 +26,7 @@ public abstract class GuiTabEditAbstract extends GuiTabCore { protected SecurityLevel mSecurityLevel; public ColorButton mColorButton; + public CustomColorButton mCustomColorButton; public FluxEditBox mNetworkName; public FluxEditBox mPassword; @@ -69,6 +72,17 @@ protected void drawForegroundLayer(GuiGraphics gr, int mouseX, int mouseY, float // .getName(), 14, 78, 0x606060); gr.drawString(font, FluxTranslate.NETWORK_COLOR.get() + ":", leftPos + 16, topPos + 89, 0xFF808080); + // "Edit color" tooltip + if (getCurrentPopup() == null) { + for (GuiButtonCore button : mButtons) { + if (button instanceof ColorButton colorButton && colorButton.isMouseHovered(mouseX, mouseY)) { + // TODO: should probably be replaced with a custom tooltip + setTooltipForNextRenderPass(FluxTranslate.CUSTOM_COLOR_EDIT.makeComponent()); + renderTooltip(gr, mouseX, mouseY); + } + } + } + renderNetwork(gr, mNetworkName.getValue(), mColorButton.mColor, topPos + 126); } } @@ -98,22 +112,49 @@ public boolean onMouseClicked(double mouseX, double mouseY, int mouseButton) { @Override public void onButtonClicked(GuiButtonCore button, float mouseX, float mouseY, int mouseButton) { super.onButtonClicked(button, mouseX, mouseY, mouseButton); - if (button instanceof ColorButton) { - mColorButton.setSelected(false); - mColorButton = (ColorButton) button; - mColorButton.setSelected(true); - onEditSettingsChanged(); - if (mouseButton == GLFW.GLFW_MOUSE_BUTTON_RIGHT) { - openPopup(new PopupCustomColor(this, mColorButton.mColor)); + if (button instanceof ColorButton colorButton) { + if (mouseButton == GLFW.GLFW_MOUSE_BUTTON_LEFT) { + mColorButton.setSelected(false); + mColorButton = colorButton; + mColorButton.setSelected(true); + onEditSettingsChanged(); + } else if (mouseButton == GLFW.GLFW_MOUSE_BUTTON_RIGHT) { + openPopup(new PopupCustomColor(this, colorButton.mColor)); } } } + protected void initColorSelector(int posX, int posY, boolean selectFirst) { + int selectedIndex = selectFirst ? 0 : EnumNetworkColor.getColorIndex(getNetwork().getNetworkColor()); + + for (int i = 0; i < EnumNetworkColor.VALUES.length; i++) { + ColorButton button = new ColorButton(this, posX + (i % 7) * 16, posY + (i / 7) * 16, EnumNetworkColor.VALUES[i].getRGB()); + if (i == selectedIndex) { + mColorButton = button; + button.setSelected(true); + } + mButtons.add(button); + } + + // Custom color button + mCustomColorButton = new CustomColorButton(this, leftPos + 32, topPos + 103); + // non-default color is selected + if (mColorButton == null) { + mCustomColorButton.mColor = getNetwork().getNetworkColor(); + mColorButton = mCustomColorButton; + mCustomColorButton.setSelected(true); + } + mButtons.add(mCustomColorButton); + } + @Override public void onPopupClose(GuiPopupCore popUp) { super.onPopupClose(popUp); - if (popUp instanceof PopupCustomColor) { - mColorButton.mColor = ((PopupCustomColor) popUp).mCurrentColor; + if (popUp instanceof PopupCustomColor colorPopup && !colorPopup.mCancelled) { + mCustomColorButton.mColor = colorPopup.mCurrentColor; + mColorButton.setSelected(false); + mColorButton = mCustomColorButton; + mColorButton.setSelected(true); onEditSettingsChanged(); } } diff --git a/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabSettings.java b/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabSettings.java index 5620d2b0..89925831 100644 --- a/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabSettings.java +++ b/src/main/java/sonar/fluxnetworks/client/gui/tab/GuiTabSettings.java @@ -6,11 +6,9 @@ import org.lwjgl.glfw.GLFW; import sonar.fluxnetworks.api.FluxConstants; import sonar.fluxnetworks.api.FluxTranslate; -import sonar.fluxnetworks.api.gui.EnumNetworkColor; import sonar.fluxnetworks.api.network.SecurityLevel; import sonar.fluxnetworks.client.gui.EnumNavigationTab; import sonar.fluxnetworks.client.gui.basic.GuiButtonCore; -import sonar.fluxnetworks.client.gui.button.ColorButton; import sonar.fluxnetworks.client.gui.button.SimpleButton; import sonar.fluxnetworks.common.connection.FluxMenu; import sonar.fluxnetworks.register.ClientMessages; @@ -68,28 +66,7 @@ public void init() { mApply.setClickable(false); mButtons.add(mApply); - boolean colorSet = false; - // two rows - for (int i = 0; i < EnumNetworkColor.VALUES.length; i++) { - final EnumNetworkColor color = EnumNetworkColor.VALUES[i]; - ColorButton button = new ColorButton(this, - leftPos + 48 + (i % 7) * 16, topPos + 87 + (i / 7) * 16, color.getRGB()); - if (!colorSet && color.getRGB() == getNetwork().getNetworkColor()) { - mColorButton = button; - button.setSelected(true); - colorSet = true; - } - mButtons.add(button); - } - - // it's a custom color - if (!colorSet) { - ColorButton button = new ColorButton(this, - leftPos + 32, topPos + 107, getNetwork().getNetworkColor()); - mColorButton = button; - button.setSelected(true); - mButtons.add(button); - } + initColorSelector(leftPos + 48, topPos + 87, false); } } diff --git a/src/main/resources/assets/fluxnetworks/lang/en_us.json b/src/main/resources/assets/fluxnetworks/lang/en_us.json index 0da70bf5..6b19b43a 100644 --- a/src/main/resources/assets/fluxnetworks/lang/en_us.json +++ b/src/main/resources/assets/fluxnetworks/lang/en_us.json @@ -87,6 +87,7 @@ "gui.fluxnetworks.label.editingconnections": "Editing %d Connections", "gui.fluxnetworks.label.connections": "Connections", "gui.fluxnetworks.label.customcolor": "Custom Color", + "gui.fluxnetworks.label.customcolor.edit": "Right-Click to Edit", "gui.fluxnetworks.label.connectingto": "Connecting to %s", "gui.fluxnetworks.label.total": "Total", "gui.fluxnetworks.label.deletenetwork": "Delete Network",