Skip to content

Commit 51ca0fd

Browse files
committed
feat: added claim modes in chunk claiming screen
freehand (default)/rectangle/circle
1 parent a789a1f commit 51ca0fd

File tree

6 files changed

+98
-17
lines changed

6 files changed

+98
-17
lines changed

common/src/main/java/dev/ftb/mods/ftbchunks/client/gui/map/ChunkScreen.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package dev.ftb.mods.ftbchunks.client.gui.map;
22

33
import dev.ftb.mods.ftbchunks.FTBChunks;
4+
import dev.ftb.mods.ftbchunks.api.FTBChunksAPI;
45
import dev.ftb.mods.ftbchunks.client.FTBChunksClient;
56
import dev.ftb.mods.ftbchunks.client.map.MapDimension;
67
import dev.ftb.mods.ftbchunks.net.SendGeneralDataPacket;
8+
import dev.ftb.mods.ftblibrary.client.gui.input.MouseButton;
79
import dev.ftb.mods.ftblibrary.client.gui.screens.AbstractThreePanelScreen;
810
import dev.ftb.mods.ftblibrary.client.gui.screens.KeyReferenceScreen;
911
import dev.ftb.mods.ftblibrary.client.gui.theme.Theme;
@@ -14,6 +16,7 @@
1416
import dev.ftb.mods.ftblibrary.client.icon.IconHelper;
1517
import dev.ftb.mods.ftblibrary.client.util.ClientUtils;
1618
import dev.ftb.mods.ftblibrary.icon.Color4I;
19+
import dev.ftb.mods.ftblibrary.icon.Icon;
1720
import dev.ftb.mods.ftblibrary.icon.Icons;
1821
import dev.ftb.mods.ftblibrary.util.TooltipList;
1922
import dev.ftb.mods.ftbteams.api.Team;
@@ -31,6 +34,8 @@ public class ChunkScreen extends AbstractThreePanelScreen<ChunkScreenPanel> {
3134
private final Team openedAs;
3235
private final SimpleButton largeMapButton;
3336

37+
static ClaimMode claimMode = ClaimMode.FREEHAND; // persist across invocations
38+
3439
private ChunkScreen(MapDimension dimension, @Nullable Team openedAs) {
3540
this.dimension = dimension;
3641
this.openedAs = openedAs;
@@ -134,6 +139,7 @@ protected class CustomTopPanel extends Panel {
134139
private final Button removeAllClaims;
135140
private final Button adminButton;
136141
private final Button mouseReferenceButton;
142+
private final Button claimModeButton;
137143

138144
public CustomTopPanel() {
139145
super(ChunkScreen.this);
@@ -157,6 +163,8 @@ public CustomTopPanel() {
157163
.setForceButtonSize(false);
158164

159165
adminButton = new AdminButton().setForceButtonSize(false);
166+
167+
claimModeButton = new ClaimModeButton();
160168
}
161169

162170
@Override
@@ -167,6 +175,7 @@ public void addWidgets() {
167175
add(adminButton);
168176
}
169177
add(mouseReferenceButton);
178+
add(claimModeButton);
170179
}
171180

172181
@Override
@@ -176,7 +185,7 @@ public void alignWidgets() {
176185

177186
closeButton.setPosAndSize(width - 16, 2, 12, 12);
178187
mouseReferenceButton.setPosAndSize(width - 32, 2, 12, 12);
179-
188+
claimModeButton.setPosAndSize(width - 48, 2, 12, 12);
180189
}
181190

182191
@Override
@@ -203,6 +212,16 @@ public void addMouseOverText(TooltipList list) {
203212
list.add(MORE_INFO);
204213
}
205214
}
215+
216+
private class ClaimModeButton extends SimpleButton {
217+
public ClaimModeButton() {
218+
super(CustomTopPanel.this, claimMode.description(), claimMode.icon(), (widget, button) -> {
219+
claimMode = claimMode.next();
220+
widget.setIcon(claimMode.icon());
221+
widget.setTitle(claimMode.description());
222+
});
223+
}
224+
}
206225
}
207226

208227
private static class ChunkMouseReferenceScreen extends KeyReferenceScreen {
@@ -217,7 +236,6 @@ public Component getTitle() {
217236
}
218237

219238
private class CustomBottomPanel extends Panel {
220-
221239
public CustomBottomPanel() {
222240
super(ChunkScreen.this);
223241
}
@@ -263,4 +281,30 @@ public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int
263281
}
264282
}
265283
}
284+
285+
enum ClaimMode {
286+
FREEHAND("freehand", Icon.getIcon(FTBChunksAPI.id("textures/freehand.png"))),
287+
RECTANGLE("rectangle", Icon.getIcon(FTBChunksAPI.id("textures/rectangle.png"))),
288+
CIRCLE("circle", Icon.getIcon(FTBChunksAPI.id("textures/circle.png")));
289+
290+
private final String key;
291+
private final Icon<?> icon;
292+
293+
ClaimMode(String key, Icon<?> icon) {
294+
this.key = key;
295+
this.icon = icon;
296+
}
297+
298+
Component description() {
299+
return Component.translatable("ftbchunks.claim_mode." + key);
300+
}
301+
302+
public Icon<?> icon() {
303+
return icon;
304+
}
305+
306+
public ClaimMode next() {
307+
return ClaimMode.values()[(ordinal() + 1) % ClaimMode.values().length];
308+
}
309+
}
266310
}

