Skip to content

Commit

Permalink
hud manager, ui transformations, long standing bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Stivais committed Nov 8, 2024
1 parent 3fdf086 commit 3343956
Show file tree
Hide file tree
Showing 52 changed files with 1,661 additions and 1,583 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private void onReceivePacket(ChannelHandlerContext context, Packet<?> packet, Ca

@Inject(method = {"sendPacket(Lnet/minecraft/network/Packet;)V"}, at = {@At("HEAD")}, cancellable = true)
private void onSendPacket(Packet<?> packet, CallbackInfo ci) {
if (!ServerUtils.INSTANCE.handleSendPacket(packet)) {
if (!ServerUtils.handleSendPacket(packet)) {
if (postAndCatch(new PacketSentEvent(packet)) && !ci.isCancelled())
ci.cancel();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private void onReceivePacket(ChannelHandlerContext context, Packet<?> packet, Ca

@Inject(method = {"sendPacket(Lnet/minecraft/network/Packet;)V"}, at = {@At("HEAD")}, cancellable = true)
private void onSendPacket(Packet<?> packet, CallbackInfo ci) {
if (!ServerUtils.INSTANCE.handleSendPacket(packet)) {
if (!ServerUtils.handleSendPacket(packet)) {
if (postAndCatch(new PacketSentEvent(packet)) && !ci.isCancelled())
ci.cancel();
}
Expand Down
1 change: 0 additions & 1 deletion odinclient/src/main/resources/mixins.odinclient.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"mixins": [
"accessors.IBlockAccessor",
"mixins.MixinChunk",
"mixins.MixinEventBus",
"mixins.MixinNetworkManager",
"mixins.MixinRendererLivingEntity",
"mixins.MixinS1CPacketEntityMetadata",
Expand Down
56 changes: 56 additions & 0 deletions src/main/kotlin/com/github/stivais/ui/animation/Animating.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,62 @@ sealed interface Animating {
fun animate(duration: Float, type: Animations): Animation?

interface Swapping : Animating {

fun swap()

class Impl(var from: Float, var to: Float) : Swapping {

private var current: Float = 0f

private var before: Float? = null

var animation: Animation? = null
private set

fun get(): Float {
animation?.let { anim ->
val progress = anim.get()
val from = before ?: from
current = from + (to - from) * progress

if (anim.finished) {
animation = null
before = null
swap()
}
return current
}
return from
}

override fun swap() {
val temp = to
to = from
from = temp
}

override fun animate(duration: Float, type: Animations): Animation? {
if (duration == 0f) {
swap()
} else {
if (animation != null) {
before = current
swap()
animation = Animation(duration * animation!!.get(), type)
} else {
animation = Animation(duration, type)
}
}
return animation
}
}

companion object {
/**
* Constant version for [Animating.Swapping.Impl], intended for delegating and overriding get()
*/
@JvmStatic
val Impl = Impl(0f, 0f)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.github.stivais.ui.constraints.measurements
import com.github.stivais.ui.animation.Animating
import com.github.stivais.ui.animation.Animation
import com.github.stivais.ui.animation.Animations
import com.github.stivais.ui.constraints.*
import com.github.stivais.ui.constraints.Constraint
import com.github.stivais.ui.constraints.Measurement
import com.github.stivais.ui.constraints.Type
import com.github.stivais.ui.elements.Element

/**
Expand All @@ -17,7 +19,10 @@ import com.github.stivais.ui.elements.Element
*
* @see Animatable.Raw
*/
class Animatable(var from: Constraint, var to: Constraint): Measurement, Animating.Swapping {
class Animatable(
var from: Constraint,
var to: Constraint
): Measurement, Animating.Swapping by Animating.Swapping.Impl {

constructor(from: Constraint, to: Constraint, swapIf: Boolean) : this(from, to) {
if (swapIf) {
Expand Down Expand Up @@ -55,27 +60,6 @@ class Animatable(var from: Constraint, var to: Constraint): Measurement, Animati
return from.get(element, type)
}

override fun animate(duration: Float, type: Animations): Animation? {
if (duration == 0f) {
swap()
} else {
if (animation != null) {
before = current
swap()
animation = Animation(duration * animation!!.get(), type)
} else {
animation = Animation(duration, type)
}
}
return animation
}

override fun swap() {
val temp = to
to = from
from = temp
}

override fun reliesOnChild(): Boolean {
return from.reliesOnChild() || to.reliesOnChild()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.stivais.ui.constraints.Size
import com.github.stivais.ui.constraints.Type
import com.github.stivais.ui.elements.Element

class Aspect(private val ratio: Float) : Size {
class AspectRatio(private val ratio: Float) : Size {
override fun get(element: Element, type: Type): Float {
return if (type.axis == HORIZONTAL) element.height * ratio else element.width / ratio
}
Expand Down
70 changes: 22 additions & 48 deletions src/main/kotlin/com/github/stivais/ui/elements/Element.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.github.stivais.ui.events.Lifetime
import com.github.stivais.ui.events.Mouse
import com.github.stivais.ui.operation.UIOperation
import com.github.stivais.ui.renderer.Renderer
import com.github.stivais.ui.transforms.Transforms
import com.github.stivais.ui.utils.loop

abstract class Element(constraints: Constraints?, var color: Color? = null) {
Expand Down Expand Up @@ -71,25 +72,10 @@ abstract class Element(constraints: Constraints?, var color: Color? = null) {
}
//

private var transforms: ArrayList<Transforms>? = null

// todo: rework

var alphaAnim: Animatable? = null
var rotateAnim: Animatable? = null

var alpha = 1f
set(value) {
field = value.coerceIn(0f, 1f)
}

// this is needed to track current scale
var scale = 1f
set(value) {
field = value.coerceAtLeast(0f)
}

var scaledCentered = true

var rotation = 0f

//

Expand Down Expand Up @@ -127,18 +113,23 @@ abstract class Element(constraints: Constraints?, var color: Color? = null) {
it.position(x, y)
it.positionChildren()
}
if (constraints.width.reliesOnChild()) width = constraints.width.get(this, Type.W)
if (constraints.height.reliesOnChild()) height = constraints.height.get(this, Type.H) + sy
val widthRelies = constraints.width.reliesOnChild()
val heightRelies = constraints.height.reliesOnChild()

if (widthRelies) width = constraints.width.get(this, Type.W)
if (heightRelies) height = constraints.height.get(this, Type.H) + sy

if (widthRelies || heightRelies) parent?.redrawInternal = true
}

private var _redraw = true
var redrawInternal = true

var redraw: Boolean
get() = _redraw
get() = redrawInternal
set(value) {
if (value) {
val element = getElementToRedraw()
element._redraw = true
element.redrawInternal = true
}
}

Expand Down Expand Up @@ -168,8 +159,8 @@ abstract class Element(constraints: Constraints?, var color: Color? = null) {

// IDK is it worth
fun preRender() {
if (_redraw) {
_redraw = false
if (redrawInternal) {
redrawInternal = false
size()
positionChildren()
clip()
Expand All @@ -187,30 +178,8 @@ abstract class Element(constraints: Constraints?, var color: Color? = null) {
fun render() {
if (!renders) return
renderer.push()
if (alphaAnim != null) {
alpha = alphaAnim!!.get(this, Type.X)
}
if (rotateAnim != null) {
rotation = rotateAnim!!.get(this, Type.X)
}
if (alpha != 1f) {
renderer.globalAlpha(alpha)
}
if (scale != 1f) {
var x = x
var y = y
if (scaledCentered) {
x += width / 2f
y += height / 2f
}
renderer.translate(x, y)
renderer.scale(scale, scale)
renderer.translate(-x, -y)
}
if (rotation != 0f) {
renderer.translate(x + width / 2f, y + height / 2f)
renderer.rotate(rotation)
renderer.translate(-(x + width / 2f), -(y + height / 2f))
transforms?.loop {
it.apply(this, renderer)
}
draw()
if (scissors) renderer.pushScissor(x, y, width, height)
Expand Down Expand Up @@ -243,6 +212,11 @@ abstract class Element(constraints: Constraints?, var color: Color? = null) {
ui.operations!!.add(operation)
}

fun addTransform(transform: Transforms) {
if (transforms == null) transforms = arrayListOf()
transforms!!.add(transform)
}

fun addElement(element: Element) {
onElementAdded(element)
if (elements == null) elements = arrayListOf()
Expand Down
19 changes: 10 additions & 9 deletions src/main/kotlin/com/github/stivais/ui/elements/impl/Block.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package com.github.stivais.ui.elements.impl

import com.github.stivais.ui.color.Color
import com.github.stivais.ui.color.alpha
import com.github.stivais.ui.constraints.*
import com.github.stivais.ui.constraints.Constraints
import com.github.stivais.ui.constraints.Measurement
import com.github.stivais.ui.constraints.Type
import com.github.stivais.ui.elements.Element
import com.github.stivais.ui.renderer.Gradient as GradientType


open class Block(
constraints: Constraints?,
color: Color,
var outlineColor: Color? = null,
var outlineThickness: Measurement? = null,
protected val radius: FloatArray? = null
) : Element(constraints, color) {

Expand All @@ -19,24 +23,21 @@ open class Block(
}
}

var outlineColor: Color? = null
var outline: Measurement? = null

override fun draw() {
if (radius == null) {
if (color!!.alpha != 0) {
renderer.rect(x, y, width, height, color!!.get(this))
}
if (outlineColor != null && outlineColor!!.alpha != 0) {
val thickness = outline!!.get(this, Type.W)
val thickness = outlineThickness!!.get(this, Type.W)
renderer.hollowRect(x, y, width, height, thickness, outlineColor!!.get(this), 0f)
}
} else {
if (color!!.alpha != 0) {
renderer.rect(x, y, width, height, color!!.rgba, radius[0], radius[1], radius[2], radius[3])
}
if (outlineColor != null && outlineColor!!.alpha != 0) {
val thickness = outline!!.get(this, Type.W)
val thickness = outlineThickness!!.get(this, Type.W)
renderer.hollowRect(x, y, width, height, thickness, outlineColor!!.rgba, radius[0], radius[1], radius[2], radius[3])
}
}
Expand All @@ -48,22 +49,22 @@ open class Block(
private val color2: Color,
radius: FloatArray?,
private val gradient: GradientType
) : Block(constraints, color1, radius) {
) : Block(constraints, color1, null, null, radius) {
override fun draw() {
if (radius == null) {
if (color!!.alpha != 0) {
renderer.gradientRect(x, y, width, height, color!!.get(this), color2.get(this), gradient)
}
if (outlineColor != null && outlineColor!!.alpha != 0) {
val thickness = outline!!.get(this, Type.W)
val thickness = outlineThickness!!.get(this, Type.W)
renderer.hollowRect(x, y, width, height, thickness, outlineColor!!.get(this), 0f)
}
} else {
if (color!!.alpha != 0) {
renderer.gradientRect(x, y, width, height, color!!.rgba, color2.rgba, gradient, radius[0], radius[1], radius[2], radius[3])
}
if (outlineColor != null && outlineColor!!.alpha != 0) {
val thickness = outline!!.get(this, Type.W)
val thickness = outlineThickness!!.get(this, Type.W)
renderer.hollowRect(x, y, width, height, thickness, outlineColor!!.rgba, radius[0], radius[1], radius[2], radius[3])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import com.github.stivais.ui.utils.replaceUndefined

class Group(constraints: Constraints?) : Element(constraints.replaceUndefined(w = Bounding, h = Bounding)) {
override fun draw() {
// renderer.hollowRect(x, y, width, height, 1f, java.awt.Color.WHITE.rgb)
// doesn't draw
}
}
Loading

0 comments on commit 3343956

Please sign in to comment.