Skip to content

Commit

Permalink
feat: Add an overall mode the statistics page (#2432)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofbolyai committed May 5, 2024
1 parent 5e80f8e commit 31e4c5d
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2022-2023.
* Copyright © Wynntils 2022-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.base.widgets;
Expand All @@ -17,13 +17,21 @@
import net.minecraft.network.chat.Component;

public class FilterButton extends WynntilsButton implements TooltipProvider {
private static final Component DEFAULT_ENABLED_TOOLTIP =
Component.translatable("screens.wynntils.content.clickToHide").withStyle(ChatFormatting.GRAY);

private static final Component DEFAULT_DISABLED_TOOLTIP =
Component.translatable("screens.wynntils.content.clickToShow").withStyle(ChatFormatting.GRAY);

private static final CustomColor BUTTON_COLOR = new CustomColor(181, 174, 151);
private static final CustomColor BUTTON_COLOR_HOVERED = new CustomColor(121, 116, 101);
private static final CustomColor BUTTON_COLOR_ENABLED = new CustomColor(164, 212, 142);

private final Texture texture;
private final boolean dynamicTexture;
private final List<Component> tooltipList;
private final List<Component> enabledActionTooltip;
private final List<Component> disabledActionTooltip;
private final Runnable onPress;
private final Supplier<Boolean> isEnabled;

Expand All @@ -42,6 +50,30 @@ public FilterButton(
this.texture = texture;
this.dynamicTexture = dynamicTexture;
this.tooltipList = tooltipList;
this.enabledActionTooltip = List.of(DEFAULT_ENABLED_TOOLTIP);
this.disabledActionTooltip = List.of(DEFAULT_DISABLED_TOOLTIP);
this.onPress = onPress;
this.isEnabled = isEnabled;
}

public FilterButton(
int x,
int y,
int width,
int height,
Texture texture,
boolean dynamicTexture,
List<Component> tooltipList,
List<Component> enabledActionTooltip,
List<Component> disabledActionTooltip,
Runnable onPress,
Supplier<Boolean> isEnabled) {
super(x, y, width, height, Component.literal("Filter Button"));
this.texture = texture;
this.dynamicTexture = dynamicTexture;
this.tooltipList = tooltipList;
this.enabledActionTooltip = enabledActionTooltip;
this.disabledActionTooltip = disabledActionTooltip;
this.onPress = onPress;
this.isEnabled = isEnabled;
}
Expand Down Expand Up @@ -113,11 +145,9 @@ public List<Component> getTooltipLines() {
List<Component> renderedTooltip = new ArrayList<>(tooltipList);

if (isEnabled.get()) {
renderedTooltip.add(Component.translatable("screens.wynntils.wynntilsDiscoveries.clickToHide")
.withStyle(ChatFormatting.GRAY));
renderedTooltip.addAll(enabledActionTooltip);
} else {
renderedTooltip.add(Component.translatable("screens.wynntils.wynntilsDiscoveries.clickToShow")
.withStyle(ChatFormatting.GRAY));
renderedTooltip.addAll(disabledActionTooltip);
}

return renderedTooltip;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/*
* Copyright © Wynntils 2023.
* Copyright © Wynntils 2023-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.statistics;

import com.mojang.blaze3d.vertex.PoseStack;
import com.wynntils.core.components.Services;
import com.wynntils.core.persisted.storage.Storage;
import com.wynntils.core.text.StyledText;
import com.wynntils.screens.base.WynntilsListScreen;
import com.wynntils.screens.base.widgets.BackButton;
import com.wynntils.screens.base.widgets.FilterButton;
import com.wynntils.screens.base.widgets.PageSelectorButton;
import com.wynntils.screens.statistics.widgets.StatisticButton;
import com.wynntils.screens.wynntilsmenu.WynntilsMenuScreen;
Expand All @@ -21,15 +23,15 @@
import com.wynntils.utils.render.type.HorizontalAlignment;
import com.wynntils.utils.render.type.TextShadow;
import com.wynntils.utils.render.type.VerticalAlignment;
import com.wynntils.utils.type.Pair;
import java.util.Arrays;
import java.util.List;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;

public final class WynntilsStatisticsScreen
extends WynntilsListScreen<Pair<StatisticKind, StatisticEntry>, StatisticButton> {
private StatisticButton highlightedButton;
public final class WynntilsStatisticsScreen extends WynntilsListScreen<StatisticKind, StatisticButton> {
private StatisticKind highlightedStatisticKind;

private WynntilsStatisticsScreen() {
super(Component.translatable("screens.wynntils.statistics.name"));
Expand Down Expand Up @@ -64,6 +66,37 @@ protected void doInit() {
Texture.FORWARD_ARROW_OFFSET.height(),
true,
this));

this.addRenderableWidget(new FilterButton(
(int) ((Texture.CONTENT_BOOK_BACKGROUND.width() / 2f - 15) / 2f),
157,
30,
30,
Texture.FAVORITE_ICON,
false,
List.of(Component.literal("[>] ")
.withStyle(ChatFormatting.GREEN)
.append(Component.translatable("screens.wynntils.statistics.modeButton.name")
.withStyle(ChatFormatting.BOLD)
.withStyle(ChatFormatting.GREEN))),
List.of(
Component.translatable("screens.wynntils.statistics.modeButton.descriptionOverall")
.withStyle(ChatFormatting.GRAY),
Component.empty(),
Component.translatable("screens.wynntils.statistics.modeButton.enabled")
.withStyle(ChatFormatting.GRAY)),
List.of(
Component.translatable("screens.wynntils.statistics.modeButton.descriptionSingle")
.withStyle(ChatFormatting.GRAY),
Component.empty(),
Component.translatable("screens.wynntils.statistics.modeButton.disabled")
.withStyle(ChatFormatting.GRAY)),
() -> {
Storage<Boolean> screenOverallMode = Services.Statistics.screenOverallMode;
screenOverallMode.store(!screenOverallMode.get());
reloadElements();
},
Services.Statistics.screenOverallMode::get));
}

@Override
Expand Down Expand Up @@ -98,7 +131,7 @@ public void doRender(GuiGraphics guiGraphics, int mouseX, int mouseY, float part
}

private void renderDescription(PoseStack poseStack) {
if (highlightedButton == null) {
if (highlightedStatisticKind == null) {
FontRenderer.getInstance()
.renderText(
poseStack,
Expand All @@ -113,20 +146,20 @@ private void renderDescription(PoseStack poseStack) {
return;
}

Pair<StatisticKind, StatisticEntry> statistic = highlightedButton.getStatistic();
StatisticKind statisticKind = statistic.a();
StatisticEntry entry = statistic.b();
StatisticEntry entry = Services.Statistics.screenOverallMode.get()
? Services.Statistics.getOverallStatistic(highlightedStatisticKind)
: Services.Statistics.getStatistic(highlightedStatisticKind);

poseStack.pushPose();
poseStack.translate(20, 80, 0);
poseStack.translate(20, 75, 0);

// Name
poseStack.pushPose();
poseStack.scale(1.2f, 1.2f, 0);
FontRenderer.getInstance()
.renderText(
poseStack,
StyledText.fromString(statisticKind.getName()),
StyledText.fromString(highlightedStatisticKind.getName()),
0,
0,
(Texture.CONTENT_BOOK_BACKGROUND.width() / 2 - 20) / 1.2f,
Expand All @@ -137,9 +170,9 @@ private void renderDescription(PoseStack poseStack) {
poseStack.popPose();

// Statistics
switch (statisticKind.getType()) {
case COUNT -> renderCountStatistics(poseStack, statisticKind, entry);
case ADVANCED -> renderAdvancedStatistics(poseStack, statisticKind, entry);
switch (highlightedStatisticKind.getType()) {
case COUNT -> renderCountStatistics(poseStack, highlightedStatisticKind, entry);
case ADVANCED -> renderAdvancedStatistics(poseStack, highlightedStatisticKind, entry);
}

poseStack.popPose();
Expand Down Expand Up @@ -227,12 +260,12 @@ private static void renderAdvancedStatistics(
TextShadow.NONE);
}

public void setHighlightedButton(StatisticButton button) {
this.highlightedButton = button;
public void setHighlightedStatisticKind(StatisticKind kind) {
this.highlightedStatisticKind = kind;
}

public StatisticButton getHighlightedButton() {
return highlightedButton;
public StatisticKind getHighlightedStatisticKind() {
return highlightedStatisticKind;
}

@Override
Expand All @@ -250,9 +283,7 @@ protected StatisticButton getButtonFromElement(int i) {
@Override
protected void reloadElementsList(String searchTerm) {
elements.addAll(Arrays.stream(StatisticKind.values())
.map(kind -> new Pair<>(kind, Services.Statistics.getStatistic(kind)))
.filter(statisticEntry ->
StringUtils.partialMatch(statisticEntry.a().getName(), searchTerm))
.filter(statisticEntry -> StringUtils.partialMatch(statisticEntry.getName(), searchTerm))
.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2023.
* Copyright © Wynntils 2023-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.statistics.widgets;
Expand All @@ -21,7 +21,6 @@
import com.wynntils.utils.render.type.HorizontalAlignment;
import com.wynntils.utils.render.type.TextShadow;
import com.wynntils.utils.render.type.VerticalAlignment;
import com.wynntils.utils.type.Pair;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.ChatFormatting;
Expand All @@ -35,16 +34,11 @@ public class StatisticButton extends WynntilsButton implements TooltipProvider {
private static final CustomColor TRACKED_BUTTON_COLOR = new CustomColor(176, 197, 148);
private static final CustomColor TRACKED_BUTTON_COLOR_HOVERED = new CustomColor(126, 211, 106);

private final Pair<StatisticKind, StatisticEntry> statistic;
private final StatisticKind statistic;
private final WynntilsStatisticsScreen screen;

public StatisticButton(
int x,
int y,
int width,
int height,
Pair<StatisticKind, StatisticEntry> statistic,
WynntilsStatisticsScreen screen) {
int x, int y, int width, int height, StatisticKind statistic, WynntilsStatisticsScreen screen) {
super(x, y, width, height, Component.literal("Statistics Button"));
this.statistic = statistic;
this.screen = screen;
Expand All @@ -62,7 +56,7 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float
.renderText(
poseStack,
StyledText.fromString(RenderedStringUtils.getMaxFittingText(
statistic.a().getName(),
statistic.getName(),
maxTextWidth,
FontRenderer.getInstance().getFont())),
this.getX() + 2,
Expand All @@ -86,37 +80,40 @@ private CustomColor getButtonBackgroundColor() {
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT) {
if (isSelected()) {
screen.setHighlightedButton(null);
screen.setHighlightedStatisticKind(null);
} else {
screen.setHighlightedButton(this);
screen.setHighlightedStatisticKind(this.statistic);
}
return true;
}

if (KeyboardUtils.isShiftDown() && button == GLFW.GLFW_MOUSE_BUTTON_MIDDLE) {
Services.Statistics.resetStatistic(statistic.a());
if (Services.Statistics.screenOverallMode.get()) {
Services.Statistics.resetStatisticOverall(statistic);
} else {
Services.Statistics.resetStatisticForCharacter(statistic);
}
screen.reloadElements();
return true;
}

return true;
}

public Pair<StatisticKind, StatisticEntry> getStatistic() {
return statistic;
}

@Override
public void onPress() {}

@Override
public List<Component> getTooltipLines() {
List<Component> lines = new ArrayList<>();

lines.add(Component.literal(statistic.a().getName()).withStyle(ChatFormatting.BOLD));
StatisticEntry entry = Services.Statistics.screenOverallMode.get()
? Services.Statistics.getOverallStatistic(statistic)
: Services.Statistics.getStatistic(statistic);

lines.add(Component.literal(statistic.getName()).withStyle(ChatFormatting.BOLD));
lines.add(Component.translatable(
"screens.wynntils.statistics.total",
statistic.a().getFormattedValue(statistic.b().total())));
"screens.wynntils.statistics.total", statistic.getFormattedValue(entry.total())));
lines.add(Component.empty());

if (isSelected()) {
Expand All @@ -128,14 +125,17 @@ public List<Component> getTooltipLines() {
.withStyle(ChatFormatting.BOLD)
.withStyle(ChatFormatting.GREEN));
}
lines.add(Component.translatable("screens.wynntils.statistics.shiftMiddleClickToReset")

lines.add((Services.Statistics.screenOverallMode.get()
? Component.translatable("screens.wynntils.statistics.shiftMiddleClickToResetOverall")
: Component.translatable("screens.wynntils.statistics.shiftMiddleClickToResetCurrent"))
.withStyle(ChatFormatting.BOLD)
.withStyle(ChatFormatting.RED));

return lines;
}

private boolean isSelected() {
return screen.getHighlightedButton() == this;
return screen.getHighlightedStatisticKind() == this.statistic;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2023.
* Copyright © Wynntils 2023-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.services.statistics;
Expand All @@ -26,6 +26,9 @@ public final class StatisticsService extends Service {
@Persisted
private final Storage<Map<String, Map<StatisticKind, StatisticEntry>>> statistics = new Storage<>(new TreeMap<>());

@Persisted
public final Storage<Boolean> screenOverallMode = new Storage<>(true);

// The currently active statistics
private Map<StatisticKind, StatisticEntry> currentStatistics = new EnumMap<>(StatisticKind.class);

Expand Down Expand Up @@ -73,11 +76,39 @@ public Map<StatisticKind, StatisticEntry> getStatistics() {
return currentStatistics;
}

public void resetStatistic(StatisticKind statistic) {
public StatisticEntry getOverallStatistic(StatisticKind statistic) {
StatisticEntry overall = StatisticEntry.EMPTY;

for (Map<StatisticKind, StatisticEntry> characterStatistics :
statistics.get().values()) {
StatisticEntry entry = characterStatistics.get(statistic);
if (entry != null) {
overall = new StatisticEntry(
overall.total() + entry.total(),
overall.count() + entry.count(),
Math.min(overall.min(), entry.min()),
Math.max(overall.max(), entry.max()),
Math.min(overall.firstModified(), entry.firstModified()),
Math.max(overall.lastModified(), entry.lastModified()));
}
}

return overall;
}

public void resetStatisticForCharacter(StatisticKind statistic) {
currentStatistics.remove(statistic);
statistics.touched();
}

public void resetStatisticOverall(StatisticKind statistic) {
for (Map<StatisticKind, StatisticEntry> characterStatistics :
statistics.get().values()) {
characterStatistics.remove(statistic);
}
statistics.touched();
}

public void resetStatistics() {
currentStatistics.clear();
statistics.touched();
Expand Down

0 comments on commit 31e4c5d

Please sign in to comment.