aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/cc/polyfrost/oneconfig/utils/dsl/RenderManagerDSL.kt
blob: ac6ff9c4185660d810c7f956da9d0058388586cc (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * 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.dsl

import cc.polyfrost.oneconfig.config.data.InfoType
import cc.polyfrost.oneconfig.renderer.RenderManager
import cc.polyfrost.oneconfig.renderer.font.Font
import cc.polyfrost.oneconfig.renderer.Image
import cc.polyfrost.oneconfig.renderer.SVG
import cc.polyfrost.oneconfig.utils.InputHandler
import cc.polyfrost.oneconfig.utils.color.ColorUtils
import org.lwjgl.nanovg.NVGColor

/**
 * Wrapper for a NanoVG instance.
 * @see nanoVG
 * @see RenderManager
 */
data class VG(val instance: Long)

/**
 * Sets up rendering, calls the block with the NanoVG instance, and then cleans up.
 *
 * To start, call this method.
 * ```kt
 * nanoVG {
 *     // Do stuff with the NanoVG instance
 * }
 * ```
 * From there, you can use the passed [VG] object to draw things. For example...
 * ```kt
 * nanoVG {
 *     drawRect(1, 1, 100, 100, Color.RED.rgb)
 *     drawText("Hello, world!", 10, 10, Color.BLACK.rgb, 9, Fonts.BOLD)
 * }
 * ```
 * You can also set the [mcScaling] parameter to true to scale the NanoVG instance to match the Minecraft GUI scale.
 *
 * @param mcScaling Whether to scale the NanoVG instance to match the Minecraft GUI scale.
 * @param block The block to run.
 */
fun nanoVG(mcScaling: Boolean = false, block: VG.() -> Unit) = RenderManager.setupAndDraw(mcScaling) {
    block.invoke(
        VG(it)
    )
}

fun nanoVG(context: Long, block: VG.() -> Unit) = block.invoke(VG(context))


fun Long.drawRect(x: Number, y: Number, width: Number, height: Number, color: Int, bypassOneConfig: Boolean = false) =
    if (bypassOneConfig) {
        RenderManager.drawRect(this, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color)
    } else {
        RenderManager.drawRect(this, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color)
    }

fun VG.drawRect(x: Number, y: Number, width: Number, height: Number, color: Int, bypassOneConfig: Boolean = false) =
    instance.drawRect(x, y, width, height, color, bypassOneConfig)

fun Long.drawRoundedRect(x: Number, y: Number, width: Number, height: Number, radius: Number, color: Int) =
    RenderManager.drawRoundedRect(
        this, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color, radius.toFloat()
    )

fun VG.drawRoundedRect(x: Number, y: Number, width: Number, height: Number, radius: Number, color: Int) =
    instance.drawRoundedRect(x, y, width, height, radius, color)

fun Long.drawHollowRoundedRect(
    x: Number, y: Number, width: Number, height: Number, radius: Number, color: Int, thickness: Number
) = RenderManager.drawHollowRoundRect(
    this, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color, radius.toFloat(), thickness.toFloat()
)

fun VG.drawHollowRoundedRect(
    x: Number, y: Number, width: Number, height: Number, radius: Number, color: Int, thickness: Number
) = instance.drawHollowRoundedRect(x, y, width, height, radius, color, thickness)

fun Long.drawRoundedRectVaried(
    x: Number,
    y: Number,
    width: Number,
    height: Number,
    color: Int,
    radiusTL: Number,
    radiusTR: Number,
    radiusBR: Number,
    radiusBL: Number
) = RenderManager.drawRoundedRectVaried(
    this,
    x.toFloat(),
    y.toFloat(),
    width.toFloat(),
    height.toFloat(),
    color,
    radiusTL.toFloat(),
    radiusTR.toFloat(),
    radiusBR.toFloat(),
    radiusBL.toFloat()
)

fun VG.drawRoundedRectVaried(
    x: Number,
    y: Number,
    width: Number,
    height: Number,
    color: Int,
    radiusTL: Number,
    radiusTR: Number,
    radiusBR: Number,
    radiusBL: Number
) = instance.drawRoundedRectVaried(x, y, width, height, color, radiusTL, radiusTR, radiusBR, radiusBL)

fun Long.drawGradientRect(x: Number, y: Number, width: Number, height: Number, color1: Int, color2: Int) =
    RenderManager.drawGradientRect(this, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color1, color2)

fun VG.drawGradientRect(x: Number, y: Number, width: Number, height: Number, color1: Int, color2: Int) =
    instance.drawGradientRect(x, y, width, height, color1, color2)

fun Long.drawGradientRoundedRect(
    x: Number, y: Number, width: Number, height: Number, color: Int, color2: Int, radius: Number
) = RenderManager.drawGradientRoundedRect(
    this, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color, color2, radius.toFloat()
)

fun VG.drawGradientRoundedRect(
    x: Number, y: Number, width: Number, height: Number, color: Int, color2: Int, radius: Number
) = instance.drawGradientRoundedRect(x, y, width, height, color, color2, radius)


fun Long.drawCircle(x: Number, y: Number, radius: Number, color: Int) =
    RenderManager.drawCircle(this, x.toFloat(), y.toFloat(), radius.toFloat(), color)

fun VG.drawCircle(x: Number, y: Number, radius: Number, color: Int) = instance.drawCircle(x, y, radius, color)


fun Long.drawText(text: String, x: Number, y: Number, color: Int, size: Number, font: Font) =
    RenderManager.drawText(this, text, x.toFloat(), y.toFloat(), color, size.toFloat(), font)

fun VG.drawText(text: String, x: Number, y: Number, color: Int, size: Number, font: Font) =
    instance.drawText(text, x, y, color, size, font)

fun Long.drawWrappedString(text: String, x: Number, y: Number, width: Number, color: Int, size: Number, font: Font) =
    RenderManager.drawWrappedString(this, text, x.toFloat(), y.toFloat(), width.toFloat(), color, size.toFloat(), font)

fun VG.drawWrappedString(text: String, x: Number, y: Number, width: Number, color: Int, size: Number, font: Font) =
    instance.drawWrappedString(text, x, y, width, color, size, font)

fun Long.drawURL(url: String, x: Number, y: Number, size: Number, font: Font, inputHandler: InputHandler) =
    RenderManager.drawURL(this, url, x.toFloat(), y.toFloat(), size.toFloat(), font, inputHandler)

fun VG.drawURL(url: String, x: Number, y: Number, size: Number, font: Font, inputHandler: InputHandler) =
    instance.drawURL(url, x, y, size, font, inputHandler)


fun Long.drawImage(filePath: String, x: Number, y: Number, width: Number, height: Number) =
    RenderManager.drawImage(this, filePath, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat())

fun VG.drawImage(filePath: String, x: Number, y: Number, width: Number, height: Number) =
    instance.drawImage(filePath, x, y, width, height)

fun Long.drawImage(filePath: String, x: Number, y: Number, width: Number, height: Number, color: Int) =
    RenderManager.drawImage(this, filePath, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color)

fun VG.drawImage(filePath: String, x: Number, y: Number, width: Number, height: Number, color: Int) =
    instance.drawImage(filePath, x, y, width, height, color)

fun Long.drawImage(image: Image, x: Number, y: Number, width: Number, height: Number) =
    RenderManager.drawImage(this, image, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat())

fun VG.drawImage(image: Image, x: Number, y: Number, width: Number, height: Number) =
    instance.drawImage(image, x, y, width, height)

fun Long.drawImage(image: Image, x: Number, y: Number, width: Number, height: Number, color: Int) =
    RenderManager.drawImage(this, image, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color)

fun VG.drawImage(image: Image, x: Number, y: Number, width: Number, height: Number, color: Int) =
    instance.drawImage(image, x, y, width, height, color)


fun Long.drawRoundedImage(filePath: String, x: Number, y: Number, width: Number, height: Number, radius: Number) =
    RenderManager.drawRoundImage(
        this, filePath, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), radius.toFloat()
    )

