-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
75 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
package com.neptuneclient.voidui.ui | ||
|
||
/** | ||
* 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. | ||
* | ||
* There are two types of components: | ||
* * [StaticComponent]: It is rendered once when the screen is built and is used to display static information. | ||
* * [ReactiveComponent]: It can hold variables which will, when changed, will rebuild the component. | ||
*/ | ||
sealed class Component( | ||
var x: Int? = null, | ||
var y: Int? = null, | ||
width: Int? = null, | ||
height: Int? = null | ||
) { | ||
var width = width ?: 0 | ||
var height = height ?: 0 | ||
var width: Int? = null, | ||
var height: Int? = null | ||
) : Drawable { | ||
|
||
abstract fun build(): Component | ||
|
||
val Number.cw | ||
get() = (this.toDouble() / 100) * width | ||
|
||
val Number.ch | ||
get() = (this.toDouble() / 100) * height | ||
abstract fun build(): Drawable | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.neptuneclient.voidui.ui | ||
|
||
import com.neptuneclient.voidui.ui.elements.Element | ||
|
||
/** | ||
* For now this is just so [Component] and [Element] have the same superclass. | ||
*/ | ||
interface Drawable { | ||
} |
39 changes: 10 additions & 29 deletions
39
src/main/kotlin/com/neptuneclient/voidui/ui/ReactiveComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,21 @@ | ||
package com.neptuneclient.voidui.ui | ||
|
||
import com.neptuneclient.voidui.event.EventManager | ||
import com.neptuneclient.voidui.event.Subscribe | ||
import com.neptuneclient.voidui.event.impl.StateChangeEvent | ||
import kotlin.properties.Delegates | ||
|
||
/** | ||
* @see Component | ||
*/ | ||
abstract class ReactiveComponent : Component() { | ||
val states = mutableListOf<State<*>>() | ||
|
||
init { | ||
EventManager.instance.register(this) | ||
for (field in this.javaClass.declaredFields) { | ||
if (field.type == State::class.java) { | ||
field.isAccessible = true | ||
val state = field.get(this) as State<*> | ||
state.init() | ||
states.add(state) | ||
state.subscribe { newValue -> | ||
// Here you can react to changes in the state. | ||
// For example, you can rebuild the component. | ||
rebuild() | ||
} | ||
for (field in javaClass.declaredFields) { | ||
if (!field.isAnnotationPresent(State::class.java)) continue | ||
val fieldObserver = Delegates.observable(field.get(this)) { _, _, new -> | ||
this.build() | ||
// TODO do proper rebuilding in the component tree | ||
} | ||
field.set(this, fieldObserver) | ||
} | ||
} | ||
|
||
override fun build(): Component { | ||
return this | ||
} | ||
|
||
open fun rebuild() { | ||
// This method is called when a state changes. | ||
// You can override it to rebuild the component. | ||
} | ||
|
||
@Subscribe(target = StateChangeEvent::class) | ||
fun onStateChange(event: StateChangeEvent<*>) { | ||
println("State changed from ${event.state.prev} to ${event.state.value}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,10 @@ | ||
package com.neptuneclient.voidui.ui | ||
|
||
import com.neptuneclient.voidui.event.EventManager | ||
import com.neptuneclient.voidui.event.impl.StateChangeEvent | ||
|
||
class State<T : Any>(initial: T) { | ||
var value: T = initial | ||
set(value) { | ||
field = value | ||
changed() | ||
} | ||
|
||
var prev: T = initial | ||
private val listeners = mutableListOf<(T) -> Unit>() | ||
|
||
fun init() { | ||
EventManager.instance.register(this) | ||
println("State initialized") | ||
} | ||
|
||
fun changed() { | ||
EventManager.instance.fire( | ||
StateChangeEvent(this) | ||
) | ||
prev = this.value | ||
listeners.forEach { it(value) } // Notify all listeners about the change | ||
} | ||
|
||
fun subscribe(listener: (T) -> Unit) { | ||
listeners.add(listener) | ||
} | ||
} | ||
/** | ||
* Used in implementations of [ReactiveComponent] to declare fields as state. | ||
* | ||
* For more info check out [ReactiveComponent]. | ||
*/ | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY) | ||
annotation class State() |
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/com/neptuneclient/voidui/ui/StaticComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
package com.neptuneclient.voidui.ui | ||
|
||
/** | ||
* @see Component | ||
*/ | ||
abstract class StaticComponent : Component() { | ||
} |
8 changes: 7 additions & 1 deletion
8
src/main/kotlin/com/neptuneclient/voidui/ui/elements/Element.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/neptuneclient/voidui/ui/elements/TestElement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.neptuneclient.voidui.ui.elements | ||
|
||
class TestElement : Element() { | ||
|
||
override fun render() { | ||
println("please kill me") | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/example/Label.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.neptuneclient.voidui.testmod.example | ||
|
||
import com.neptuneclient.voidui.ui.Drawable | ||
import com.neptuneclient.voidui.ui.ReactiveComponent | ||
import com.neptuneclient.voidui.ui.State | ||
import com.neptuneclient.voidui.ui.elements.TestElement | ||
|
||
class Label : ReactiveComponent() { | ||
|
||
@State | ||
private var counter = 0 | ||
|
||
override fun build(): Drawable { | ||
return TestElement() | ||
} | ||
|
||
} |