Skip to content

Commit dbbd6fc

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

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,75 @@
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.title = title;
28+
this.description = description;
29+
30+
this.icon = icon;
31+
32+
this.visibility = Visibility.SHOW;
33+
}
34+
35+
@Override
36+
public @NotNull Visibility getWantedVisibility() {
37+
return this.visibility;
38+
}
39+
40+
@Override
41+
public void update(ToastManager toastManager, long currentTime) {
42+
if (this.firstRender == 0) {
43+
return;
44+
}
45+
46+
if (currentTime - this.firstRender >= 5000.0 * toastManager.getNotificationDisplayTimeMultiplier()) {
47+
this.visibility = Visibility.HIDE;
48+
}
49+
}
50+
51+
@Override
52+
public void render(GuiGraphics graphics, Font font, long currentTime) {
53+
if (this.firstRender == 0) {
54+
this.onFirstRender();
55+
this.firstRender = currentTime;
56+
}
57+
58+
// type, resource, x, y, ?, ?, width, height, width, height
59+
graphics.blit(RenderType::guiTextured, BACKGROUND_IMAGE, 0, 0, 0.0f, 0, this.width(), this.height(), this.width(), this.height());
60+
61+
int x = 8;
62+
63+
if (this.icon != null) {
64+
x += 22;
65+
66+
graphics.blit(RenderType::guiTextured, MOD_ICON, 2, 2, 0.0f, 0, 8, 8, 8, 8);
67+
graphics.blit(RenderType::guiTextured, this.icon, 8, 8, 0.0f, 0, 16, 16, 16, 16);
68+
}
69+
70+
graphics.drawString(Minecraft.getInstance().font, this.title, x, 7, 0x5f3315, false);
71+
graphics.drawString(Minecraft.getInstance().font, this.description, x, 18, -16777216, false);
72+
}
73+
74+
public void onFirstRender() {}
75+
}
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

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Collection;
1919
import java.util.Collections;
2020

21+
import static dynamic_fps.impl.util.Localization.localized;
22+
2123
public class BatteryTracker {
2224
private static boolean readInitialData = false;
2325

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

171183
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)