-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from LostLuma/feature/error-toast
Add error toast with hints
- Loading branch information
Showing
5 changed files
with
120 additions
and
53 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
platforms/common/src/main/java/dynamic_fps/impl/feature/battery/BaseToast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
platforms/common/src/main/java/dynamic_fps/impl/feature/battery/ErrorToast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters