Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Shape #2

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import net.prismclient.aether.ui.component.util.interfaces.UIFocusable
import net.prismclient.aether.ui.style.UIProvider
import net.prismclient.aether.ui.style.UIStyleSheet
import net.prismclient.aether.ui.unit.UIUnit
import net.prismclient.aether.ui.unit.util.*
import net.prismclient.aether.ui.util.UIKey
import net.prismclient.aether.ui.util.extensions.calculateX
import net.prismclient.aether.ui.util.extensions.calculateY
Expand Down Expand Up @@ -135,7 +134,7 @@ abstract class UIComponent<T : UIStyleSheet>(style: String) {
calculateUnitX(this, width, ignore)

fun calculateUnitX(unit: UIUnit?, width: Float, ignore: Boolean): Float = if (unit == null) 0f else {
calculateX(unit, this, ignore, width)
calculateX(unit, this, width, ignore)
}

@JvmOverloads
Expand All @@ -146,7 +145,7 @@ abstract class UIComponent<T : UIStyleSheet>(style: String) {
calculateUnitY(this, height, ignore)

fun calculateUnitY(unit: UIUnit?, height: Float, ignore: Boolean): Float = if (unit == null) 0f else {
calculateY(unit, this, ignore, height)
calculateY(unit, this, height, ignore)
}

fun getParentX() = if (parent != null)
Expand Down Expand Up @@ -184,15 +183,18 @@ abstract class UIComponent<T : UIStyleSheet>(style: String) {

fun isFocused(): Boolean = UICore.instance.focusedComponent == this

protected fun getMouseX(): Float =
fun getMouseX(): Float =
UICore.mouseX - getParentXOffset()

protected fun getMouseY(): Float =
fun getMouseY(): Float =
UICore.mouseY - getParentYOffset()

protected fun getParentXOffset(): Float =
if (parent != null && parent is UIFrame && (parent!!.style as UIFrameSheet).clipContent) parent!!.relX else 0f

protected fun getParentYOffset(): Float =
if (parent != null && parent is UIFrame && (parent!!.style as UIFrameSheet).clipContent) parent!!.relY else 0f

protected fun isWithinBounds(x: Float, y: Float, width: Float, height: Float) =
(x <= getMouseX() && y <= getMouseY() && x + width >= getMouseX() && y + height >= getMouseY())
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,58 @@
package net.prismclient.aether.ui.component.type.input.slider

import net.prismclient.aether.ui.component.UIComponent
import net.prismclient.aether.ui.util.extensions.renderer
import kotlin.math.roundToInt

/**
* [UISlider]
*
* @author sen
* @since 5/13/2022
*/
open class UISlider<T : Number>(
var value: T,
var min: T,
var max: T,
var step: T,
style: String
) : UIComponent<UISliderSheet>(style) {
protected var controlX = 0f
protected var controlY = 0f
protected var controlWidth = 0f
protected var controlHeight = 0f
open class UISlider(var value: Float, var min: Float, var max: Float, var step: Float, style: String) : UIComponent<UISliderSheet>(style) {
protected var normalizedValue = value / (max - min)

protected var offsetX = 0f
protected var selected = false

override fun update() {
super.update()
controlX = style.controlX.getX(relWidth)
controlY = style.controlY.getY(relHeight)
controlWidth = style.controlWidth.getX(relWidth)
controlHeight = style.controlHeight.getY(relHeight)
style.sliderControl.update(this)
}

override fun renderComponent() {
renderer {

style.sliderControl.offsetX = normalizedValue * (relWidth - style.sliderControl.cachedWidth)
style.sliderControl.render()
}

override fun mouseMoved(mouseX: Float, mouseY: Float) {
super.mouseMoved(mouseX, mouseY)
if (selected) { // I got lazy
normalizedValue = (((mouseX - offsetX - style.sliderControl.cachedX)) / (relWidth - style.sliderControl.cachedWidth))
.coerceAtLeast(0f)
.coerceAtMost(1f)
value = (max - min) * normalizedValue + min
value = (value / step).roundToInt() * step
normalizedValue = value / (max - min)
}
}

override fun mouseClicked(mouseX: Float, mouseY: Float) {
super.mouseClicked(mouseX, mouseY)

if (isWithinBounds(
style.sliderControl.cachedX + style.sliderControl.offsetX,
style.sliderControl.cachedY + style.sliderControl.offsetY,
style.sliderControl.cachedWidth,
style.sliderControl.cachedHeight
)) {
offsetX = mouseX - (style.sliderControl.cachedX + style.sliderControl.offsetX)
selected = true
}
}

override fun mouseReleased(mouseX: Float, mouseY: Float) {
super.mouseReleased(mouseX, mouseY)
selected = false
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package net.prismclient.aether.ui.component.type.input.slider

import net.prismclient.aether.ui.shape.UIShape
import net.prismclient.aether.ui.shape.type.UIRectangle
import net.prismclient.aether.ui.style.UIStyleSheet
import net.prismclient.aether.ui.unit.UIUnit
import net.prismclient.aether.ui.util.extensions.px
import net.prismclient.aether.ui.util.extensions.radius
import net.prismclient.aether.ui.util.extensions.rel

class UISliderSheet : UIStyleSheet() {
var controlX: UIUnit = rel(0.5)
var controlY: UIUnit = rel(0.5)
var controlWidth: UIUnit = px(40)
var controlHeight: UIUnit = rel(1)
var sliderControl: UIShape = UIRectangle().also {
it.color = -1
it.width = px(40)
it.height = rel(1)
it.radius = radius(2.5f)
}

override fun copy(): UIStyleSheet {
val it = UISliderSheet()
it.apply(this)
it.sliderControl = sliderControl.copy()
return it
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class UIRadius(
var topRight: Float = 0f,
var bottomRight: Float = 0f,
var bottomLeft: Float = 0f
) : UICopy<UIRadius> { // TODO: Convert to UIUnit
) : UICopy<UIRadius> {
constructor(radius: Float) : this(radius, radius, radius, radius)

fun set(radius: Float) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.prismclient.aether.ui.renderer.other

import net.prismclient.aether.ui.component.UIComponent

/**
* [UIRenderable]
*
* @author sen
* @since 5/13/2022
*/
interface UIRenderable {
fun update(component: UIComponent<*>)

fun render()
}
16 changes: 7 additions & 9 deletions src/main/kotlin/net/prismclient/aether/ui/screen/UIScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class UIScreen {
// components.forEach(UIComponent::initialize)
}

// protected inline fun style(block: UIStyle.() -> Unit) = UIStyle.block()
// protected inline fun style(block: UIStyle.() -> Unit) = UIStyle.block()

// protected inline fun build(block: UIBuilder.() -> Unit) = UIBuilder.block()

Expand All @@ -29,33 +29,31 @@ abstract class UIScreen {
}

open fun renderContent() {
for (i in 0 until frames.size)
frames[i].renderContent()
for (i in 0 until frames.size) frames[i].renderContent()
}

open fun render() {
for (i in 0 until components.size)
frames[i].render()
for (i in 0 until components.size) components[i].render()
}

open fun mouseMoved(mouseX: Float, mouseY: Float) {
components.forEach { it.mouseMoved(mouseX, mouseY) }
components.forEach { it.mouseMoved(it.getMouseX(), it.getMouseY()) }
}

open fun mousePressed(mouseX: Float, mouseY: Float) {
components.forEach { it.mouseClicked(mouseX, mouseY) }
components.forEach { it.mouseClicked(it.getMouseX(), it.getMouseY()) }
}

open fun mouseReleased(mouseX: Float, mouseY: Float) {
components.forEach { it.mouseReleased(mouseX, mouseY) }
components.forEach { it.mouseReleased(it.getMouseX(), it.getMouseY()) }
}

open fun keyPressed(key: UIKey, character: Char) {
components.forEach { it.keyPressed(key, character) }
}

open fun mouseScrolled(mouseX: Float, mouseY: Float, scrollAmount: Float) {
components.forEach { it.mouseScrolled(mouseX, mouseY, scrollAmount) }
components.forEach { it.mouseScrolled(it.getMouseX(), it.getMouseY(), scrollAmount) }
}

}
45 changes: 45 additions & 0 deletions src/main/kotlin/net/prismclient/aether/ui/shape/UIShape.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.prismclient.aether.ui.shape

import net.prismclient.aether.ui.component.UIComponent
import net.prismclient.aether.ui.renderer.other.UIRenderable
import net.prismclient.aether.ui.unit.UIUnit
import net.prismclient.aether.ui.util.UICopy
import net.prismclient.aether.ui.util.extensions.calculateX
import net.prismclient.aether.ui.util.extensions.calculateY

abstract class UIShape : UIRenderable, UICopy<UIShape> {
var color = 0
var x: UIUnit? = null
var y: UIUnit? = null
var width: UIUnit? = null
var height: UIUnit? = null

var offsetX = 0f
var offsetY = 0f

var cachedX = 0f
protected set
var cachedY = 0f
protected set
var cachedWidth = 0f
protected set
var cachedHeight = 0f
protected set


override fun update(component: UIComponent<*>) {
cachedX = calculateX(x, component, component.relWidth) + component.relX
cachedY = calculateY(y, component, component.relHeight) + component.relY
cachedWidth = calculateX(width, component, component.relWidth)
cachedHeight = calculateY(height, component, component.relHeight)
}

open fun apply(shape: UIShape): UIShape {
x = shape.x?.copy()
y = shape.y?.copy()
width = shape.width?.copy()
height = shape.height?.copy()
color = shape.color
return this
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.prismclient.aether.ui.shape.type

import net.prismclient.aether.ui.renderer.impl.property.UIRadius
import net.prismclient.aether.ui.shape.UIShape
import net.prismclient.aether.ui.util.extensions.renderer

/**
* [UIRectangle]
*
* @author sen
* @since 5/13/2022
*/
class UIRectangle : UIShape() {
var radius: UIRadius? = null

override fun render() {
renderer {
color(color)
rect(cachedX + offsetX, cachedY + offsetY, cachedWidth, cachedHeight, radius)
}
}

override fun copy(): UIShape =
UIRectangle().also {
it.radius = radius?.copy()
}.apply(this)
}
10 changes: 10 additions & 0 deletions src/main/kotlin/net/prismclient/aether/ui/style/UIStyleSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ open class UIStyleSheet : UICopy<UIStyleSheet> {
return this
}

fun position(x: UIUnit, y: UIUnit) {
this.x = x
this.y = y
}

fun size(width: UIUnit, height: UIUnit) {
this.width = width
this.height = height
}

inline fun background(block: UIBackground.() -> Unit) {
background = UIBackground()
background!!.block()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/net/prismclient/aether/ui/util/UICopy.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.prismclient.aether.ui.util

/**
* Returns a new instance of it's self
* Returns a new instance of [Self]
*
* @author sen
* @since 4/26/2022
Expand Down

This file was deleted.

Loading