Skip to content

Commit

Permalink
Hud screen dev. progress
Browse files Browse the repository at this point in the history
Can now add and remove HUD widgets. However, they don't automatically update yet, and there isn't a "grid snapping" feature when dragging the widgets around. Still a lot of work left to do.
  • Loading branch information
TheCSDev committed Sep 27, 2023
1 parent 52360f4 commit ddd942e
Show file tree
Hide file tree
Showing 16 changed files with 370 additions and 10 deletions.
4 changes: 2 additions & 2 deletions betterstats-3-fabric-1.20.1/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ org.gradle.jvmargs=-Xmx1G
mod_name = Better Statistics Screen
mod_description = Improves the statistics screen and makes it more useful.
mod_author = TheCSDev
mod_version = 3.0.0-beta.1+fabric-1.20.1
mod_version = 3.0.0+fabric-1.20.1

mod_contact_homepage = https://github.com/TheCSMods
mod_contact_sources = https://github.com/TheCSMods/mc-better-stats
Expand All @@ -38,7 +38,7 @@ org.gradle.jvmargs=-Xmx1G
mod_depends_minecraft = >=1.20
mod_depends_java = >=17

mod_jar_tcdcommons = META-INF/jarjar/tcdcommons-3.0.0-beta.1+fabric-1.20.1.jar
mod_jar_tcdcommons = META-INF/jarjar/tcdcommons-3.0.0+fabric-1.20.1.jar

pack_mcmeta_format = 15

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.thecsdev.betterstats.api.client.gui.stats.widget;

import org.jetbrains.annotations.Nullable;

import io.github.thecsdev.betterstats.api.util.stats.SUGeneralStat;
import io.github.thecsdev.tcdcommons.api.client.gui.other.TBlankElement;
import io.github.thecsdev.tcdcommons.api.client.gui.util.TDrawContext;
import io.github.thecsdev.tcdcommons.api.util.annotations.Virtual;
import io.github.thecsdev.tcdcommons.api.util.enumerations.HorizontalAlignment;
import net.minecraft.text.Text;

/**
* A {@link TBlankElement} that only renders {@link Text}s on the
* left and right side of the {@link CustomStatElement}.<br/>
* Does not handle any user input or have a background color.
*/
public @Virtual class CustomStatElement extends TBlankElement
{
// ==================================================
public static final int HEIGHT = Math.max(GeneralStatWidget.HEIGHT, ItemStatWidget.SIZE);
// --------------------------------------------------
protected @Nullable Text txtLeft, txtRight;
// ==================================================
public CustomStatElement(int x, int y, int width, SUGeneralStat generalStat)
{
this(x, y, width, generalStat.getStatLabel(), generalStat.valueText);
}

public CustomStatElement(int x, int y, int width, @Nullable Text left, @Nullable Text right)
{
super(x, y, width, HEIGHT);
this.txtLeft = left;
this.txtRight = right;
}
// ==================================================
public final @Nullable Text getLeftText() { return this.txtLeft; }
public final @Nullable Text getRightText() { return this.txtLeft; }
public @Virtual void setLeftText(@Nullable Text left) { this.txtLeft = left; }
public @Virtual void setRightText(@Nullable Text right) { this.txtRight = right; }
// ==================================================
public @Virtual @Override void render(TDrawContext pencil)
{
pencil.drawTElementTextTH(this.txtLeft, HorizontalAlignment.LEFT);
pencil.drawTElementTextTH(this.txtRight, HorizontalAlignment.RIGHT);
}
// ==================================================
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ private BSRegistries() {}
ITEM_WIKIS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
MOB_WIKIS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

//the default Wiki for 'minecraft' was the Fandom wiki
/*final String mc = new Identifier("air").getNamespace();
ITEM_WIKIS.put(mc, id -> "https://minecraft.fandom.com/wiki/" + id.getPath());
MOB_WIKIS.put(mc, id -> "https://minecraft.fandom.com/wiki/" + id.getPath());*/
//the default Wiki for 'minecraft' is now 'minecraft.wiki'
final String mc = new Identifier("air").getNamespace();
ITEM_WIKIS.put(mc, id -> "https://minecraft.wiki/" + id.getPath());
MOB_WIKIS.put(mc, id -> "https://minecraft.wiki/" + id.getPath());
}
// ==================================================
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.thecsdev.betterstats.api.util.enumerations;

