-
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.
Merge branch 'master' of https://github.com/NeptuneMC/void-ui
- Loading branch information
Showing
10 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
package com.neptuneclient.voidui.event | ||
|
||
/** | ||
* @author refactoring | ||
* @date 01-09-2023 | ||
*/ | ||
open class Event() |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/neptuneclient/voidui/event/EventManager.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,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
14
src/main/kotlin/com/neptuneclient/voidui/event/Subscribe.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,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<*>) |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/neptuneclient/voidui/event/impl/StateChangeEvent.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,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 | ||
} |
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
36 changes: 36 additions & 0 deletions
36
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,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}") | ||
} | ||
} |
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,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) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/mixins/TitleScreenMixin.java
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,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); | ||
|
||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/mixins/void/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,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}") | ||
} | ||
} |