Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error toast with hints #232

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package dynamic_fps.impl.feature.battery;

import dynamic_fps.impl.util.ResourceLocations;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.toasts.Toast;
import net.minecraft.client.gui.components.toasts.ToastManager;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class BaseToast implements Toast {
private long firstRender;
private Visibility visibility;

protected Component title;
protected Component description;
protected @Nullable ResourceLocation icon;

private static final ResourceLocation MOD_ICON = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background_icon.png");
private static final ResourceLocation BACKGROUND_IMAGE = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background.png");

protected BaseToast(Component title, Component description, @Nullable ResourceLocation icon) {
this.title = title;
this.description = description;

this.icon = icon;

this.visibility = Visibility.SHOW;
}

@Override
public @NotNull Visibility getWantedVisibility() {
return this.visibility;
}

@Override
public void update(ToastManager toastManager, long currentTime) {
if (this.firstRender == 0) {
return;
}

if (currentTime - this.firstRender >= 5000.0 * toastManager.getNotificationDisplayTimeMultiplier()) {
this.visibility = Visibility.HIDE;
}
}

@Override
public void render(GuiGraphics graphics, Font font, long currentTime) {
if (this.firstRender == 0) {
this.onFirstRender();
this.firstRender = currentTime;
}

// type, resource, x, y, ?, ?, width, height, width, height
graphics.blit(RenderType::guiTextured, BACKGROUND_IMAGE, 0, 0, 0.0f, 0, this.width(), this.height(), this.width(), this.height());

int x = 8;

if (this.icon != null) {
x += 22;

graphics.blit(RenderType::guiTextured, MOD_ICON, 2, 2, 0.0f, 0, 8, 8, 8, 8);
graphics.blit(RenderType::guiTextured, this.icon, 8, 8, 0.0f, 0, 16, 16, 16, 16);
}

graphics.drawString(Minecraft.getInstance().font, this.title, x, 7, 0x5f3315, false);
graphics.drawString(Minecraft.getInstance().font, this.description, x, 18, -16777216, false);
}

public void onFirstRender() {}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
package dynamic_fps.impl.feature.battery;

import dynamic_fps.impl.util.ResourceLocations;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.toasts.Toast;
import net.minecraft.client.gui.components.toasts.ToastManager;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

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

public class BatteryToast implements Toast {
private long firstRender;

private Component title;
private Component description;
private ResourceLocation icon;
private Visibility visibility;

public class BatteryToast extends BaseToast {
private static BatteryToast queuedToast;

private static final ResourceLocation MOD_ICON = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background_icon.png");
private static final ResourceLocation BACKGROUND_IMAGE = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background.png");

private BatteryToast(Component title, ResourceLocation icon) {
this.title = title;
this.icon = icon;
this.visibility = Visibility.SHOW;
super(title, Component.empty(), icon);
}

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

@Override
public @NotNull Visibility getWantedVisibility() {
return this.visibility;
}

@Override
public void update(ToastManager toastManager, long currentTime) {
if (this.firstRender == 0) {
return;
}

if (currentTime - this.firstRender >= 5000.0 * toastManager.getNotificationDisplayTimeMultiplier()) {
this.visibility = Visibility.HIDE;
}
}

@Override
public void render(GuiGraphics graphics, Font font, long currentTime) {
if (this.firstRender == 0) {
if (this == queuedToast) {
queuedToast = null;
}

this.firstRender = currentTime;
// Initialize when first rendering so the battery percentage is mostly up-to-date
this.description = localized("toast", "battery_charge", BatteryTracker.charge());
public void onFirstRender() {
if (this == queuedToast) {
queuedToast = null;
}
// resource, x, y, z, ?, ?, width, height, width, height
graphics.blit(RenderType::guiTextured, BACKGROUND_IMAGE, 0, 0, 0.0f, 0, this.width(), this.height(), this.width(), this.height());

graphics.blit(RenderType::guiTextured, MOD_ICON, 2, 2, 0.0f, 0, 8, 8, 8, 8);
graphics.blit(RenderType::guiTextured, this.icon, 8, 8, 0.0f, 0, 16, 16, 16, 16);

graphics.drawString(Minecraft.getInstance().font, this.title, 30, 7, 0x5f3315, false);
graphics.drawString(Minecraft.getInstance().font, this.description, 30, 18, -16777216, false);
// Initialize when first rendering so the battery percentage is mostly up-to-date
this.description = localized("toast", "battery_charge", BatteryTracker.charge());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Collection;
import java.util.Collections;

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

public class BatteryTracker {
private static boolean readInitialData = false;

Expand Down Expand Up @@ -166,6 +168,16 @@ private static Manager createManager() {
} catch (LibraryLoadError e) {
// No native backend library is available for this OS or platform
Logging.getLogger().warn("Battery tracker feature unavailable!");

String path;

if (DynamicFPSConfig.INSTANCE.downloadNatives()) {
path = "no_support";
} else {
path = "no_library";
}

ErrorToast.queueToast(localized("toast", path));
}

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dynamic_fps.impl.feature.battery;

import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;

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

public class ErrorToast extends BaseToast {
private static final Component TITLE = localized("toast", "error");

private ErrorToast(Component description) {
super(TITLE, description, null);
}

/**
* Queue some information to be shown as a toast.
*/
public static void queueToast(Component description) {
ErrorToast toast = new ErrorToast(description);
Minecraft.getInstance().getToastManager().addToast(toast);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
"gui.dynamic_fps.hud.reducing": "Dynamic FPS: Forcing Reduced FPS",
"gui.dynamic_fps.hud.disabled": "Dynamic FPS Disabled",

"toast.dynamic_fps.error": "Dynamic FPS Battery Error",
"toast.dynamic_fps.no_support": "Computer is not supported",
"toast.dynamic_fps.no_library": "Library download disabled",

"toast.dynamic_fps.battery_charging": "Charging!",
"toast.dynamic_fps.battery_draining": "Discharging!",
"toast.dynamic_fps.battery_critical": "Battery Low!",
Expand Down