Merge pull request #2729 from kevlahnota/newmaster2

update dialog buttons
This commit is contained in:
Anthony Calosa
2023-03-22 16:25:49 +08:00
committed by GitHub
2 changed files with 242 additions and 285 deletions

View File

@@ -19,6 +19,8 @@ import com.github.tommyettinger.textra.TextraLabel;
import forge.Forge;
import forge.adventure.stage.GameHUD;
import forge.adventure.util.*;
import forge.sound.SoundEffectType;
import forge.sound.SoundSystem;
/**
* Base class for an GUI scene where the elements are loaded from a json file
@@ -27,26 +29,23 @@ public class UIScene extends Scene {
protected UIActor ui;
public static class Selectable<T extends Actor>
{
public static class Selectable<T extends Actor> {
public T actor;
public float getY()
{
public float getY() {
Actor act = actor;
float y = 0;
while (act!=null)
{
while (act != null) {
y += act.getY();
act = act.getParent();
}
return y;
}
public float getX()
{
public float getX() {
Actor act = actor;
float x = 0;
while (act!=null)
{
while (act != null) {
x += act.getX();
act = act.getParent();
}
@@ -57,43 +56,43 @@ public class UIScene extends Scene {
actor = newActor;
}
public void onSelect(UIScene scene)
{
public void onSelect(UIScene scene) {
}
public void onDeSelect()
{
public void onDeSelect() {
//actor.fire(UIScene.eventExit());
}
public void onPressDown(UIScene scene)
{
if(actor instanceof TextField)
{
public void onPressDown(UIScene scene) {
if (actor instanceof TextField) {
scene.requestTextInput(((TextField) actor).getText(), text -> ((TextField) actor).setText(text));
}
actor.fire(UIScene.eventTouchDown());
}
public void onPressUp()
{
public void onPressUp() {
actor.fire(UIScene.eventTouchUp());
}
public float yDiff(Selectable finalOne) {
return Math.abs(finalOne.getY() - getY());
}
public float xDiff(Selectable finalOne) {
return Math.abs(finalOne.getX() - getX());
}
}
static final public int ButtonYes = 0x1;
static final public int ButtonNo = 0x2;
static final public int ButtonOk = 0x4;
static final public int ButtonAbort = 0x8;
public Dialog prepareDialog(String header, int buttons, Runnable onOkOrYes) {
Dialog dialog =new Dialog(header, Controls.getSkin())
{
protected void result(Object object)
{
Dialog dialog = new Dialog(header, Controls.getSkin()) {
protected void result(Object object) {
SoundSystem.instance.play(SoundEffectType.ButtonPress, false);
if (onOkOrYes != null && object != null && object.equals(true))
onOkOrYes.run();
this.hide();
@@ -114,8 +113,8 @@ public class UIScene extends Scene {
dialog.setResizable(false);
return dialog;
}
public void showDialog(Dialog dialog)
{
public void showDialog(Dialog dialog) {
stage.addActor(dialog);
possibleSelectionStack.add(new Array<>());
addToSelectable(dialog.getContentTable());
@@ -147,39 +146,40 @@ public class UIScene extends Scene {
public Array<Array<Selectable>> possibleSelectionStack = new Array<>();
public Array<Dialog> dialogs = new Array<>();
public Array<Selectable> getPossibleSelection()
{
public Array<Selectable> getPossibleSelection() {
if (possibleSelectionStack.isEmpty())
possibleSelectionStack.add(ui.selectActors);
return possibleSelectionStack.get(possibleSelectionStack.size - 1);
}
protected Stage stage;
String uiFile;
public static InputEvent eventTouchUp()
{
public static InputEvent eventTouchUp() {
InputEvent event = new InputEvent();
event.setPointer(-1);
event.setType(InputEvent.Type.touchUp);
return event;
}
public static InputEvent eventTouchDown()
{
public static InputEvent eventTouchDown() {
InputEvent event = new InputEvent();
event.setPointer(-1);
event.setType(InputEvent.Type.touchDown);
return event;
}
public static InputEvent eventExit()
{
public static InputEvent eventExit() {
InputEvent event = new InputEvent();
event.setPointer(-1);
event.setType(InputEvent.Type.exit);
return event;
}
public static InputEvent eventEnter()
{
public static InputEvent eventEnter() {
InputEvent event = new InputEvent();
event.setPointer(-1);
event.setType(InputEvent.Type.enter);
@@ -190,24 +190,28 @@ public class UIScene extends Scene {
public boolean buttonUp(Controller controller, int keycode) {
return stage.keyUp(KeyBinding.controllerButtonToKey(controller, keycode));
}
@Override
public boolean buttonDown(Controller controller, int keycode) {
return stage.keyDown(KeyBinding.controllerButtonToKey(controller, keycode));
}
protected void addToSelectable(Table table) {
for(Cell cell:table.getCells())
{
for (Cell cell : table.getCells()) {
if (cell.getActor() != null && cell.getActor().getClass() != Actor.class && !(cell.getActor() instanceof Label) && !(cell.getActor() instanceof TextraLabel))
getPossibleSelection().add(new Selectable(cell.getActor()));
}
}
protected void addToSelectable(Button button)//prevent to addToSelectable(Table) fallback
{
getPossibleSelection().add(new Selectable(button));
}
protected void addToSelectable(Actor button) {
getPossibleSelection().add(new Selectable(button));
}
protected void addToSelectable(Selectable selectable) {
getPossibleSelection().add(selectable);
}
@@ -225,11 +229,13 @@ public class UIScene extends Scene {
keyReleased(keycode);
return super.keyUp(keycode);
}
@Override
public boolean keyDown(int keyCode) {
keyPressed(keyCode);
return super.keyDown(keyCode);
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
pointerMoved(screenX, screenY);
@@ -237,8 +243,7 @@ public class UIScene extends Scene {
}
};
ui = new UIActor(Config.instance().getFile(uiFile));
for(Actor actor:ui.getChildren())
{
for (Actor actor : ui.getChildren()) {
if (actor instanceof ScrollPane)
stage.setScrollFocus(actor);
}
@@ -249,20 +254,16 @@ public class UIScene extends Scene {
private void removeDialog() {
if(!dialogs.isEmpty())
{
if (!dialogs.isEmpty()) {
dialogs.get(dialogs.size - 1).remove();
dialogs.removeIndex(dialogs.size - 1);
if (!dialogs.isEmpty())
dialogs.get(dialogs.size - 1).show(stage);
}
if(possibleSelectionStack.isEmpty())
{
if (possibleSelectionStack.isEmpty()) {
getPossibleSelection();
}
else
{
} else {
possibleSelectionStack.removeIndex(possibleSelectionStack.size - 1);
}
}
@@ -295,24 +296,21 @@ public class UIScene extends Scene {
Forge.switchToLast();
return true;
}
public Selectable getSelected()
{
for(Selectable selectable: getPossibleSelection())
{
public Selectable getSelected() {
for (Selectable selectable : getPossibleSelection()) {
if (stage.getKeyboardFocus() == selectable.actor)
return selectable;
}
return null;
}
public boolean keyReleased(int keycode)
{
public boolean keyReleased(int keycode) {
ui.pressUp(keycode);
if(!dialogShowing())
{
if (!dialogShowing()) {
Button pressedButton = ui.buttonPressed(keycode);
if(pressedButton!=null)
{
if (pressedButton != null) {
if (pressedButton.isVisible())
pressedButton.fire(eventTouchUp());
}
@@ -325,16 +323,14 @@ public class UIScene extends Scene {
return true;
}
public boolean keyPressed(int keycode) {
Selectable selection = getSelected();
ui.pressDown(keycode);
if(stage.getKeyboardFocus() instanceof SelectBox)
{
if (stage.getKeyboardFocus() instanceof SelectBox) {
SelectBox box = (SelectBox) stage.getKeyboardFocus();
if(box.getScrollPane().hasParent())
{
if(KeyBinding.Use.isPressed(keycode))
{
if (box.getScrollPane().hasParent()) {
if (KeyBinding.Use.isPressed(keycode)) {
box.getSelection().choose(box.getList().getSelected());
box.getScrollPane().hide();
}
@@ -346,20 +342,16 @@ public class UIScene extends Scene {
selection.onPressDown(this);
}
if(KeyBinding.ScrollUp.isPressed(keycode))
{
if (KeyBinding.ScrollUp.isPressed(keycode)) {
Actor focus = stage.getScrollFocus();
if(focus!=null&&focus instanceof ScrollPane)
{
if (focus != null && focus instanceof ScrollPane) {
ScrollPane scroll = ((ScrollPane) focus);
scroll.setScrollY(scroll.getScrollY() - 20);
}
}
if(KeyBinding.ScrollDown.isPressed(keycode))
{
if (KeyBinding.ScrollDown.isPressed(keycode)) {
Actor focus = stage.getScrollFocus();
if(focus!=null&&focus instanceof ScrollPane)
{
if (focus != null && focus instanceof ScrollPane) {
ScrollPane scroll = ((ScrollPane) focus);
scroll.setScrollY(scroll.getScrollY() + 20);
}
@@ -368,18 +360,15 @@ public class UIScene extends Scene {
selectNextDown();
if (KeyBinding.Up.isPressed(keycode))
selectNextUp();
if(!(stage.getKeyboardFocus() instanceof Selector)&&!(stage.getKeyboardFocus() instanceof TextField)&&!(stage.getKeyboardFocus() instanceof Slider))
{
if (!(stage.getKeyboardFocus() instanceof Selector) && !(stage.getKeyboardFocus() instanceof TextField) && !(stage.getKeyboardFocus() instanceof Slider)) {
if (KeyBinding.Right.isPressed(keycode))
selectNextRight();
if (KeyBinding.Left.isPressed(keycode))
selectNextLeft();
}
if(!dialogShowing())
{
if (!dialogShowing()) {
Button pressedButton = ui.buttonPressed(keycode);
if(pressedButton!=null)
{
if (pressedButton != null) {
if (pressedButton.isVisible())
pressedButton.fire(eventTouchDown());
}
@@ -396,11 +385,13 @@ public class UIScene extends Scene {
public void disconnected(final Controller controller) {
ui.controllerDisconnected();
}
@Override
public void connected(final Controller controller) {
selectFirst();
ui.controllerConnected();
}
public boolean pointerMoved(int screenX, int screenY) {
unselectActors();
return false;
@@ -417,41 +408,35 @@ public class UIScene extends Scene {
}
}, 0.10f);
}
public void unselectActors() {
for (Selectable selectable : getPossibleSelection()) {
selectable.onDeSelect();
}
}
Array<Selectable> visibleSelection()
{
Array<Selectable> visibleSelection() {
Array<Selectable> selectables = new Array<>();
for (Selectable selectable : getPossibleSelection()) {
if(selectable.actor.isVisible())
{
if(selectable.actor instanceof Button)
{
if (selectable.actor.isVisible()) {
if (selectable.actor instanceof Button) {
if (!((Button) selectable.actor).isDisabled())
selectables.add(selectable);
}
else
{
} else {
selectables.add(selectable);
}
}
}
return selectables;
}
public void selectNextDown() {
if(getSelected()==null)
{
if (getSelected() == null) {
selectFirst();
}
else
{
} else {
Selectable current = getSelected();
Array<Selectable> candidates = new Array<>();
for(Selectable selectable:visibleSelection())
{
for (Selectable selectable : visibleSelection()) {
if (selectable.xDiff(current) < 0.1 && selectable != current)
candidates.add(selectable);
}
@@ -459,20 +444,16 @@ public class UIScene extends Scene {
candidates.addAll(visibleSelection());
Selectable finalOne = null;
Selectable fallback = null;
for(Selectable candidate:candidates)
{
for (Selectable candidate : candidates) {
if (fallback == null || candidate.getY() > fallback.getY())
fallback = candidate;
if(candidate.getY()<current.getY()&&(finalOne==null||current.yDiff(candidate)<current.yDiff(finalOne)))
{
if (candidate.getY() < current.getY() && (finalOne == null || current.yDiff(candidate) < current.yDiff(finalOne))) {
finalOne = candidate;
}
}
if (finalOne == null)
for(Selectable candidate:visibleSelection())
{
if(candidate.getY()<current.getY()&&(finalOne==null||current.yDiff(candidate)<current.yDiff(finalOne)))
{
for (Selectable candidate : visibleSelection()) {
if (candidate.getY() < current.getY() && (finalOne == null || current.yDiff(candidate) < current.yDiff(finalOne))) {
finalOne = candidate;
}
}
@@ -483,17 +464,14 @@ public class UIScene extends Scene {
}
}
private void selectNextLeft() {
if(getSelected()==null)
{
if (getSelected() == null) {
selectFirst();
}
else
{
} else {
Selectable current = getSelected();
Array<Selectable> candidates = new Array<>();
for(Selectable selectable:visibleSelection())
{
for (Selectable selectable : visibleSelection()) {
if (selectable.yDiff(current) < 0.1 && selectable != current)
candidates.add(selectable);
}
@@ -501,20 +479,16 @@ public class UIScene extends Scene {
candidates.addAll(visibleSelection());
Selectable finalOne = null;
Selectable fallback = null;
for(Selectable candidate:candidates)
{
for (Selectable candidate : candidates) {
if (fallback == null || candidate.getX() > fallback.getX())
fallback = candidate;
if(candidate.getX()<current.getX()&&(finalOne==null||current.xDiff(candidate)<current.xDiff(finalOne)))
{
if (candidate.getX() < current.getX() && (finalOne == null || current.xDiff(candidate) < current.xDiff(finalOne))) {
finalOne = candidate;
}
}
if (finalOne == null)
for(Selectable candidate:visibleSelection())
{
if(candidate.getX()<current.getX()&&(finalOne==null||current.xDiff(candidate)<current.xDiff(finalOne)))
{
for (Selectable candidate : visibleSelection()) {
if (candidate.getX() < current.getX() && (finalOne == null || current.xDiff(candidate) < current.xDiff(finalOne))) {
finalOne = candidate;
}
}
@@ -525,17 +499,14 @@ public class UIScene extends Scene {
}
}
private void selectNextRight() {
if(getSelected()==null)
{
if (getSelected() == null) {
selectFirst();
}
else
{
} else {
Selectable current = getSelected();
Array<Selectable> candidates = new Array<>();
for(Selectable selectable:visibleSelection())
{
for (Selectable selectable : visibleSelection()) {
if (selectable.yDiff(current) < 0.1 && selectable != current)
candidates.add(selectable);
}
@@ -543,20 +514,16 @@ public class UIScene extends Scene {
candidates.addAll(visibleSelection());
Selectable finalOne = null;
Selectable fallback = null;
for(Selectable candidate:candidates)
{
for (Selectable candidate : candidates) {
if (fallback == null || candidate.getX() < fallback.getX())
fallback = candidate;
if(candidate.getX()>current.getX()&&(finalOne==null||current.xDiff(candidate)<current.xDiff(finalOne)))
{
if (candidate.getX() > current.getX() && (finalOne == null || current.xDiff(candidate) < current.xDiff(finalOne))) {
finalOne = candidate;
}
}
if (finalOne == null)
for(Selectable candidate:visibleSelection())
{
if(candidate.getX()>current.getX()&&(finalOne==null||current.xDiff(candidate)<current.xDiff(finalOne)))
{
for (Selectable candidate : visibleSelection()) {
if (candidate.getX() > current.getX() && (finalOne == null || current.xDiff(candidate) < current.xDiff(finalOne))) {
finalOne = candidate;
}
}
@@ -571,16 +538,12 @@ public class UIScene extends Scene {
public void selectNextUp() {
if(getSelected()==null)
{
if (getSelected() == null) {
selectFirst();
}
else
{
} else {
Selectable current = getSelected();
Array<Selectable> candidates = new Array<>();
for(Selectable selectable:visibleSelection())
{
for (Selectable selectable : visibleSelection()) {
if (selectable.xDiff(current) < 0.1 && selectable != current)
candidates.add(selectable);
}
@@ -588,20 +551,16 @@ public class UIScene extends Scene {
candidates.addAll(visibleSelection());
Selectable finalOne = null;
Selectable fallback = null;
for(Selectable candidate:candidates)
{
for (Selectable candidate : candidates) {
if (fallback == null || candidate.getY() < fallback.getY())
fallback = candidate;
if(candidate.getY()>current.getY()&&(finalOne==null||current.yDiff(candidate)<current.yDiff(finalOne)))
{
if (candidate.getY() > current.getY() && (finalOne == null || current.yDiff(candidate) < current.yDiff(finalOne))) {
finalOne = candidate;
}
}
if (finalOne == null)//allowAllNow
for(Selectable candidate:visibleSelection())
{
if(candidate.getY()>current.getY()&&(finalOne==null||current.yDiff(candidate)<current.yDiff(finalOne)))
{
for (Selectable candidate : visibleSelection()) {
if (candidate.getY() > current.getY() && (finalOne == null || current.yDiff(candidate) < current.yDiff(finalOne))) {
finalOne = candidate;
}
}
@@ -616,21 +575,17 @@ public class UIScene extends Scene {
private void selectFirst() {
Selectable result = null;
for(Selectable candidate: getPossibleSelection())
{
if(result==null|| candidate.getY()>result.getY())
{
for (Selectable candidate : getPossibleSelection()) {
if (result == null || candidate.getY() > result.getY()) {
result = candidate;
}
}
selectActor(result);
}
ScrollPane scrollPaneOfActor(Actor actor)
{
while (actor!=null)
{
if(actor.getParent() instanceof ScrollPane)
{
ScrollPane scrollPaneOfActor(Actor actor) {
while (actor != null) {
if (actor.getParent() instanceof ScrollPane) {
return (ScrollPane) actor.getParent();
}
actor = actor.getParent();
@@ -645,12 +600,12 @@ public class UIScene extends Scene {
if (actor == null) return;
stage.setKeyboardFocus(actor.actor);
ScrollPane scrollPane = scrollPaneOfActor(actor.actor);
if(scrollPane!=null)
{
if (scrollPane != null) {
scrollPane.scrollTo(actor.actor.getX(), actor.actor.getY(), actor.actor.getWidth(), actor.actor.getHeight(), false, false);
}
actor.onSelect(this);
}
Image screenImage;
TextureRegion backgroundTexture;
@@ -659,6 +614,7 @@ public class UIScene extends Scene {
stage.cancelTouchFocus();
return super.leave();
}
@Override
public void enter() {
if (screenImage != null) {
@@ -674,6 +630,7 @@ public class UIScene extends Scene {
Gdx.input.setInputProcessor(stage);
super.enter();
}
public TextureRegion getUIBackground() {
try {
Actor a = ui.getChild(0);

Binary file not shown.