Skip to content

Commit

Permalink
state works again i think
Browse files Browse the repository at this point in the history
  • Loading branch information
Hobbyshop committed Mar 20, 2024
1 parent 6a17418 commit 68c6979
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/Element.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ import com.neptuneclient.voidui.themes.styles.PanelStyleSheet
*/
sealed class Element<S : StyleSheet> : Widget() {

/**
* Holds the screen in which the element is rendered.
*/
private lateinit var screen: Screen

/**
* The style sheet for this element.
*/
Expand All @@ -35,6 +30,7 @@ sealed class Element<S : StyleSheet> : Widget() {
*/
override fun init(screen: Screen, parent: Widget?) {
this.screen = screen
this.parent = parent
this.styles = screen.void.theme.getStyleSheet(this::class, Styles.Type.NORMAL)

val size = sizeSelf(screen, parent)
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.neptuneclient.voidui.widgets
import com.neptuneclient.voidui.VoidUI
import com.neptuneclient.voidui.rendering.ElementStack
import kotlin.math.round
import kotlin.properties.Delegates

/**
* The base class for screens. A screen is the base node of a widget tree, which can be rendered to the screen.
Expand Down Expand Up @@ -49,6 +50,15 @@ constructor(val void: VoidUI) {
elementStack.render()
}

/**
* Lets you declare a property which will trigger the screen to be rebuilt once it's value changes.
*
* @param initialValue The initial value of the property.
*/
protected fun <T> state(initialValue: T) = Delegates.observable(initialValue) { _, _, _ ->
init()
}

/**
* A dsl feature which adds the view-width unit from HTML to screens.
*/
Expand Down
27 changes: 26 additions & 1 deletion src/main/kotlin/com/neptuneclient/voidui/widgets/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.neptuneclient.voidui.widgets

import java.awt.Rectangle
import kotlin.math.round
import kotlin.properties.Delegates

/**
* The base for every node in the widget tree.
Expand All @@ -14,12 +15,25 @@ abstract class Widget {
var height: Int = 0

/**
* Called by [Screen.init] and initializes the widget. This includes setting position and size as well as initlializing the child widget.
* Holds the screen in which this widget lives.
*/
protected lateinit var screen: Screen

/**
* The parent widget. If this is the first widget in the tree, the parent widget is null.
*/
protected var parent: Widget? = null

/**
* Called by [Screen.init] and initializes the widget. This includes setting position and size as well as initializing the child widget.
*
* @param screen The screen in which this widget lives.
* @param parent The parent widget. If this is the first widget in the tree, the parent widget is null.
*/
internal open fun init(screen: Screen, parent: Widget?) {
this.screen = screen
this.parent = parent

val size = sizeSelf(screen, parent)
x = size.x
y = size.y
Expand All @@ -43,6 +57,17 @@ abstract class Widget {
* @return The child node in the widget tree.
*/
abstract fun build(): Widget

/**
* Lets you declare a property which will trigger the widget to be rebuilt once it's value changes.
*
* @param initialValue The initial value of the property.
*
* For now this just rebuilds the entire screen.
*/
protected fun <T> state(initialValue: T) = Delegates.observable(initialValue) { _, _, _ ->
screen.init()
}

/**
* A dsl feature which adds the view-width unit from HTML to components.
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/widgets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ package com.neptuneclient.voidui.widgets
internal class Group(protected val children: Array<Widget>) : Widget() {

override fun init(screen: Screen, parent: Widget?) {
this.screen = screen
this.parent = parent

val size = sizeSelf(screen, parent)
x = size.x
y = size.y
Expand Down

0 comments on commit 68c6979

Please sign in to comment.