diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/HOptionChoice.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/HOptionChoice.java new file mode 100644 index 00000000..3e958b1a --- /dev/null +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/HOptionChoice.java @@ -0,0 +1,56 @@ +package com.bartlomiejpluta.base.api.game.gui.component; + +import com.bartlomiejpluta.base.api.game.input.Key; +import com.bartlomiejpluta.base.api.game.input.KeyAction; +import com.bartlomiejpluta.base.api.game.input.KeyEvent; + +import java.util.EnumSet; + +public class HOptionChoice extends HLayout { + private static final EnumSet ACTIONS = EnumSet.of(KeyAction.PRESS, KeyAction.REPEAT); + private int selected = 0; + + @Override + public void focus() { + super.focus(); + + if (!children.isEmpty()) { + children.get(selected).focus(); + } + } + + @Override + public void handleKeyEvent(KeyEvent event) { + if (children.isEmpty()) { + return; + } + + // First we want to propagate it down tree + children.get(selected).handleKeyEvent(event); + + // If event is still not consumed, we try to consume it right here + if (event.isConsumed()) { + return; + } + + if (event.getKey() == Key.KEY_RIGHT && ACTIONS.contains(event.getAction())) { + blurAll(); + selected = (++selected) % children.size(); + children.get(selected).focus(); + event.consume(); + + } else if (event.getKey() == Key.KEY_LEFT && ACTIONS.contains(event.getAction())) { + blurAll(); + var size = children.size(); + selected = (((--selected) % size) + size) % size; + children.get(selected).focus(); + event.consume(); + } + } + + private void blurAll() { + for (var child : children) { + child.blur(); + } + } +} diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/VOptionChoice.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/VOptionChoice.java new file mode 100644 index 00000000..e547ffcf --- /dev/null +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/VOptionChoice.java @@ -0,0 +1,61 @@ +package com.bartlomiejpluta.base.api.game.gui.component; + +import com.bartlomiejpluta.base.api.game.input.Key; +import com.bartlomiejpluta.base.api.game.input.KeyAction; +import com.bartlomiejpluta.base.api.game.input.KeyEvent; + +import java.util.EnumSet; + +public class VOptionChoice extends VLayout { + private static final EnumSet ACTIONS = EnumSet.of(KeyAction.PRESS, KeyAction.REPEAT); + private int selected = 0; + + @Override + public void focus() { + super.focus(); + + if (!children.isEmpty()) { + children.get(selected).focus(); + } + } + + @Override + public void handleKeyEvent(KeyEvent event) { + if (children.isEmpty()) { + return; + } + + // First we want to propagate it down tree + children.get(selected).handleKeyEvent(event); + + // If event is still not consumed, we try to consume it right here + if (event.isConsumed()) { + return; + } + + if (event.getKey() == Key.KEY_DOWN) { + if (ACTIONS.contains(event.getAction())) { + blurAll(); + selected = (++selected) % children.size(); + children.get(selected).focus(); + event.consume(); + } + } else if (event.getKey() == Key.KEY_UP) { + if (ACTIONS.contains(event.getAction())) { + blurAll(); + var size = children.size(); + selected = (((--selected) % size) + size) % size; + children.get(selected).focus(); + event.consume(); + } + } else { + + } + } + + private void blurAll() { + for (var child : children) { + child.blur(); + } + } +}