diff --git a/ui/CHANGES.md b/ui/CHANGES.md index f2614ea6..7ce4cb2f 100644 --- a/ui/CHANGES.md +++ b/ui/CHANGES.md @@ -1,6 +1,7 @@ #### Version: 1.5.1-SNAPSHOT (libGDX 1.11.0) - - Updated to libGDX 1.11.0 +- **Changed**: [#374](https://github.com/kotcrab/vis-ui/issues/374) - `ToastManager` now supports 'center' horizontal alignment for 'top' and 'bottom' alignments. + - Also `ToastManager.updateToastsPositions` and `ToastManager` member variables are now protected, allowing further customization of the toast positions. #### Version: 1.5.0 (libGDX 1.10.0) - Updated to libGDX 1.10.0 diff --git a/ui/src/main/java/com/kotcrab/vis/ui/util/ToastManager.java b/ui/src/main/java/com/kotcrab/vis/ui/util/ToastManager.java index d84692be..c14330be 100644 --- a/ui/src/main/java/com/kotcrab/vis/ui/util/ToastManager.java +++ b/ui/src/main/java/com/kotcrab/vis/ui/util/ToastManager.java @@ -48,15 +48,15 @@ public class ToastManager { public static final int UNTIL_CLOSED = -1; - private final Group root; + protected final Group root; - private int screenPaddingX = 20; - private int screenPaddingY = 20; - private int messagePadding = 5; - private int alignment = Align.topRight; + protected int screenPaddingX = 20; + protected int screenPaddingY = 20; + protected int messagePadding = 5; + protected int alignment = Align.topRight; - private Array toasts = new Array(); - private ObjectMap timersTasks = new ObjectMap(); + protected Array toasts = new Array(); + protected ObjectMap timersTasks = new ObjectMap(); /** Toast manager will create own group to host toasts and put it into the stage root. */ public ToastManager (Stage stage) { @@ -186,16 +186,18 @@ public void toFront () { root.toFront(); } - private void updateToastsPositions () { + protected void updateToastsPositions () { boolean bottom = (alignment & Align.bottom) != 0; boolean left = (alignment & Align.left) != 0; + boolean center = (alignment & Align.center) != 0; float y = bottom ? screenPaddingY : root.getHeight() - screenPaddingY; for (Toast toast : toasts) { Table table = toast.getMainTable(); - table.setPosition( - left ? screenPaddingX : root.getWidth() - table.getWidth() - screenPaddingX, - bottom ? y : y - table.getHeight()); + float x = left ? screenPaddingX + : center ? (root.getWidth() - table.getWidth() - screenPaddingX) / 2f + : /*right*/ root.getWidth() - table.getWidth() - screenPaddingX; + table.setPosition(x, bottom ? y : y - table.getHeight()); y += (table.getHeight() + messagePadding) * (bottom ? 1 : -1); } diff --git a/ui/src/test/java/com/kotcrab/vis/ui/test/manual/TestToasts.java b/ui/src/test/java/com/kotcrab/vis/ui/test/manual/TestToasts.java index b9a64132..a8e73a83 100644 --- a/ui/src/test/java/com/kotcrab/vis/ui/test/manual/TestToasts.java +++ b/ui/src/test/java/com/kotcrab/vis/ui/test/manual/TestToasts.java @@ -49,7 +49,7 @@ public void resize () { }); final VisSelectBox alignment = new VisSelectBox(); - alignment.setItems("top left", "top right", "bottom left", "bottom right"); + alignment.setItems("top left", "top right", "top center", "bottom left", "bottom right", "bottom center"); alignment.setSelectedIndex(1); alignment.addListener(new ChangeListener() { @Override @@ -63,11 +63,17 @@ public void changed (ChangeEvent event, Actor actor) { toastManager.setAlignment(Align.topRight); break; case 2: - toastManager.setAlignment(Align.bottomLeft); + toastManager.setAlignment(Align.top | Align.center); break; case 3: + toastManager.setAlignment(Align.bottomLeft); + break; + case 4: toastManager.setAlignment(Align.bottomRight); break; + case 5: + toastManager.setAlignment(Align.bottom | Align.center); + break; } } });