-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSimpleToast.java
130 lines (117 loc) · 4.46 KB
/
SimpleToast.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2024 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.toasts;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.internal.impl.toast.GuiToastImpl;
import com.falsepattern.lib.toasts.icon.ToastBG;
import com.falsepattern.lib.toasts.icon.ToastIcon;
import com.falsepattern.lib.util.MathUtil;
import lombok.NonNull;
import lombok.val;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.gui.Gui;
import net.minecraft.util.IChatComponent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import javax.annotation.Nullable;
@SideOnly(Side.CLIENT)
@StableAPI(since = "0.10.0")
public class SimpleToast implements IToast {
private final ToastBG background;
private final ToastIcon icon;
private final String title;
private final String subtitle;
private final boolean hasProgressBar;
private final long timeout;
private IToast.Visibility visibility = IToast.Visibility.SHOW;
private long lastDelta;
private float displayedProgress;
private float currentProgress;
@StableAPI.Expose
public SimpleToast(@NonNull ToastBG background,
@Nullable ToastIcon icon, IChatComponent titleComponent,
@Nullable IChatComponent subtitleComponent, boolean drawProgressBar, long timeout) {
this.background = background;
this.icon = icon;
this.title = titleComponent.getFormattedText();
this.subtitle = subtitleComponent == null ? null : subtitleComponent.getFormattedText();
this.hasProgressBar = drawProgressBar;
this.timeout = timeout;
}
@Override
public IToast.Visibility draw(GuiToastImpl toastGui, long delta) {
GL11.glColor3f(1, 1, 1);
background.draw(toastGui, 0, 0);
if (icon != null) {
icon.draw(toastGui, 6, 6);
}
val x = icon == null ? 5 : 5 + icon.getIcon().getIconWidth();
val bgHeight = background.getIcon().getIconHeight();
val fontRenderer = toastGui.getMinecraft().fontRenderer;
if (subtitle == null) {
fontRenderer.drawString(title, x, bgHeight / 2 - 5, 0xFFFFFFFF);
} else {
fontRenderer.drawString(title, x, 7, 0xFFFFFFFF);
fontRenderer.drawString(subtitle, x, bgHeight - 14, 0xFFFFFFFF);
}
if (hasProgressBar) {
val bgWidth = background.getIcon().getIconWidth();
Gui.drawRect(3, bgHeight - 4, bgWidth - 3, bgHeight - 3, 0xFFFFFFFF);
float f = (float) MathUtil.clampedLerp(displayedProgress,
currentProgress,
(float) (delta - lastDelta) / 100.0F);
int i;
if (currentProgress >= displayedProgress) {
i = 0xff005500;
} else {
i = 0xff550000;
}
val barLength = bgWidth - 6;
Gui.drawRect(3, bgHeight - 4, (int) (3.0F + barLength * f), bgHeight - 3, i);
displayedProgress = f;
lastDelta = delta;
}
if (timeout > 0) {
if (delta >= timeout) {
hide();
}
}
return visibility;
}
@Override
public int width() {
return background.getIcon().getIconWidth();
}
@Override
public int height() {
return background.getIcon().getIconHeight();
}
@StableAPI.Expose
public void hide() {
visibility = IToast.Visibility.HIDE;
}
@StableAPI.Expose
public void setProgress(float progress) {
this.currentProgress = progress;
}
}