generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 23
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
13 changed files
with
672 additions
and
23 deletions.
There are no files selected for viewing
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/github/zly2006/reden/malilib/data/CommandHotkey.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,16 @@ | ||
package com.github.zly2006.reden.malilib.data | ||
|
||
import fi.dy.masa.malilib.hotkeys.IKeybind | ||
import fi.dy.masa.malilib.hotkeys.KeybindMulti | ||
import fi.dy.masa.malilib.hotkeys.KeybindSettings | ||
|
||
data class CommandHotkey( | ||
val commands: MutableList<String>, | ||
val keybind: IKeybind | ||
) { | ||
companion object { | ||
fun new(): CommandHotkey { | ||
return CommandHotkey(mutableListOf(), KeybindMulti.fromStorageString("", KeybindSettings.DEFAULT)) | ||
} | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
src/main/java/com/github/zly2006/reden/malilib/gui/GuiCommandHotkeyListEdit.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,141 @@ | ||
package com.github.zly2006.reden.malilib.gui | ||
|
||
import com.github.zly2006.reden.malilib.data.CommandHotkey | ||
import com.github.zly2006.reden.malilib.gui.widget.WidgetCommandHotkeyList | ||
import com.github.zly2006.reden.malilib.gui.widget.WidgetCommandHotkeyListEntry | ||
import com.github.zly2006.reden.malilib.options.RedenConfigCommandHotkeyList | ||
import fi.dy.masa.malilib.config.ConfigManager | ||
import fi.dy.masa.malilib.event.InputEventHandler | ||
import fi.dy.masa.malilib.gui.GuiListBase | ||
import fi.dy.masa.malilib.gui.button.ConfigButtonKeybind | ||
import fi.dy.masa.malilib.gui.interfaces.IConfigGui | ||
import fi.dy.masa.malilib.gui.interfaces.IDialogHandler | ||
import fi.dy.masa.malilib.render.RenderUtils | ||
import fi.dy.masa.malilib.util.GuiUtils | ||
import fi.dy.masa.malilib.util.KeyCodes | ||
import fi.dy.masa.malilib.util.StringUtils | ||
import net.minecraft.client.gui.DrawContext | ||
import net.minecraft.client.gui.screen.Screen | ||
|
||
class GuiCommandHotkeyListEdit( | ||
val config: RedenConfigCommandHotkeyList, | ||
val configGui: IConfigGui, | ||
val dialogHandler: IDialogHandler?, | ||
parent: Screen? | ||
) : GuiListBase<CommandHotkey, WidgetCommandHotkeyListEntry, WidgetCommandHotkeyList>(0, 0) { | ||
|
||
private var dialogWidth: Int = 0 | ||
private var dialogHeight: Int = 0 | ||
private var dialogLeft: Int = 0 | ||
private var dialogTop: Int = 0 | ||
|
||
var activeKeybindButton: ConfigButtonKeybind? = null | ||
|
||
init { | ||
title = StringUtils.translate("reden.widget.title.commandHotkeyListEdit") | ||
if (dialogHandler == null) { | ||
setParent(parent) | ||
} | ||
} | ||
|
||
private fun setWidthAndHeight() { | ||
dialogWidth = 400 | ||
dialogHeight = GuiUtils.getScaledWindowHeight() - 90 | ||
} | ||
|
||
private fun centerOnScreen() { | ||
if (parent != null) { | ||
val parent = parent!! | ||
dialogLeft = parent.width / 2 - dialogWidth / 2 | ||
dialogTop = parent.height / 2 - dialogHeight / 2 | ||
} else { | ||
dialogLeft = 20 | ||
dialogTop = 20 | ||
} | ||
} | ||
|
||
override fun initGui() { | ||
setWidthAndHeight() | ||
centerOnScreen() | ||
reCreateListWidget() | ||
super.initGui() | ||
} | ||
|
||
override fun removed() { | ||
if (listWidget!!.wereConfigsModified()) { | ||
listWidget!!.applyPendingModifications() | ||
ConfigManager.getInstance().onConfigsChanged(configGui.modId) | ||
InputEventHandler.getKeybindManager().updateUsedKeys() | ||
} | ||
super.removed() | ||
} | ||
|
||
override fun createListWidget(listX: Int, listY: Int): WidgetCommandHotkeyList { | ||
return WidgetCommandHotkeyList( | ||
dialogLeft + 10, | ||
dialogTop + 20, | ||
browserWidth, | ||
browserHeight, | ||
dialogWidth - 100, | ||
this | ||
) | ||
} | ||
|
||
override fun getBrowserWidth(): Int { | ||
return dialogWidth - 14 | ||
} | ||
|
||
override fun getBrowserHeight(): Int { | ||
return dialogHeight - 30 | ||
} | ||
|
||
override fun render(drawContext: DrawContext?, mouseX: Int, mouseY: Int, partialTicks: Float) { | ||
if (parent != null) { | ||
parent!!.render(drawContext, mouseX, mouseY, partialTicks) | ||
} | ||
|
||
super.render(drawContext, mouseX, mouseY, partialTicks) | ||
} | ||
|
||
override fun drawScreenBackground(mouseX: Int, mouseY: Int) { | ||
RenderUtils.drawOutlinedBox( | ||
dialogLeft, dialogTop, dialogWidth, dialogHeight, | ||
0xFF000000.toInt(), | ||
0xFF999999.toInt() | ||
) | ||
} | ||
|
||
override fun drawTitle(drawContext: DrawContext?, mouseX: Int, mouseY: Int, partialTicks: Float) { | ||
drawStringWithShadow(drawContext, title, dialogLeft + 10, dialogTop + 6, 0xFFFFFFFF.toInt()) | ||
} | ||
|
||
override fun onKeyTyped(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { | ||
if (activeKeybindButton != null) { | ||
if (activeKeybindButton!!.isSelected) { | ||
activeKeybindButton!!.onKeyPressed(keyCode) | ||
return true | ||
} | ||
} | ||
|
||
return if (keyCode == KeyCodes.KEY_ESCAPE && dialogHandler != null) { | ||
dialogHandler.closeDialog() | ||
true | ||
} else { | ||
super.onKeyTyped(keyCode, scanCode, modifiers) | ||
} | ||
} | ||
|
||
override fun onMouseClicked(mouseX: Int, mouseY: Int, mouseButton: Int): Boolean { | ||
if (super.onMouseClicked(mouseX, mouseY, mouseButton)) { | ||
return true | ||
} | ||
|
||
if (activeKeybindButton != null) { | ||
activeKeybindButton!!.onClearSelection() | ||
activeKeybindButton = null | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/github/zly2006/reden/malilib/gui/button/ConfigButtonCommandHotkeyList.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,39 @@ | ||
package com.github.zly2006.reden.malilib.gui.button | ||
|
||
import com.github.zly2006.reden.malilib.gui.GuiCommandHotkeyListEdit | ||
import com.github.zly2006.reden.malilib.options.RedenConfigCommandHotkeyList | ||
import fi.dy.masa.malilib.gui.GuiBase | ||
import fi.dy.masa.malilib.gui.button.ButtonGeneric | ||
import fi.dy.masa.malilib.gui.interfaces.IConfigGui | ||
import fi.dy.masa.malilib.gui.interfaces.IDialogHandler | ||
import fi.dy.masa.malilib.util.GuiUtils | ||
import fi.dy.masa.malilib.util.StringUtils | ||
|
||
class ConfigButtonCommandHotkeyList( | ||
x: Int, | ||
y: Int, | ||
width: Int, | ||
height: Int, | ||
private val config: RedenConfigCommandHotkeyList, | ||
private val configGui: IConfigGui, | ||
private val dialogHandler: IDialogHandler? | ||
) : ButtonGeneric(x, y, width, height, "") { | ||
|
||
init { | ||
updateDisplayString() | ||
} | ||
|
||
override fun onMouseClickedImpl(mouseX: Int, mouseY: Int, mouseButton: Int): Boolean { | ||
super.onMouseClickedImpl(mouseX, mouseY, mouseButton) | ||
if (dialogHandler != null) { | ||
dialogHandler.openDialog(GuiCommandHotkeyListEdit(config, configGui, dialogHandler, null)) | ||
} else { | ||
GuiBase.openGui(GuiCommandHotkeyListEdit(config, configGui, null, GuiUtils.getCurrentScreen())) | ||
} | ||
return true | ||
} | ||
|
||
override fun updateDisplayString() { | ||
displayString = StringUtils.translate("reden.widget.button.commandHotkeyListEdit") | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/github/zly2006/reden/malilib/gui/button/ConfigButtonKeybindInList.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,28 @@ | ||
package com.github.zly2006.reden.malilib.gui.button | ||
|
||
import com.github.zly2006.reden.malilib.gui.GuiCommandHotkeyListEdit | ||
import fi.dy.masa.malilib.gui.button.ConfigButtonKeybind | ||
import fi.dy.masa.malilib.gui.interfaces.IKeybindConfigGui | ||
import fi.dy.masa.malilib.hotkeys.IKeybind | ||
|
||
class ConfigButtonKeybindInList( | ||
x: Int, | ||
y: Int, | ||
width: Int, | ||
height: Int, | ||
keybind: IKeybind, | ||
private val listGui: GuiCommandHotkeyListEdit | ||
) : ConfigButtonKeybind(x, y, width, height, keybind, listGui.configGui as IKeybindConfigGui) { | ||
override fun onMouseClickedImpl(mouseX: Int, mouseY: Int, mouseButton: Int): Boolean { | ||
super.onMouseClickedImpl(mouseX, mouseY, mouseButton) | ||
if (selected) { | ||
listGui.activeKeybindButton = this | ||
} | ||
return true | ||
} | ||
|
||
override fun onClearSelection() { | ||
super.onClearSelection() | ||
listGui.activeKeybindButton = null | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/com/github/zly2006/reden/malilib/gui/widget/WidgetCommandHotkeyList.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,81 @@ | ||
package com.github.zly2006.reden.malilib.gui.widget | ||
|
||
import com.github.zly2006.reden.malilib.data.CommandHotkey | ||
import com.github.zly2006.reden.malilib.gui.GuiCommandHotkeyListEdit | ||
import com.github.zly2006.reden.malilib.options.RedenConfigCommandHotkeyList | ||
import fi.dy.masa.malilib.gui.widgets.WidgetListConfigOptionsBase | ||
|
||
class WidgetCommandHotkeyList( | ||
x: Int, | ||
y: Int, | ||
width: Int, | ||
height: Int, | ||
configWidth: Int, | ||
val parent: GuiCommandHotkeyListEdit | ||
) : WidgetListConfigOptionsBase<CommandHotkey, WidgetCommandHotkeyListEntry>(x, y, width, height, configWidth) { | ||
|
||
val config: RedenConfigCommandHotkeyList = parent.config | ||
|
||
init { | ||
config.saveCallback = { | ||
for (widget in listWidgets) { | ||
widget.syncWithConfig() | ||
} | ||
} | ||
} | ||
|
||
override fun getAllEntries(): MutableCollection<CommandHotkey> { | ||
return config.commandHotkeyList | ||
} | ||
|
||
override fun reCreateListEntryWidgets() { | ||
if (listContents.size == 0) { | ||
listWidgets.clear() | ||
maxVisibleBrowserEntries = 1 | ||
|
||
val x = posX + 2 | ||
val y = posY + 4 + browserEntriesOffsetY | ||
|
||
listWidgets.add(createListEntryWidget(x, y, -1, false, CommandHotkey.new())) | ||
scrollbar.maxValue = 0 | ||
} else { | ||
super.reCreateListEntryWidgets() | ||
} | ||
} | ||
|
||
override fun createListEntryWidget( | ||
x: Int, | ||
y: Int, | ||
listIndex: Int, | ||
isOdd: Boolean, | ||
entry: CommandHotkey? | ||
): WidgetCommandHotkeyListEntry { | ||
if (listIndex >= 0 && listIndex < config.commandHotkeyList.size) { | ||
val defaultValue = | ||
if (config.defaultList.size > listIndex) config.defaultList[listIndex] else CommandHotkey.new() | ||
return WidgetCommandHotkeyListEntry( | ||
x, | ||
y, | ||
browserEntryWidth, | ||
browserEntryHeight, | ||
listIndex, | ||
config.commandHotkeyList[listIndex], | ||
this, | ||
isOdd, | ||
defaultValue | ||
) | ||
} else { | ||
return WidgetCommandHotkeyListEntry( | ||
x, | ||
y, | ||
browserEntryWidth, | ||
browserEntryHeight, | ||
listIndex, | ||
CommandHotkey.new(), | ||
this, | ||
isOdd, | ||
CommandHotkey.new() | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.