Skip to content

Commit 63eabec

Browse files
committed
added gui open command, added crosshair auto scaling
1 parent 11b0a9d commit 63eabec

File tree

5 files changed

+32
-57
lines changed

5 files changed

+32
-57
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.polyfrost.crosshair
2+
3+
import cc.polyfrost.oneconfig.utils.commands.annotations.Command
4+
import cc.polyfrost.oneconfig.utils.commands.annotations.Main
5+
import org.polyfrost.crosshair.config.ModConfig
6+
7+
@Command(value = PolyCrosshair.MODID)
8+
class ModCommand {
9+
10+
@Main
11+
fun openGui() {
12+
ModConfig.openGui()
13+
}
14+
15+
}

src/main/kotlin/org/polyfrost/crosshair/PolyCrosshair.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import cc.polyfrost.oneconfig.config.core.ConfigUtils
44
import cc.polyfrost.oneconfig.events.EventManager
55
import cc.polyfrost.oneconfig.events.event.ShutdownEvent
66
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe
7+
import cc.polyfrost.oneconfig.utils.commands.CommandManager
78
import net.minecraftforge.common.MinecraftForge
89
import net.minecraftforge.fml.common.Mod
910
import net.minecraftforge.fml.common.event.FMLInitializationEvent
@@ -34,6 +35,7 @@ object PolyCrosshair {
3435
ModConfig
3536
MinecraftForge.EVENT_BUS.register(CrosshairRenderer)
3637
EventManager.INSTANCE.register(this)
38+
CommandManager.INSTANCE.registerCommand(ModCommand())
3739
}
3840

3941
@Mod.EventHandler

src/main/kotlin/org/polyfrost/crosshair/config/Drawer.kt

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
3838

3939
var removeQueue = ArrayList<CrosshairEntry>()
4040

41-
var moveQueue = ArrayList<MoveType>()
42-
4341
private var scroll = 0f
4442

4543
private var scrollTarget = 0f
@@ -67,7 +65,7 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
6765
}
6866
resetButton.setClickAction {
6967
runAsync {
70-
reset()
68+
clear()
7169
}
7270
}
7371
saveButton.setClickAction {
@@ -110,16 +108,6 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
110108
}
111109

112110
override fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler) {
113-
if (moveQueue.isNotEmpty()) {
114-
var x = 0
115-
var y = 0
116-
for (i in moveQueue) {
117-
x += i.x
118-
y += i.y
119-
}
120-
move(x, y)
121-
moveQueue.clear()
122-
}
123111

124112
for (posY in 0..<ModConfig.canvaSize) {
125113
for (posX in 0..<ModConfig.canvaSize) {
@@ -202,6 +190,12 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
202190
return bufferedImage
203191
}
204192

193+
fun clear() {
194+
for (i in pixels) {
195+
i.isToggled = false
196+
}
197+
}
198+
205199
fun loadImage(image: BufferedImage?, save: Boolean, entry: CrosshairEntry = CrosshairEntry()): OneImage? {
206200
val loadedImage = OneImage(image)
207201
val dimensionsSame = loadedImage.width == loadedImage.height
@@ -242,42 +236,11 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
242236
return image
243237
}
244238

245-
fun reset() {
246-
val newEntry = CrosshairEntry()
247-
toBufferedImage(newEntry.img)?.let {
248-
loadImage(it, false, newEntry)
249-
}
250-
}
251-
252-
fun move(x: Int, y: Int) {
253-
val newPositions = HashMap<Pos, Int>()
254-
for (i in ModConfig.drawer) {
255-
val pos = indexToPos(i.key)
256-
val posX = pos.x + x
257-
val posY = pos.y + y
258-
pixels[i.key].isToggled = false
259-
if (posX !in 0..<ModConfig.canvaSize || posY !in 0..<ModConfig.canvaSize) continue
260-
newPositions[Pos(posX, posY)] = i.value
261-
}
262-
for (i in newPositions) {
263-
val index = i.key.y * 32 + i.key.x
264-
pixels[index].isToggled = true
265-
pixels[index].color = i.value
266-
}
267-
}
268-
269239
fun getElement(entry: CrosshairEntry): PresetElement {
270240
elements[entry] ?: elements.put(entry, PresetElement(entry))
271241
return elements[entry]!!
272242
}
273243

274-
enum class MoveType(val x: Int, val y: Int) {
275-
UP(0, -1),
276-
DOWN(0, 1),
277-
LEFT(-1, 0),
278-
RIGHT(1, 0)
279-
}
280-
281244
override fun finishUpAndClose() {
282245
val image = saveFromDrawer(true) ?: return
283246
ModConfig.newCurrentCrosshair.img = toBase64(image.image)
@@ -288,18 +251,11 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
288251

289252
override fun keyTyped(key: Char, keyCode: Int) {
290253
if (mc.currentScreen !is OneConfigGui) return
291-
if (keyCode == UKeyboard.KEY_W) moveQueue.add(MoveType.UP)
292-
if (keyCode == UKeyboard.KEY_S) {
293-
if (UKeyboard.isCtrlKeyDown()) {
294-
runAsync {
295-
save(saveFromDrawer(false))
296-
}
297-
} else {
298-
moveQueue.add(MoveType.DOWN)
254+
if (UKeyboard.isCtrlKeyDown() && keyCode == UKeyboard.KEY_S) {
255+
runAsync {
256+
save(saveFromDrawer(false))
299257
}
300258
}
301-
if (keyCode == UKeyboard.KEY_A) moveQueue.add(MoveType.LEFT)
302-
if (keyCode == UKeyboard.KEY_D) moveQueue.add(MoveType.RIGHT)
303259
}
304260

305261
}

src/main/kotlin/org/polyfrost/crosshair/elements/PresetElement.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class PresetElement(val crosshair: CrosshairEntry) : BasicElement(149, 149, Colo
7575
override fun onClick() {
7676
if (copyButton.isHovered) return
7777
if (removeButton.isHovered) return
78+
Drawer.clear()
7879
Drawer.loadImage(bufferedImage, false, crosshair)
7980
}
8081
}

src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ object CrosshairRenderer {
101101
GL.translate((UResolution.windowWidth / 2).toFloat(), (UResolution.windowHeight / 2).toFloat(), 0f)
102102
GL.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f)
103103
val scale = crosshair.scale / 100f
104-
val textureSize = if (ModConfig.mode) drawingImage.width else 16
105-
val size = ceil(textureSize * mcScale * scale).toInt()
106-
val translation = if (ModConfig.mode) if (crosshair.centered) (-size / 2).toFloat() else (-(size - mcScale) / 2).toInt().toFloat() else ceil(-7 * mcScale * scale)
104+
val textureSize = 16
105+
val autoScaledSize = if (ModConfig.canvaSize % 2 == 0) 16 else 15
106+
val size = ceil((if (ModConfig.mode) autoScaledSize else textureSize) * mcScale * scale).toInt()
107+
val translation = ceil((if (ModConfig.mode && crosshair.centered) -autoScaledSize / 2f else -7f) * mcScale * scale)
107108
GL.translate(translation, translation, 0f)
108109
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
109110
val c = getColor()

0 commit comments

Comments
 (0)