-
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
7 changed files
with
113 additions
and
1 deletion.
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
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.event | ||
|
||
import com.neptuneclient.voidui.VoidUI | ||
|
||
/** | ||
* The base class for events in VoidUI. | ||
*/ | ||
abstract class Event { | ||
|
||
/** | ||
* Calls the event. | ||
* | ||
* @param void The instance of VoidUI in which the event will be called. | ||
*/ | ||
fun call(void: VoidUI) = void.eventHandler.eventCalled(this) | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
src/main/kotlin/com/neptuneclient/voidui/event/EventHandler.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,72 @@ | ||
package com.neptuneclient.voidui.event | ||
|
||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Handles all event calls of the library. | ||
*/ | ||
class EventHandler { | ||
|
||
/** | ||
* A table of all actions which are registered. | ||
* | ||
* See [ActionRegistry] for more info. | ||
*/ | ||
private val actions = mutableListOf<ActionRegistry>() | ||
|
||
/** | ||
* Register a new action. | ||
* | ||
* @param action The action which will be invoked at the event call. | ||
* @param event The class of the event which will trigger the action. | ||
* @param key An optional key which will identify an action when it gets unregistered, keys don't need to be unique but | ||
* in case of a duplicate key all actions with this key will get unregistered. | ||
*/ | ||
fun <T : Event> register(event: KClass<T>, key: Any? = null, action: (T) -> Unit) { | ||
actions.add(ActionRegistry(action as (Event) -> Unit, event as KClass<Event>, key)) | ||
} | ||
|
||
/** | ||
* Invokes all actions which are registered with the provided event. | ||
*/ | ||
fun eventCalled(event: Event) { | ||
for ((action, eventClass, _) in actions) { | ||
if (event::class != eventClass) continue | ||
action(event) | ||
} | ||
} | ||
|
||
/** | ||
* Unregisters all actions with the provided key. | ||
*/ | ||
fun unregister(key: Any) { | ||
val indexCache = mutableListOf<Int>() | ||
|
||
// fuck you ConcurrentModificationException | ||
for ((index, data) in actions.withIndex()) { | ||
if (key != data.key) continue | ||
indexCache.add(index) | ||
} | ||
|
||
for (index in indexCache) | ||
actions.removeAt(index) | ||
} | ||
|
||
/** | ||
* Unregisters all actions. | ||
*/ | ||
fun unregisterAll() { | ||
actions.clear() | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Stores information about an event action. | ||
* | ||
* @param action The action which will be invoked at the event call. | ||
* @param event The class of the event which will trigger the action. | ||
* @param key An optional key which will identify an action when it gets unregistered, keys don't need to be unique but | ||
* in case of a duplicate key all actions with this key will get unregistered. | ||
*/ | ||
private data class ActionRegistry(val action: (Event) -> Unit, val event: KClass<Event>, val key: Any?) |
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,3 @@ | ||
package com.neptuneclient.voidui.event | ||
|
||
class TestEvent(val test: String) : Event() |
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
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