diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/guide')
6 files changed, 290 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideGUI.kt b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideGUI.kt new file mode 100644 index 000000000..944a3201a --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideGUI.kt @@ -0,0 +1,121 @@ +package at.hannibal2.skyhanni.utils.guide + +import at.hannibal2.skyhanni.test.command.ErrorManager +import at.hannibal2.skyhanni.utils.RenderUtils +import at.hannibal2.skyhanni.utils.renderables.Renderable +import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.renderXYAligned +import net.minecraft.client.gui.GuiScreen +import net.minecraft.client.renderer.GlStateManager +import net.minecraft.item.ItemStack + +const val selectedColor = 0x50000000 +const val notSelectedColor = 0x50303030 +const val tabSpacing = 5 +const val tabShortSide = 25 +const val tabLongSide = 28 + +abstract class GuideGUI<pageEnum : Enum<*>>(defaultScreen: pageEnum) : GuiScreen() { + + abstract val sizeX: Int + abstract val sizeY: Int + lateinit var pageList: Map<pageEnum, GuidePage> + lateinit var horizontalTabs: List<GuideTab> + lateinit var verticalTabs: List<GuideTab> + protected var currentPage: pageEnum = defaultScreen + set(value) { + pageList[field]?.onLeave() + pageList[value]?.onEnter() + field = value + } + + val lastVerticalTabWrapper = object : tabWrapper { + override var tab: GuideTab? = null + } + val lastHorizontalTabWrapper = object : tabWrapper { + override var tab: GuideTab? = null + } + + fun hTab(item: ItemStack, tip: Renderable, onClick: (GuideTab) -> Unit) = + GuideTab(item, tip, false, lastHorizontalTabWrapper, onClick) + + fun vTab(item: ItemStack, tip: Renderable, onClick: (GuideTab) -> Unit) = + GuideTab(item, tip, true, lastVerticalTabWrapper, onClick) + + interface tabWrapper { + var tab: GuideTab? + } + + fun refreshPage() { + pageList[currentPage]?.refresh() + } + + private fun renderHorizontalTabs() { + var offset = Pair(tabSpacing.toFloat() * 3f, -tabLongSide.toFloat()) + GlStateManager.translate(offset.first, offset.second, 0f) + for (tab in horizontalTabs) { + tab.render(offset.first.toInt(), offset.second.toInt()) + val xShift = (tabShortSide + tabSpacing).toFloat() + offset = offset.first + xShift to offset.second + GlStateManager.translate(xShift, 0f, 0f) + } + GlStateManager.translate(-offset.first, -offset.second, 0f) + } + + private fun renderVerticalTabs() { + var offset = Pair(-tabLongSide.toFloat(), tabSpacing.toFloat() * 3f) + GlStateManager.translate(offset.first, offset.second, 0f) + for (tab in verticalTabs) { + tab.render(offset.first.toInt(), offset.second.toInt()) + val yShift = (tabShortSide + tabSpacing).toFloat() + offset = offset.first to offset.second + yShift + GlStateManager.translate(0f, yShift, 0f) + } + GlStateManager.translate(-offset.first, -offset.second, 0f) + } + + override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) = try { + super.drawScreen(mouseX, mouseY, partialTicks) + drawDefaultBackground() + val guiLeft = (width - sizeX) / 2 + val guiTop = (height - sizeY) / 2 + + /* + val mouseX = Mouse.getX() * width / Minecraft.getMinecraft().displayWidth + val mouseY = height - Mouse.getY() * height / Minecraft.getMinecraft().displayHeight - 1 + */ + + val relativeMouseX = mouseX - guiLeft + val relativeMouseY = mouseY - guiTop + + GlStateManager.pushMatrix() + GlStateManager.translate(guiLeft.toFloat(), guiTop.toFloat(), 0f) + drawRect(0, 0, sizeX, sizeY, 0x50000000) + + Renderable.withMousePosition(relativeMouseX, relativeMouseY) { + renderHorizontalTabs() + renderVerticalTabs() + + Renderable.string( + "ยง7SkyHanni ", + horizontalAlign = RenderUtils.HorizontalAlignment.RIGHT, + verticalAlign = RenderUtils.VerticalAlignment.BOTTOM + ).renderXYAligned(0, 0, sizeX, sizeY) + + val page = pageList[currentPage] + page?.drawPage(relativeMouseX, relativeMouseY) + + GlStateManager.translate(-guiLeft.toFloat(), -guiTop.toFloat(), 0f) + } + + GlStateManager.popMatrix() + + } catch (e: Exception) { + GlStateManager.popMatrix() + ErrorManager.logErrorWithData( + e, "Something broke in GuideGUI", + "Guide" to this.javaClass.typeName, + "Page" to currentPage.name + ) + } +} + diff --git a/src/main/java/at/hannibal2/skyhanni/utils/guide/GuidePage.kt b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuidePage.kt new file mode 100644 index 000000000..a563a531c --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuidePage.kt @@ -0,0 +1,15 @@ +package at.hannibal2.skyhanni.utils.guide + +abstract class GuidePage { + abstract fun drawPage(mouseX: Int, mouseY: Int) + + abstract fun onEnter() + + abstract fun onLeave() + + fun refresh() { + onLeave() + onEnter() + } + +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideRenderablePage.kt b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideRenderablePage.kt new file mode 100644 index 000000000..ce68a4afe --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideRenderablePage.kt @@ -0,0 +1,23 @@ +package at.hannibal2.skyhanni.utils.guide + +import at.hannibal2.skyhanni.utils.renderables.Renderable +import net.minecraft.client.renderer.GlStateManager + +abstract class GuideRenderablePage( + val paddingX: Int = 0, + val paddingY: Int = 0 +) : GuidePage() { + + protected var renderable: Renderable? = null + + final override fun drawPage(mouseX: Int, mouseY: Int) { + GlStateManager.translate(paddingX.toFloat(), paddingY.toFloat(), 0f) + renderable?.render(paddingX, paddingY) + GlStateManager.translate(-paddingX.toFloat(), -paddingY.toFloat(), 0f) + } + + override fun onLeave() { + renderable = null + } + +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideScrollPage.kt b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideScrollPage.kt new file mode 100644 index 000000000..97cf68442 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideScrollPage.kt @@ -0,0 +1,31 @@ +package at.hannibal2.skyhanni.utils.guide + +import at.hannibal2.skyhanni.utils.CollectionUtils.tableStretchXPadding +import at.hannibal2.skyhanni.utils.renderables.Renderable +import at.hannibal2.skyhanni.utils.renderables.ScrollValue + +abstract class GuideScrollPage( + val sizeX: Int, + val sizeY: Int, + paddingX: Int = 0, + paddingY: Int = 0, + val marginY: Int = 5, + val velocity: Double = 3.0, + val hasHeader: Boolean = true, +) : GuideRenderablePage(paddingX, paddingY) { + + private val scroll = ScrollValue() + + fun update(content: List<List<Renderable>>) { + renderable = Renderable.scrollTable( + content = content, + height = sizeY - paddingY * 2, + scrollValue = scroll, + velocity = velocity, + xPadding = content.tableStretchXPadding(sizeX - paddingX * 2), + yPadding = marginY, + hasHeader = hasHeader, + button = 0 + ) + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideTab.kt b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideTab.kt new file mode 100644 index 000000000..acc69c8cc --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideTab.kt @@ -0,0 +1,67 @@ +package at.hannibal2.skyhanni.utils.guide + +import at.hannibal2.skyhanni.utils.RenderUtils.HorizontalAlignment +import at.hannibal2.skyhanni.utils.RenderUtils.VerticalAlignment +import at.hannibal2.skyhanni.utils.SoundUtils +import at.hannibal2.skyhanni.utils.renderables.Renderable +import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.renderXYAligned +import net.minecraft.client.gui.Gui +import net.minecraft.item.ItemStack + +class GuideTab( + val item: ItemStack, + val tip: Renderable, + val isVertical: Boolean = false, + var lastTab: GuideGUI.tabWrapper, + val onClick: (GuideTab) -> Unit +) { + + fun fakeClick() = click() + + private fun click() { + onClick.invoke(this) + this.select() + if (lastTab.tab != this) { + lastTab.tab?.unSelect() + lastTab.tab = this + } + } + + fun select() { + selectColor = selectedColor + } + + fun unSelect() { + selectColor = notSelectedColor + } + + fun isSelected() = selectColor == selectedColor + + val width = if (isVertical) tabLongSide else tabShortSide + val height = if (isVertical) tabShortSide else tabLongSide + + private var selectColor = notSelectedColor + + private val renderable = Renderable.clickAndHover(object : Renderable { + override val width = this@GuideTab.width + override val height = this@GuideTab.height + override val horizontalAlign: HorizontalAlignment = HorizontalAlignment.LEFT + override val verticalAlign: VerticalAlignment = VerticalAlignment.TOP + + val itemRender = Renderable.itemStack( + item, 1.0, horizontalAlign = HorizontalAlignment.CENTER, verticalAlign = VerticalAlignment.CENTER + ) + + override fun render(posX: Int, posY: Int) { + Gui.drawRect(0, 0, width, height, selectColor) + itemRender.renderXYAligned(posX, posY, width, height) + } + }, listOf(tip), onClick = { + click() + SoundUtils.playClickSound() + }) + + fun render(posX: Int, posY: Int) { + renderable.render(posX, posY) + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideTablePage.kt b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideTablePage.kt new file mode 100644 index 000000000..85dd5bdda --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/guide/GuideTablePage.kt @@ -0,0 +1,33 @@ +package at.hannibal2.skyhanni.utils.guide + +import at.hannibal2.skyhanni.utils.CollectionUtils.tableStretchXPadding +import at.hannibal2.skyhanni.utils.CollectionUtils.tableStretchYPadding +import at.hannibal2.skyhanni.utils.RenderUtils.HorizontalAlignment +import at.hannibal2.skyhanni.utils.renderables.Renderable + +abstract class GuideTablePage( + val sizeX: Int, + val sizeY: Int, + paddingX: Int = 0, + paddingY: Int = 0, + val footerSpacing: Int = 2 +) : GuideRenderablePage(paddingX, paddingY) { + + fun update( + content: List<List<Renderable>>, + footer: List<Renderable> = emptyList() + ) { + val ySpace = (content + listOf(footer)).tableStretchYPadding(sizeY - paddingY * 2) + renderable = + Renderable.verticalContainer( + listOf( + Renderable.table( + content, + xPadding = content.tableStretchXPadding(sizeX - paddingX * 2), + yPadding = ySpace + ), + Renderable.horizontalContainer(footer, footerSpacing, horizontalAlign = HorizontalAlignment.CENTER) + ), spacing = ySpace + ) + } +} |