Skip to content

Commit

Permalink
feat: now using FTB Library color selector
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Apr 12, 2024
1 parent bd245f5 commit 300d39b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import dev.ftb.mods.ftbchunks.FTBChunks;
import dev.ftb.mods.ftbchunks.FTBChunksWorldConfig;
import dev.ftb.mods.ftbchunks.api.FTBChunksAPI;
import dev.ftb.mods.ftbchunks.api.client.FTBChunksClientAPI;
import dev.ftb.mods.ftbchunks.api.client.event.MapIconEvent;
import dev.ftb.mods.ftbchunks.api.client.icon.MapIcon;
import dev.ftb.mods.ftbchunks.api.client.icon.MapType;
import dev.ftb.mods.ftbchunks.api.client.icon.WaypointIcon;
import dev.ftb.mods.ftbchunks.api.client.waypoint.Waypoint;
import dev.ftb.mods.ftbchunks.client.gui.AddWaypointOverlay;
import dev.ftb.mods.ftbchunks.client.gui.ChunkScreen;
import dev.ftb.mods.ftbchunks.client.gui.LargeMapScreen;
import dev.ftb.mods.ftbchunks.client.gui.WaypointEditorScreen;
Expand All @@ -32,6 +32,7 @@
import dev.ftb.mods.ftbchunks.client.mapicon.*;
import dev.ftb.mods.ftbchunks.net.PartialPackets;
import dev.ftb.mods.ftbchunks.net.SendGeneralDataPacket.GeneralChunkData;
import dev.ftb.mods.ftblibrary.config.ColorConfig;
import dev.ftb.mods.ftblibrary.config.StringConfig;
import dev.ftb.mods.ftblibrary.config.ui.EditConfigFromStringScreen;
import dev.ftb.mods.ftblibrary.config.ui.EditStringConfigOverlay;
Expand Down Expand Up @@ -381,7 +382,7 @@ private EventResult addQuickWaypoint() {
if (player == null) return EventResult.pass();

return MapManager.getInstance().map(manager -> {
BaseScreen screen = new WaypointAddScreen(name, manager, player);
BaseScreen screen = new WaypointAddScreen(name, player);
screen.openGuiLater();
// later needed to prevent keypress being passed into gui
return EventResult.interruptTrue();
Expand Down Expand Up @@ -1169,13 +1170,11 @@ public static Waypoint addWaypoint(Player player, String name, BlockPos position

private static class WaypointAddScreen extends BaseScreen {
private final StringConfig name;
private final MapManager manager;
private final Player player;

public WaypointAddScreen(StringConfig name, MapManager manager, Player player) {
public WaypointAddScreen(StringConfig name, Player player) {
super();
this.name = name;
this.manager = manager;
this.player = player;
this.setHeight(35);
}
Expand All @@ -1186,16 +1185,22 @@ public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int

@Override
public void addWidgets() {
EditStringConfigOverlay<String> overlay = new EditStringConfigOverlay<>(this, name, set -> {
ColorConfig col = new ColorConfig();
col.setValue(Color4I.hsb(MathUtils.RAND.nextFloat(), 1F, 1F));
AddWaypointOverlay overlay = new AddWaypointOverlay(this, name, col, set -> {
if (set && !name.getValue().isEmpty()) {
Waypoint wp = addWaypoint(player, name.getValue(), player.blockPosition(), Color4I.hsb(MathUtils.RAND.nextFloat(), 1F, 1F).rgba());
Waypoint wp = addWaypoint(player, name.getValue(), player.blockPosition(), col.getValue().rgba());
Minecraft.getInstance().player.displayClientMessage(
Component.translatable("ftbchunks.waypoint_added",
Component.literal(wp.getName()).withStyle(ChatFormatting.YELLOW)
), true);
}
closeGui();
}, Component.translatable("key.ftbchunks.add_waypoint"));
}) {
@Override
public void onClosed() {
closeGui();
}
};
overlay.setWidth(this.width);
pushModalPanel(overlay);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.ftb.mods.ftbchunks.client.gui;

import dev.ftb.mods.ftblibrary.config.ColorConfig;
import dev.ftb.mods.ftblibrary.config.ConfigCallback;
import dev.ftb.mods.ftblibrary.config.ConfigFromString;
import dev.ftb.mods.ftblibrary.config.ui.EditStringConfigOverlay;
import dev.ftb.mods.ftblibrary.icon.Color4I;
import dev.ftb.mods.ftblibrary.icon.Icon;
import dev.ftb.mods.ftblibrary.ui.*;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;

public class AddWaypointOverlay extends EditStringConfigOverlay<String> {
private final ColorButton colorButton;

public AddWaypointOverlay(Panel panel, ConfigFromString<String> config, ColorConfig colorConfig, ConfigCallback callback) {
super(panel, config, callback, Component.translatable("ftbchunks.gui.add_waypoint"));

colorButton = new ColorButton(colorConfig.getValue(), (btn, mb) -> {
ColorSelectorPanel.popupAtMouse(getGui(), colorConfig, accepted -> {
if (accepted) {
btn.setIcon(colorConfig.getValue());
} else {
colorConfig.setValue(((ColorButton) btn).getIcon());
}
});
});
}

@Override
public void addWidgets() {
super.addWidgets();
add(colorButton);
}

@Override
public void alignWidgets() {
super.alignWidgets();
colorButton.setPosAndSize(width - 11, 1, 10, 10);
}

private class ColorButton extends SimpleButton {
public ColorButton(Icon icon, Callback c) {
super(AddWaypointOverlay.this, Component.empty(), icon, c);
}

Color4I getIcon() {
return icon instanceof Color4I c ? c : Color4I.empty();
}

@Override
public void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
icon.draw(graphics, x, y, w, h);
Color4I shade = getIcon().addBrightness(-0.15f);
GuiHelper.drawHollowRect(graphics, x, y, w, h, shade, false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import dev.ftb.mods.ftbchunks.client.map.*;
import dev.ftb.mods.ftbchunks.net.TeleportFromMapPacket;
import dev.ftb.mods.ftbchunks.util.HeightUtils;
import dev.ftb.mods.ftblibrary.config.ColorConfig;
import dev.ftb.mods.ftblibrary.config.StringConfig;
import dev.ftb.mods.ftblibrary.config.ui.EditStringConfigOverlay;
import dev.ftb.mods.ftblibrary.icon.Color4I;
import dev.ftb.mods.ftblibrary.icon.Icon;
import dev.ftb.mods.ftblibrary.icon.Icons;
Expand Down Expand Up @@ -223,15 +223,17 @@ public boolean mousePressed(MouseButton button) {
list.add(new ContextMenuItem(Component.translatable("ftbchunks.gui.add_waypoint"), Icons.ADD, btn -> {
StringConfig name = new StringConfig();
name.setValue("");
var overlay = new EditStringConfigOverlay<>(getGui(), name, accepted -> {
if (accepted) {
WaypointImpl waypoint = new WaypointImpl(WaypointType.DEFAULT, dimension, pos)
.setName(name.getValue())
.setColor(Color4I.hsb(MathUtils.RAND.nextFloat(), 1F, 1F).rgba());
dimension.getWaypointManager().add(waypoint);
refreshWidgets();
}
}, Component.translatable("ftbchunks.gui.add_waypoint")).atMousePosition();
ColorConfig col = new ColorConfig();
col.setValue(Color4I.hsb(MathUtils.RAND.nextFloat(), 1F, 1F));
var overlay = new AddWaypointOverlay(getGui(), name, col, accepted -> {
if (accepted) {
WaypointImpl waypoint = new WaypointImpl(WaypointType.DEFAULT, dimension, pos)
.setName(name.getValue())
.setColor(col.getValue().rgba());
dimension.getWaypointManager().add(waypoint);
refreshWidgets();
}
}).atMousePosition();
overlay.setWidth(150);
overlay.setX(Math.min(overlay.getX(), getScreen().getGuiScaledWidth() - 155));
getGui().pushModalPanel(overlay);
Expand Down Expand Up @@ -481,4 +483,5 @@ public boolean isEnabled() {
return shouldDraw();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.ftb.mods.ftbchunks.client.map.MapManager;
import dev.ftb.mods.ftbchunks.client.map.WaypointImpl;
import dev.ftb.mods.ftbchunks.net.TeleportFromMapPacket;
import dev.ftb.mods.ftblibrary.config.ColorConfig;
import dev.ftb.mods.ftblibrary.config.StringConfig;
import dev.ftb.mods.ftblibrary.icon.Color4I;
import dev.ftb.mods.ftblibrary.icon.Icon;
Expand Down Expand Up @@ -136,7 +137,7 @@ public void alignWidgets() {

@Override
public Theme getTheme() {
return THEME;
return super.getTheme();
}

@Override
Expand Down Expand Up @@ -296,18 +297,18 @@ public boolean mousePressed(MouseButton button) {
}));
if (wp.getType().canChangeColor()) {
list.add(new ContextMenuItem(Component.translatable("ftbchunks.gui.change_color"), Icons.COLOR_RGB, btn -> {
int r = (wp.getColor() >> 16) & 0xFF;
int g = (wp.getColor() >> 8) & 0xFF;
int b = wp.getColor() & 0xFF;
float[] hsb = Color.RGBtoHSB(r, g, b, new float[3]);
float add = isShiftKeyDown() ? -1F/12F : 1F/12F;
Color4I col = Color4I.hsb(hsb[0] + add, hsb[1], hsb[2]);
wp.setColor(col.rgba());
wp.refreshIcon();
if (widgets.get(1) instanceof TextField tf) {
tf.setColor(Color4I.rgb(wp.getColor()));
}
}).setCloseMenu(false));
ColorConfig col = new ColorConfig();
col.setValue(Color4I.rgb(wp.getColor()));
ColorSelectorPanel.popupAtMouse(btn.getGui(), col, accepted -> {
if (accepted) {
wp.setColor(col.getValue().rgba());
wp.refreshIcon();
if (widgets.get(1) instanceof TextField tf) {
tf.setColor(Color4I.rgb(wp.getColor()));
}
}
});
}));
}
if (Minecraft.getInstance().player.hasPermissions(2)) { // permissions are checked again on server!
list.add(new ContextMenuItem(Component.translatable("ftbchunks.gui.teleport"), PEARL_ICON, btn -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import dev.ftb.mods.ftbchunks.api.client.icon.WaypointIcon;
import dev.ftb.mods.ftbchunks.client.gui.LargeMapScreen;
import dev.ftb.mods.ftbchunks.client.map.WaypointImpl;
import dev.ftb.mods.ftblibrary.config.ColorConfig;
import dev.ftb.mods.ftblibrary.config.StringConfig;
import dev.ftb.mods.ftblibrary.icon.Color4I;
import dev.ftb.mods.ftblibrary.icon.Icon;
import dev.ftb.mods.ftblibrary.icon.Icons;
import dev.ftb.mods.ftblibrary.icon.ImageIcon;
import dev.ftb.mods.ftblibrary.math.MathUtils;
import dev.ftb.mods.ftblibrary.ui.BaseScreen;
import dev.ftb.mods.ftblibrary.ui.ColorSelectorPanel;
import dev.ftb.mods.ftblibrary.ui.ContextMenuItem;
import dev.ftb.mods.ftblibrary.ui.Widget;
import dev.ftb.mods.ftblibrary.ui.input.Key;
Expand Down Expand Up @@ -109,17 +111,17 @@ private void openWPContextMenu(LargeMapScreen screen) {

if (waypoint.getType().canChangeColor()) {
contextMenu.add(new ContextMenuItem(Component.translatable("ftbchunks.gui.change_color"), Icons.COLOR_RGB, btn -> {
int r = (waypoint.getColor() >> 16) & 0xFF;
int g = (waypoint.getColor() >> 8) & 0xFF;
int b = waypoint.getColor() & 0xFF;
float[] hsb = Color.RGBtoHSB(r, g, b, new float[3]);
float add = Widget.isShiftKeyDown() ? -1F/12F : 1F/12F;
Color4I col = Color4I.hsb(hsb[0] + add, hsb[1], hsb[2]);
waypoint.setColor(col.rgba());
icon = Color4I.empty();
outsideIcon = Color4I.empty();
checkIcon();
}).setCloseMenu(false));
ColorConfig col = new ColorConfig();
col.setValue(Color4I.rgb(waypoint.getColor()));
ColorSelectorPanel.popupAtMouse(btn.getGui(), col, accepted -> {
if (accepted) {
waypoint.setColor(col.getValue().rgba());
icon = Color4I.empty();
outsideIcon = Color4I.empty();
checkIcon();
}
});
}));
}

contextMenu.add(new ContextMenuItem(Component.translatable("ftbchunks.label." + (waypoint.isHidden() ? "show" : "hide")), Icons.BEACON, b -> {
Expand Down

0 comments on commit 300d39b

Please sign in to comment.