Skip to content

Commit

Permalink
gradients
Browse files Browse the repository at this point in the history
  • Loading branch information
twenty48lol committed Dec 10, 2024
1 parent a7e2ec0 commit e72de24
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
97 changes: 96 additions & 1 deletion src/main/kotlin/dev/blend/util/render/DrawUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dev.blend.util.IAccessor
import dev.blend.util.misc.MiscUtil
import kotlinx.io.IOException
import org.lwjgl.nanovg.NVGColor
import org.lwjgl.nanovg.NVGPaint
import org.lwjgl.nanovg.NanoVG.*
import org.lwjgl.nanovg.NanoVGGL3
import org.lwjgl.opengl.GL11
Expand Down Expand Up @@ -92,7 +93,7 @@ object DrawUtil: IAccessor {
nvgClosePath(context)
}
@JvmStatic
fun rectOutline(x: Number, y: Number, width: Number, height: Number, stroke: Number, color: Color, alignment: Alignment = Alignment.TOP_LEFT) {
fun rect(x: Number, y: Number, width: Number, height: Number, stroke: Number, color: Color, alignment: Alignment = Alignment.TOP_LEFT) {
nvgBeginPath(context)
NVGColor.calloc().use { nvgColor ->
nvgRect(context, alignX(x, width, alignment), alignY(y, height, alignment), width.toFloat(), height.toFloat())
Expand All @@ -103,6 +104,41 @@ object DrawUtil: IAccessor {
}
nvgClosePath(context)
}
@JvmStatic
fun rect(x: Number, y: Number, width: Number, height: Number, gradient: Gradient, alignment: Alignment = Alignment.TOP_LEFT) {
nvgBeginPath(context)
NVGPaint.calloc().use { nvgPaint ->
nvgRect(context, alignX(x, width, alignment), alignY(y, height, alignment), width.toFloat(), height.toFloat())
NVGColor.calloc().use { primary ->
NVGColor.calloc().use { secondary ->
nvgRGBAf(gradient.primary.red / 255f, gradient.primary.green / 255f, gradient.primary.blue / 255f, gradient.primary.alpha / 255f, primary)
nvgRGBAf(gradient.secondary.red / 255f, gradient.secondary.green / 255f, gradient.secondary.blue / 255f, gradient.secondary.alpha / 255f, secondary)
nvgLinearGradient(context, gradient.origin.x.toFloat(), gradient.origin.y.toFloat(), gradient.end.x.toFloat(), gradient.end.y.toFloat(), primary, secondary, nvgPaint)
}
}
nvgFillPaint(context, nvgPaint)
nvgFill(context)
}
nvgClosePath(context)
}
@JvmStatic
fun rect(x: Number, y: Number, width: Number, height: Number, stroke: Number, gradient: Gradient, alignment: Alignment = Alignment.TOP_LEFT) {
nvgBeginPath(context)
NVGPaint.calloc().use { nvgPaint ->
nvgRect(context, alignX(x, width, alignment), alignY(y, height, alignment), width.toFloat(), height.toFloat())
NVGColor.calloc().use { primary ->
NVGColor.calloc().use { secondary ->
nvgRGBAf(gradient.primary.red / 255f, gradient.primary.green / 255f, gradient.primary.blue / 255f, gradient.primary.alpha / 255f, primary)
nvgRGBAf(gradient.secondary.red / 255f, gradient.secondary.green / 255f, gradient.secondary.blue / 255f, gradient.secondary.alpha / 255f, secondary)
nvgLinearGradient(context, gradient.origin.x.toFloat(), gradient.origin.y.toFloat(), gradient.end.x.toFloat(), gradient.end.y.toFloat(), primary, secondary, nvgPaint)
}
}
nvgStrokeWidth(context, stroke.toFloat())
nvgStrokePaint(context, nvgPaint)
nvgStroke(context)
}
nvgClosePath(context)
}

@JvmStatic
fun roundedRect(x: Number, y: Number, width: Number, height: Number, radius: Number, color: Color, alignment: Alignment = Alignment.TOP_LEFT) {
Expand All @@ -116,6 +152,23 @@ object DrawUtil: IAccessor {
nvgClosePath(context)
}
@JvmStatic
fun roundedRect(x: Number, y: Number, width: Number, height: Number, radius: Number, gradient: Gradient, alignment: Alignment = Alignment.TOP_LEFT) {
nvgBeginPath(context)
NVGPaint.calloc().use { nvgPaint ->
nvgRoundedRect(context, alignX(x, width, alignment), alignY(y, height, alignment), width.toFloat(), height.toFloat(), radius.toFloat())
NVGColor.calloc().use { primary ->
NVGColor.calloc().use { secondary ->
nvgRGBAf(gradient.primary.red / 255f, gradient.primary.green / 255f, gradient.primary.blue / 255f, gradient.primary.alpha / 255f, primary)
nvgRGBAf(gradient.secondary.red / 255f, gradient.secondary.green / 255f, gradient.secondary.blue / 255f, gradient.secondary.alpha / 255f, secondary)
nvgLinearGradient(context, gradient.origin.x.toFloat(), gradient.origin.y.toFloat(), gradient.end.x.toFloat(), gradient.end.y.toFloat(), primary, secondary, nvgPaint)
}
}
nvgFillPaint(context, nvgPaint)
nvgFill(context)
}
nvgClosePath(context)
}
@JvmStatic
fun roundedRect(x: Number, y: Number, width: Number, height: Number, radius: Number, stroke: Number, color: Color, alignment: Alignment = Alignment.TOP_LEFT) {
nvgBeginPath(context)
NVGColor.calloc().use { nvgColor ->
Expand All @@ -128,6 +181,24 @@ object DrawUtil: IAccessor {
nvgClosePath(context)
}
@JvmStatic
fun roundedRect(x: Number, y: Number, width: Number, height: Number, radius: Number, stroke: Number, gradient: Gradient, alignment: Alignment = Alignment.TOP_LEFT) {
nvgBeginPath(context)
NVGPaint.calloc().use { nvgPaint ->
nvgRoundedRect(context, alignX(x, width, alignment), alignY(y, height, alignment), width.toFloat(), height.toFloat(), radius.toFloat())
NVGColor.calloc().use { primary ->
NVGColor.calloc().use { secondary ->
nvgRGBAf(gradient.primary.red / 255f, gradient.primary.green / 255f, gradient.primary.blue / 255f, gradient.primary.alpha / 255f, primary)
nvgRGBAf(gradient.secondary.red / 255f, gradient.secondary.green / 255f, gradient.secondary.blue / 255f, gradient.secondary.alpha / 255f, secondary)
nvgLinearGradient(context, gradient.origin.x.toFloat(), gradient.origin.y.toFloat(), gradient.end.x.toFloat(), gradient.end.y.toFloat(), primary, secondary, nvgPaint)
}
}
nvgStrokeWidth(context, stroke.toFloat())
nvgStrokePaint(context, nvgPaint)
nvgStroke(context)
}
nvgClosePath(context)
}
@JvmStatic
fun roundedRect(x: Number, y: Number, width: Number, height: Number, radius: DoubleArray, color: Color, alignment: Alignment = Alignment.TOP_LEFT) {
if (radius.size != 4) {
throw IllegalArgumentException("DoubleArray of size 4 required. only ${radius.size} found.")
Expand Down Expand Up @@ -160,6 +231,30 @@ object DrawUtil: IAccessor {
}
nvgClosePath(context)
}

@JvmStatic
fun drawString(text: String?, x: Number, y: Number, size: Number, gradient: Gradient, alignment: Alignment = Alignment.TOP_LEFT) {
drawString("regular", text!!, x, y, size, gradient, alignment)
}
@JvmStatic
fun drawString(font: String, text: String, x: Number, y: Number, size: Number, gradient: Gradient, alignment: Alignment = Alignment.TOP_LEFT) {
nvgBeginPath(context)
NVGPaint.calloc().use { nvgPaint ->
NVGColor.calloc().use { primary ->
NVGColor.calloc().use { secondary ->
nvgRGBAf(gradient.primary.red / 255f, gradient.primary.green / 255f, gradient.primary.blue / 255f, gradient.primary.alpha / 255f, primary)
nvgRGBAf(gradient.secondary.red / 255f, gradient.secondary.green / 255f, gradient.secondary.blue / 255f, gradient.secondary.alpha / 255f, secondary)
nvgLinearGradient(context, gradient.origin.x.toFloat(), gradient.origin.y.toFloat(), gradient.end.x.toFloat(), gradient.end.y.toFloat(), primary, secondary, nvgPaint)
}
}
nvgFillPaint(context, nvgPaint)
nvgFontFace(context, font)
nvgFontSize(context, size.toFloat())
nvgTextAlign(context, getAlignFlags(alignment))
nvgText(context, x.toFloat(), y.toFloat(), text)
}
nvgClosePath(context)
}
@JvmStatic
fun getStringWidth(font: String, text: String, size: Number): Double {
var width: Float
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/dev/blend/util/render/Gradient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.blend.util.render

import dev.blend.util.IAccessor
import net.minecraft.client.MinecraftClient
import java.awt.Color

class Gradient(
val primary: Color,
val secondary: Color,
var origin: Point = Point(0, 0),
var end: Point = Point(MinecraftClient.getInstance().window.width, MinecraftClient.getInstance().window.height)
): IAccessor

class Point(val x: Number, val y: Number)

0 comments on commit e72de24

Please sign in to comment.