fun VG.drawRoundedImage(filePath: String, x: Number, y: Number, width: Number, height: Number, radius: Number) =
    instance.drawRoundedImage(filePath, x, y, width, height, radius)

fun Long.drawRoundedImage(image: Image, x: Number, y: Number, width: Number, height: Number, radius: Number) =
    RenderManager.drawRoundImage(
        this, image, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), radius.toFloat()
    )

fun VG.drawRoundedImage(image: Image, x: Number, y: Number, width: Number, height: Number, radius: Number) =
    instance.drawRoundedImage(image, x, y, width, height, radius)


fun Long.getTextWidth(text: String, size: Number, font: Font) =
    RenderManager.getTextWidth(this, text, size.toFloat(), font)

fun VG.getTextWidth(text: String, size: Number, font: Font) = instance.getTextWidth(text, size, font)


fun Long.drawLine(x1: Number, y1: Number, x2: Number, y2: Number, width: Number, color: Int) =
    RenderManager.drawLine(this, x1.toFloat(), y1.toFloat(), x2.toFloat(), y2.toFloat(), width.toFloat(), color)

fun VG.drawLine(x1: Number, y1: Number, x2: Number, y2: Number, width: Number, color: Int) =
    instance.drawLine(x1, y1, x2, y2, width, color)


