mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-18 03:44:54 -04:00
menu: improve keyboard navigation
This commit is contained in:
parent
4466a69b41
commit
00c2f7b1bf
@ -54,6 +54,8 @@ abstract class Element(val guiRenderer: GUIRenderer) : InputElement {
|
||||
|
||||
protected open var _prefSize: Vec2i = Vec2i.EMPTY
|
||||
|
||||
open val canFocus: Boolean get() = false
|
||||
|
||||
/**
|
||||
* If maxSize was infinity, what size would the element have? (Excluded margin!)
|
||||
*/
|
||||
|
@ -84,6 +84,9 @@ open class ButtonElement(
|
||||
forceApply()
|
||||
}
|
||||
|
||||
override val canFocus: Boolean
|
||||
get() = !disabled
|
||||
|
||||
|
||||
init {
|
||||
size = textElement.size + Vec2i(TEXT_PADDING * 2, TEXT_PADDING * 2)
|
||||
|
@ -31,7 +31,6 @@ abstract class Menu(
|
||||
val preferredElementWidth: Int = 150,
|
||||
) : Screen(guiRenderer) {
|
||||
private val elements: MutableList<Element> = mutableListOf()
|
||||
private var focusedIndex: Int = -1
|
||||
|
||||
private var maxElementWidth = -1
|
||||
private var totalHeight = -1
|
||||
@ -88,7 +87,6 @@ abstract class Menu(
|
||||
if (activeElement != element) {
|
||||
activeElement?.onMouseLeave()
|
||||
element?.onMouseEnter(delta)
|
||||
focusedIndex = elements.indexOf(element)
|
||||
activeElement = element
|
||||
return
|
||||
}
|
||||
@ -146,17 +144,35 @@ abstract class Menu(
|
||||
for (element in elements) {
|
||||
element.onClose()
|
||||
}
|
||||
focusedIndex = -1
|
||||
activeElement?.onMouseLeave()
|
||||
activeElement = null
|
||||
}
|
||||
|
||||
override fun onSpecialKey(key: InputSpecialKey, type: KeyChangeTypes) {
|
||||
super.onSpecialKey(key, type)
|
||||
if (type != KeyChangeTypes.RELEASE && key == InputSpecialKey.KEY_TAB) {
|
||||
focusedIndex++
|
||||
if (focusedIndex >= elements.size) {
|
||||
focusedIndex = 0
|
||||
var element: Element?
|
||||
var initialIndex = elements.indexOf(activeElement)
|
||||
if (initialIndex == -1) {
|
||||
initialIndex = 0
|
||||
}
|
||||
var index = initialIndex
|
||||
while (true) {
|
||||
index++
|
||||
if (index >= elements.size) {
|
||||
index = 0
|
||||
}
|
||||
if (index == initialIndex) {
|
||||
return
|
||||
}
|
||||
element = elements.getOrNull(index) ?: return
|
||||
if (element.canFocus) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (element == null) {
|
||||
return
|
||||
}
|
||||
val element = elements.getOrNull(focusedIndex) ?: return
|
||||
|
||||
activeElement?.onMouseLeave()
|
||||
element.onMouseEnter(Vec2i.EMPTY)
|
||||
|
Loading…
x
Reference in New Issue
Block a user