-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
233 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/GridWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.