Skip to content

Commit

Permalink
Toast manager center alignment support (#374)
Browse files Browse the repository at this point in the history
* Make ToastManager.updateToastsPositions and member variables protected (#374)

* Add support for 'center' alignments in ToastManager (#374)

* Update TestToasts to include top/bottom center alignment options (#374)

* Change log updated with ToastManager changes related to (#374)
  • Loading branch information
bploeckelman authored Dec 10, 2022
1 parent 20e14e3 commit 4bf70db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
3 changes: 2 additions & 1 deletion ui/CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 13 additions & 11 deletions ui/src/main/java/com/kotcrab/vis/ui/util/ToastManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Toast> toasts = new Array<Toast>();
private ObjectMap<Toast, Timer.Task> timersTasks = new ObjectMap<Toast, Timer.Task>();
protected Array<Toast> toasts = new Array<Toast>();
protected ObjectMap<Toast, Timer.Task> timersTasks = new ObjectMap<Toast, Timer.Task>();

/** Toast manager will create own group to host toasts and put it into the stage root. */
public ToastManager (Stage stage) {
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 8 additions & 2 deletions ui/src/test/java/com/kotcrab/vis/ui/test/manual/TestToasts.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void resize () {
});

final VisSelectBox<String> alignment = new VisSelectBox<String>();
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
Expand All @@ -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;
}
}
});
Expand Down

0 comments on commit 4bf70db

Please sign in to comment.