Skip to content

Commit

Permalink
Add text element
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon3244 committed Mar 11, 2024
1 parent 06835dd commit 196ee7f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/rendering/Renderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@ interface Renderer {
roundedRectangle(x.toFloat(), y.toFloat(), width.toFloat(), height.toFloat(), radius.toFloat(), color)
}

/**
* Renders a text with the given dimensions, size, font and color.
*
* @param x x coordinate of the text
* @param y y coordinate of the text
* @param text text to render
* @param font font of the text
* @param color color of the text
*/
fun text(x: Float, y: Float, text: String, font: Font, color: Color)

}
12 changes: 12 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/ui/elements/Text.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.neptuneclient.voidui.ui.elements

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

class Text(var text: String, var font: Font, var color: Color, var x: Float, var y: Float): Element() {
override fun render() {
draw {
text(x, y, text, font, color)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class MockRenderer: Renderer {
}

override fun roundedRectangle(x: Float, y: Float, width: Float, height: Float, radius: Float, color: Color) {
TODO("Not yet implemented")
println("roundedRectangle")
}

override fun text(x: Float, y: Float, text: String, font: Font, color: Color) {
println("text")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class TestRenderer : Renderer {
}
}

fun text(x: Float, y: Float, text: String, font: Font, color: Color) {
override fun text(x: Float, y: Float, text: String, font: Font, color: Color) {
color.use {
NanoVG.nvgRGBAf(color.red / 255f, color.green / 255f, color.blue / 255f, color.alpha / 255f, it)
NanoVG.nvgBeginPath(vg)
Expand Down
9 changes: 6 additions & 3 deletions src/test/kotlin/com/neptuneclient/voidui/tests/tests.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package com.neptuneclient.voidui.tests

import com.neptuneclient.voidui.VoidUI
import com.neptuneclient.voidui.ui.elements.Text
import com.neptuneclient.voidui.utils.Font
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
import org.lwjgl.glfw.GLFW
import java.awt.Color
import java.nio.file.Path

@DisabledIfEnvironmentVariable(named = "CI", matches = "\\w+")
class FontTest() {

val void = VoidUI(TestRenderer())

@Test
// Disable if running in CI
@DisabledIfEnvironmentVariable(named = "CI", matches = "\\w+")
fun byteBufferTest() {
val font = Font(void, "test", Path.of("fonts/WorkSans-Regular.ttf"), 30)
println(font.data.toString())

val text = Text("Hello, World!", font, Color.WHITE, 100f, 100f)
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.text(100f, 100f, "Hello, World!", font, Color.WHITE)
text.render()
renderer.endFrame()
}

Expand Down

0 comments on commit 196ee7f

Please sign in to comment.