diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-08-24 22:51:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-24 22:51:58 +0200 |
commit | daed9569a0f1d2231c4f824f6cffd72d5f8ae8bd (patch) | |
tree | 6efee702c616d96e40a3e69ce89425f0936a7598 /src/main/java/cc/polyfrost/oneconfig/internal/utils/Notification.java | |
parent | a76b49be6dbeb0be3f88870e33d3e10e0e7f8e1c (diff) | |
download | OneConfig-daed9569a0f1d2231c4f824f6cffd72d5f8ae8bd.tar.gz OneConfig-daed9569a0f1d2231c4f824f6cffd72d5f8ae8bd.tar.bz2 OneConfig-daed9569a0f1d2231c4f824f6cffd72d5f8ae8bd.zip |
Notifs (#111)
* Start on notifications
* Finish notifications (pog)
* oop
* internalizing
* misc: set default duration to 4000ms
* Scaling notifs stuff
Co-authored-by: Wyvest <45589059+Wyvest@users.noreply.github.com>
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/internal/utils/Notification.java')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/internal/utils/Notification.java | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/utils/Notification.java b/src/main/java/cc/polyfrost/oneconfig/internal/utils/Notification.java new file mode 100644 index 0000000..e2c0b05 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/internal/utils/Notification.java @@ -0,0 +1,118 @@ +/* + * 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.internal.utils; + +import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.gui.animations.*; +import cc.polyfrost.oneconfig.internal.assets.Colors; +import cc.polyfrost.oneconfig.libs.universal.UResolution; +import cc.polyfrost.oneconfig.renderer.Icon; +import cc.polyfrost.oneconfig.renderer.RenderManager; +import cc.polyfrost.oneconfig.renderer.font.Fonts; +import cc.polyfrost.oneconfig.renderer.scissor.Scissor; +import cc.polyfrost.oneconfig.renderer.scissor.ScissorManager; +import cc.polyfrost.oneconfig.utils.InputHandler; +import cc.polyfrost.oneconfig.utils.MathUtils; +import cc.polyfrost.oneconfig.utils.color.ColorPalette; +import cc.polyfrost.oneconfig.utils.gui.GuiUtils; +import org.jetbrains.annotations.Nullable; + +import java.util.concurrent.Callable; + +public final class Notification { + private final String title; + private final String message; + private final Icon icon; + private final Animation animation; + private final Callable<Float> progressBar; + private final Runnable action; + private final InputHandler inputHandler = new InputHandler(); + private final ColorAnimation bgColor = new ColorAnimation(new ColorPalette(Colors.GRAY_800, Colors.GRAY_700, Colors.GRAY_900)); + private final ColorAnimation titleColor = new ColorAnimation(new ColorPalette(Colors.WHITE_80, Colors.WHITE, Colors.WHITE)); + private final ColorAnimation messageColor = new ColorAnimation(new ColorPalette(Colors.WHITE_60, Colors.WHITE_90, Colors.WHITE_90)); + private boolean hovered = false; + + public Notification(String title, String message, @Nullable Icon icon, float duration, @Nullable Callable<Float> progressBar, @Nullable Runnable action) { + this.title = title; + this.message = message; + this.icon = icon; + this.animation = new ChainedAnimation( + new EaseInOutQuad(250, 0, 330, false), + progressBar == null ? new DummyAnimation(330, duration) : new DummyAnimation(330, () -> progressBar.call() >= 1f), + new EaseInOutQuad(250, 330, 0, false) + ); + this.progressBar = progressBar; + this.action = action; + } + + public float draw(final long vg, float y, float scale) { + RenderManager.scale(vg, scale, scale); + inputHandler.scale(scale, scale); + float x = (UResolution.getWindowWidth() / scale - animation.get(hovered ? 0 : GuiUtils.getDeltaTime())); + float textX = icon == null ? x + 16 : x + 64; + float textMaxLength = icon == null ? 268 : 220; + float messageHeight = RenderManager.getWrappedStringHeight(vg, message, textMaxLength, 12f, 1.75f, Fonts.REGULAR); + float height = getHeight(messageHeight); + y -= height; + hovered = inputHandler.isAreaHovered(x, y, 314, height); + if (hovered && inputHandler.isClicked() && action != null) action.run(); + int bgColor = this.bgColor.getColor(hovered, hovered && inputHandler.isMouseDown()); + int titleColor = this.titleColor.getColor(hovered, hovered && inputHandler.isMouseDown()); + int messageColor = this.messageColor.getColor(hovered, hovered && inputHandler.isMouseDown()); + RenderManager.drawRoundedRect(vg, x, y, 314f, height, bgColor, 8f); + if (icon != null) + icon.draw(vg, x + 16f, y + (height - (progressBar == null ? 0f : 5f)) / 2f - 16f, 32f, 32f, titleColor, scale); + RenderManager.drawText(vg, title, textX, y + 30, titleColor, 16f, Fonts.SEMIBOLD); + RenderManager.drawWrappedString(vg, message, textX, y + 46, textMaxLength, messageColor, 12f, 1.75f, Fonts.REGULAR); + if (progressBar != null) { + try { + float progress = MathUtils.clamp(progressBar.call()); + Scissor scissor1 = ScissorManager.scissor(vg, x + 314f * progress, y + height - 5f, 314f * (1 - progress), 5f); + RenderManager.drawRoundedRect(vg, x, y, 314f, height, Colors.PRIMARY_800, 8f); + ScissorManager.resetScissor(vg, scissor1); + Scissor scissor2 = ScissorManager.scissor(vg, x, y + height - 5f, 314f * progress - (314f * progress < 2.5f || 311.5f * progress > 2.5f ? 0f : 2.5f), 5f); + RenderManager.drawRoundedRect(vg, x, y, 314f, height, Colors.PRIMARY_500, 8f); + ScissorManager.resetScissor(vg, scissor2); + if (314f * progress >= 2.5f && 311.5f * progress <= 2.5f) + RenderManager.drawRoundedRect(vg, x + 2.5f, y + height - 5f, Math.max(0, 314f * progress - 5f), 5f, Colors.PRIMARY_500, 2.5f); + } catch (Exception ignored) { + } + } + RenderManager.resetTransform(vg); + return height; + } + + private float getHeight(float messageHeight) { + float height = 68 + messageHeight; + if (progressBar != null) height += 5f; + return height; + } + + public boolean isFinished() { + return animation.isFinished(); + } +} |