Skip to content

Commit

Permalink
Add support for different padding on all 4 sides
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon3244 committed Mar 11, 2024
1 parent c2d9b62 commit 8b5c9d3
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ interface Renderer {
text(x.toFloat(), y.toFloat(), text, font, color)
}

/**
* Returns the bounds of the given text with the given font.
*
* @param text text to measure
* @param font font of the text
* @return the width and height of the text
*/
fun getTextBounds(text: String, font: Font): Pair<Float, Float>

}
6 changes: 4 additions & 2 deletions src/main/kotlin/com/neptuneclient/voidui/ui/Component.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.neptuneclient.voidui.ui

import com.neptuneclient.voidui.ui.objects.EdgeInsets

/**
* Components are a small reusable chunk of drawables. Syntactically they are very similar to screens, but instead of being
* displayed on the screen, you can use them just like elements within other parts of your UI.
Expand All @@ -14,8 +16,8 @@ sealed class Component(
width: Int? = null,
height: Int? = null,

margin: Int? = null,
padding: Int? = null,
margin: EdgeInsets = EdgeInsets.zero,
padding: EdgeInsets = EdgeInsets.zero,

val children: MutableList<Drawable>? = null
) : Drawable(x, y, width, height, margin, padding) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/neptuneclient/voidui/ui/Drawable.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.neptuneclient.voidui.ui

import com.neptuneclient.voidui.VoidUI
import com.neptuneclient.voidui.ui.objects.EdgeInsets

/**
* The base class for anything which represents an object in the UI.
Expand All @@ -11,8 +12,8 @@ abstract class Drawable(
var width: Int? = null,
var height: Int? = null,

var margin: Int? = null,
var padding: Int? = null
var margin: EdgeInsets = EdgeInsets.zero,
var padding: EdgeInsets = EdgeInsets.zero
) {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.neptuneclient.voidui.ui

import com.neptuneclient.voidui.ui.objects.EdgeInsets
import kotlin.properties.Delegates

/**
Expand All @@ -11,8 +12,8 @@ abstract class ReactiveComponent(
width: Int? = null,
height: Int? = null,

margin: Int? = null,
padding: Int? = null,
margin: EdgeInsets = EdgeInsets.zero,
padding: EdgeInsets = EdgeInsets.zero,

children: MutableList<Drawable>? = null
) : Component(x, y, width, height, margin, padding, children) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.neptuneclient.voidui.ui

import com.neptuneclient.voidui.ui.objects.EdgeInsets

/**
* @see Component
*/
Expand All @@ -9,8 +11,8 @@ abstract class StaticComponent(
width: Int? = null,
height: Int? = null,

margin: Int? = null,
padding: Int? = null,
margin: EdgeInsets = EdgeInsets.zero,
padding: EdgeInsets = EdgeInsets.zero,

children: MutableList<Drawable>? = null
) : Component(x, y, width, height, margin, padding, children)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.neptuneclient.voidui.ui.elements

import com.neptuneclient.voidui.rendering.Renderer
import com.neptuneclient.voidui.ui.Drawable
import com.neptuneclient.voidui.ui.objects.EdgeInsets

