Skip to content

Commit

Permalink
themes are back
Browse files Browse the repository at this point in the history
  • Loading branch information
Hobbyshop committed Apr 18, 2024
1 parent 4443fda commit bbd0d0b
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 136 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/neptuneclient/voidui/VoidUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.neptuneclient.voidui

import com.neptuneclient.voidui.event.EventHandler
import com.neptuneclient.voidui.rendering.Renderer
import com.neptuneclient.voidui.theme.Theme
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand All @@ -17,7 +18,7 @@ class VoidUI
*
* @param theme The theme used to style UI elements.
*/
constructor(val renderer: Renderer, ) {
constructor(val renderer: Renderer, val theme: Theme) {

/**
* The event handler which handles all events in the instance.
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/framework/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.neptuneclient.voidui.framework
import com.neptuneclient.voidui.VoidUI
import com.neptuneclient.voidui.event.Event
import com.neptuneclient.voidui.rendering.Renderer
import com.neptuneclient.voidui.theme.Theme
import com.neptuneclient.voidui.widgets.Placeholder
import kotlin.properties.Delegates
import kotlin.reflect.KClass
Expand Down Expand Up @@ -51,6 +52,9 @@ abstract class Widget {

abstract class LeafWidget : Widget() {

protected val theme: Theme
get() = screen.voidUI.theme

override fun init(screen: Screen, parent: Widget) {
this.voidUI = screen.voidUI
this.screen = screen
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/objects/CornerRadius.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.neptuneclient.voidui.objects

data class CornerRadius(
val topLeft: Float,
val topRight: Float,
val bottomRight: Float,
val bottomLeft: Float,
) {

companion object {
fun all(value: Float) = CornerRadius(value, value, value, value)
}

fun isEmpty() = (topLeft + topRight + bottomLeft + bottomRight) == 0.0f

}
23 changes: 16 additions & 7 deletions src/main/kotlin/com/neptuneclient/voidui/rendering/Renderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.neptuneclient.voidui.rendering

import com.neptuneclient.voidui.framework.Offset
import com.neptuneclient.voidui.framework.Size
import com.neptuneclient.voidui.objects.CornerRadius
import com.neptuneclient.voidui.utils.Font
import com.neptuneclient.voidui.utils.Image
import com.neptuneclient.voidui.widgets.TextStyle
Expand Down Expand Up @@ -139,18 +140,18 @@ interface Renderer {
* @param thickness thickness of the frame
* @param color color of the rectangle
*/
fun roundedRectangleFrame(x: Float, y: Float, width: Float, height: Float, r0: Float, r1: Float, r2: Float, r3: Float, thickness: Float, color: Color)
fun roundedRectangleFrame(x: Float, y: Float, width: Float, height: Float, radius: CornerRadius, thickness: Float, color: Color)

fun roundedRectangleFrame(x: Int, y: Int, width: Int, height: Int, r0: Int, r1: Int, r2: Int, r3: Int, thickness: Int, color: Color) {
roundedRectangleFrame(x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), r0.toFloat(), r1.toFloat(), r2.toFloat(), r3.toFloat(), thickness.toFloat(), color)
fun roundedRectangleFrame(x: Int, y: Int, width: Int, height: Int, radius: CornerRadius, thickness: Int, color: Color) {
roundedRectangleFrame(x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), radius, thickness.toFloat(), color)
}

fun roundedRectangleFrame(x: Float, y: Float, width: Float, height: Float, radius: Float, thickness: Float, color: Color) {
roundedRectangleFrame(x, y, width, height, radius, radius, radius, radius, thickness, color)
roundedRectangleFrame(x, y, width, height, CornerRadius.all(radius), thickness, color)
}

fun roundedRectangleFrame(x: Int, y: Int, width: Int, height: Int, radius: Int, thickness: Int, color: Color) {
roundedRectangleFrame(x, y, width, height, radius, radius, radius, radius, thickness, color)
roundedRectangleFrame(x, y, width, height, CornerRadius.all(radius.toFloat()), thickness, color)
}

fun image(x: Float, y: Float, width: Float, height: Float, image: Image)
Expand All @@ -159,10 +160,18 @@ interface Renderer {
image(x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), image)
}

