aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/utils/Notifications.java
blob: 5ce15d446521bd0a546dfa69ba7dc63b4a209bf1 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * This file is part of OneConfig.
 * OneConfig - Next Generation Config Library for Minecraft: Java Edition
 * Copyright (C) 2021, 2022 Polyfrost.
 *   <https://polyfrost.cc> <https://github.com/Polyfrost/>
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 *   OneConfig is licensed under the terms of version 3 of the GNU Lesser
 * General Public License as published by the Free Software Foundation, AND
 * under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
 * either version 1.0 of the Additional Terms, or (at your option) any later
 * version.
 *
 *   This program 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.  If not, see <https://www.gnu.org/licenses/>. You should
 * have also received a copy of the Additional Terms Applicable
 * to OneConfig, as published by Polyfrost. If not, see
 * <https://polyfrost.cc/legal/oneconfig/additional-terms>
 */

package cc.polyfrost.oneconfig.utils;

import cc.polyfrost.oneconfig.events.event.HudRenderEvent;
import cc.polyfrost.oneconfig.events.event.RenderEvent;
import cc.polyfrost.oneconfig.events.event.Stage;
import cc.polyfrost.oneconfig.gui.OneConfigGui;
import cc.polyfrost.oneconfig.gui.animations.Animation;
import cc.polyfrost.oneconfig.gui.animations.DummyAnimation;
import cc.polyfrost.oneconfig.gui.animations.EaseInOutQuad;
import cc.polyfrost.oneconfig.internal.utils.Notification;
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
import cc.polyfrost.oneconfig.libs.universal.UResolution;
import cc.polyfrost.oneconfig.renderer.Icon;
import cc.polyfrost.oneconfig.renderer.RenderManager;
import cc.polyfrost.oneconfig.utils.gui.GuiUtils;
import org.jetbrains.annotations.Nullable;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;

public final class Notifications {
    public static final Notifications INSTANCE = new Notifications();
    // animation stores the bottom y of the notification
    private final LinkedHashMap<Notification, Animation> notifications = new LinkedHashMap<>();
    private final float DEFAULT_DURATION = 4000;

    private Notifications() {
    }

    /**
     * Send a notification to the user
     *
     * @param title       The title of the notification
     * @param message     The message of the notification
     * @param icon        The icon of the notification, null for none
     * @param duration    The duration the notification is on screen in ms
     * @param progressbar A callable that returns the progress from 0-1
     * @param action      The action executed when the notification is pressed
     */
    public void send(String title, String message, @Nullable Icon icon, float duration, @Nullable Callable<Float> progressbar, @Nullable Runnable action) {
        Notification notification = new Notification(title, message, icon, duration, progressbar, action);
        notifications.put(notification, new DummyAnimation(-1));
    }

    /**
     * Send a notification to the user
     *
     * @param title       The title of the notification
     * @param message     The message of the notification
     * @param duration    The duration the notification is on screen in ms
     * @param progressbar A callable that returns the progress from 0-1
     * @param action      The action executed when the notification is pressed
     */
    public void send(String title, String message, float duration, @Nullable Callable<Float> progressbar, @Nullable Runnable action) {
        send(title, message, null, duration, progressbar, action);
    }

    /**
     * Send a notification to the user
     *
     * @param title       The title of the notification
     * @param message     The message of the notification
     * @param icon        The icon of the notification, null for none
     * @param duration    The duration the notification is on screen in ms
     * @param progressbar A callable that returns the progress from 0-1
     */
    public void send(String title, String message, @Nullable Icon icon, float duration, @Nullable Callable<Float> progressbar) {
        send(title, message, icon, duration, progressbar, null);
    }

    /**
     * Send a notification to the user
     *
     * @param title    The title of the notification
     * @param message  The message of the notification
     * @param icon     The icon of the notification, null for none
     * @param duration The duration the notification is on screen in ms
     * @param action   The action executed when the notification is pressed
     */
    public void send(String title, String message, @Nullable Icon icon, float duration, @Nullable Runnable action) {
        send(title, message, icon, duration, null, action);
    }

    /**
     * Send a notification to the user
     *
     * @param title       The title of the notification
     * @param message     The message of the notification
     * @param duration    The duration the notification is on screen in ms
     * @param progressbar A callable that returns the progress from 0-1
     */
    public void send(String title, String message, float duration, @Nullable Callable<Float> progressbar) {
        send(title, message, duration, progressbar, null);
    }

