Skip to content

Commit e705eaa

Browse files
committed
Add error toast with hints
1 parent 5c4ef92 commit e705eaa

File tree

5 files changed

+120
-53
lines changed

5 files changed

+120
-53
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package dynamic_fps.impl.feature.battery;
2+
3+
import dynamic_fps.impl.util.ResourceLocations;
4+
import net.minecraft.client.Minecraft;
5+
import net.minecraft.client.gui.Font;
6+
import net.minecraft.client.gui.GuiGraphics;
7+
import net.minecraft.client.gui.components.toasts.Toast;
8+
import net.minecraft.client.gui.components.toasts.ToastManager;
9+
import net.minecraft.client.renderer.RenderType;
10+
import net.minecraft.network.chat.Component;
11+
import net.minecraft.resources.ResourceLocation;
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
14+
15+
public class BaseToast implements Toast {
16+
private long firstRender;
17+
private Visibility visibility;
18+
19+
protected Component title;
20+
protected Component description;
21+
protected @Nullable ResourceLocation icon;
22+
23+
private static final ResourceLocation MOD_ICON = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background_icon.png");
24+
private static final ResourceLocation BACKGROUND_IMAGE = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background.png");
25+
26+
protected BaseToast(Component title, Component description, @Nullable ResourceLocation icon) {
27+
this.visibility = Visibility.SHOW;
28+
this.icon = icon;
29+
30+
this.title = title;
31+
this.description = description;
32+
}
33+
34+
@Override
35+
public @NotNull Visibility getWantedVisibility() {
36+
return this.visibility;
37+
}
38+
39+
@Override
40+
public void update(ToastManager toastManager, long currentTime) {
41+
if (this.firstRender == 0) {
42+
return;
43+
}
44+
45+
if (currentTime - this.firstRender >= 5000.0 * toastManager.getNotificationDisplayTimeMultiplier()) {
46+
this.visibility = Visibility.HIDE;
47+
}
48+
}
49+
50+
@Override
51+
public void render(GuiGraphics graphics, Font font, long currentTime) {
52+
if (this.firstRender == 0) {
53+
this.onFirstRender();
54+
this.firstRender = currentTime;
55+
}
56+
57+
// type, resource, x, y, ?, ?, width, height, width, height
58+
graphics.blit(RenderType::guiTextured, BACKGROUND_IMAGE, 0, 0, 0.0f, 0, this.width(), this.height(), this.width(), this.height());
59+
60+
int x = 8;
61+
62+
if (this.icon != null) {
63+
x += 22;
64+
65+
graphics.blit(RenderType::guiTextured, MOD_ICON, 2, 2, 0.0f, 0, 8, 8, 8, 8);
66+
graphics.blit(RenderType::guiTextured, this.icon, 8, 8, 0.0f, 0, 16, 16, 16, 16);
67+
}
68+
69+
graphics.drawString(Minecraft.getInstance().font, this.title, x, 7, 0x5f3315, false);
70+
graphics.drawString(Minecraft.getInstance().font, this.description, x, 18, -16777216, false);
71+
}
72+
73+
public void onFirstRender() {}
74+
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
11
package dynamic_fps.impl.feature.battery;
22

3-
import dynamic_fps.impl.util.ResourceLocations;
43
import net.minecraft.client.Minecraft;
5-
import net.minecraft.client.gui.Font;
6-
import net.minecraft.client.gui.GuiGraphics;
7-
import net.minecraft.client.gui.components.toasts.Toast;
8-
import net.minecraft.client.gui.components.toasts.ToastManager;
9-
import net.minecraft.client.renderer.RenderType;
104
import net.minecraft.network.chat.Component;
115
import net.minecraft.resources.ResourceLocation;
12-
import org.jetbrains.annotations.NotNull;
136

147
import static dynamic_fps.impl.util.Localization.localized;
158

16-
public class BatteryToast implements Toast {
17-
private long firstRender;
18-
19-
private Component title;
20-
private Component description;
21-
private ResourceLocation icon;
22-
private Visibility visibility;
23-
9+
public class BatteryToast extends BaseToast {
2410
private static BatteryToast queuedToast;
2511

26-
private static final ResourceLocation MOD_ICON = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background_icon.png");
27-
private static final ResourceLocation BACKGROUND_IMAGE = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background.png");
28-
2912
private BatteryToast(Component title, ResourceLocation icon) {
30-
this.title = title;
31-
this.icon = icon;
32-
this.visibility = Visibility.SHOW;
13+
super(title, Component.empty(), icon);
3314
}
3415

3516
/**
@@ -47,39 +28,12 @@ public static void queueToast(Component title, ResourceLocation icon) {
4728
}
4829

4930
@Override
50-
public @NotNull Visibility getWantedVisibility() {
51-
return this.visibility;
52-
}
53-
54-
@Override
55-
public void update(ToastManager toastManager, long currentTime) {
56-
if (this.firstRender == 0) {
57-
return;
58-
}
59-
60-
if (currentTime - this.firstRender >= 5000.0 * toastManager.getNotificationDisplayTimeMultiplier()) {
61-
this.visibility = Visibility.HIDE;
62-
}
63-
}
64-
65-
@Override
66-
public void render(GuiGraphics graphics, Font font, long currentTime) {
67-
if (this.firstRender == 0) {
68-
if (this == queuedToast) {
69-
queuedToast = null;
70-
}
71-
72-
this.firstRender = currentTime;
73-
// Initialize when first rendering so the battery percentage is mostly up-to-date
74-
this.description = localized("toast", "battery_charge", BatteryTracker.charge());
31+
public void onFirstRender() {
32+
if (this == queuedToast) {
33+
queuedToast = null;
7534
}
76-
// resource, x, y, z, ?, ?, width, height, width, height
77-
graphics.blit(RenderType::guiTextured, BACKGROUND_IMAGE, 0, 0, 0.0f, 0, this.width(), this.height(), this.width(), this.height());
78-
79-
graphics.blit(RenderType::guiTextured, MOD_ICON, 2, 2, 0.0f, 0, 8, 8, 8, 8);
80-
graphics.blit(RenderType::guiTextured, this.icon, 8, 8, 0.0f, 0, 16, 16, 16, 16);
8135

82-
graphics.drawString(Minecraft.getInstance().font, this.title, 30, 7, 0x5f3315, false);
83-
graphics.drawString(Minecraft.getInstance().font, this.description, 30, 18, -16777216, false);
36+
// Initialize when first rendering so the battery percentage is mostly up-to-date
37+
this.description = localized("toast", "battery_charge", BatteryTracker.charge());
8438
}
8539
}

platforms/common/src/main/java/dynamic_fps/impl/feature/battery/BatteryTracker.java

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.lostluma.battery.api.exception.LibraryLoadError;
1111
import net.lostluma.battery.api.util.LibraryUtil;
1212
import net.minecraft.client.Minecraft;
13+
import net.minecraft.network.chat.Component;
1314
import org.jetbrains.annotations.Nullable;
1415

1516
import java.io.IOException;
@@ -18,6 +19,8 @@
1819
import java.util.Collection;
1920
import java.util.Collections;
2021

22+
import static dynamic_fps.impl.util.Localization.localized;
23+
2124
public class BatteryTracker {
2225
private static boolean readInitialData = false;
2326

@@ -166,6 +169,16 @@ private static Manager createManager() {
166169
} catch (LibraryLoadError e) {
167170
// No native backend library is available for this OS or platform
168171
Logging.getLogger().warn("Battery tracker feature unavailable!");
172+
173+
String path;
174+
175+
if (DynamicFPSConfig.INSTANCE.downloadNatives()) {
176+
path = "no_support";
177+
} else {
178+
path = "no_library";
179+
}
180+
181+
ErrorToast.queueToast(localized("toast", path));
169182
}
170183

171184
return result;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package dynamic_fps.impl.feature.battery;
2+
3+
import net.minecraft.client.Minecraft;
4+
import net.minecraft.network.chat.Component;
5+
6+
import static dynamic_fps.impl.util.Localization.localized;
7+
8+
public class ErrorToast extends BaseToast {
9+
private static final Component TITLE = localized("toast", "error");
10+
11+
private ErrorToast(Component description) {
12+
super(TITLE, description, null);
13+
}
14+
15+
/**
16+
* Queue some information to be shown as a toast.
17+
*/
18+
public static void queueToast(Component description) {
19+
ErrorToast toast = new ErrorToast(description);
20+
Minecraft.getInstance().getToastManager().addToast(toast);
21+
}
22+
}

platforms/common/src/main/resources/assets/dynamic_fps/lang/en_us.json

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
"gui.dynamic_fps.hud.reducing": "Dynamic FPS: Forcing Reduced FPS",
8787
"gui.dynamic_fps.hud.disabled": "Dynamic FPS Disabled",
8888

89+
"toast.dynamic_fps.error": "Dynamic FPS Battery Error",
90+
"toast.dynamic_fps.no_support": "Computer is not supported",
91+
"toast.dynamic_fps.no_library": "Library download disabled",
92+
8993
"toast.dynamic_fps.battery_charging": "Charging!",
9094
"toast.dynamic_fps.battery_draining": "Discharging!",
9195
"toast.dynamic_fps.battery_critical": "Battery Low!",

0 commit comments

Comments
 (0)