-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
326 additions
and
16 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/dummy/java/cc/polyfrost/oneconfig/internal/assets/SVGs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/me/imtoggle/quickswitch/KeyBindsRenderer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/kotlin/me/imtoggle/quickswitch/element/ItemTypeElement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
src/main/kotlin/me/imtoggle/quickswitch/element/KeyBindButton.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.