Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved custom color editor #558

Open
wants to merge 1 commit into
base: 1.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/sonar/fluxnetworks/api/FluxTranslate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,8 @@ public class PopupCustomColor extends GuiPopupCore<GuiTabEditAbstract> {
public SimpleButton mCancel;
public SimpleButton mApply;
public int mCurrentColor;
public boolean mCancelled = true;
private ColorButton mColorPreview;

public PopupCustomColor(GuiTabEditAbstract host, int currentColor) {
super(host);
Expand All @@ -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);
Expand All @@ -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();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/fluxnetworks/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down