Skip to content

Commit

Permalink
[1.21.1] Update waypoint add screen (#322)
Browse files Browse the repository at this point in the history
feat: Update waypoint add screen
  • Loading branch information
UnRealDinnerbone authored Oct 9, 2024
1 parent d583815 commit ece1d5f
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1305,10 +1305,12 @@ public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int
@Override
public void addWidgets() {
ColorConfig col = new ColorConfig();
AddWaypointOverlay.GlobalPosConfig globalPosConfig = new AddWaypointOverlay.GlobalPosConfig();
globalPosConfig.setValue(waypointLocation);
col.setValue(Color4I.hsb(MathUtils.RAND.nextFloat(), 1F, 1F));
AddWaypointOverlay overlay = new AddWaypointOverlay(this, name, col, set -> {
AddWaypointOverlay overlay = new AddWaypointOverlay(this, globalPosConfig, name, col, set -> {
if (set && !name.getValue().isEmpty()) {
Waypoint wp = addWaypoint(name.getValue(), waypointLocation, col.getValue().rgba());
Waypoint wp = addWaypoint(name.getValue(), globalPosConfig.getValue(), col.getValue().rgba());
Minecraft.getInstance().player.displayClientMessage(
Component.translatable("ftbchunks.waypoint_added",
Component.literal(wp.getName()).withStyle(ChatFormatting.YELLOW)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
package dev.ftb.mods.ftbchunks.client.gui;

import dev.ftb.mods.ftbchunks.client.map.MapManager;
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 dev.ftb.mods.ftblibrary.icon.Icons;
import dev.ftb.mods.ftblibrary.ui.Button;
import dev.ftb.mods.ftblibrary.ui.ColorSelectorPanel;
import dev.ftb.mods.ftblibrary.ui.ContextMenuItem;
import dev.ftb.mods.ftblibrary.ui.DropDownMenu;
import dev.ftb.mods.ftblibrary.ui.GuiHelper;
import dev.ftb.mods.ftblibrary.ui.IntTextBox;
import dev.ftb.mods.ftblibrary.ui.Panel;
import dev.ftb.mods.ftblibrary.ui.SimpleButton;
import dev.ftb.mods.ftblibrary.ui.SimpleTextButton;
import dev.ftb.mods.ftblibrary.ui.TextBox;
import dev.ftb.mods.ftblibrary.ui.Theme;
import dev.ftb.mods.ftblibrary.ui.input.MouseButton;
import dev.ftb.mods.ftblibrary.util.TextComponentUtils;
import dev.ftb.mods.ftblibrary.util.TooltipList;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import static dev.ftb.mods.ftblibrary.util.TextComponentUtils.hotkeyTooltip;

public class AddWaypointOverlay extends EditStringConfigOverlay<String> {
private final ColorButton colorButton;
private final TextBox dimension;
private final IntTextBox x, y, z;
private final Button buttonAccept, buttonCancel;
private final Button dropDownButton;
private final GlobalPosConfig pos;

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

public AddWaypointOverlay(Panel panel, GlobalPosConfig pos, ConfigFromString<String> config, ColorConfig colorConfig, ConfigCallback callback) {
super(panel, config, callback, Component.translatable("ftbchunks.gui.add_waypoint"));
this.pos = pos;
colorButton = new ColorButton(colorConfig.getValue(), (btn, mb) -> {
ColorSelectorPanel.popupAtMouse(getGui(), colorConfig, accepted -> {
if (accepted) {
Expand All @@ -25,18 +59,142 @@ public AddWaypointOverlay(Panel panel, ConfigFromString<String> config, ColorCon
}
});
});
this.dimension = new TextBox(this) {
@Override
public boolean allowInput() {
return false;
}
};
List<ContextMenuItem> contextMenuItems = createDimContextItems(key -> {
dimension.setText(key.location().toString());
getGui().closeContextMenu();
});
this.dropDownButton = new SimpleButton(this, Component.empty(), Icons.DROPDOWN_OUT, (widget, mouseButton) -> {
DropDownMenu dropDownMenu = getGui().openDropdownMenu(contextMenuItems);
dropDownMenu.setPos(dimension.getX(), dimension.getY() + dimension.getHeight());
}) {
@Override
public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
theme.drawButton(graphics, x, y, w, h, getWidgetType());
}
};
this.x = new AddWaypointIntBox(this);
this.y = new AddWaypointIntBox(this);
this.z = new AddWaypointIntBox(this);

this.textBox.setLabel(Component.translatable("ftbchunks.gui.label.name"));
this.dimension.setLabel(Component.translatable("ftbchunks.gui.label.dimension"));
this.x.setLabel(Component.literal("X"));
this.y.setLabel(Component.literal("Y"));
this.z.setLabel(Component.literal("Z"));

this.dimension.setText(pos.getValue().dimension().location().toString());
this.x.setAmount(pos.getValue().pos().getX());
this.y.setAmount(pos.getValue().pos().getY());
this.z.setAmount(pos.getValue().pos().getZ());

this.x.setMinMax(Integer.MIN_VALUE, Integer.MAX_VALUE);
this.y.setMinMax(Integer.MIN_VALUE, Integer.MAX_VALUE);
this.z.setMinMax(Integer.MIN_VALUE, Integer.MAX_VALUE);


buttonAccept = new SimpleTextButton(this, Component.translatable("gui.accept"), Icons.ACCEPT) {
@Override
public void onClicked(MouseButton button) {
handleAccepted(button);
}

@Override
public void addMouseOverText(TooltipList list) {
if (isNameValid()) {
list.add(hotkeyTooltip("⇧ + Enter"));
} else {
list.add(Component.translatable("ftbchunks.gui.waypoint.no_name"));
}
}

@Override
public void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
buttonAccept.setIcon(isNameValid() ? Icons.ACCEPT : Icons.ACCEPT_GRAY);
super.draw(graphics, theme, x, y, w, h);
}
};
SimpleTextButton.accept(this, this::handleAccepted, hotkeyTooltip("⇧ + Enter"));
buttonCancel = SimpleTextButton.cancel(this, this::handleCancelled, hotkeyTooltip("ESC"));

setAddAcceptCancelButtons(false);

}

private boolean isNameValid() {
return !textBox.getText().isBlank();
}

private void handleAccepted(MouseButton mouseButton) {
if (!isNameValid()) {
return;
}
playClickSound();
onAccepted(buttonAccept, mouseButton);
}

private void handleCancelled(MouseButton mouseButton) {
playClickSound();
onCancelled(buttonCancel, mouseButton);
}

private void updatePos() {
ResourceLocation dimension = ResourceLocation.parse(this.dimension.getText());
ResourceKey<Level> resourceKey = ResourceKey.create(Registries.DIMENSION, dimension);
if (x.getText().isBlank() || y.getText().isBlank() || z.getText().isBlank()) {
return;
}
BlockPos blockPos = new BlockPos(x.getIntValue(), y.getIntValue(), z.getIntValue());
this.pos.setValue(GlobalPos.of(resourceKey, blockPos));
}

@Override
public void addWidgets() {
super.addWidgets();

add(colorButton);
add(dimension);
add(x);
add(y);
add(z);
add(buttonAccept);
add(buttonCancel);
add(dropDownButton);
}

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

int widgetH = getGui().getTheme().getFontHeight() + 4;
textBox.setPos(2, widgetH + 6);
textBox.setWidth(width - widgetH - 4 - 4);
colorButton.setPosAndSize(width - widgetH - 4, widgetH + 6, widgetH + 1, widgetH + 1);
int w = (width - 3) / 3;
int y1 = textBox.posY + textBox.getHeight() + widgetH / 2 + 2;
dimension.setPosAndSize(2, y1, width - widgetH - 4 - 4, widgetH);
dropDownButton.setPosAndSize(width - widgetH - 4, y1, widgetH + 1, widgetH);
int xyzHeight = dimension.posY + dimension.getHeight() + widgetH / 2 + 2;
x.setPosAndSize(2, xyzHeight, w - 2, widgetH);
y.setPosAndSize(2 + w, xyzHeight, w - 2, widgetH);
z.setPosAndSize(2 + w * 2, xyzHeight, w - 2, widgetH);

int buttonW = w + w / 2;
int buttonHeight = x.posY + x.getHeight() + 2;
buttonAccept.setPosAndSize(width - buttonW * 2 - 2, buttonHeight, buttonW, widgetH + 1);
buttonCancel.setPosAndSize(width - buttonW, buttonHeight, buttonW - 3, widgetH + 1);

height += 28 + 14 + 20;
}

@Override
public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
super.drawBackground(graphics, theme, x, y, w, h);
}

private class ColorButton extends SimpleButton {
Expand All @@ -55,4 +213,32 @@ public void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, int h)
GuiHelper.drawHollowRect(graphics, x, y, w, h, shade, false);
}
}

public static List<ContextMenuItem> createDimContextItems(Consumer<ResourceKey<Level>> onSelected) {
List<ContextMenuItem> contextMenuItems = new ArrayList<>();
MapManager manager = MapManager.getInstance().orElseThrow();
manager.getDimensions().forEach((key, value) ->
contextMenuItems.add(new ContextMenuItem(TextComponentUtils.translatedDimension(key), Icon.empty(), (button) -> onSelected.accept(key))));
return contextMenuItems;
}

public class AddWaypointIntBox extends IntTextBox {

public AddWaypointIntBox(Panel panel) {
super(panel);
}

@Override
public void onTextChanged() {
super.onTextChanged();
updatePos();
}
}

public static class GlobalPosConfig extends ConfigFromString<GlobalPos> {
@Override
public boolean parse(@Nullable Consumer<GlobalPos> callback, String string) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import dev.ftb.mods.ftblibrary.ui.BaseScreen;
import dev.ftb.mods.ftblibrary.ui.Button;
import dev.ftb.mods.ftblibrary.ui.ContextMenuItem;
import dev.ftb.mods.ftblibrary.ui.DropDownMenu;
import dev.ftb.mods.ftblibrary.ui.NordTheme;
import dev.ftb.mods.ftblibrary.ui.Panel;
import dev.ftb.mods.ftblibrary.ui.ScreenWrapper;
Expand All @@ -36,13 +37,15 @@
import dev.ftb.mods.ftblibrary.ui.input.MouseButton;
import dev.ftb.mods.ftblibrary.ui.misc.KeyReferenceScreen;
import dev.ftb.mods.ftblibrary.util.StringUtils;
import dev.ftb.mods.ftblibrary.util.TextComponentUtils;
import dev.ftb.mods.ftblibrary.util.TooltipList;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.Mth;
Expand Down Expand Up @@ -169,9 +172,18 @@ public void addWidgets() {
}));
*/

Component dimName = Component.literal(dimension.dimension.location().getPath().replace('_', ' '));
Component dimName = TextComponentUtils.translatedDimension(dimension.dimension);
List<ContextMenuItem> dimItems = AddWaypointOverlay.createDimContextItems(key -> {
dimension = dimension.getManager().getDimension(key);
refreshWidgets();
movedToPlayer = false;
});

add(dimensionButton = new SimpleButton(this, dimName, Icons.GLOBE,
(b, m) -> cycleVisibleDimension(m)));
(b, m) -> {
DropDownMenu dropDownMenu = getGui().openDropdownMenu(dimItems);
dropDownMenu.setPos(dimensionButton.getX() + dimensionButton.width, dimensionButton.getY() + dimensionButton.height - dropDownMenu.height);
}));

add(settingsButton = new SimpleTooltipButton(this, Component.translatable("ftbchunks.gui.settings"), Icons.SETTINGS,
(b, m) -> FTBChunksClientConfig.openSettings(new ScreenWrapper(this)),
Expand Down Expand Up @@ -240,18 +252,22 @@ public boolean mousePressed(MouseButton button) {
return true;
} else if (button.isRight()) {
final BlockPos pos = new BlockPos(regionPanel.blockX, regionPanel.blockY, regionPanel.blockZ);
GlobalPos globalPos = GlobalPos.of(dimension.dimension, pos);
List<ContextMenuItem> list = new ArrayList<>();
list.add(new ContextMenuItem(Component.translatable("ftbchunks.gui.add_waypoint"), Icons.ADD, btn -> {
StringConfig name = new StringConfig();
name.setValue("");
ColorConfig col = new ColorConfig();
col.setValue(Color4I.hsb(MathUtils.RAND.nextFloat(), 1F, 1F));
var overlay = new AddWaypointOverlay(getGui(), name, col, accepted -> {
AddWaypointOverlay.GlobalPosConfig globalPosConfig = new AddWaypointOverlay.GlobalPosConfig();
globalPosConfig.setValue(globalPos);
var overlay = new AddWaypointOverlay(getGui(), globalPosConfig, name, col, accepted -> {
if (accepted) {
WaypointImpl waypoint = new WaypointImpl(WaypointType.DEFAULT, dimension, pos)
MapDimension mapDimension = MapManager.getInstance().orElseThrow().getDimension(globalPosConfig.getValue().dimension());
WaypointImpl waypoint = new WaypointImpl(WaypointType.DEFAULT, mapDimension, globalPosConfig.getValue().pos())
.setName(name.getValue())
.setColor(col.getValue().rgba());
dimension.getWaypointManager().add(waypoint);
mapDimension.getWaypointManager().add(waypoint);
refreshWidgets();
}
}).atMousePosition();
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/resources/assets/ftbchunks/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
"ftbchunks.gui.unclaim_all.description": "Unclaim all chunks visible on the map?",
"ftbchunks.gui.entity_icon_settings": "Entity Icon Settings",
"ftbchunks.gui.enabled_disabled_count": "Enabled: %d, Disabled: %d",
"ftbchunks.gui.label.dimension": "Dimension",
"ftbchunks.gui.label.name": "Name",
"ftbchunks.gui.waypoint.no_name": "Please enter a name",
"ftbteamsconfig.ftbchunks": "FTB Chunks Properties",
"ftbteamsconfig.ftbchunks.allow_fake_players": "Allow All Fake Players",
"ftbteamsconfig.ftbchunks.allow_fake_players.tooltip": "Treat ALL fake players as allies of the team.\nWARNING: Setting this to true could allow hostile players to access your claims via any fake player. Set this to false if you're unsure.",
Expand Down

0 comments on commit ece1d5f

Please sign in to comment.