From 7a103e7e706e72ec31c52bf80a90d7854cefce80 Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Wed, 22 Mar 2023 16:23:10 +0800 Subject: [PATCH] update dialog buttons - increase button_press volume --- .../src/forge/adventure/scene/UIScene.java | 527 ++++++++---------- forge-gui/res/sound/button_press.mp3 | Bin 1560 -> 1632 bytes 2 files changed, 242 insertions(+), 285 deletions(-) diff --git a/forge-gui-mobile/src/forge/adventure/scene/UIScene.java b/forge-gui-mobile/src/forge/adventure/scene/UIScene.java index 821c034968c..0e8fe6f184b 100644 --- a/forge-gui-mobile/src/forge/adventure/scene/UIScene.java +++ b/forge-gui-mobile/src/forge/adventure/scene/UIScene.java @@ -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,86 +29,83 @@ public class UIScene extends Scene { protected UIActor ui; - public static class Selectable - { + public static class Selectable { public T actor; - public float getY() - { - Actor act=actor; - float y=0; - while (act!=null) - { - y+=act.getY(); - act=act.getParent(); + + public float getY() { + Actor act = actor; + float y = 0; + while (act != null) { + y += act.getY(); + act = act.getParent(); } return y; } - public float getX() - { - Actor act=actor; - float x=0; - while (act!=null) - { - x+=act.getX(); - act=act.getParent(); + + public float getX() { + Actor act = actor; + float x = 0; + while (act != null) { + x += act.getX(); + act = act.getParent(); } return x; } public Selectable(T newActor) { - actor=newActor; + 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) - { - scene.requestTextInput(((TextField) actor).getText(),text-> ((TextField) actor).setText(text)); + + 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()); + return Math.abs(finalOne.getY() - getY()); } + public float xDiff(Selectable finalOne) { - return Math.abs(finalOne.getX()-getX()); + 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; + + 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) - { - if(onOkOrYes!=null&&object!=null&&object.equals(true)) + 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(); + this.hide(); removeDialog(); } }; - if((buttons&ButtonYes)!=0) + if ((buttons & ButtonYes) != 0) dialog.button(Forge.getLocalizer().getMessage("lblYes"), true); - if((buttons&ButtonNo)!=0) + if ((buttons & ButtonNo) != 0) dialog.button(Forge.getLocalizer().getMessage("lblNo"), false); - if((buttons&ButtonOk)!=0) + if ((buttons & ButtonOk) != 0) dialog.button(Forge.getLocalizer().getMessage("lblOk"), true); - if((buttons&ButtonAbort)!=0) + if ((buttons & ButtonAbort) != 0) dialog.button(Forge.getLocalizer().getMessage("lblAbort"), false); dialog.setMovable(false); @@ -114,16 +113,16 @@ 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()); addToSelectable(dialog.getButtonTable()); - dialog.getColor().a=0; + dialog.getColor().a = 0; stage.setKeyboardFocus(dialog); stage.setScrollFocus(dialog); - for(Dialog otherDialogs:dialogs) + for (Dialog otherDialogs : dialogs) otherDialogs.hide(); dialogs.add(dialog); selectFirst(); @@ -136,7 +135,7 @@ public class UIScene extends Scene { { @Override protected void result(@Null Object object) { - removeDialog(); + removeDialog(); } }; keyboardDialog.setText(text); @@ -145,41 +144,42 @@ public class UIScene extends Scene { //possibleSelection=keyboardDialog.keys(); } - public Array< Array> possibleSelectionStack=new Array<>(); - public Array< Dialog> dialogs=new Array<>(); - public Array getPossibleSelection() - { - if(possibleSelectionStack.isEmpty()) + public Array> possibleSelectionStack = new Array<>(); + public Array dialogs = new Array<>(); + + public Array getPossibleSelection() { + if (possibleSelectionStack.isEmpty()) possibleSelectionStack.add(ui.selectActors); - return possibleSelectionStack.get(possibleSelectionStack.size-1); - } + 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); @@ -188,26 +188,30 @@ public class UIScene extends Scene { @Override public boolean buttonUp(Controller controller, int keycode) { - return stage.keyUp(KeyBinding.controllerButtonToKey(controller,keycode)); + return stage.keyUp(KeyBinding.controllerButtonToKey(controller, keycode)); } + @Override public boolean buttonDown(Controller controller, int keycode) { - return stage.keyDown(KeyBinding.controllerButtonToKey(controller,keycode)); + return stage.keyDown(KeyBinding.controllerButtonToKey(controller, keycode)); } + protected void addToSelectable(Table table) { - for(Cell cell:table.getCells()) - { - if(cell.getActor()!=null&&cell.getActor().getClass()!=Actor.class&&!(cell.getActor()instanceof Label)&&!(cell.getActor()instanceof TextraLabel)) + 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); } @@ -222,14 +226,16 @@ public class UIScene extends Scene { stage = new Stage(new ScalingViewport(Scaling.stretch, getIntendedWidth(), getIntendedHeight())) { @Override public boolean keyUp(int keycode) { - keyReleased(keycode); + keyReleased(keycode); return super.keyUp(keycode); } + @Override public boolean keyDown(int keyCode) { - keyPressed(keyCode); + keyPressed(keyCode); return super.keyDown(keyCode); } + @Override public boolean mouseMoved(int screenX, int screenY) { pointerMoved(screenX, screenY); @@ -237,9 +243,8 @@ public class UIScene extends Scene { } }; ui = new UIActor(Config.instance().getFile(uiFile)); - for(Actor actor:ui.getChildren()) - { - if(actor instanceof ScrollPane) + for (Actor actor : ui.getChildren()) { + if (actor instanceof ScrollPane) stage.setScrollFocus(actor); } possibleSelectionStack.add(ui.selectActors); @@ -249,21 +254,17 @@ public class UIScene extends Scene { private void removeDialog() { - if(!dialogs.isEmpty()) - { - dialogs.get(dialogs.size-1).remove(); - dialogs.removeIndex(dialogs.size-1); + 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 (!dialogs.isEmpty()) + dialogs.get(dialogs.size - 1).show(stage); } - if(possibleSelectionStack.isEmpty()) - { + if (possibleSelectionStack.isEmpty()) { getPossibleSelection(); - } - else - { - possibleSelectionStack.removeIndex(possibleSelectionStack.size-1); + } else { + possibleSelectionStack.removeIndex(possibleSelectionStack.size - 1); } } @@ -295,92 +296,80 @@ public class UIScene extends Scene { Forge.switchToLast(); return true; } - public Selectable getSelected() - { - for(Selectable selectable: getPossibleSelection()) - { - if(stage.getKeyboardFocus()==selectable.actor) + + 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()) - { - Button pressedButton=ui.buttonPressed(keycode); - if(pressedButton!=null) - { - if(pressedButton.isVisible()) + if (!dialogShowing()) { + Button pressedButton = ui.buttonPressed(keycode); + if (pressedButton != null) { + if (pressedButton.isVisible()) pressedButton.fire(eventTouchUp()); } } - if(KeyBinding.Use.isPressed(keycode)){ - if(getSelected()!=null) + if (KeyBinding.Use.isPressed(keycode)) { + if (getSelected() != null) getSelected().onPressUp();//order is important, this might remove a dialog } return true; } + public boolean keyPressed(int keycode) { - Selectable selection=getSelected(); + Selectable selection = getSelected(); ui.pressDown(keycode); - if(stage.getKeyboardFocus() instanceof SelectBox) - { - SelectBox box=(SelectBox) stage.getKeyboardFocus(); - if(box.getScrollPane().hasParent()) - { - if(KeyBinding.Use.isPressed(keycode)) - { + if (stage.getKeyboardFocus() instanceof SelectBox) { + SelectBox box = (SelectBox) stage.getKeyboardFocus(); + if (box.getScrollPane().hasParent()) { + if (KeyBinding.Use.isPressed(keycode)) { box.getSelection().choose(box.getList().getSelected()); box.getScrollPane().hide(); } return false; } } - if(KeyBinding.Use.isPressed(keycode)){ - if(selection!=null) + if (KeyBinding.Use.isPressed(keycode)) { + if (selection != null) selection.onPressDown(this); } - if(KeyBinding.ScrollUp.isPressed(keycode)) - { - Actor focus=stage.getScrollFocus(); - if(focus!=null&&focus instanceof ScrollPane) - { - ScrollPane scroll=((ScrollPane)focus); - scroll.setScrollY(scroll.getScrollY()-20); + if (KeyBinding.ScrollUp.isPressed(keycode)) { + Actor focus = stage.getScrollFocus(); + if (focus != null && focus instanceof ScrollPane) { + ScrollPane scroll = ((ScrollPane) focus); + scroll.setScrollY(scroll.getScrollY() - 20); } } - if(KeyBinding.ScrollDown.isPressed(keycode)) - { - Actor focus=stage.getScrollFocus(); - if(focus!=null&&focus instanceof ScrollPane) - { - ScrollPane scroll=((ScrollPane)focus); - scroll.setScrollY(scroll.getScrollY()+20); + if (KeyBinding.ScrollDown.isPressed(keycode)) { + Actor focus = stage.getScrollFocus(); + if (focus != null && focus instanceof ScrollPane) { + ScrollPane scroll = ((ScrollPane) focus); + scroll.setScrollY(scroll.getScrollY() + 20); } } - if(KeyBinding.Down.isPressed(keycode)) + if (KeyBinding.Down.isPressed(keycode)) selectNextDown(); - if(KeyBinding.Up.isPressed(keycode)) + if (KeyBinding.Up.isPressed(keycode)) selectNextUp(); - if(!(stage.getKeyboardFocus() instanceof Selector)&&!(stage.getKeyboardFocus() instanceof TextField)&&!(stage.getKeyboardFocus() instanceof Slider)) - { - if(KeyBinding.Right.isPressed(keycode)) + if (!(stage.getKeyboardFocus() instanceof Selector) && !(stage.getKeyboardFocus() instanceof TextField) && !(stage.getKeyboardFocus() instanceof Slider)) { + if (KeyBinding.Right.isPressed(keycode)) selectNextRight(); - if(KeyBinding.Left.isPressed(keycode)) + if (KeyBinding.Left.isPressed(keycode)) selectNextLeft(); } - if(!dialogShowing()) - { - Button pressedButton=ui.buttonPressed(keycode); - if(pressedButton!=null) - { - if(pressedButton.isVisible()) + if (!dialogShowing()) { + Button pressedButton = ui.buttonPressed(keycode); + 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,152 +408,128 @@ public class UIScene extends Scene { } }, 0.10f); } + public void unselectActors() { for (Selectable selectable : getPossibleSelection()) { - selectable.onDeSelect(); + selectable.onDeSelect(); } } - Array visibleSelection() - { - Array selectables=new Array<>(); + + Array visibleSelection() { + Array selectables = new Array<>(); for (Selectable selectable : getPossibleSelection()) { - if(selectable.actor.isVisible()) - { - if(selectable.actor instanceof Button) - { - if(!((Button)selectable.actor).isDisabled()) + 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 - { - Selectable current =getSelected(); - Array candidates=new Array<>(); - for(Selectable selectable:visibleSelection()) - { - if(selectable.xDiff(current)<0.1&&selectable!=current) + } else { + Selectable current = getSelected(); + Array candidates = new Array<>(); + for (Selectable selectable : visibleSelection()) { + if (selectable.xDiff(current) < 0.1 && selectable != current) candidates.add(selectable); } - if(candidates.isEmpty()) + if (candidates.isEmpty()) candidates.addAll(visibleSelection()); - Selectable finalOne=null; - Selectable fallback=null; - for(Selectable candidate:candidates) - { - if(fallback==null||candidate.getY()>fallback.getY()) - fallback=candidate; - if(candidate.getY() fallback.getY()) + fallback = candidate; + 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() candidates=new Array<>(); - for(Selectable selectable:visibleSelection()) - { - if(selectable.yDiff(current)<0.1&&selectable!=current) + } else { + Selectable current = getSelected(); + Array candidates = new Array<>(); + for (Selectable selectable : visibleSelection()) { + if (selectable.yDiff(current) < 0.1 && selectable != current) candidates.add(selectable); } - if(candidates.isEmpty()) + if (candidates.isEmpty()) candidates.addAll(visibleSelection()); - Selectable finalOne=null; - Selectable fallback=null; - for(Selectable candidate:candidates) - { - if(fallback==null||candidate.getX()>fallback.getX()) - fallback=candidate; - if(candidate.getX() fallback.getX()) + fallback = candidate; + 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() candidates=new Array<>(); - for(Selectable selectable:visibleSelection()) - { - if(selectable.yDiff(current)<0.1&&selectable!=current) + } else { + Selectable current = getSelected(); + Array candidates = new Array<>(); + for (Selectable selectable : visibleSelection()) { + if (selectable.yDiff(current) < 0.1 && selectable != current) candidates.add(selectable); } - if(candidates.isEmpty()) + if (candidates.isEmpty()) candidates.addAll(visibleSelection()); - Selectable finalOne=null; - Selectable fallback=null; - for(Selectable candidate:candidates) - { - if(fallback==null||candidate.getX()current.getX()&&(finalOne==null||current.xDiff(candidate) 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.getX() && (finalOne == null || current.xDiff(candidate) < current.xDiff(finalOne))) { + finalOne = candidate; } } - if(finalOne!=null) + if (finalOne != null) selectActor(finalOne); - else if(fallback!=null) + else if (fallback != null) selectActor(fallback); } @@ -571,43 +538,35 @@ public class UIScene extends Scene { public void selectNextUp() { - if(getSelected()==null) - { + if (getSelected() == null) { selectFirst(); - } - else - { - Selectable current =getSelected(); - Array candidates=new Array<>(); - for(Selectable selectable:visibleSelection()) - { - if(selectable.xDiff(current)<0.1&&selectable!=current) + } else { + Selectable current = getSelected(); + Array candidates = new Array<>(); + for (Selectable selectable : visibleSelection()) { + if (selectable.xDiff(current) < 0.1 && selectable != current) candidates.add(selectable); } - if(candidates.isEmpty()) + if (candidates.isEmpty()) candidates.addAll(visibleSelection()); - Selectable finalOne=null; - Selectable fallback=null; - for(Selectable candidate:candidates) - { - if(fallback==null||candidate.getY()current.getY()&&(finalOne==null||current.yDiff(candidate) 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.getY() && (finalOne == null || current.yDiff(candidate) < current.yDiff(finalOne))) { + finalOne = candidate; } } - if(finalOne!=null) + if (finalOne != null) selectActor(finalOne); - else if(fallback!=null) + else if (fallback != null) selectActor(fallback); } @@ -615,22 +574,18 @@ public class UIScene extends Scene { private void selectFirst() { - Selectable result=null; - for(Selectable candidate: getPossibleSelection()) - { - if(result==null|| candidate.getY()>result.getY()) - { - result=candidate; + Selectable result = null; + 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(); @@ -641,16 +596,16 @@ public class UIScene extends Scene { public void selectActor(Selectable actor) { - unselectActors(); - if(actor==null)return; - stage.setKeyboardFocus(actor.actor); - ScrollPane scrollPane=scrollPaneOfActor(actor.actor); - if(scrollPane!=null) - { - scrollPane.scrollTo(actor.actor.getX(),actor.actor.getY(),actor.actor.getWidth(),actor.actor.getHeight(),false,false); - } - actor.onSelect(this); + unselectActors(); + if (actor == null) return; + stage.setKeyboardFocus(actor.actor); + ScrollPane scrollPane = scrollPaneOfActor(actor.actor); + 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); diff --git a/forge-gui/res/sound/button_press.mp3 b/forge-gui/res/sound/button_press.mp3 index c85aaa0a801efc92ea9e01e81d02004a932afbf3..1f56bbde7baeb3baec7861e20ef43ac9dc1ea2ee 100644 GIT binary patch literal 1632 zcmezWd&&_8bP$o5mkt!;2V!;x2DSu-G88Zu1#CnCM^M2H6z~QG{6_&myF`2(eO-<9 z3=IreX26^zs-VJ}*vP=R1mq6(clzBhiD5$j|Gfnm{>*2X1Gfk;Xjb**)EiE6;8Z)o z6ny~bL;gOwSBL-b*x0`3k3Yb0`1rpBkhi%OFflCP^IB>CK?B6~-@Bkmn?urFa>wh5 z3!6k?ZNdk6J2|cxMf|d&D4F$QKK%o>Vi}} zxBQ*UbMF@@^*Edpv|HkLO%MJ(3=MLkf*s)^bF^}Tk~EGi$f$6dC%vtB z)!EHIc?%zybf=VDm&ic!Al|i5#JDqks+~3rILHFibpf>}snP+$e00B2A1~ zqyGO6IdUBsxv{A+KN&RUa=l(LVO}c{RuOAH5k^s@TOhnZTGXF!vp*y-hElZ9(2xncPFr~!?RS88rkootS52}dTGt6*OU|_I6z`(#l gk%_39LFV6LKES~EkAZ=KmgWNsSdjT94b*i%0Dl4WKt4=zflFz<1qvvJbOu6~ZtOJA3s2sp?pCiK6(IUw7rgMZExhE5GO!$V@rd^)eSTIrd% zm6?1vx#-es(I>834OciA_AzYM5;!#5sHI0hWl_-ey)$*iZ|R5a6}}ihb?t%xuAjiL z|JmsCWlc@WlqV;)>@tgJI5D^LKvu`y2~F!?#Xo6bm0i zi`3%mX_|HAwq=&+DxEv6T5y9_5ersMbYNKG$?n8+Fv-B&W_C6(7V>FoZ(P7iqM69b(alGeqXql_Z>$dL~Whc4&`w9N2W5!CWhV9JcCXlf|og3N!`cxZyIEwjp@ l1%eG4BEYha5(Clng3Q0g49nLH4EDgx!-A%U5-zRH2LPz-3oQTu