fun roundedImage(x: Float, y: Float, width: Float, height: Float, radius: Float, image: Image)
fun roundedImage(x: Float, y: Float, width: Float, height: Float, radius: CornerRadius, image: Image)

fun roundedImage(x: Int, y: Int, width: Int, height: Int, radius: CornerRadius, image: Image) {
roundedImage(x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), radius, image)
}

fun roundedImage(x: Float, y: Float, width: Float, height: Float, radius: Float, image: Image) {
roundedImage(x, y, width, height, CornerRadius.all(radius), image)
}

fun roundedImage(x: Int, y: Int, width: Int, height: Int, radius: Int, image: Image) {
roundedImage(x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), radius.toFloat(), image)
roundedImage(x, y, width, height, CornerRadius.all(radius.toFloat()), image)
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/theme/DefaultStyles.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.neptuneclient.voidui.theme

import com.neptuneclient.voidui.objects.CornerRadius
import com.neptuneclient.voidui.utils.Font
import java.awt.Color

data class DefaultStyles(
val regularText: TextStyle,
val smallText: TextStyle = regularText,
val mediumText: TextStyle = regularText,
val largeText: TextStyle = regularText,

val image: ImageStyle
)

class ImageStyle(
val cornerRadius: CornerRadius,
)

data class TextStyle(
val font: Font,
val color: Color,
val size: Int,
val letterSpacing: Float
)
4 changes: 2 additions & 2 deletions src/main/kotlin/com/neptuneclient/voidui/theme/Theme.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.neptuneclient.voidui.theme

