Skip to content

Commit

Permalink
temp state handling and fixed jvm segfault 🙏
Browse files Browse the repository at this point in the history
  • Loading branch information
Hobbyshop committed May 22, 2024
1 parent 458ae8d commit ba05e95
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/neptuneclient/voidui/VoidUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.slf4j.LoggerFactory
*/
class VoidUI(
val renderer: Renderer,
val theme: Theme,
var theme: Theme,
val settings: Settings = Settings(),
val template: Template = Template { it }
) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/neptuneclient/voidui/framework/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class Screen(voidUI: VoidUI) : Widget() {
val renderStack = RenderStack(voidUI.renderer)

init {
super.voidUI = voidUI
this.voidUI = voidUI
}

/**
Expand All @@ -27,6 +27,8 @@ abstract class Screen(voidUI: VoidUI) : Widget() {
* **Needs to be called by the user.**
*/
fun init() {
screen = this

val template = voidUI.template.build(this.build())
root = if (voidUI.settings.centeredScreen)
Align(template, Alignment.center)
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/neptuneclient/voidui/framework/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ abstract class Widget {
* Defines a stateful variable, which rebuilds the widget once it changes its value.
*/
fun <T> stateOf(initialValue: T) = Delegates.observable(initialValue) { _, _, _ ->
// redo ui
// temp
screen.remove()
screen.init()
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/com/neptuneclient/voidui/utils/Font.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import java.nio.file.Path
*/
data class Font(val name: String, val path: Path) {

/**
* The font data in a byte buffer.
*/
private val data = getBufferData(path)

/**
* Registers the font to the renderer.
*
* @param renderer The renderer to register the font to.
*/
fun register(renderer: Renderer) {
try {
val data = getBufferData(path)
renderer.registerFont(name, data)
} catch (e: IOException) {
VoidUI.LOGGER.error("Failed to load font data, this may cause a crash later!", e)
Expand Down
148 changes: 148 additions & 0 deletions src/test/kotlin/com/neptuneclient/voidui/tests/MockRenderer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.neptuneclient.voidui.tests

import com.neptuneclient.voidui.event.MouseClickedEvent
import com.neptuneclient.voidui.framework.Offset
import com.neptuneclient.voidui.framework.Size
import com.neptuneclient.voidui.objects.CornerRadius
import com.neptuneclient.voidui.rendering.Renderer
import com.neptuneclient.voidui.theme.TextStyle
import com.neptuneclient.voidui.utils.Font
import com.neptuneclient.voidui.utils.ImageBuffer
import org.lwjgl.glfw.GLFW
import java.awt.Color
import java.nio.ByteBuffer
import java.nio.file.Path

class MockRenderer : Renderer {

private var eventCounter = 0

override fun windowWidth(): Int {
println("windowWidth")
return 0
}

override fun windowHeight(): Int {
println("windowHeight")
return 0
}

override fun beginFrame() {
eventCounter++
if (eventCounter >= 1000) {
mouseEvent(0, GLFW.GLFW_PRESS, 0f, 0f)
eventCounter = 0
}

println("beginFrame")
}

override fun endFrame() {
println("endFrame")
}

override fun destroy() {
println("destroy")
}

override fun mousePosition(): Offset {
println("mousePosition")
return Offset.zero
}

override fun registerFont(name: String, data: ByteBuffer) {
println("registerFont")
}

override fun registerImage(path: Path, data: ByteBuffer): Int {
println("registerImage")
return 0
}

override fun unregisterImage(image: ImageBuffer) {
println("unregisterImage")
}

override fun scissor(x: Float, y: Float, width: Float, height: Float) {
println("scissor")
}

override fun disableScissor() {
println("disableScissor")
}

override fun line(x: Float, y: Float, x2: Float, y2: Float, thickness: Float, color: Color) {
println("line")
}

override fun rectangle(x: Float, y: Float, width: Float, height: Float, color: Color) {
println("rectangle")
}

override fun rectangleFrame(x: Float, y: Float, width: Float, height: Float, thickness: Float, color: Color) {
println("rectangleFrame")
}

override fun roundedRectangle(x: Float, y: Float, width: Float, height: Float, radius: CornerRadius, color: Color) {
println("roundedRectangle")
}

override fun roundedRectangleFrame(
x: Float,
y: Float,
width: Float,
height: Float,
radius: CornerRadius,
thickness: Float,
color: Color
) {
println("roundedRectangleFrame")
}

override fun image(x: Float, y: Float, width: Float, height: Float, image: ImageBuffer, imageOffset: Offset) {
println("image")
}

override fun roundedImage(
x: Float,
y: Float,
width: Float,
height: Float,
radius: CornerRadius,
image: ImageBuffer,
imageOffset: Offset
) {
println("roundedImage")
}

override fun text(x: Float, y: Float, text: String, font: Font, style: TextStyle) {
println("text")
}

override fun getTextBounds(text: String, font: Font, style: TextStyle): Size {
println("getTextBounds")
return Size.zero
}

override fun compileShaderProgram(vertexSource: String, fragmentSource: String): Int {
println("compileShaderProgram")
return 0
}

override fun deleteShaderProgram(program: Int) {
println("deleteShaderProgram")
}

override fun useShaderProgram(program: Int) {
println("useShaderProgram")
}

override fun setUniform(program: Int, name: String, value: Number) {
println("setUniform")
}

override fun setUniform(program: Int, name: String, value: Array<out Number>) {
println("setUniform")
}

}
11 changes: 8 additions & 3 deletions src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ fun main() {
val screen = TestScreen(voidUI)
screen.init()

val renderer = voidUI.renderer as TestRenderer
while (!GLFW.glfwWindowShouldClose(renderer.window)) {
screen.render()
if (voidUI.renderer is TestRenderer) {
val renderer = voidUI.renderer as TestRenderer
while (!GLFW.glfwWindowShouldClose(renderer.window)) {
screen.render()
}
} else {
while (true)
screen.render()
}

screen.remove()
Expand Down

0 comments on commit ba05e95

Please sign in to comment.