aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/api/OptionDescription.java
blob: fce7e2f01695b466ace806acb46562bf5f18c364 (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
package dev.isxander.yacl3.api;

import dev.isxander.yacl3.gui.image.ImageRenderer;
import dev.isxander.yacl3.impl.OptionDescriptionImpl;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

/**
 * Provides all information for the description panel in the GUI.
 * This provides no functional benefit, and is purely for UX.
 */
public interface OptionDescription {
    /**
     * The description of the option, this is automatically wrapped and supports all styling,
     * including {@link net.minecraft.network.chat.ClickEvent}s and {@link net.minecraft.network.chat.HoverEvent}s.
     * @return The description of the option, with all lines merged with \n.
     */
    Component text();

    /**
     * The image to display with the description. If the Optional is empty, no image has been provided.
     * Usually, the image renderers are constructed asynchronously, so this method returns a {@link CompletableFuture}.
     * <p>
     * Image renderers are cached throughout the whole lifecycle of the game, and should not be generated more than once
     * per image. See {@link ImageRenderer#getOrMakeAsync(ResourceLocation, Supplier)} for implementation details.
     */
    CompletableFuture<Optional<ImageRenderer>> image();

    /**
     * @return a new builder for an {@link OptionDescription}.
     */
    static Builder createBuilder() {
        return new OptionDescriptionImpl.BuilderImpl();
    }

    static OptionDescription of(Component... description) {
        return createBuilder().text(description).build();
    }

    OptionDescription EMPTY = new OptionDescriptionImpl(CommonComponents.EMPTY, CompletableFuture.completedFuture(Optional.empty()));

    interface Builder {
        /**
         * Appends lines to the main description of the option. This can be called multiple times.
         * On {@link Builder#build()}, the lines are merged with \n.
         * @see OptionDescription#text()
         *
         * @param description the lines to append to the description.
         * @return this builder
         */
        Builder text(Component... description);

        /**
         * Appends lines to the main description of the option. This can be called multiple times.
         * On {@link Builder#build()}, the lines are merged with \n.
         * @see OptionDescription#text()
         *
         * @param lines the lines to append to the description.
         * @return this builder
         */
        Builder text(Collection<? extends Component> lines);

        /**
         * Sets a static image to display with the description. This is backed by a regular minecraft resource
         * in your mod's /assets folder.
         *
         * @param image the location of the image to display from the resource manager
         * @param width the width of the texture
         * @param height the height of the texture
         * @return this builder
         */
        Builder image(ResourceLocation image, int width, int height);

        /**
         * Sets a static image to display with the description. This is backed by a regular minecraft resource
         * in your mod's /assets folder. This overload method allows you to specify a subsection of the texture to render.
         *
         * @param image the location of the image to display from the resource manager
         * @param u the u coordinate
         * @param v the v coordinate
         * @param width the width of the subsection
         * @param height the height of the subsection
         * @param textureWidth the width of the whole texture file
         * @param textureHeight the height of whole texture file
         * @return this builder
         */
        Builder image(ResourceLocation image, float u, float v, int width, int height, int textureWidth, int textureHeight);

        /**
         * Sets a static image to display with the description. This is backed by a file on disk.
         * The width and height is automatically determined from the image processing.
         *
         * @param path the absolute path to the image file
         * @param uniqueLocation the unique identifier for the image, used for caching and resource manager registrar
         * @return this builder
         */
        Builder image(Path path, ResourceLocation uniqueLocation);

        /**
         * Sets a static OR ANIMATED webP image to display with the description. This is backed by a regular minecraft resource
         * in your mod's /assets folder.
         *
         * @param image the location of the image to display from the resource manager
         * @return this builder
         */
        Builder webpImage(ResourceLocation image);

        /**
         * Sets a static OR ANIMATED webP image to display with the description. This is backed by a file on disk.
         * The width and height is automatically determined from the image processing.
         *
         * @param path the absolute path to the image file
         * @param uniqueLocation the unique identifier for the image, used for caching and resource manager registrar
         * @return this builder
         */
        Builder webpImage(Path path, ResourceLocation uniqueLocation);

        /**
         * Sets a custom image renderer to display with the description.
         * This is useful for rendering other abstract things relevant to your mod.
         * <p>
         * However, <strong>THIS IS NOT API SAFE!</strong> As part of the gui package, things
         * may change that could break compatibility with future versions of YACL.
         * A helpful utility (that is also not API safe) is {@link dev.isxander.yacl3.gui.image.ImageRendererManager#registerOrGetImage(ResourceLocation, Supplier)}
         * which will cache the image renderer for the whole game lifecycle and construct it asynchronously to the render thread.
         * @param image the image renderer to display
         * @return this builder
         */
        Builder customImage(CompletableFuture<Optional<ImageRenderer>> image);

        /**
         * Sets a custom image renderer to display with the description.
         * This is useful for rendering other abstract things relevant to your mod.
         * <p>
         * However, <strong>THIS IS NOT API SAFE!</strong> As part of the gui package, things
         * may change that could break compatibility with future versions of YACL.
         * A helpful utility (that is also not API safe) is {@link dev.isxander.yacl3.gui.image.ImageRendererManager#registerOrGetImage(ResourceLocation, Supplier)}
         * which will cache the image renderer for the whole game lifecycle and construct it asynchronously to the render thread.
         * @param image the image renderer to display
         * @return this builder
         */
        default Builder customImage(ImageRenderer image) {
            return this.customImage(CompletableFuture.completedFuture(Optional.of(image)));
        }

        /**
         * Sets an animated GIF image to display with the description. This is backed by a regular minecraft resource
         * in your mod's /assets folder.
         *
         * @param image the location of the image to display from the resource manager
         * @return this builder
         */
        @Deprecated
        Builder gifImage(ResourceLocation image);

        /**
         * Sets an animated GIF image to display with the description. This is backed by a file on disk.
         * The width and height is automatically determined from the image processing.
         *
         * @param path the absolute path to the image file
         * @param uniqueLocation the unique identifier for the image, used for caching and resource manager registrar
         * @return this builder
         */
        @Deprecated
        Builder gifImage(Path path, ResourceLocation uniqueLocation);

        OptionDescription build();
    }
}