Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/NeptuneMC/void-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Hobbyshop committed Mar 7, 2024
2 parents a4bfff2 + f8dfc24 commit 7c067d2
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/event/Event.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.neptuneclient.voidui.event

/**
* @author refactoring
* @date 01-09-2023
*/
open class Event()
47 changes: 47 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/event/EventManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.neptuneclient.voidui.event

import java.lang.reflect.InvocationTargetException


/**
* @author refactoring
* @date 01-09-2023
*/
class EventManager {
var targetClasses: MutableMap<Class<*>, Any> = HashMap()
fun register(`object`: Any) {
if (targetClasses.containsKey(`object`.javaClass)) return
targetClasses[`object`.javaClass] = `object`
}

fun unregister(`object`: Any) {
targetClasses.remove(`object`.javaClass)
}

fun clearTargets() {
targetClasses.clear()
}

fun fire(event: Event) {
targetClasses.forEach { (clazz: Class<*>, `object`: Any?) ->
for (method in clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(Subscribe::class.java) && method.getAnnotation(
Subscribe::class.java
).target == event.javaClass
) {
try {
method.invoke(`object`, event)
} catch (e: IllegalAccessException) {
throw RuntimeException(e)
} catch (e: InvocationTargetException) {
throw RuntimeException(e)
}
}
}
}
}

companion object {
val instance = EventManager()
}
}
14 changes: 14 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/event/Subscribe.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.neptuneclient.voidui.event

import kotlin.reflect.KClass


/**
* @author refactoring
* @date 01-09-2023
*/
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(
AnnotationRetention.RUNTIME
)
annotation class Subscribe(val target: KClass<*>)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.neptuneclient.voidui.event.impl

import com.neptuneclient.voidui.event.Event
import com.neptuneclient.voidui.ui.State

class StateChangeEvent<T : Any>(state: State<T>) : Event() {
var state: State<T> = state
}
1 change: 0 additions & 1 deletion src/main/kotlin/com/neptuneclient/voidui/ui/Component.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ sealed class Component(
width: Int? = null,
height: Int? = null
) {

var width = width ?: 0
var height = height ?: 0

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

import com.neptuneclient.voidui.event.EventManager
import com.neptuneclient.voidui.event.Subscribe
import com.neptuneclient.voidui.event.impl.StateChangeEvent

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()
}
}
}
}

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}")
}
}
32 changes: 32 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/ui/State.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.neptuneclient.voidui.testmod.mixins;

import com.neptuneclient.voidui.VoidUI;
import com.neptuneclient.voidui.ui.Screen;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.TitleScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(TitleScreen.class)
public class TitleScreenMixin {
VoidUI voidUI = new VoidUI();
/**
* @author
* @reason
*/
@Overwrite
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
voidUI.setCurrentScreen((Screen) (Object) this);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.neptuneclient.voidui.testmod.mixins.void

import com.neptuneclient.voidui.ui.ReactiveComponent
import com.neptuneclient.voidui.ui.State

class Label(initialText: String) : ReactiveComponent() {
val text = State(initialText)

init {
text.subscribe {
rebuild()
}
}

override fun rebuild() {
// Logic to update the UI goes here.
// This might involve re-rendering the component's UI, updating its data, etc.
println("Current text: ${text.value}")
}
}

0 comments on commit 7c067d2

Please sign in to comment.