/**
* Elements are the core building blocks of the library. They can not be created by the user and do not have children.
Expand All @@ -13,8 +14,8 @@ sealed class Element(
width: Int? = null,
height: Int? = null,

margin: Int? = null,
padding: Int? = null
margin: EdgeInsets = EdgeInsets.zero,
padding: EdgeInsets = EdgeInsets.zero
) : Drawable(x, y, width, height, margin, padding) {

abstract fun render()
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/com/neptuneclient/voidui/ui/elements/Text.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.neptuneclient.voidui.ui.elements

import com.neptuneclient.voidui.ui.objects.EdgeInsets
import com.neptuneclient.voidui.utils.Font
import java.awt.Color

class Text(
val text: String,
val font: Font,
val color: Color,
val backgroundColor: Color = Color(0, 0, 0, 0),

x: Int? = null,
y: Int? = null,

margin: Int? = null
) : Element(x, y, null, null, margin) {
margin: EdgeInsets = EdgeInsets.zero,
padding: EdgeInsets = EdgeInsets.zero
) : Element(x, y, null, null, margin, padding) {
override fun render() {
draw {
val (width, height) = getTextBounds(text, font)
val rectX = x!! - padding.left
val rectY = y!! - height - padding.top
rectangle(rectX, rectY, padding.horizontal + width, padding.vertical + height, backgroundColor)
text(x!!, y!!, text, font, color)
rectangle(x!!, y!!, 1, 1, Color(255, 0, 0))
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/ui/objects/EdgeInsets.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.neptuneclient.voidui.ui.objects

data class EdgeInsets(
val left: Float,
val top: Float,
val right: Float,
val bottom: Float,
) {
companion object {
@JvmStatic
fun all(value: Float) = EdgeInsets(value, value, value, value)
@JvmStatic
fun only(
left: Float = 0f,
top: Float = 0f,
right: Float = 0f,
bottom: Float = 0f
) = EdgeInsets(left, top, right, bottom)
@JvmStatic
fun symmetric(
vertical: Float = 0f,
horizontal: Float = 0f
) = EdgeInsets(horizontal, vertical, horizontal, vertical)
val zero = EdgeInsets(0f, 0f, 0f, 0f)
}

val horizontal get() = left + right
val vertical get() = top + bottom

operator fun plus(other: EdgeInsets) = EdgeInsets(
left + other.left,
top + other.top,
right + other.right,
bottom + other.bottom
)

operator fun minus(other: EdgeInsets) = EdgeInsets(
left - other.left,
top - other.top,
right - other.right,
bottom - other.bottom
)

operator fun times(other: EdgeInsets) = EdgeInsets(
left * other.left,
top * other.top,
right * other.right,
bottom * other.bottom
)

operator fun div(other: EdgeInsets) = EdgeInsets(
left / other.left,
top / other.top,
right / other.right,
bottom / other.bottom
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ class MockRenderer: Renderer {
override fun text(x: Float, y: Float, text: String, font: Font, color: Color) {
println("text")
}

override fun getTextBounds(text: String, font: Font): Pair<Float, Float> {
println("getTextBounds")
return Pair(0f, 0f)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.neptuneclient.voidui.tests

import com.neptuneclient.voidui.rendering.Renderer
import com.neptuneclient.voidui.utils.Font
import org.lwjgl.BufferUtils
import org.lwjgl.glfw.GLFW
import org.lwjgl.nanovg.NVGColor
import org.lwjgl.nanovg.NanoVG
Expand All @@ -10,6 +11,7 @@ import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL11
import org.lwjgl.system.MemoryStack
import java.awt.Color
import java.nio.FloatBuffer
import kotlin.math.max

class TestRenderer : Renderer {
Expand Down Expand Up @@ -146,4 +148,8 @@ class TestRenderer : Renderer {
NanoVG.nvgClosePath(vg)
}
}

override fun getTextBounds(text: String, font: Font): Pair<Float, Float> {
TODO("Not yet implemented")
}
}
7 changes: 5 additions & 2 deletions src/test/kotlin/com/neptuneclient/voidui/tests/tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.neptuneclient.voidui.tests

import com.neptuneclient.voidui.VoidUI
import com.neptuneclient.voidui.ui.elements.Text
import com.neptuneclient.voidui.ui.objects.EdgeInsets
import com.neptuneclient.voidui.utils.Font
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
Expand All @@ -24,14 +25,16 @@ class FontTest() {
y = 100,
text = "Hello World!",
font = font,
color = Color.WHITE
color = Color.WHITE,
padding = EdgeInsets.all(50f),
backgroundColor = Color.BLUE,
)
text.void = void

val renderer = void.renderer as TestRenderer
while (!GLFW.glfwWindowShouldClose(renderer.window)) {
renderer.beginFrame()
renderer.roundedRectangle(0f, 0f, 100f, 100f, 20f, Color.RED)
// renderer.roundedRectangle(0f, 0f, 100f, 100f, 20f, Color.RED)
text.render()
renderer.endFrame()
}
Expand Down

0 comments on commit 8b5c9d3

Please sign in to comment.