Skip to content

Commit

Permalink
Add align.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Jun 22, 2024
1 parent 73e3754 commit 11dd627
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
10 changes: 10 additions & 0 deletions api/src/main/java/kr/toxicity/inventory/api/gui/GuiAsset.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
package kr.toxicity.inventory.api.gui;

import org.jetbrains.annotations.NotNull;

public interface GuiAsset {
enum Align {
LEFT,
CENTER,
RIGHT
}
default @NotNull Align getAlign() {
return Align.LEFT;
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ allprojects {
apply(plugin = "kotlin")

group = "kr.toxicity.inventory"
version = "1.4"
version = "1.5"

repositories {
mavenCentral()
Expand Down
29 changes: 26 additions & 3 deletions dist/src/main/kotlin/kr/toxicity/inventory/gui/GuiBuilderImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import kr.toxicity.inventory.manager.AnimationManager
import kr.toxicity.inventory.manager.BackgroundManager
import kr.toxicity.inventory.manager.TextManager
import kr.toxicity.inventory.util.*
import net.kyori.adventure.text.Component
import org.bukkit.entity.Player
import kotlin.math.roundToInt

class GuiBuilderImpl: GuiBuilder {

Expand Down Expand Up @@ -46,18 +48,39 @@ class GuiBuilderImpl: GuiBuilder {
private inner class GuiImpl: Gui {
override fun open(player: Player) {
var comp = EMPTY_WIDTH_COMPONENT
var maxWidth = 0
val assetsComp = ArrayList<Pair<GuiAsset.Align, WidthComponent>>()
assets.forEach {
BackgroundManager.getBackground(it.javaClass)?.let { background ->
comp += background + NEGATIVE_ONE_SPACE_COMPONENT + NEW_LAYER + (-background.width).toSpaceComponent()
val addComp = background + NEGATIVE_ONE_SPACE_COMPONENT + NEW_LAYER
if (maxWidth < addComp.width) maxWidth = addComp.width
assetsComp.add(it.align to addComp)
}
if (it is GuiText) {
val text = it.text(player)
TextManager.getText(it.javaClass)?.let { converter ->
val textComponent = converter.toComponent(text)
comp += textComponent + (-textComponent.width).toSpaceComponent()
val addComp = converter.toComponent(text)
if (maxWidth < addComp.width) maxWidth = addComp.width
assetsComp.add(it.align to addComp)
}
}
}
assetsComp.forEach {
when (it.first) {
GuiAsset.Align.LEFT -> {
comp += it.second + (-it.second.width).toSpaceComponent()
}
GuiAsset.Align.CENTER -> {
val value = ((maxWidth - it.second.width).toDouble() / 2).roundToInt()
comp += value.toSpaceComponent() + it.second + (-value - it.second.width).toSpaceComponent()
}
GuiAsset.Align.RIGHT -> {
val value = maxWidth - it.second.width
comp += value.toSpaceComponent() + it.second + (-value - it.second.width).toSpaceComponent()
}
}
}

val renderer = mutableMapOf<Class<*>, AnimationRenderer>().apply {
assets.forEach {
AnimationManager.getAnimation(it.javaClass)?.let { animation ->
Expand Down

0 comments on commit 11dd627

Please sign in to comment.