import static io.github.thecsdev.betterstats.api.client.gui.stats.widget.ItemStatWidget.TEXT_STAT_BROKEN;
import static io.github.thecsdev.betterstats.api.client.gui.stats.widget.ItemStatWidget.TEXT_STAT_CRAFTED;
import static io.github.thecsdev.betterstats.api.client.gui.stats.widget.ItemStatWidget.TEXT_STAT_DROPPED;
import static io.github.thecsdev.betterstats.api.client.gui.stats.widget.ItemStatWidget.TEXT_STAT_MINED;
import static io.github.thecsdev.betterstats.api.client.gui.stats.widget.ItemStatWidget.TEXT_STAT_PICKED_UP;
import static io.github.thecsdev.betterstats.api.client.gui.stats.widget.ItemStatWidget.TEXT_STAT_USED;

import java.util.Objects;
import java.util.function.Function;

import io.github.thecsdev.betterstats.api.util.stats.SUItemStat;
import io.github.thecsdev.tcdcommons.api.util.interfaces.ITextProvider;
import net.minecraft.text.Text;

public enum ItemStatType implements ITextProvider
{
// ==================================================
MINED(TEXT_STAT_MINED, s -> s.mined),
CRAFTED(TEXT_STAT_CRAFTED, s -> s.crafted),
PICKED_UP(TEXT_STAT_PICKED_UP, s -> s.pickedUp),
DROPPED(TEXT_STAT_DROPPED, s -> s.dropped),
USED(TEXT_STAT_USED, s -> s.used),
BROKEN(TEXT_STAT_BROKEN, s -> s.broken);
// ==================================================
private final Text text;
private final Function<SUItemStat, Integer> statValueSupplier;
// ==================================================
private ItemStatType(Text text, Function<SUItemStat, Integer> statValueSupplier)
{
this.text = Objects.requireNonNull(text);
this.statValueSupplier = Objects.requireNonNull(statValueSupplier);
}
// ==================================================
public final @Override Text getText() { return this.text; }
public int getStatValue(SUItemStat stat) throws NullPointerException
{
return this.statValueSupplier.apply(Objects.requireNonNull(stat));
}
// ==================================================
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.thecsdev.betterstats.api.util.enumerations;

import static io.github.thecsdev.betterstats.api.client.gui.stats.widget.MobStatWidget.TEXT_STAT_DEATHS;
import static io.github.thecsdev.betterstats.api.client.gui.stats.widget.MobStatWidget.TEXT_STAT_KILLS;

import java.util.Objects;
import java.util.function.Function;

import io.github.thecsdev.betterstats.api.util.stats.SUMobStat;
import io.github.thecsdev.tcdcommons.api.util.interfaces.ITextProvider;
import net.minecraft.text.Text;

public enum MobStatType implements ITextProvider
{
// ==================================================
KILLED(TEXT_STAT_KILLS, s -> s.kills),
KILLED_BY(TEXT_STAT_DEATHS, s -> s.deaths);
// ==================================================
private final Text text;
private final Function<SUMobStat, Integer> statValueSupplier;
// ==================================================
private MobStatType(Text text, Function<SUMobStat, Integer> statValueSupplier)
{
this.text = Objects.requireNonNull(text);
this.statValueSupplier = Objects.requireNonNull(statValueSupplier);
}
// ==================================================
public final @Override Text getText() { return this.text; }
public int getStatValue(SUMobStat stat) throws NullPointerException
{
return this.statValueSupplier.apply(Objects.requireNonNull(stat));
}
// ==================================================
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.thecsdev.betterstats.client.gui.screen.hud;

import static io.github.thecsdev.betterstats.BetterStats.getModID;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable;

import org.jetbrains.annotations.Nullable;

import io.github.thecsdev.betterstats.BetterStats;
import io.github.thecsdev.tcdcommons.api.client.gui.screen.TWidgetHudScreen;
import io.github.thecsdev.tcdcommons.api.client.util.interfaces.IParentScreenProvider;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

/**
* {@link BetterStats}'s {@link TWidgetHudScreen}.
*/
public final class BetterStatsHudScreen extends TWidgetHudScreen implements IParentScreenProvider
{
// ==================================================
public static final Text TEXT_TITLE = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen");
public static final Identifier HUD_SCREEN_ID = new Identifier(getModID(), "stats_hud");
// --------------------------------------------------
private static final BetterStatsHudScreen INSTANCE = new BetterStatsHudScreen();
// --------------------------------------------------
private @Nullable Screen parent;
// ==================================================
private BetterStatsHudScreen() { super(TEXT_TITLE, HUD_SCREEN_ID); }
// ==================================================
/**
* Returns the current instance of {@link BetterStatsHudScreen}.
*/
public static BetterStatsHudScreen getInstance() { return INSTANCE; }
// ==================================================
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.github.thecsdev.betterstats.client.gui.screen.hud.entry;

import static io.github.thecsdev.tcdcommons.api.util.TextUtils.literal;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable;

import java.util.Objects;

import org.jetbrains.annotations.Nullable;

import io.github.thecsdev.betterstats.api.client.gui.stats.widget.CustomStatElement;
import io.github.thecsdev.betterstats.api.client.gui.stats.widget.ItemStatWidget;
import io.github.thecsdev.betterstats.api.util.enumerations.ItemStatType;
import io.github.thecsdev.betterstats.api.util.stats.SUItemStat;
import io.github.thecsdev.tcdcommons.api.client.gui.TElement;
import io.github.thecsdev.tcdcommons.api.client.gui.panel.TPanelElement;
import io.github.thecsdev.tcdcommons.api.client.gui.screen.TWidgetHudScreen;
import io.github.thecsdev.tcdcommons.api.client.gui.util.TDrawContext;
import net.minecraft.text.Text;

public final class StatsHudItemEntry extends TWidgetHudScreen.WidgetEntry<TElement>
{
// ==================================================
static final int WIDTH = 150;
// --------------------------------------------------
protected final SUItemStat stat;
protected ItemStatType mode = ItemStatType.MINED;
// ==================================================
public StatsHudItemEntry(SUItemStat stat) throws NullPointerException
{
super(0.5, 0.25);
this.stat = Objects.requireNonNull(stat);
}
// ==================================================
public final @Override TElement createWidget()
{
final var el = new Element();
el.eContextMenu.register((__, cMenu) ->
{
for(final var ist : ItemStatType.values())
{
cMenu.addButton(ist.getText(), ___ ->
{
this.mode = ist;
refreshEntry();
});
}
cMenu.addSeparator();
cMenu.addButton(translatable("selectWorld.delete"), ___ -> removeEntry());
});
return el;
}
// --------------------------------------------------
private final CustomStatElement createCustomStatElement()
{
//collect info
final int i = ItemStatWidget.SIZE;
@Nullable Text left = this.mode.getText();
@Nullable Text right = literal(Integer.toString(this.mode.getStatValue(this.stat)));
//create and return
return new CustomStatElement(i, 0, WIDTH - i, left, right);
}
// ==================================================
private final class Element extends TElement
{
public Element()
{
super(0, 0, WIDTH, CustomStatElement.HEIGHT);
addChild(new ItemStatWidget(0, 0, stat), true);
addChild(createCustomStatElement(), true);
}
public @Override void render(TDrawContext pencil) { pencil.drawTFill(TPanelElement.COLOR_BACKGROUND); }
}
// ==================================================
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.github.thecsdev.betterstats.client.gui.screen.hud.entry;

import static io.github.thecsdev.tcdcommons.api.util.TextUtils.literal;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable;

import java.util.Objects;

import org.jetbrains.annotations.Nullable;

import io.github.thecsdev.betterstats.api.client.gui.stats.widget.CustomStatElement;
import io.github.thecsdev.betterstats.api.client.gui.stats.widget.ItemStatWidget;
import io.github.thecsdev.betterstats.api.client.gui.stats.widget.MobStatWidget;
import io.github.thecsdev.betterstats.api.util.enumerations.MobStatType;
import io.github.thecsdev.betterstats.api.util.stats.SUMobStat;
import io.github.thecsdev.tcdcommons.api.client.gui.TElement;
import io.github.thecsdev.tcdcommons.api.client.gui.panel.TPanelElement;
import io.github.thecsdev.tcdcommons.api.client.gui.screen.TWidgetHudScreen;
import io.github.thecsdev.tcdcommons.api.client.gui.util.TDrawContext;
import net.minecraft.text.Text;

public final class StatsHudMobEntry extends TWidgetHudScreen.WidgetEntry<TElement>
{
// ==================================================
static final int WIDTH = StatsHudItemEntry.WIDTH;
// --------------------------------------------------
protected final SUMobStat stat;
protected MobStatType mode = MobStatType.KILLED;
// ==================================================
public StatsHudMobEntry(SUMobStat stat) throws NullPointerException
{
super(0.5, 0.25);
this.stat = Objects.requireNonNull(stat);
}
// ==================================================
public final @Override TElement createWidget()
{
final var el = new Element();
el.eContextMenu.register((__, cMenu) ->
{
for(final var ist : MobStatType.values())
{
cMenu.addButton(ist.getText(), ___ ->
{
this.mode = ist;
refreshEntry();
});
}
cMenu.addSeparator();
cMenu.addButton(translatable("selectWorld.delete"), ___ -> removeEntry());
});
return el;
}
// --------------------------------------------------
private final CustomStatElement createCustomStatElement()
{
//collect info
final int i = ItemStatWidget.SIZE;
@Nullable Text left = this.mode.getText();
@Nullable Text right = literal(Integer.toString(this.mode.getStatValue(this.stat)));
//create and return
return new CustomStatElement(i, 0, WIDTH - i, left, right);
}
// ==================================================
private final class Element extends TElement
{
public Element()
{
super(0, 0, WIDTH, CustomStatElement.HEIGHT);

final var ms = new MobStatWidget(0, 0, stat);
ms.setSize(this.height, this.height);

addChild(ms, true);
addChild(createCustomStatElement(), true);
}
public @Override void render(TDrawContext pencil) { pencil.drawTFill(TPanelElement.COLOR_BACKGROUND); }
}
// ==================================================
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.github.thecsdev.betterstats.api.util.io.IEditableStatsProvider;
import io.github.thecsdev.betterstats.api.util.io.IStatsProvider;
import io.github.thecsdev.betterstats.api.util.io.StatsProviderIO;
import io.github.thecsdev.betterstats.client.gui.screen.hud.BetterStatsHudScreen;
import io.github.thecsdev.betterstats.client.gui.stats.panel.impl.BetterStatsPanel.BetterStatsPanelProxy;
import io.github.thecsdev.tcdcommons.api.client.gui.other.TLabelElement;
import io.github.thecsdev.tcdcommons.api.client.gui.panel.menu.TContextMenuPanel;
Expand Down Expand Up @@ -140,6 +141,14 @@ public MenuBarPanel(int x, int y, int width, MenuBarPanelProxy proxy) throws Nul
localPlayer.getStatHandler()));
});

//statistics hud
cMenu.addButton(BetterStatsHudScreen.TEXT_TITLE, __ ->
{
final var sc = BetterStatsHudScreen.getInstance();
sc.setParentScreen(MC_CLIENT.currentScreen);
MC_CLIENT.setScreen(sc.getAsScreen());
});

//stats tab entries
cMenu.addSeparator();
for(final Entry<Identifier, StatsTab> statsTabEntry : STATS_TAB)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public final double getVerticalScrollBarValue()
//create a scroll-bar for the stats panel
this.scroll_panel = new ScrollBarWidget(this.panel.getWidth(), 0, 8, this.panel.getHeight(), this.panel);
addChild(this.scroll_panel, true);
this.scroll_panel.eClicked.register(__ -> fil.setProperty(FILTER_ID_SCROLL_CACHE, this.scroll_panel.getValue()));

this.panel.eScrolledVertically.register((__, val) ->
fil.setProperty(FILTER_ID_SCROLL_CACHE, this.scroll_panel.getValue()));

//initialize the panel
final StatsTab selTab = this.proxy.getSelectedStatsTab();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
// --------------------------------------------------
protected final @Override void processWidget(ItemStatWidget widget)
{
super.processWidget(widget);
if(widget.getStat().used > 0) widget.setOutlineColor(BSStatsTabs.COLOR_SPECIAL);
else if(!widget.getStat().isEmpty()) widget.setOutlineColor(TPanelElement.COLOR_OUTLINE);
}
Expand Down
Loading

0 comments on commit ddd942e

Please sign in to comment.