Skip to content

Commit c91af86

Browse files
committed
Implement more keyboard-only splicing table actions
1 parent bd38b4e commit c91af86

File tree

8 files changed

+260
-39
lines changed

8 files changed

+260
-39
lines changed

Common/src/main/kotlin/gay/object/hexdebug/blocks/splicing/SplicingTableBlockEntity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class SplicingTableBlockEntity(pos: BlockPos, state: BlockState) :
250250
if (writeList(list)) {
251251
shouldConsumeMedia = true
252252
selection = Selection.edge(typedSelection.start + 1)?.also {
253-
makeEdgeVisible(it.index)
253+
makeEdgeVisible(it)
254254
}
255255
pushUndoState(
256256
list = Some(list),

Common/src/main/kotlin/gay/object/hexdebug/config/HexDebugClientConfig.kt

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import at.petrak.hexcasting.api.utils.asTranslatedComponent
44
import com.mojang.blaze3d.platform.InputConstants
55
import gay.`object`.hexdebug.HexDebug
66
import gay.`object`.hexdebug.gui.splicing.SplicingTableScreen
7+
import gay.`object`.hexdebug.splicing.SplicingTableAction
78
import me.shedaniel.autoconfig.AutoConfig
89
import me.shedaniel.autoconfig.ConfigData
910
import me.shedaniel.autoconfig.ConfigHolder
@@ -42,7 +43,7 @@ object HexDebugClientConfig {
4243
}
4344
else -> i18n.asTranslatedComponent
4445
},
45-
getUnsafely(field, config, ConfigModifierKey(InputConstants.UNKNOWN.name)).inner,
46+
getUnsafely(field, config, ConfigModifierKey()).inner,
4647
)
4748
.setModifierDefaultValue { (getUnsafely(field, defaults) as ConfigModifierKey).inner }
4849
.setModifierSaveConsumer { setUnsafely(field, config, ConfigModifierKey(it)) }
@@ -103,19 +104,78 @@ object HexDebugClientConfig {
103104
@CollapsibleObject
104105
val enlightenedSplicingTableKeybinds = EnlightenedSplicingTableKeybinds()
105106

107+
@Suppress("MemberVisibilityCanBePrivate")
106108
class SplicingTableKeybinds {
109+
@Tooltip
110+
val overrideVanillaArrowKeys = true
111+
112+
val viewLeft = ConfigModifierKey(InputConstants.KEY_UP)
113+
val viewLeftPage = ConfigModifierKey(InputConstants.KEY_PAGEUP)
114+
val viewLeftFull = ConfigModifierKey(InputConstants.KEY_HOME)
115+
val viewRight = ConfigModifierKey(InputConstants.KEY_DOWN)
116+
val viewRightPage = ConfigModifierKey(InputConstants.KEY_PAGEDOWN)
117+
val viewRightFull = ConfigModifierKey(InputConstants.KEY_END)
118+
119+
val cursorLeft = ConfigModifierKey(InputConstants.KEY_LEFT)
120+
val cursorRight = ConfigModifierKey(InputConstants.KEY_RIGHT)
121+
122+
val expandSelectionLeft = ConfigModifierKey(InputConstants.KEY_LEFT, shift = true)
123+
val expandSelectionRight = ConfigModifierKey(InputConstants.KEY_RIGHT, shift = true)
124+
val moveSelectionLeft = ConfigModifierKey(InputConstants.KEY_LEFT, ctrl = true, shift = true)
125+
val moveSelectionRight = ConfigModifierKey(InputConstants.KEY_RIGHT, ctrl = true, shift = true)
126+
107127
val selectNone = ConfigModifierKey(InputConstants.KEY_A, ctrl = true, shift = true)
108128
val selectAll = ConfigModifierKey(InputConstants.KEY_A, ctrl = true)
129+
109130
val undo = ConfigModifierKey(InputConstants.KEY_Z, ctrl = true)
110131
val redo = ConfigModifierKey(InputConstants.KEY_Y, ctrl = true)
132+
111133
val nudgeLeft = ConfigModifierKey(InputConstants.KEY_LEFT, ctrl = true)
112134
val nudgeRight = ConfigModifierKey(InputConstants.KEY_RIGHT, ctrl = true)
135+
113136
val duplicate = ConfigModifierKey(InputConstants.KEY_D, ctrl = true)
114137
val delete = ConfigModifierKey(InputConstants.KEY_DELETE)
138+
val backspace = ConfigModifierKey(InputConstants.KEY_BACKSPACE)
139+
115140
val cut = ConfigModifierKey(InputConstants.KEY_X, ctrl = true)
116141
val copy = ConfigModifierKey(InputConstants.KEY_C, ctrl = true)
117142
val pasteSplat = ConfigModifierKey(InputConstants.KEY_V, ctrl = true)
118143
val pasteVerbatim = ConfigModifierKey(InputConstants.KEY_V, ctrl = true, shift = true)
144+
145+
fun getActionForKey(keyCode: Int, scanCode: Int): SplicingTableAction? {
146+
for ((key, action) in arrayOf(
147+
viewLeft to SplicingTableAction.VIEW_LEFT,
148+
viewLeftPage to SplicingTableAction.VIEW_LEFT_PAGE,
149+
viewLeftFull to SplicingTableAction.VIEW_LEFT_FULL,
150+
viewRight to SplicingTableAction.VIEW_RIGHT,
151+
viewRightPage to SplicingTableAction.VIEW_RIGHT_PAGE,
152+
viewRightFull to SplicingTableAction.VIEW_RIGHT_FULL,
153+
cursorLeft to SplicingTableAction.CURSOR_LEFT,
154+
cursorRight to SplicingTableAction.CURSOR_RIGHT,
155+
expandSelectionLeft to SplicingTableAction.EXPAND_SELECTION_LEFT,
156+
expandSelectionRight to SplicingTableAction.EXPAND_SELECTION_RIGHT,
157+
moveSelectionLeft to SplicingTableAction.MOVE_SELECTION_LEFT,
158+
moveSelectionRight to SplicingTableAction.MOVE_SELECTION_RIGHT,
159+
selectNone to SplicingTableAction.SELECT_NONE,
160+
selectAll to SplicingTableAction.SELECT_ALL,
161+
undo to SplicingTableAction.UNDO,
162+
redo to SplicingTableAction.REDO,
163+
nudgeLeft to SplicingTableAction.NUDGE_LEFT,
164+
nudgeRight to SplicingTableAction.NUDGE_RIGHT,
165+
duplicate to SplicingTableAction.DUPLICATE,
166+
delete to SplicingTableAction.DELETE,
167+
backspace to SplicingTableAction.BACKSPACE,
168+
cut to SplicingTableAction.CUT,
169+
copy to SplicingTableAction.COPY,
170+
pasteSplat to SplicingTableAction.PASTE_SPLAT,
171+
pasteVerbatim to SplicingTableAction.PASTE_VERBATIM,
172+
)) {
173+
if (key.matchesKey(keyCode, scanCode)) {
174+
return action
175+
}
176+
}
177+
return null
178+
}
119179
}
120180

121181
class EnlightenedSplicingTableKeybinds {

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/SplicingTableScreen.kt

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import net.minecraft.network.chat.Component
2626
import net.minecraft.sounds.SoundEvents
2727
import net.minecraft.world.InteractionHand
2828
import net.minecraft.world.entity.player.Inventory
29+
import org.lwjgl.glfw.GLFW
2930
import java.awt.Color
3031
import java.util.function.BiConsumer
3132
import kotlin.math.*
@@ -539,6 +540,16 @@ class SplicingTableScreen(
539540
override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
540541
// AbstractContainerScreen.keyPressed always returns true, so check our keys first
541542
if (keyPressedInner(keyCode, scanCode)) return true
543+
544+
if (HexDebugClientConfig.config.splicingTableKeybinds.overrideVanillaArrowKeys) {
545+
when (keyCode) {
546+
GLFW.GLFW_KEY_UP,
547+
GLFW.GLFW_KEY_DOWN,
548+
GLFW.GLFW_KEY_LEFT,
549+
GLFW.GLFW_KEY_RIGHT -> return true
550+
}
551+
}
552+
542553
return super.keyPressed(keyCode, scanCode, modifiers)
543554
}
544555

@@ -554,23 +565,8 @@ class SplicingTableScreen(
554565
return true
555566
}
556567

557-
val action = HexDebugClientConfig.config.splicingTableKeybinds.run {
558-
when {
559-
selectNone.matchesKey(keyCode, scanCode) -> SplicingTableAction.SELECT_NONE
560-
selectAll.matchesKey(keyCode, scanCode) -> SplicingTableAction.SELECT_ALL
561-
undo.matchesKey(keyCode, scanCode) -> SplicingTableAction.UNDO
562-
redo.matchesKey(keyCode, scanCode) -> SplicingTableAction.REDO
563-
nudgeLeft.matchesKey(keyCode, scanCode) -> SplicingTableAction.NUDGE_LEFT
564-
nudgeRight.matchesKey(keyCode, scanCode) -> SplicingTableAction.NUDGE_RIGHT
565-
duplicate.matchesKey(keyCode, scanCode) -> SplicingTableAction.DUPLICATE
566-
delete.matchesKey(keyCode, scanCode) -> SplicingTableAction.DELETE
567-
cut.matchesKey(keyCode, scanCode) -> SplicingTableAction.CUT
568-
copy.matchesKey(keyCode, scanCode) -> SplicingTableAction.COPY
569-
pasteSplat.matchesKey(keyCode, scanCode) -> SplicingTableAction.PASTE_SPLAT
570-
pasteVerbatim.matchesKey(keyCode, scanCode) -> SplicingTableAction.PASTE_VERBATIM
571-
else -> return false
572-
}
573-
}
568+
val action = HexDebugClientConfig.config.splicingTableKeybinds.getActionForKey(keyCode, scanCode)
569+
?: return false
574570

575571
if (data.isListReadable && action.test()) {
576572
action.onPress()
@@ -853,6 +849,8 @@ class SplicingTableScreen(
853849

854850
override val iotaView get() = data.list?.getOrNull(index)
855851

852+
private var wasLastSelection = false
853+
856854
override fun onPress() {
857855
onSelectIota(index)
858856
}
@@ -863,6 +861,17 @@ class SplicingTableScreen(
863861
return super.testHitbox(mouseX, mouseY) && mouseX >= x + 2 && mouseX < x + width - 2
864862
}
865863

864+
override fun updateFocus() {
865+
val selection = selection
866+
val isLastSelection = data.isInRange(index)
867+
&& selection is Selection.Range
868+
&& index == selection.to
869+
if (isLastSelection != wasLastSelection) {
870+
wasLastSelection = isLastSelection
871+
isFocused = isLastSelection && Minecraft.getInstance().lastInputType.isKeyboard
872+
}
873+
}
874+
866875
init {
867876
reload()
868877
}

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/widgets/SplicingTableButton.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package gay.`object`.hexdebug.gui.splicing.widgets
22

3+
import gay.`object`.hexdebug.config.HexDebugClientConfig
34
import gay.`object`.hexdebug.gui.splicing.SplicingTableScreen
5+
import net.minecraft.client.InputType
6+
import net.minecraft.client.Minecraft
47
import net.minecraft.client.gui.GuiGraphics
58
import net.minecraft.client.gui.components.AbstractButton
69
import net.minecraft.client.gui.components.Tooltip
@@ -61,4 +64,17 @@ abstract class SplicingTableButton(
6164
override fun updateWidgetNarration(output: NarrationElementOutput) = defaultButtonNarrationText(output)
6265

6366
open fun reload() {}
67+
68+
override fun updateTooltip() {
69+
if (HexDebugClientConfig.config.splicingTableKeybinds.overrideVanillaArrowKeys) {
70+
updateFocus()
71+
}
72+
super.updateTooltip()
73+
}
74+
75+
open fun updateFocus() {
76+
if (Minecraft.getInstance().lastInputType == InputType.KEYBOARD_ARROW) {
77+
isFocused = false
78+
}
79+
}
6480
}

Common/src/main/kotlin/gay/object/hexdebug/splicing/Selection.kt

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ import kotlin.math.min
1111
sealed class Selection private constructor(val from: Int, open val to: Int?) {
1212
abstract val start: Int
1313
abstract val end: Int?
14+
/** For ranges, the end index. For edges, the index of the iota to the left. */
15+
abstract val lastIndex: Int
1416

1517
protected val isValid by lazy { start >= 0 && (end?.let { it >= start } ?: true) }
1618

17-
val lastIndex by lazy { end ?: start }
18-
1919
val size by lazy { end?.let { it - start + 1 } ?: 0 }
2020

2121
abstract operator fun contains(value: Number): Boolean
2222

23-
abstract fun expandRight(extra: Int): Selection?
23+
/* Expand the selection. A positive value expands to the right, while a negative value expands to the left. */
24+
abstract fun expandBy(extra: Int): Selection?
2425

25-
fun moveBy(delta: Int) = of(start + delta, end?.plus(delta))
26+
abstract fun moveBy(delta: Int): Selection?
2627

2728
fun <T> subList(list: List<T>) = list.subList(start, end?.plus(1) ?: start).toList()
2829

@@ -39,15 +40,14 @@ sealed class Selection private constructor(val from: Int, open val to: Int?) {
3940
class Range private constructor(from: Int, override val to: Int) : Selection(from, to) {
4041
override val start = min(from, to)
4142
override val end = max(from, to)
43+
override val lastIndex = end
4244

4345
private val range = start..end
4446
override fun contains(value: Number) = value in range
4547

46-
override fun expandRight(extra: Int) = if (from <= to) {
47-
of(from, to + extra)
48-
} else {
49-
of(from + extra, to)
50-
}
48+
override fun expandBy(extra: Int) = of(from, to + extra)
49+
50+
override fun moveBy(delta: Int) = of(from + delta, to + delta)
5151

5252
companion object {
5353
fun of(from: Int, to: Int) = Range(from, to).takeIf { it.isValid }
@@ -59,15 +59,20 @@ sealed class Selection private constructor(val from: Int, open val to: Int?) {
5959
override val to = null
6060
override val start = index
6161
override val end = null
62+
override val lastIndex = index - 1
6263

6364
override fun contains(value: Number) = false
6465

65-
override fun expandRight(extra: Int) = when {
66-
extra > 0 -> range(index, index + extra)
66+
override fun expandBy(extra: Int) = when {
67+
// eg. index = 10, extra = 1 -> range(10, 10)
68+
extra > 0 -> range(index, index + extra - 1)
69+
// eg. index = 10, extra = -1 -> range(9, 9)
6770
extra < 0 -> range(index - 1, index + extra)
6871
else -> this
6972
}
7073

74+
override fun moveBy(delta: Int) = of(index + delta)
75+
7176
companion object {
7277
fun of(index: Int) = Edge(index).takeIf { it.isValid }
7378
}

0 commit comments

Comments
 (0)