Skip to content

Commit

Permalink
List sprites in demo screen
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jun 8, 2024
1 parent e0e481f commit d2fff66
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import net.smyler.smylib.gui.containers.WidgetContainer;
import fr.thesmyler.smylibgui.screen.HudScreen;
import net.smyler.smylib.gui.widgets.WarningWidget;
import net.smyler.smylib.gui.sprites.WarningWidget;
import net.smyler.smylib.gui.widgets.SpriteWidget;
import fr.thesmyler.terramap.MapContext;
import fr.thesmyler.terramap.TerramapClientContext;
import fr.thesmyler.terramap.TerramapConfig;
Expand All @@ -21,7 +22,7 @@ public abstract class HudScreenHandler {

private static MinimapWidget map;
private static RibbonCompassWidget compass;
private final static WarningWidget offsetWarning = new WarningWidget(0, 0, 50);
private final static SpriteWidget offsetWarning = new WarningWidget(0, 0, 50);

public static void init(WidgetContainer screen) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.smyler.smylib.game.Key;
import net.smyler.smylib.gui.DrawContext;
import net.smyler.smylib.gui.Scissor;
import net.smyler.smylib.gui.sprites.WarningWidget;
import net.smyler.smylib.text.ImmutableText;
import net.smyler.smylib.text.TextStyle;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -38,7 +39,7 @@
import net.smyler.smylib.gui.widgets.Widget;
import net.smyler.smylib.gui.widgets.ScrollbarWidget;
import net.smyler.smylib.gui.widgets.ScrollbarWidget.ScrollbarOrientation;
import net.smyler.smylib.gui.widgets.WarningWidget;
import net.smyler.smylib.gui.widgets.SpriteWidget;
import net.smyler.smylib.gui.widgets.buttons.AbstractButtonWidget;
import net.smyler.smylib.gui.widgets.buttons.TextButtonWidget;
import net.smyler.smylib.gui.widgets.buttons.SpriteButtonWidget;
Expand Down Expand Up @@ -92,7 +93,7 @@ public class TerramapScreen extends Screen implements ITabCompleter {
private final SpriteButtonWidget centerButton = new SpriteButtonWidget(50, ButtonSprites.CENTER);
private final SpriteButtonWidget styleButton = new SpriteButtonWidget(50, ButtonSprites.PAPER);
private final CircularCompassWidget compass = new CircularCompassWidget(100, 100, 50, 100);
private final WarningWidget offsetWarning = new WarningWidget(0, 0, 50);
private final SpriteWidget offsetWarning = new WarningWidget(0, 0, 50);

// Info panel widgets
private TextWidget zoomText;
Expand Down
5 changes: 5 additions & 0 deletions smylib/core/src/main/java/net/smyler/smylib/Identifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public static boolean isValidPath(String path) {
return true;
}

@Override
public String toString() {
return this.namespace + ":" + this.path;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import net.smyler.smylib.Identifier;
import net.smyler.smylib.gui.Font;
import net.smyler.smylib.gui.containers.FlexibleWidgetContainer;
import net.smyler.smylib.gui.containers.ScrollableWidgetContainer;
import net.smyler.smylib.gui.containers.WidgetContainer;
import net.smyler.smylib.Animation;
import net.smyler.smylib.Animation.AnimationState;
import net.smyler.smylib.Color;
import net.smyler.smylib.gui.sprites.Sprite;
import net.smyler.smylib.gui.widgets.MenuWidget;
import net.smyler.smylib.gui.widgets.SpriteWidget;
import net.smyler.smylib.gui.widgets.buttons.OptionButtonWidget;
import net.smyler.smylib.gui.widgets.buttons.TextButtonWidget;
import net.smyler.smylib.gui.widgets.buttons.SpriteButtonWidget;
Expand All @@ -24,6 +29,13 @@
import net.smyler.smylib.text.ImmutableText;
import net.smyler.smylib.text.Text;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.lang.Math.max;
import static java.lang.Math.round;
import static java.util.Comparator.comparing;
import static net.smyler.smylib.Color.RED;
import static net.smyler.smylib.Color.WHITE;
import static net.smyler.smylib.SmyLib.getGameClient;
Expand All @@ -46,7 +58,7 @@ public class TestScreen extends Screen {
private final SpriteButtonWidget previous;
private final SpriteButtonWidget next;

private final Gson textJsonParser = new GsonBuilder().registerTypeAdapter(Text.class, new TextJsonAdapter()).create();;
private final Gson textJsonParser = new GsonBuilder().registerTypeAdapter(Text.class, new TextJsonAdapter()).create();

public TestScreen(Screen parent) {
super(BackgroundOption.DEFAULT);
Expand Down Expand Up @@ -80,7 +92,9 @@ public void init() {
WidgetContainer sliderScreen = new FlexibleWidgetContainer(20, 50, 1, width - 40, height - 70);
WidgetContainer menuScreen = new FlexibleWidgetContainer(20, 50, 1, width - 40, height - 70);
WidgetContainer jsonTextScreen = new FlexibleWidgetContainer(20, 50, 1, width - 40, height - 70);
this.subScreens = new WidgetContainer[] { textScreen, buttonScreen, sliderScreen, menuScreen, jsonTextScreen};
FlexibleWidgetContainer spriteScreenContent = new FlexibleWidgetContainer(20, 50, 1, width - 40, 10f);
WidgetContainer spritesScreen = new ScrollableWidgetContainer(20, 50, 1, width - 40, height - 70, spriteScreenContent);
this.subScreens = new WidgetContainer[] { textScreen, buttonScreen, sliderScreen, menuScreen, jsonTextScreen, spritesScreen};
for(WidgetContainer container: this.subScreens) container.setDoScissor(false);

TextWidget title = new TextWidget(width / 2f, 20, 10, ofPlainText("SmyLibGui demo test screen"), TextAlignment.CENTER, getGameClient().defaultFont());
Expand Down Expand Up @@ -201,6 +215,34 @@ TextAlignment.CENTER, getGameClient().defaultFont()
}
});

// ==== Sprites ==== /
float y = 0f;
Font font = getGameClient().defaultFont();
List<Map.Entry<Identifier, Sprite>> sprites = getGameClient().sprites().getSprites()
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey(comparing(Identifier::toString)))
.collect(Collectors.toList());
for (Map.Entry<Identifier, Sprite> entry: sprites) {
Text id = ImmutableText.ofPlainText(entry.getKey().toString());
Sprite sprite = entry.getValue();
float entryHeight = round(max(font.height(), sprite.height())) + 10f;
spriteScreenContent.addWidget(new TextWidget(
0, y + (entryHeight - font.height()) / 2, 0,
id,
font
));
SpriteWidget spriteWidget = new SpriteWidget(
font.computeWidth(id) + 10f, (float) (y + (entryHeight - sprite.height()) / 2), 0,
sprite
);
spriteScreenContent.addWidget(spriteWidget);
spriteScreenContent.setWidth(max(spriteScreenContent.getWidth(), spriteWidget.getX() + spriteWidget.getWidth() + 10f));
y += entryHeight;
}
spriteScreenContent.setHeight(y + 10f);
spritesScreen.setDoScissor(true);


// ==== Getting everything ready and setting up scheduled tasks === //

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import java.util.Locale;
import java.util.Map;

import static java.util.Collections.unmodifiableMap;
import static java.util.Objects.requireNonNull;
import static net.smyler.smylib.Preconditions.checkArgument;
import static net.smyler.smylib.Preconditions.checkState;

public class SpriteLibrary {

private final Map<Identifier, Sprite> registered = new HashMap<>();
private final Map<Identifier, Sprite> readOnly = unmodifiableMap(this.registered);

public SpriteLibrary() {
for(SmyLibSprites sprite: SmyLibSprites.values()) {
Expand Down Expand Up @@ -50,4 +52,13 @@ public Sprite getSprite(@NotNull String identifier) {
return this.registered.get(Identifier.parse(identifier));
}

/**
* Allow access to all registered sprites.
*
* @return a read-only vue over the registered sprites
*/
public Map<Identifier, Sprite> getSprites() {
return this.readOnly;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.smyler.smylib.gui.sprites;

import net.smyler.smylib.Identifier;
import net.smyler.smylib.gui.widgets.SpriteWidget;

public class WarningWidget extends SpriteWidget {

private static final Identifier TEXTURE = new Identifier("terramap", "textures/gui/widgets.png");
private static final Sprite SPRITE = new Sprite(TEXTURE, 256d, 256d, 15d, 54d, 30d, 69d);

public WarningWidget(float x, float y, int z) {
super(x, y, z, SPRITE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.smyler.smylib.gui.widgets;

import net.smyler.smylib.gui.sprites.Sprite;
import net.smyler.smylib.gui.containers.WidgetContainer;
import net.smyler.smylib.gui.DrawContext;

public class SpriteWidget extends AbstractSolidWidget {


private final Sprite sprite;

public SpriteWidget(float x, float y, int z, Sprite sprite) {
super(x, y, z, (float)sprite.width(), (float)sprite.height());
this.sprite = sprite;
}

@Override
public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) {
context.drawSprite(x, y, this.sprite);
}

public SpriteWidget setPosition(float x, float y) {
this.x = x;
this.y = y;
return this;
}

}

This file was deleted.

0 comments on commit d2fff66

Please sign in to comment.