aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java
blob: 2440bfc415bf03bc1b6d5ebc56895d33ae071655 (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
/*
 * 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.hud;

import cc.polyfrost.oneconfig.config.Config;
import cc.polyfrost.oneconfig.config.annotations.Switch;
import cc.polyfrost.oneconfig.gui.OneConfigGui;
import cc.polyfrost.oneconfig.libs.universal.UMatrixStack;
import cc.polyfrost.oneconfig.platform.Platform;

/**
 * Represents a HUD element in OneConfig.
 * A HUD element can be used to display useful information to the user, like FPS or CPS.
 * <p>
 * If you simply want to display text, extend {@link TextHud} or {@link SingleTextHud},
 * whichever applies to the use case. Then, override the required methods.
 * <p>
 * If you want to display something else, extend this class and override {@link Hud#getWidth(float, boolean)}, {@link Hud#getHeight(float, boolean)}, and {@link Hud#draw(UMatrixStack, float, float, float, boolean)} with the width, height, and the drawing code respectively.
 * </p>
 * <p>
 * It should also be noted that additional options to the HUD can be added simply by declaring them.
 * <pre>{@code
 *     public class TestHud extends SingleTextHud {
 *         @literal @Switch(
 *             name = "Additional Option"
 *         )
 *         public boolean additionalOption = true;
 *     }
 *     }</pre>
 * </p>
 * To register an element, add it to your OneConfig {@link Config}.
 * <pre>{@code
 *  *     public class YourConfig extends Config {
 *  *         @literal @HUD(
 *  *             name = "HUD Element"
 *  *         )
 *  *         public YourHudElement hudElement = new YourHudElement("Title");
 *  *     }
 *  *     }</pre>
 */
public abstract class Hud {
    protected boolean enabled;
    transient private Config config;
    public final Position position;
    protected float scale;

    /**
     * @param enabled If the hud is enabled
     * @param x       X-coordinate of hud on a 1080p display
     * @param y       Y-coordinate of hud on a 1080p display
     * @param scale   Scale of the hud
     */
    public Hud(boolean enabled, float x, float y, float scale) {
        this.enabled = enabled;
        this.scale = scale;
        position = new Position(x, y, getWidth(scale, true), getHeight(scale, true));
    }

    /**
     * @param enabled If the hud is enabled
     * @param x       X-coordinate of hud on a 1080p display
     * @param y       Y-coordinate of hud on a 1080p display
     */
    public Hud(boolean enabled, float x, float y) {
        this(enabled, x, y, 1);
    }

    /**
     * @param enabled If the hud is enabled
     */
    public Hud(boolean enabled) {
        this(enabled, 0, 0, 1);
    }

    public Hud() {
        this(false, 0, 0, 1);
    }

    /**
     * Function called when drawing the hud
     *
     * @param matrices The UMatrixStack used for rendering in higher versions
     * @param x        Top left x-coordinate of the hud
     * @param y        Top left y-coordinate of the hud
     * @param scale    Scale of the hud
     * @param example  If the HUD is being rendered in example form
     */
    protected abstract void draw(UMatrixStack matrices, float x, float y, float scale, boolean example);

    /**
     * @param scale   Scale of the hud
     * @param example If the HUD is being rendered in example form
     * @return The width of the hud
     */
    protected abstract float getWidth(float scale, boolean example);

    /**
     * @param scale   Scale of the hud
     * @param example If the HUD is being rendered in example form
     * @return The height of the hud
     */
    protected abstract float getHeight(float scale, boolean example);

    /**
     * Function to do things before rendering anything
     *
     * @param example If the HUD is being rendered in example form
     */
    protected void preRender(boolean example) {
    }

    /**
     * Draw the background, the hud and all childed huds, used by HudCore
     */
    public void drawAll(UMatrixStack matrices, boolean example) {
        if (!example && !shouldShow()) return;
        preRender(example);
        position.setSize(getWidth(scale, example), getHeight(scale, example));
        draw(matrices, position.getX(), position.getY(), scale, example);
    }

    protected boolean shouldShow() {
        if (!showInGuis && Platform.getGuiPlatform().getCurrentScreen() != null && !(Platform.getGuiPlatform().getCurrentScreen() instanceof OneConfigGui))
            return false;
        if (!showInChat && Platform.getGuiPlatform().isInChat()) return false;
        return showInDebug || !Platform.getGuiPlatform().isInDebug();
    }

    /**
     * @return If the hud is enabled
     */
    public boolean isEnabled() {
        return enabled && (config == null || config.enabled);
    }

    /**
     * Set the config to disable accordingly, intended for internal use
     *
     * @param config The config instance
     */
    public void setConfig(Config config) {
        this.config = config;
    }

    /**
     * @return The config of this HUD
     */
    public Config getConfig() {
        return this.config;
    }

    /**
     * @return The scale of the Hud
     */
    public float getScale() {
        return scale;
    }

    /**
     * Set a new scale value
     *
     * @param scale   The new scale
     * @param example If the HUD is being rendered in example form
     */
    public void setScale(float scale, boolean example) {
        this.scale = scale;
        position.updateSizePosition(getWidth(scale, example), getHeight(scale, example));
    }

    @Switch(
            name = "Show in Chat"
    )
    public boolean showInChat = true;

    @Switch(
            name = "Show in F3 (Debug)"
    )
    public boolean showInDebug = false;

    @Switch(
            name = "Show in GUIs"
    )
    public boolean showInGuis = true;
}