Skip to content

Commit

Permalink
basic gui
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Jun 2, 2024
1 parent cff49af commit 410fea6
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cc.polyfrost.oneconfig.internal.assets;

import cc.polyfrost.oneconfig.renderer.asset.SVG;

public interface SVGs {
SVG KEYSTROKE = null;
}
24 changes: 13 additions & 11 deletions src/main/kotlin/me/imtoggle/quickswitch/ItemTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,24 @@ enum class ItemTypes(val displayName: String, val type: Any, val renderItem: Ite
WaterBucket("Water Bucket", Items.water_bucket, Items.water_bucket),
LavaBucket("Lava Bucket", Items.lava_bucket, Items.lava_bucket);

fun getRenderItem(index: Int): Item {
for (i in entries) {
if (i.ordinal == index) {
return i.renderItem
companion object {
fun getRenderItem(index: Int): Item {
for (i in entries) {
if (i.ordinal == index) {
return i.renderItem
}
}
return Item.getItemFromBlock(Blocks.air)
}
return Item.getItemFromBlock(Blocks.air)
}

fun getType(name: String): Any {
for (i in entries) {
if (i.displayName == name) {
return i.type
fun getType(name: String): Any {
for (i in entries) {
if (i.displayName == name) {
return i.type
}
}
return ItemBlock::class.java
}
return ItemBlock::class.java
}

}
52 changes: 52 additions & 0 deletions src/main/kotlin/me/imtoggle/quickswitch/KeyBindsRenderer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.imtoggle.quickswitch

import cc.polyfrost.oneconfig.config.elements.BasicOption
import cc.polyfrost.oneconfig.gui.elements.BasicButton
import cc.polyfrost.oneconfig.utils.InputHandler
import cc.polyfrost.oneconfig.utils.color.ColorPalette
import me.imtoggle.quickswitch.config.KeyBindEntry
import me.imtoggle.quickswitch.config.ModConfig
import me.imtoggle.quickswitch.element.KeyBindElement

@Suppress("UnstableAPIUsage")
object KeyBindsRenderer : BasicOption(null, null, "", "", "", "", 2) {

private val addButton = BasicButton(32, 32, "", BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY)

var keyElements = ArrayList<KeyBindElement>()

var removeQueue = ArrayList<KeyBindElement>()

var currentHeight = 0

init {
addButton.setClickAction {
val entry = KeyBindEntry()
ModConfig.entries.add(entry)
entry.onAdd()
}
}

override fun getHeight(): Int = currentHeight

override fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler) {
var renderY = y
for (element in keyElements) {
element.draw(vg, x.toFloat(), renderY.toFloat(), inputHandler)
renderY += 96
}
for (element in removeQueue) {
keyElements.remove(element)
element.entry.onRemove()
}
addButton.draw(vg, x.toFloat(), renderY.toFloat(), inputHandler)

currentHeight = renderY - y + 32
}

override fun keyTyped(key: Char, keyCode: Int) {
for (element in keyElements) {
element.keyBindButton.isKeyTyped(keyCode)
}
}
}
46 changes: 41 additions & 5 deletions src/main/kotlin/me/imtoggle/quickswitch/Selector.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
package me.imtoggle.quickswitch

import cc.polyfrost.oneconfig.config.elements.BasicOption
import cc.polyfrost.oneconfig.events.EventManager
import cc.polyfrost.oneconfig.events.event.RawMouseEvent
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe
import cc.polyfrost.oneconfig.utils.InputHandler
import me.imtoggle.quickswitch.element.ItemTypeElement

object Selector : BasicOption(null, null, "", "", "", "", 2) {
object Selector : BasicOption(null, null, "", "", "", "", 2) {

override fun getHeight(): Int {
TODO("Not yet implemented")
var elements: Array<ItemTypeElement> = Array(ItemTypes.entries.size) { ItemTypeElement(it, true) }

var adding: ItemTypeElement? = null

var xOffset = 0f
var yOffset = 0f
var stop = false

init {
EventManager.INSTANCE.register(this)
}

override fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler?) {
TODO("Not yet implemented")
@Subscribe
private fun onMouse(event: RawMouseEvent) {
if (event.button == 0 && event.state == 0) {
adding = null
stop = true
}
}

override fun getHeight(): Int = 48

override fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler) {
var renderX = x
for (element in elements) {
element.draw(vg, renderX.toFloat(), y.toFloat(), inputHandler)
renderX += 48
}
if (inputHandler.isMouseDown) {
adding?.let {
inputHandler.blockAllInput()
it.draw(vg, inputHandler.mouseX() - xOffset, inputHandler.mouseY() - yOffset, inputHandler)
}
}
if (stop) {
inputHandler.stopBlockingInput()
stop = false
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import cc.polyfrost.oneconfig.config.annotations.Exclude
import cc.polyfrost.oneconfig.config.core.OneKeyBind
import cc.polyfrost.oneconfig.libs.universal.UKeyboard
import me.imtoggle.quickswitch.ItemTypes
import me.imtoggle.quickswitch.KeyBindsRenderer
import me.imtoggle.quickswitch.QuickSwitch
import me.imtoggle.quickswitch.element.KeyBindElement
import java.lang.reflect.Field

class KeyBindEntry {
Expand All @@ -27,9 +29,11 @@ class KeyBindEntry {
fun onAdd() {
keyBind.setRunnable { run() }
ModConfig.addKeyBind(map, keyBind)
KeyBindsRenderer.keyElements.add(KeyBindElement(this))
}

fun onRemove() {
ModConfig.entries.remove(this)
ModConfig.removeKeyBind(map)
}

Expand Down
27 changes: 27 additions & 0 deletions src/main/kotlin/me/imtoggle/quickswitch/config/ModConfig.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package me.imtoggle.quickswitch.config

import cc.polyfrost.oneconfig.config.Config
import cc.polyfrost.oneconfig.config.annotations.CustomOption
import cc.polyfrost.oneconfig.config.core.ConfigUtils
import cc.polyfrost.oneconfig.config.core.OneKeyBind
import cc.polyfrost.oneconfig.config.data.*
import cc.polyfrost.oneconfig.config.elements.BasicOption
import cc.polyfrost.oneconfig.config.elements.OptionPage
import cc.polyfrost.oneconfig.internal.config.core.KeyBindHandler
import me.imtoggle.quickswitch.KeyBindsRenderer
import me.imtoggle.quickswitch.QuickSwitch
import me.imtoggle.quickswitch.Selector
import me.imtoggle.quickswitch.mixin.KeyBindHandlerAccessor
import java.lang.reflect.Field

object ModConfig : Config(Mod(QuickSwitch.NAME, ModType.UTIL_QOL), "${QuickSwitch.MODID}.json") {

@CustomOption(id = "selector")
var selector = Runnable { }

@CustomOption(id = "renderer")
var entries = ArrayList<KeyBindEntry>()

fun addKeyBind(map: MutableMap.MutableEntry<Field?, Any?>, keyBind: OneKeyBind) {
Expand All @@ -20,6 +30,23 @@ object ModConfig : Config(Mod(QuickSwitch.NAME, ModType.UTIL_QOL), "${QuickSwitc
(KeyBindHandler.INSTANCE as KeyBindHandlerAccessor).keyBinds.remove(map)
}

override fun getCustomOption(
field: Field,
annotation: CustomOption,
page: OptionPage,
mod: Mod,
migrate: Boolean
): BasicOption? {
var option: BasicOption? = null
when (annotation.id) {
"renderer" -> option = KeyBindsRenderer
"selector" -> option = Selector
}

ConfigUtils.getSubCategory(page, "General", "").options.add(option)
return option
}

init {
initialize()
for (entry in entries) {
Expand Down
82 changes: 82 additions & 0 deletions src/main/kotlin/me/imtoggle/quickswitch/element/ItemTypeElement.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package me.imtoggle.quickswitch.element

import cc.polyfrost.oneconfig.gui.OneConfigGui
import cc.polyfrost.oneconfig.gui.elements.BasicElement
import cc.polyfrost.oneconfig.libs.universal.UResolution
import cc.polyfrost.oneconfig.utils.InputHandler
import cc.polyfrost.oneconfig.utils.color.ColorPalette
import cc.polyfrost.oneconfig.utils.dsl.mc
import me.imtoggle.quickswitch.ItemTypes
import me.imtoggle.quickswitch.Selector
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.client.renderer.RenderHelper
import net.minecraft.item.ItemStack
import net.minecraftforge.client.event.GuiScreenEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.opengl.GL11

@Suppress("UnstableAPIUsage")
class ItemTypeElement(val id: Int, val action: Boolean): BasicElement(32, 32, ColorPalette.SECONDARY, true, 10f) {

data class RenderInfo(val x: Float, val y: Float)

private val item = ItemStack(ItemTypes.getRenderItem(id))
private var renderInfo: RenderInfo? = null
private var lastPressed = false

init {
MinecraftForge.EVENT_BUS.register(this)
}

override fun update(x: Float, y: Float, inputHandler: InputHandler) {
super.update(x, y, inputHandler)
if (lastPressed != pressed) {
lastPressed = pressed
if (pressed && action) {
Selector.xOffset = inputHandler.mouseX() - x
Selector.yOffset = inputHandler.mouseY() - y
Selector.adding = ItemTypeElement(id, false)
}
}
}

override fun draw(vg: Long, x: Float, y: Float, inputHandler: InputHandler) {
super.draw(vg, x, y, inputHandler)
renderInfo = RenderInfo(x + 4, y + 4)
}

@SubscribeEvent
fun draw(e: GuiScreenEvent.DrawScreenEvent.Post) {
val (x, y) = renderInfo ?: return
renderInfo = null
val oneConfigGui = mc.currentScreen as? OneConfigGui ?: return
val unscaleMC = 1 / UResolution.scaleFactor
val oneUIScale = OneConfigGui.getScaleFactor() * oneConfigGui.animationScaleFactor
val rawX = ((UResolution.windowWidth - 800 * oneUIScale) / 2f).toInt()
val rawY = ((UResolution.windowHeight - 768 * oneUIScale) / 2f).toInt()
GlStateManager.pushMatrix()
GL11.glEnable(GL11.GL_SCISSOR_TEST)
GL11.glScissor(rawX, rawY, (1024 * oneUIScale).toInt(), (696 * oneUIScale).toInt())
GlStateManager.scale(unscaleMC * oneUIScale, unscaleMC * oneUIScale, 1.0)
GlStateManager.pushMatrix()
GlStateManager.enableRescaleNormal()
GlStateManager.enableBlend()
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0)
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f)
GL11.glColor4f(1f, 1f, 1f, 1f)
RenderHelper.enableGUIStandardItemLighting()
val itemRenderer = mc.renderItem
GlStateManager.translate(x, y, 0f)
GlStateManager.scale(24 / 16f, 24 / 16f, 1f)
itemRenderer.renderItemAndEffectIntoGUI(item, 0, 0)
RenderHelper.disableStandardItemLighting()
GlStateManager.disableBlend()
GlStateManager.disableRescaleNormal()
GlStateManager.enableAlpha()
GlStateManager.popMatrix()
GL11.glDisable(GL11.GL_SCISSOR_TEST)
GlStateManager.popMatrix()
}

}
74 changes: 74 additions & 0 deletions src/main/kotlin/me/imtoggle/quickswitch/element/KeyBindButton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package me.imtoggle.quickswitch.element

import cc.polyfrost.oneconfig.config.core.OneKeyBind
import cc.polyfrost.oneconfig.events.EventManager
import cc.polyfrost.oneconfig.events.event.RawMouseEvent
import cc.polyfrost.oneconfig.gui.OneConfigGui
import cc.polyfrost.oneconfig.gui.elements.BasicButton
import cc.polyfrost.oneconfig.internal.assets.SVGs
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe
import cc.polyfrost.oneconfig.libs.universal.UKeyboard
import cc.polyfrost.oneconfig.utils.InputHandler
import cc.polyfrost.oneconfig.utils.color.ColorPalette

@Suppress("UnstableAPIUsage")
class KeyBindButton(
private val keyBind: OneKeyBind
) : BasicButton(256, 32, SVGs.KEYSTROKE, ALIGNMENT_JUSTIFIED, ColorPalette.SECONDARY) {
private val allKeysReleased get() = keyBind.size != 0 && !keyBind.isActive
private val recording get() = isToggled

private fun stopRecording() {
isToggled = false
OneConfigGui.INSTANCE.allowClose = true
}

init {
setToggleable(true)
setClickAction {
OneConfigGui.INSTANCE.allowClose = !recording
}
EventManager.INSTANCE.register(this)
}

@Subscribe
private fun onMouse(event: RawMouseEvent) {
if (recording && event.state == 1) {
keyBind.addKey(event.button, true)
}
}

override fun update(x: Float, y: Float, inputHandler: InputHandler) {
super.update(x, y, inputHandler)

if (recording) whileListening()
text = getDisplayText()
}

private fun whileListening() {
when {
isClicked -> keyBind.clearKeys()
allKeysReleased -> stopRecording()
}
}

private fun getDisplayText() = keyBind.display.ifEmpty {
if (recording) "Recording... (ESC to clear)"
else "None"
}

fun isKeyTyped(keyCode: Int): Boolean {
if (!recording) return false

when (keyCode) {
UKeyboard.KEY_ESCAPE -> {
keyBind.clearKeys()
stopRecording()
}

else -> keyBind.addKey(keyCode)
}

return true
}
}
Loading

0 comments on commit 410fea6

Please sign in to comment.