fun Long.drawDropShadow(
    x: Number, y: Number, w: Number, h: Number, blur: Number, spread: Number, cornerRadius: Number
) = RenderManager.drawDropShadow(
    this, x.toFloat(), y.toFloat(), w.toFloat(), h.toFloat(), blur.toFloat(), spread.toFloat(), cornerRadius.toFloat()
)

fun VG.drawDropShadow(x: Number, y: Number, w: Number, h: Number, blur: Number, spread: Number, cornerRadius: Number) =
    instance.drawDropShadow(x, y, w, h, blur, spread, cornerRadius)


fun Long.newColor(r: Int, g: Int, b: Int, a: Int) = RenderManager.color(this, ColorUtils.getColor(r, g, b, a))
fun VG.newColor(r: Int, g: Int, b: Int, a: Int) = instance.newColor(r, g, b, a)

fun Long.newColor(color: Int) = RenderManager.color(this, color)
fun VG.newColor(color: Int) = instance.newColor(color)

fun NVGColor.fill(color: Int) = fill(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha())
fun NVGColor.fill(r: Int, g: Int, b: Int, a: Int) = RenderManager.fillNVGColorWithRGBA(
    r.toFloat() / 255f, g.toFloat() / 255f, b.toFloat() / 255f, a.toFloat() / 255f, this
)

fun NVGColor.fill(r: Float, g: Float, b: Float, a: Float) = RenderManager.fillNVGColorWithRGBA(r, g, b, a, this)


fun Long.scale(x: Float, y: Float) = RenderManager.scale(this, x, y)
fun VG.scale(x: Float, y: Float) = instance.scale(x, y)


fun Long.setAlpha(alpha: Float) = RenderManager.setAlpha(this, alpha)
fun VG.setAlpha(alpha: Float) = instance.setAlpha(alpha)


fun Long.drawSVG(filePath: String, x: Number, y: Number, width: Number, height: Number) =
    RenderManager.drawSvg(this, filePath, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat())

fun VG.drawSVG(filePath: String, x: Number, y: Number, width: Number, height: Number) =
    instance.drawSVG(filePath, x, y, width, height)

fun Long.drawSVG(filePath: String, x: Number, y: Number, width: Number, height: Number, color: Int) =
    RenderManager.drawSvg(this, filePath, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color)

fun VG.drawSVG(filePath: String, x: Number, y: Number, width: Number, height: Number, color: Int) =
    instance.drawSVG(filePath, x, y, width, height, color)

fun Long.drawSVG(svg: SVG, x: Number, y: Number, width: Number, height: Number) =
    RenderManager.drawSvg(this, svg, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat())

fun VG.drawSVG(svg: SVG, x: Number, y: Number, width: Number, height: Number) =
    instance.drawSVG(svg, x, y, width, height)

fun Long.drawSVG(svg: SVG, x: Number, y: Number, width: Number, height: Number, color: Int) =
    RenderManager.drawSvg(this, svg, x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), color)

fun VG.drawSVG(svg: SVG, x: Number, y: Number, width: Number, height: Number, color: Int) =
    instance.drawSVG(svg, x, y, width, height, color)


fun Long.drawInfo(type: InfoType, x: Number, y: Number, size: Number) =
    RenderManager.drawInfo(this, type, x.toFloat(), y.toFloat(), size.toFloat())

fun VG.drawInfo(type: InfoType, x: Number, y: Number, size: Number) = instance.drawInfo(type, x, y, size)