common/src/main/java/dev/ftb/mods/ftbchunks/client/gui/map/ChunkScreenPanel.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import net.minecraft.network.chat.Component;
4040
import net.minecraft.network.chat.MutableComponent;
4141
import net.minecraft.server.permissions.Permissions;
42+
import net.minecraft.util.Mth;
4243
import net.minecraft.world.entity.player.Player;
4344
import net.minecraft.world.level.ChunkPos;
4445
import org.lwjgl.glfw.GLFW;
@@ -54,6 +55,7 @@ public class ChunkScreenPanel extends Panel {
5455
private static final ImageIcon CHECKERED_ICON = new ImageIcon(FTBChunksAPI.id("textures/checkered.png"));
5556

5657
private final List<ChunkButton> chunkButtons = new ArrayList<>();
58+
private XZ firstSelectedChunk = null;
5759
private final Set<XZ> selectedChunks = new HashSet<>();
5860
private final List<ChunkButtonPos> chunkedPosList = new ArrayList<>();
5961
public boolean isAdminEnabled;
@@ -124,11 +126,11 @@ public void alignWidgets() {
124126

125127
this.tileSizeX = maxWidth / FTBChunks.TILES;
126128
this.tileSizeY = maxHeight / FTBChunks.TILES;
127-
for (ChunkButtonPos chunkedPos : chunkedPosList) {
128-
chunkedPos.button.setPos(xPos + tileSizeX * chunkedPos.x, yPos + tileSizeY * chunkedPos.y);
129-
chunkedPos.button.setSize(tileSizeX, tileSizeY);
130-
}
131-
}
129+
for (ChunkButtonPos chunkedPos : chunkedPosList) {
130+
chunkedPos.button.setPos(xPos + tileSizeX * chunkedPos.x, yPos + tileSizeY * chunkedPos.y);
131+
chunkedPos.button.setSize(tileSizeX, tileSizeY);
132+
}
133+
}
132134

133135
@Override
134136
public void mouseReleased(MouseButton button) {
@@ -138,16 +140,17 @@ public void mouseReleased(MouseButton button) {
138140
Optional<UUID> teamId = Optional.ofNullable(chunkScreen.getOpenedAs()).map(Team::getTeamId);
139141
NetworkManager.sendToServer(new RequestChunkChangePacket(ChunkChangeOp.create(button.isLeft(), isShiftKeyDown()), selectedChunks, canChangeAsAdmin(), teamId));
140142
selectedChunks.clear();
143+
firstSelectedChunk = null;
141144
playClickSound();
142145
}
143146
}
144147

145148
public void removeAllClaims() {
146149
Optional<UUID> teamId = Optional.ofNullable(chunkScreen.getOpenedAs()).map(Team::getTeamId);
147150
Set<XZ> allChunks = chunkedPosList.stream()
148-
.map(ChunkButtonPos::button)
149-
.map(ChunkButton::getChunkPos)
150-
.collect(Collectors.toSet());
151+
.map(ChunkButtonPos::button)
152+
.map(ChunkButton::getChunkPos)
153+
.collect(Collectors.toSet());
151154
NetworkManager.sendToServer(new RequestChunkChangePacket(ChunkChangeOp.UNCLAIM, allChunks, canChangeAsAdmin(), teamId));
152155
}
153156

