Skip to content

Commit

Permalink
More improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kiip1 committed Feb 25, 2023
1 parent 402de76 commit 8c6e3e8
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minestom.server.map.MapColors;
import nl.kiipdevelopment.minescreen.screen.ScreenGui;
import org.jetbrains.annotations.ApiStatus;

public interface MapGraphics {
static MapGraphics of(ScreenGui gui) {
Expand Down Expand Up @@ -48,5 +49,8 @@ default void drawDot(MapColors color, int x, int y) {

void drawDot(byte color, int x, int y);

@ApiStatus.Internal
void drawDirectDot(byte color, int x, int y);

MapGraphics relative(int x, int y, int width, int height);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ public MapGraphicsImpl(ScreenGui gui) {

@Override
public void fill(byte color) {
for (SubMap subView : gui.map().subs()) {
for (SubMap subView : gui.map().subs())
Arrays.fill(subView.colors(), color);
}
}

@Override
Expand All @@ -38,8 +37,6 @@ public void drawRectangle(byte color, int startX, int startY, int endX, int endY
public void drawString(byte color, String value, int x, int y) {
ensureWithinBounds(x, y);

// TODO: Implement

throw new UnsupportedOperationException("MapGraphicsImpl#drawString isn't implemented yet.");
}

Expand All @@ -50,7 +47,8 @@ public void drawDot(byte color, int x, int y) {
drawDirectDot(color, x, y);
}

protected void drawDirectDot(byte color, int x, int y) {
@Override
public void drawDirectDot(byte color, int x, int y) {
Map.SubMapAndIndex subMapAndIndex = map.sub(x, y);

subMapAndIndex.map().colors()[subMapAndIndex.index()] = color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import nl.kiipdevelopment.minescreen.screen.ScreenGui;

final class RelativeMapGraphicsImpl extends MapGraphicsImpl {
private final int x, y;
private final int width, height;
private final int x;
private final int y;

private final int width;
private final int height;

public RelativeMapGraphicsImpl(ScreenGui gui, int x, int y, int width, int height) {
super(gui);
Expand All @@ -21,46 +24,26 @@ public void fill(byte color) {
}

@Override
public void drawRectangle(byte color, int startX, int startY, int endX, int endY) {
startX = fixX(startX);
startY = fixY(startY);
endX = fixX(endX);
endY = fixY(endY);

super.drawRectangle(color, startX, startY, endX, endY);
}

@Override
public void drawString(byte color, String value, int x, int y) {
x = fixX(x);
y = fixY(y);

super.drawString(color, value, x, y);
}

@Override
public void drawDot(byte color, int x, int y) {
x = fixX(x);
y = fixY(y);

super.drawDot(color, x, y);
public void drawDirectDot(byte color, int x, int y) {
super.drawDirectDot(color, absX(x), absY(y));
}

@Override
public MapGraphics relative(int x, int y, int width, int height) {
x = fixX(x);
y = fixY(y);
width = fixX(width);
height = fixY(height);

return super.relative(x, y, width, height);
return new RelativeMapGraphicsImpl(
gui,
absX(x) - (absX(x) + width > this.x + this.width ? width : 0),
absY(y) - (absY(y) + height > this.y + this.height ? height : 0),
Math.min(width, this.width),
Math.min(height, this.height)
);
}

private int fixX(int x) {
return x + this.x;
private int absX(int x) {
return this.x + Math.min(x, width);
}

private int fixY(int y) {
return y + this.y;
private int absY(int y) {
return this.y + Math.min(y, height);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.SendablePacket;
import net.minestom.server.network.packet.server.play.MapDataPacket;
import net.minestom.server.utils.PacketUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -74,9 +73,9 @@ public byte[] colors() {

@Override
public void sendPacket(Collection<Player> players) {
final SendablePacket packet = PacketUtils.allocateTrimmedPacket(new MapDataPacket(
final SendablePacket packet = new MapDataPacket(
id(), (byte) 1, true, false, List.of(),
new MapDataPacket.ColorContent((byte) width, (byte) height, (byte) 0, (byte) 0, colors)));
new MapDataPacket.ColorContent((byte) width, (byte) height, (byte) 0, (byte) 0, colors));
for (Player player : players)
player.sendPacket(packet);
}
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/nl/kiipdevelopment/minescreen/widget/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import nl.kiipdevelopment.minescreen.graphics.MapGraphics;
import nl.kiipdevelopment.minescreen.widget.widgets.ButtonWidget;
import nl.kiipdevelopment.minescreen.widget.widgets.ContainerWidget;
import nl.kiipdevelopment.minescreen.widget.widgets.GridWidget;
import nl.kiipdevelopment.minescreen.widget.widgets.ImageWidget;
import nl.kiipdevelopment.minescreen.widget.widgets.PlayerWidget;
import nl.kiipdevelopment.minescreen.widget.widgets.RenderWidget;
Expand Down Expand Up @@ -65,6 +66,12 @@ public interface Click {
final class Containers {
private Containers() {}

public static Widget grid(int spacing, int width, int height, @Nullable Widget... widgets) {
return new GridWidget(spacing, width, height, Arrays.stream(widgets)
.filter(Objects::nonNull)
.toList());
}

public static Widget row(int width, int height, @Nullable Widget... widgets) {
return new ContainerWidget(ContainerWidget.Type.ROW, width, height, Arrays.stream(widgets)
.filter(Objects::nonNull)
Expand Down Expand Up @@ -131,12 +138,12 @@ public static Widget render(int width, int height, @Nullable Consumer<MapGraphic
final class Shapes {
private Shapes() {}

public static Widget rectangle(int width, int height, int xOffset, int yOffset, MapColors color) {
return new ShapeWidget(ShapeWidget.Type.RECTANGLE, width, height, xOffset, yOffset, color);
public static Widget rectangle(int width, int height, MapColors color) {
return new ShapeWidget(ShapeWidget.Type.RECTANGLE, width, height, color);
}

public static Widget square(int width, int height, int xOffset, int yOffset, MapColors color) {
return new ShapeWidget(ShapeWidget.Type.SQUARE, width, height, xOffset, yOffset, color);
public static Widget square(int length, MapColors color) {
return new ShapeWidget(ShapeWidget.Type.SQUARE, length, length, color);
}
}

Expand All @@ -147,7 +154,11 @@ final class Spacers {
private Spacers() {}

public static Widget spacer(int width, int height) {
return new SpacerWidget(width, height);
return new SpacerWidget(width, height, null);
}

public static Widget offset(int width, int height, @NotNull Widget widget) {
return new SpacerWidget(width, height, widget);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void draw(MapGraphics renderer) {
}
case STACK -> {
for (Widget widget : widgets) {
widget.draw(renderer.relative(0, 0, widget.width(), widget.height()));
widget.draw(renderer);
}
}
}
Expand Down Expand Up @@ -84,11 +84,8 @@ public void onHover(Player player, int x, int y) {
}
case STACK -> {
for (Widget widget : widgets) {
if (widget instanceof Interactable interactable) {
if (x <= widget.width() && y <= widget.height()) {
interactable.onHover(player, x, y);
}
}
if (widget instanceof Interactable interactable && x <= widget.width() && y <= widget.height())
interactable.onHover(player, x, y);
}
}
}
Expand Down Expand Up @@ -129,11 +126,8 @@ public void onClick(Player player, int x, int y) {
}
case STACK -> {
for (Widget widget : widgets) {
if (widget instanceof Interactable interactable) {
if (x <= widget.width() && y <= widget.height()) {
interactable.onClick(player, x, y);
}
}
if (widget instanceof Interactable interactable && x <= widget.width() && y <= widget.height())
interactable.onClick(player, x, y);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package nl.kiipdevelopment.minescreen.widget.widgets;

import net.minestom.server.entity.Player;
import nl.kiipdevelopment.minescreen.graphics.MapGraphics;
import nl.kiipdevelopment.minescreen.util.MathUtils;
import nl.kiipdevelopment.minescreen.widget.AbstractWidget;
import nl.kiipdevelopment.minescreen.widget.Interactable;
import nl.kiipdevelopment.minescreen.widget.Widget;
import org.jetbrains.annotations.ApiStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiStatus.Internal
public final class GridWidget extends AbstractWidget implements Interactable {

private final Map<Area, Widget> widgets = new HashMap<>();

public GridWidget(int spacing, int width, int height, List<Widget> widgets) {
super(width, height);

int xOffset = 0;
int yOffset = 0;
int largestY = 0;
for (Widget widget : widgets) {
if (widget.height() > largestY)
largestY = widget.height();
if (xOffset + widget.width() > width()) {
yOffset += largestY + spacing;
largestY = 0;
xOffset = 0;
}

this.widgets.put(new Area(xOffset, yOffset, widget.width(), widget.height()), widget);
xOffset += widget.width() + spacing;
}
}

@Override
public void draw(MapGraphics renderer) {
for (Map.Entry<Area, Widget> entry : widgets.entrySet()) {
final Area area = entry.getKey();
entry.getValue().draw(renderer.relative(area.x(), area.y(), area.width(), area.height()));
}
}

@Override
public void onHover(Player player, int x, int y) {
for (Map.Entry<Area, Widget> entry : widgets.entrySet()) {
final Area area = entry.getKey();
if (entry.getValue() instanceof Interactable interactable && area.intersect(x, y))
interactable.onHover(player, x, y);
}
}

@Override
public void onClick(Player player, int x, int y) {
for (Map.Entry<Area, Widget> entry : widgets.entrySet()) {
final Area area = entry.getKey();
if (entry.getValue() instanceof Interactable interactable && area.intersect(x, y))
interactable.onClick(player, x, y);
}
}

private record Area(int x, int y, int width, int height) {
public boolean intersect(int x, int y) {
return MathUtils.isBetween(x, this.x, this.x + width) &&
MathUtils.isBetween(y, this.y, this.y + height);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public ImageWidget(int width, int height, BufferedImage image) {

width = Math.min(image.getWidth(), width());
height = Math.min(image.getHeight(), height());

colors = ByteBuffer.allocate(width * height);

for (int x = 0; x < width; x++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public PlayerWidget(Type type, int width, int height, UUID player) {

@Override
public void draw(MapGraphics renderer) {
image.thenAccept(widget -> widget.draw(renderer));
Widget widget = this.image.getNow(null);
if (widget != null) widget.draw(renderer);
}

public enum Type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public RenderWidget(int width, int height, Consumer<MapGraphics> draw) {

@Override
public void draw(MapGraphics renderer) {
if (draw != null) {
if (draw != null)
draw.accept(renderer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@

@ApiStatus.Internal
public final class ShapeWidget extends AbstractWidget {
private final int xOffset, yOffset;
private final Type type;
private final MapColors color;

public ShapeWidget(Type type, int width, int height, int xOffset, int yOffset, MapColors color) {
public ShapeWidget(Type type, int width, int height, MapColors color) {
super(width, height);

this.xOffset = xOffset;
this.yOffset = yOffset;
this.type = type;
this.color = color;
}

@Override
public void draw(MapGraphics renderer) {
switch (type) {
case SQUARE, RECTANGLE -> renderer.drawRectangle(color, xOffset, yOffset, width(), type == Type.SQUARE ? width() : height());
case SQUARE, RECTANGLE -> renderer.drawRectangle(color, 0, 0, width(), (type == Type.SQUARE ? width() : height()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@

import nl.kiipdevelopment.minescreen.widget.AbstractWidget;
import nl.kiipdevelopment.minescreen.graphics.MapGraphics;
import nl.kiipdevelopment.minescreen.widget.Widget;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
public final class SpacerWidget extends AbstractWidget {
public SpacerWidget(int width, int height) {
super(width, height);
private final int x;
private final int y;
private final Widget widget;

public SpacerWidget(int width, int height, @Nullable Widget widget) {
super(width + (widget == null ? 0 : widget.width()), height + (widget == null ? 0 : widget.height()));
this.x = width;
this.y = height;
this.widget = widget;
}

@Override
public void draw(MapGraphics renderer) {}
public void draw(MapGraphics renderer) {
if (widget != null)
widget.draw(renderer.relative(x, y, widget.width(), widget.height()));
}
}
Loading

0 comments on commit 8c6e3e8

Please sign in to comment.