class Theme(

abstract class Theme(
val defaultStyles: DefaultStyles
)
11 changes: 8 additions & 3 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/Image.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.neptuneclient.voidui.widgets

import com.neptuneclient.voidui.framework.*
import com.neptuneclient.voidui.objects.CornerRadius
import com.neptuneclient.voidui.rendering.Renderer

class Image(
private val src: com.neptuneclient.voidui.utils.Image,
private val imageSize: Size? = null,
private val borderRadius: Int? = null
private val cornerRadius: CornerRadius? = null
) : LeafWidget() {

private lateinit var radius: CornerRadius

override fun init(screen: Screen, parent: Widget) {
super.init(screen, parent)

radius = cornerRadius ?: theme.defaultStyles.image.cornerRadius
src.register(screen.voidUI.renderer)
}

Expand All @@ -20,8 +25,8 @@ class Image(
}

override fun render(renderer: Renderer) {
if (borderRadius != null)
renderer.roundedImage(offset.x, offset.y, size.width, size.height, borderRadius.toFloat(), src)
if (!radius.isEmpty())
renderer.roundedImage(offset.x, offset.y, size.width, size.height, radius, src)
else
renderer.image(offset.x, offset.y, size.width, size.height, src)
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/Text.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ package com.neptuneclient.voidui.widgets
import com.neptuneclient.voidui.framework.*
import com.neptuneclient.voidui.objects.TextAlign
import com.neptuneclient.voidui.rendering.Renderer
import com.neptuneclient.voidui.utils.Font
import java.awt.Color
import java.nio.file.Path
import com.neptuneclient.voidui.theme.TextStyle

class Text(
private val label: String = "",
private val align: TextAlign = TextAlign.START,
private val style: TextStyle = TextStyle(Font("name", Path.of("fonts/WorkSans-Regular.ttf")), Color.WHITE, 16, 0f),
private val textStyle: TextStyle? = null,
private val type: TextType = TextType.REGULAR
) : LeafWidget() {

private lateinit var style: TextStyle

override fun init(screen: Screen, parent: Widget) {
super.init(screen, parent)

style = textStyle ?: when (type) {
TextType.SMALL -> theme.defaultStyles.smallText
TextType.REGULAR -> theme.defaultStyles.regularText
TextType.MEDIUM -> theme.defaultStyles.mediumText
TextType.LARGE -> theme.defaultStyles.largeText
}
style.font.register(screen.voidUI.renderer)
}

Expand All @@ -34,4 +42,4 @@ class Text(
}
}

data class TextStyle(val font: Font, val color: Color, val size: Int, val letterSpacing: Float)
enum class TextType { SMALL, REGULAR, MEDIUM, LARGE }
12 changes: 3 additions & 9 deletions src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,20 @@ import com.neptuneclient.voidui.VoidUI
import com.neptuneclient.voidui.event.MouseClickedEvent
import com.neptuneclient.voidui.event.MouseReleasedEvent
import com.neptuneclient.voidui.framework.Screen
import com.neptuneclient.voidui.framework.Size
import com.neptuneclient.voidui.framework.Widget
import com.neptuneclient.voidui.utils.image
import com.neptuneclient.voidui.widgets.Image
import com.neptuneclient.voidui.widgets.*
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
import org.lwjgl.glfw.GLFW

class TestScreen(voidUI: VoidUI) : Screen(voidUI) {

override fun build(): Widget {
return Image(
src = image("images/hampter.png"),
imageSize = Size(100f, 100f),
borderRadius = 10,
)
return Text("Test")
}

}

val voidUI = VoidUI(TestRenderer())
val voidUI = VoidUI(TestRenderer(), TestTheme())

fun main() {
val screen = TestScreen(voidUI)
Expand Down
12 changes: 5 additions & 7 deletions src/test/kotlin/com/neptuneclient/voidui/tests/TestRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.neptuneclient.voidui.tests
import com.neptuneclient.voidui.rendering.Renderer
import com.neptuneclient.voidui.framework.Offset
import com.neptuneclient.voidui.framework.Size
import com.neptuneclient.voidui.objects.CornerRadius
import com.neptuneclient.voidui.utils.Font
import com.neptuneclient.voidui.utils.Image
import com.neptuneclient.voidui.widgets.TextStyle
Expand Down Expand Up @@ -203,17 +204,14 @@ class TestRenderer : Renderer {
y: Float,
width: Float,
height: Float,
r0: Float,
r1: Float,
r2: Float,
r3: Float,
radius: CornerRadius,
thickness: Float,
color: Color
) {
color.use {
NanoVG.nvgRGBAf(color.red / 255f, color.green / 255f, color.blue / 255f, color.alpha / 255f, it)
NanoVG.nvgBeginPath(vg)
NanoVG.nvgRoundedRectVarying(vg, x, y, width, height, r0, r1, r2, r3)
NanoVG.nvgRoundedRectVarying(vg, x, y, width, height, radius.topLeft, radius.topRight, radius.bottomRight, radius.bottomLeft)
NanoVG.nvgStrokeColor(vg, it)
NanoVG.nvgStrokeWidth(vg, thickness)
NanoVG.nvgStroke(vg)
Expand All @@ -234,13 +232,13 @@ class TestRenderer : Renderer {
paint.free()
}

override fun roundedImage(x: Float, y: Float, width: Float, height: Float, radius: Float, image: Image) {
override fun roundedImage(x: Float, y: Float, width: Float, height: Float, radius: CornerRadius, image: Image) {
if (image.id == null)
throw IllegalStateException("Image was not registered properly!")

val paint = NanoVG.nvgImagePattern(vg, x, y, width, height, 0f, image.id!!, 1.0F, NVGPaint.calloc())
NanoVG.nvgBeginPath(vg)
NanoVG.nvgRoundedRect(vg, x, y, width, height, radius)
NanoVG.nvgRoundedRectVarying(vg, x, y, width, height, radius.topLeft, radius.topRight, radius.bottomRight, radius.bottomLeft)
NanoVG.nvgFillPaint(vg, paint)
NanoVG.nvgFill(vg)
NanoVG.nvgClosePath(vg)
Expand Down
Loading

0 comments on commit bbd0d0b

Please sign in to comment.