@@ -223,6 +226,10 @@ public ChunkButton(Panel panel, XZ xz) {
223226

224227
@Override
225228
public void onClicked(MouseButton mouseButton) {
229+
if (selectedChunks.isEmpty()) {
230+
firstSelectedChunk = chunkPos;
231+
}
232+
226233
selectedChunks.add(chunkPos);
227234
}
228235

@@ -244,12 +251,39 @@ public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int
244251

245252
@Override
246253
public boolean mouseDragged(int button, double dragX, double dragY) {
247-
if (isMouseOver() && (isMouseButtonDown(MouseButton.LEFT) || isMouseButtonDown(MouseButton.RIGHT))) {
248-
selectedChunks.add(chunkPos);
249-
}
254+
if (isMouseOver() && (isMouseButtonDown(MouseButton.LEFT) || isMouseButtonDown(MouseButton.RIGHT))) {
255+
if (ChunkScreen.claimMode != ChunkScreen.ClaimMode.FREEHAND && firstSelectedChunk != null) {
256+
addChunksToSelection();
257+
} else {
258+
selectedChunks.add(chunkPos);
259+
}
260+
}
250261
return super.mouseDragged(button, dragX, dragY);
251262
}
252263

264+
private void addChunksToSelection() {
265+
int x1 = Math.min(firstSelectedChunk.x(), chunkPos.x());
266+
int x2 = Math.max(firstSelectedChunk.x(), chunkPos.x());
267+
int z1 = Math.min(firstSelectedChunk.z(), chunkPos.z());
268+
int z2 = Math.max(firstSelectedChunk.z(), chunkPos.z());
269+
XZ centre = new XZ((x1 + x2) / 2, (z1 + z2) / 2);
270+
selectedChunks.clear();
271+
for (int x = x1; x <= x2; x++) {
272+
for (int z = z1; z <= z2; z++) {
273+
XZ xz = new XZ(x, z);
274+
if (ChunkScreen.claimMode == ChunkScreen.ClaimMode.RECTANGLE
275+
|| distance(xz, centre) - 0.5f <= Math.min(centre.x() - x1, centre.z() - z1))
276+
{
277+
selectedChunks.add(xz);
278+
}
279+
}
280+
}
281+
}
282+
283+
private float distance(XZ xz1, XZ xz2) {
284+
return Mth.sqrt((xz2.x() - xz1.x()) * (xz2.x() - xz1.x()) + (xz2.z() - xz1.z()) * (xz2.z() - xz1.z()));
285+
}
286+
253287
@Override
254288
public void addMouseOverText(TooltipList list) {
255289
chunk.getTeam().ifPresent(team -> {
@@ -287,9 +321,9 @@ public void addMouseOverText(TooltipList list) {
287321
}
288322
});
289323

290-
if (!altPressed && (chunk.getClaimedDate().isPresent() || chunk.getForceLoadedDate().isPresent())) {
291-
list.add(Component.translatable("ftbchunks.gui.hold_alt_for_dates").withStyle(ChatFormatting.DARK_GRAY));
292-
}
324+
if (!altPressed && (chunk.getClaimedDate().isPresent() || chunk.getForceLoadedDate().isPresent())) {
325+
list.add(Component.translatable("ftbchunks.gui.hold_alt_for_dates").withStyle(ChatFormatting.DARK_GRAY));
326+
}
293327
});
294328
}
295329

common/src/main/resources/assets/ftbchunks/lang/en_us.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,5 +335,8 @@
335335
"ftbchunks.command.bypass_protection_enabled": "Bypass Protection: Enabled",
336336
"ftbchunks.command.bypass_protection_disabled": "Bypass Protection: Disabled",
337337
"ftbchunks.command.view_loaded": "Chunks Loaded: %d. Check the map to see loaded chunks",
338-
"ftbchunks.command.waypoint_added": "Added waypoint '%s'"
338+
"ftbchunks.command.waypoint_added": "Added waypoint '%s'",
339+
"ftbchunks.claim_mode.freehand": "Freehand Claiming",
340+
"ftbchunks.claim_mode.rectangle": "Rectangular Claiming",
341+
"ftbchunks.claim_mode.circle": "Circular Claiming"
339342
}
5.91 KB
Loading
6.06 KB
Loading
5.83 KB
Loading

0 commit comments

Comments
 (0)