Skip to content

Commit

Permalink
Cleanup and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
senxd committed Jul 15, 2022
1 parent d02e14e commit b8103a2
Show file tree
Hide file tree
Showing 56 changed files with 946 additions and 1,365 deletions.
51 changes: 18 additions & 33 deletions src/main/kotlin/net/prismclient/aether/ui/Aether.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import net.prismclient.aether.ui.Aether.Properties
import net.prismclient.aether.ui.component.UIComponent
import net.prismclient.aether.ui.component.controller.UIController
import net.prismclient.aether.ui.component.type.layout.UIFrame
import net.prismclient.aether.ui.component.type.layout.container.UIContainer
import net.prismclient.aether.ui.component.type.layout.styles.UIContainerSheet
import net.prismclient.aether.ui.component.type.layout.UIContainer
import net.prismclient.aether.ui.component.type.layout.UIContainerSheet
import net.prismclient.aether.ui.event.input.UIMouseEvent
import net.prismclient.aether.ui.renderer.UIProvider
import net.prismclient.aether.ui.renderer.UIRenderer
Expand Down Expand Up @@ -33,9 +33,10 @@ import java.util.function.Consumer
* functions [Properties.updateSize] and [Properties.updateMouse] to update the values without
* invoking the [update], and [mouseMoved] functions.
*
* [Aether Documentation](https://aether.prismclient.net/getting-started)
*
* @author sen
* @since 1.0
* @see <a href="https://aether.prismclient.net/getting-started">UICore documentation</a>
* @see UIProvider
*/
open class Aether(renderer: UIRenderer) {
Expand Down Expand Up @@ -108,12 +109,14 @@ open class Aether(renderer: UIRenderer) {
open fun render() {
renderer {
if (activeScreen != null) {
timings.onFrameRenderStart()
beginFrame(width, height, devicePxRatio)
for (i in 0 until components!!.size) {
val component = components!![i]
if (component.visible) component.render()
}
endFrame()
timings.onFrameRenderEnd()
}
}
}
Expand All @@ -123,7 +126,7 @@ open class Aether(renderer: UIRenderer) {
* eligibility to be focused or bubbled. The [Properties.mouseX] and [Properties.mouseY]
* properties can be found in [Aether.Properties].
*/
fun mouseMoved(mouseX: Float, mouseY: Float) {
open fun mouseMoved(mouseX: Float, mouseY: Float) {
updateMouse(mouseX, mouseY)
mouseMoveListeners?.forEach { it.value.run() }
if (activeScreen != null) for (i in 0 until components!!.size) components!![i].mouseMoved(mouseX, mouseY)
Expand All @@ -139,10 +142,10 @@ open class Aether(renderer: UIRenderer) {
*
* @see mouseScrolled
*/
fun mouseChanged(mouseButton: Int, isRelease: Boolean) {
open fun mouseChanged(mouseButton: Int, isRelease: Boolean) {
if (isRelease) {
mouseReleasedListeners?.forEach { it.value.run() }
components?.forEach { it.mouseReleased(mouseX, mouseY) }
components?.forEach { it.mouseReleased(it.getMouseX(), it.getMouseY()) }
return
}

Expand Down Expand Up @@ -182,7 +185,7 @@ open class Aether(renderer: UIRenderer) {

return if (component != null) {
component.focus()
component.mousePressed(UIMouseEvent(mouseX, mouseY, mouseButton, clickCount))
component.mousePressed(UIMouseEvent(component.getMouseX(), component.getMouseY(), mouseButton, clickCount))
true
} else false
}
Expand Down Expand Up @@ -211,7 +214,7 @@ open class Aether(renderer: UIRenderer) {
i++
}
c?.focus()
c?.mousePressed(UIMouseEvent(mouseX, mouseY, mouseButton, clickCount))
c?.mousePressed(UIMouseEvent(c.getMouseX(), c.getMouseY(), mouseButton, clickCount))
}

/**
Expand All @@ -220,7 +223,7 @@ open class Aether(renderer: UIRenderer) {
* @param character The key which was pressed or '\u0000'
* @see updateModifierKey To update keys such as Shift, Alt, Tab etc...
*/
fun keyPressed(character: Char) {
open fun keyPressed(character: Char) {
keyPressListeners?.forEach { it.value.accept(character) }
(focusedComponent as? UIComponent<*>)?.keyPressed(character)
}
Expand All @@ -230,6 +233,7 @@ open class Aether(renderer: UIRenderer) {
* of their eligibility to be focused or bubbled.
*/
open fun mouseScrolled(scrollAmount: Float) {
if (scrollAmount == 0f) return
tryFocus()
mouseScrollListeners?.forEach { it.value.accept(scrollAmount) }
components?.forEach { it.mouseScrolled(mouseX, mouseY, scrollAmount) }
Expand All @@ -241,20 +245,20 @@ open class Aether(renderer: UIRenderer) {
* as de-focusing the focused component and adding listeners to input.
*/
companion object Properties {

val timings: Timings = Timings()

@JvmStatic
var debug: Boolean = true

@JvmStatic
lateinit var instance: Aether
protected set

@JvmStatic
lateinit var renderer: UIRenderer
protected set

@JvmStatic
var activeScreen: UIScreen? = null
protected set

/**
* The focused component (if applicable).
Expand All @@ -265,92 +269,79 @@ open class Aether(renderer: UIRenderer) {
*/
@JvmStatic
var focusedComponent: UIFocusable? = null
protected set

/**
* The width of the screen. It can be set via [update]
*/
@JvmStatic
var width: Float = 0f
protected set

/**
* The width of the screen. It can be set via [update]
*/
@JvmStatic
var height: Float = 0f
protected set

/**
* The device pixel ratio. It can be set via [update]. It is the equivalent of content scale.
*/
@JvmStatic
var devicePxRatio: Float = 1f
protected set

/**
* The x position of the mouse relative to the screen
*/
@JvmStatic
var mouseX: Float = 0f
protected set

/**
* The y position of the mouse relative to the screen
*/
@JvmStatic
var mouseY: Float = 0f
protected set

/**
* Invoked whenever the layout needs to be updated. This can be when the screen
* is resized or created. Invoked prior to components.
*/
@JvmStatic
var updateListeners: HashMap<String, Runnable>? = null
protected set

/**
* The listeners for then the mouse is moved. Invoked prior to components.
*/
@JvmStatic
var mouseMoveListeners: HashMap<String, Runnable>? = null
protected set

/**
* Invoked when the mouse is pressed. Invoked prior to components.
*/
@JvmStatic
var mousePressedListeners: HashMap<String, Runnable>? = null
protected set

/**
* Invoked when the mouse is released. Invoked prior to components.
*/
@JvmStatic
var mouseReleasedListeners: HashMap<String, Runnable>? = null
protected set

/**
* Invoked when a key is pressed. Invoked prior to components.
*/
@JvmStatic
var keyPressListeners: HashMap<String, Consumer<Char>>? = null
protected set

/**
* Invoked when the mouse is scrolled. Invoked prior to components.
*/
@JvmStatic
var mouseScrollListeners: HashMap<String, Consumer<Float>>? = null
protected set

/**
* Invoked when the screen is deleted. This is used to deallocate listeners added to UICore.
*/
@JvmStatic
var deallocationListeners: HashMap<String, Runnable>? = null
protected set

/**
* The list of modifier keys. The value is if the key is pressed
Expand Down Expand Up @@ -502,13 +493,7 @@ open class Aether(renderer: UIRenderer) {
* Focuses the component. Please use [UIComponent.focus] instead.
*/
@JvmStatic
fun focus(component: UIFocusable) {
// Check if the given value is a valid instance of UIComponent
try {
component as UIComponent<*>
} catch (castException: ClassCastException) {
throw RuntimeException("When trying to focus, the provided value is not an instance of UIComponent. Make sure you are only using the UIFocus interface to focus UIComponents.")
}
fun <T> focus(component: T) where T : UIComponent<*>, T : UIFocusable {
focusedComponent = component
}

Expand Down Expand Up @@ -548,7 +533,7 @@ open class Aether(renderer: UIRenderer) {
for (i in 0 until instance.frames!!.size) {
// UIContainers are what control scrolling, so
// if it is not an instance of it, skip and continue
val container = instance.frames!![i] as? UIContainer<*> ?: continue
val container = instance.frames!![i] as? UIContainer<UIContainerSheet> ?: continue
if (container.isMouseInsideBounds() && container.expandedHeight > 0f && container.style.overflowY != UIContainerSheet.Overflow.None) {
// Iterate through the frame to see if there are more
// containers with it. If there are, it will pass true
Expand Down
83 changes: 83 additions & 0 deletions src/main/kotlin/net/prismclient/aether/ui/Timings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package net.prismclient.aether.ui

class Timings {

/**
* Time when the current frame render started in milliseconds
*/
var frameRenderStartTime = 0L
private set

/**
* Time when the last frame render started in milliseconds
*/
var lastFrameRenderStartTime = 0L
private set

/**
* Time when the current frame render ended in milliseconds
*/
var frameRenderEndTime = 0L
private set

/**
* Time when the last frame render ended in milliseconds
*/
var lastFrameRenderEndTime = 0L
private set

/**
* The delta time (frame render start - frame render end) of the current frame in milliseconds
*/
val frameRenderDeltaTime
get() = frameRenderEndTime - frameRenderStartTime

/**
* The delta time (frame render start - frame render end) of the current frame in seconds
*/
val deltaFrameRenderTimeSeconds
get() = frameRenderDeltaTime / 1000.0

/**
* The delta time (frame render start - frame render end) of the last frame in milliseconds
*/
val lastFrameRenderDeltaTime
get() = lastFrameRenderEndTime - lastFrameRenderStartTime

/**
* The delta time (frame render start - frame render end) of the last frame in seconds
*/
val lastFrameRenderDeltaTimeSeconds
get() = lastFrameRenderDeltaTime / 1000.0

/**
* The approximate amount of renders the last frame would have made in a second
*/
val lastFrameRate
get() = 1000.0 / lastFrameRenderDeltaTime

/**
* The approximate amount of renders the current frame would have made in a second
*/
val frameRate
get() = (1000 / (frameRenderDeltaTime + 1)).toInt()

/**
* Set current & last render start times
*/
fun onFrameRenderStart() {
lastFrameRenderStartTime = frameRenderStartTime

frameRenderStartTime = System.currentTimeMillis()
}

/**
* Set current & last render end times
*/
fun onFrameRenderEnd() {
lastFrameRenderEndTime = frameRenderEndTime

frameRenderEndTime = System.currentTimeMillis()
}

}

This file was deleted.

Loading

0 comments on commit b8103a2

Please sign in to comment.