    /**
     * Send a notification to the user
     *
     * @param title    The title of the notification
     * @param message  The message of the notification
     * @param duration The duration the notification is on screen in ms
     * @param action   The action executed when the notification is pressed
     */
    public void send(String title, String message, float duration, @Nullable Runnable action) {
        send(title, message, duration, null, action);
    }

    /**
     * Send a notification to the user
     *
     * @param title       The title of the notification
     * @param message     The message of the notification
     * @param icon        The icon of the notification, null for none
     * @param progressbar A callable that returns the progress from 0-1
     */
    public void send(String title, String message, @Nullable Icon icon, @Nullable Callable<Float> progressbar) {
        send(title, message, icon, DEFAULT_DURATION, progressbar);
    }

    /**
     * Send a notification to the user
     *
     * @param title   The title of the notification
     * @param message The message of the notification
     * @param icon    The icon of the notification, null for none
     * @param action  The action executed when the notification is pressed
     */
    public void send(String title, String message, @Nullable Icon icon, @Nullable Runnable action) {
        send(title, message, icon, DEFAULT_DURATION, action);
    }

    /**
     * Send a notification to the user
     *
     * @param title       The title of the notification
     * @param message     The message of the notification
     * @param progressbar A callable that returns the progress from 0-1
     */
    public void send(String title, String message, @Nullable Callable<Float> progressbar) {
        send(title, message, DEFAULT_DURATION, progressbar);
    }

    /**
     * Send a notification to the user
     *
     * @param title    The title of the notification
     * @param message  The message of the notification
     * @param icon     The icon of the notification, null for none
     * @param duration The duration the notification is on screen in ms
     */
    public void send(String title, String message, @Nullable Icon icon, float duration) {
        send(title, message, icon, duration, (Callable<Float>) null);
    }

    /**
     * Send a notification to the user
     *
     * @param title   The title of the notification
     * @param message The message of the notification
     * @param action  The action executed when the notification is pressed
     */
    public void send(String title, String message, @Nullable Runnable action) {
        send(title, message, DEFAULT_DURATION, action);
    }

    /**
     * Send a notification to the user
     *
     * @param title    The title of the notification
     * @param message  The message of the notification
     * @param duration The duration the notification is on screen in ms
     */
    public void send(String title, String message, float duration) {
        send(title, message, duration, (Callable<Float>) null);
    }

    /**
     * Send a notification to the user
     *
     * @param title   The title of the notification
     * @param message The message of the notification
     * @param icon    The icon of the notification, null for none
     */
    public void send(String title, String message, @Nullable Icon icon) {
        send(title, message, icon, (Callable<Float>) null);
    }

    /**
     * Send a notification to the user
     *
     * @param title   The title of the notification
     * @param message The message of the notification
     */
    public void send(String title, String message) {
        send(title, message, (Callable<Float>) null);
    }

    private float deltaTime = 0;

    @Subscribe
    private void onHudRender(HudRenderEvent event) {
        if (notifications.size() == 0) return;
        RenderManager.setupAndDraw((vg) -> {
            float desiredPosition = -16f;
            float scale = OneConfigGui.getScaleFactor();
            for (Map.Entry<Notification, Animation> entry : notifications.entrySet()) {
                if (entry.getValue().getEnd() == -1f)
                    entry.setValue(new DummyAnimation(desiredPosition));
                else if (desiredPosition != entry.getValue().getEnd())
                    entry.setValue(new EaseInOutQuad(250, entry.getValue().get(0), desiredPosition, false));
                float height = entry.getKey().draw(vg, UResolution.getWindowHeight() / scale + entry.getValue().get(deltaTime), scale, deltaTime);
                desiredPosition -= height + 16f;
            }
            notifications.entrySet().removeIf(entry -> entry.getKey().isFinished());
        });
        deltaTime = 0;
    }

    @Subscribe
    private void onRenderEvent(RenderEvent event) {
        if (event.stage == Stage.START) {
            deltaTime += GuiUtils.getDeltaTime(); // add up deltatime since we might not render every frame because of hud caching
        }
    }
}