Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CollapseAction to support custom duration and interpolation #350

Merged
merged 2 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions ui/src/main/java/com/kotcrab/vis/ui/widget/CollapsibleWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
package com.kotcrab.vis.ui.widget;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.FloatAction;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.utils.GdxRuntimeException;

/**
* Widget containing table that can be vertically collapsed.
*
* @author Kotcrab
* @see HorizontalCollapsibleWidget
* @since 0.3.1
Expand All @@ -33,6 +35,8 @@ public class CollapsibleWidget extends WidgetGroup {
private Table table;

private CollapseAction collapseAction = new CollapseAction();
private float collapseDuration = 0.3f;
private Interpolation collapseInterpolation = Interpolation.pow3Out;

private boolean collapsed;
private boolean actionRunning;
Expand Down Expand Up @@ -64,6 +68,11 @@ public void setCollapsed (boolean collapse, boolean withAnimation) {
actionRunning = true;

if (withAnimation) {
collapseAction.reset();
collapseAction.setStart(currentHeight);
collapseAction.setEnd(collapse ? 0f : table.getPrefHeight());
collapseAction.setDuration(collapseDuration);
collapseAction.setInterpolation(collapseInterpolation);
addAction(collapseAction);
} else {
if (collapse) {
Expand Down Expand Up @@ -94,6 +103,14 @@ private void updateTouchable () {
setTouchable(Touchable.enabled);
}

public void setCollapseDuration (float collapseDuration) {
this.collapseDuration = collapseDuration;
}

public void setCollapseInterpolation (Interpolation collapseInterpolation) {
this.collapseInterpolation = collapseInterpolation;
}

@Override
public void draw (Batch batch, float parentAlpha) {
if (currentHeight > 1) {
Expand Down Expand Up @@ -152,27 +169,19 @@ protected void childrenChanged () {
if (getChildren().size > 1) throw new GdxRuntimeException("Only one actor can be added to CollapsibleWidget");
}

private class CollapseAction extends Action {
private class CollapseAction extends FloatAction {

@Override
public boolean act (float delta) {
if (collapsed) {
currentHeight -= delta * 1000;
if (currentHeight <= 0) {
currentHeight = 0;
collapsed = true;
actionRunning = false;
}
} else {
currentHeight += delta * 1000;
if (currentHeight > table.getPrefHeight()) {
currentHeight = table.getPrefHeight();
collapsed = false;
actionRunning = false;
}
protected void update (float percent) {
super.update(percent);
currentHeight = getValue();

if (percent == 1) {
actionRunning = false;
collapsed = currentHeight == 0;
}

invalidateHierarchy();
return !actionRunning;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package com.kotcrab.vis.ui.widget;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.FloatAction;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.utils.GdxRuntimeException;
Expand All @@ -33,6 +34,8 @@ public class HorizontalCollapsibleWidget extends WidgetGroup {
private Table table;

private CollapseAction collapseAction = new CollapseAction();
private float collapseDuration = 0.3f;
private Interpolation collapseInterpolation = Interpolation.pow3Out;

private boolean collapsed;
private boolean actionRunning;
Expand Down Expand Up @@ -64,6 +67,11 @@ public void setCollapsed (boolean collapse, boolean withAnimation) {
actionRunning = true;

if (withAnimation) {
collapseAction.reset();
collapseAction.setStart(currentWidth);
collapseAction.setEnd(collapse ? 0f : table.getPrefWidth());
collapseAction.setDuration(collapseDuration);
collapseAction.setInterpolation(collapseInterpolation);
addAction(collapseAction);
} else {
if (collapse) {
Expand Down Expand Up @@ -152,27 +160,19 @@ protected void childrenChanged () {
if (getChildren().size > 1) throw new GdxRuntimeException("Only one actor can be added to CollapsibleWidget");
}

private class CollapseAction extends Action {
private class CollapseAction extends FloatAction {

@Override
public boolean act (float delta) {
if (collapsed) {
currentWidth -= delta * 1000;
if (currentWidth <= 0) {
currentWidth = 0;
collapsed = true;
actionRunning = false;
}
} else {
currentWidth += delta * 1000;
if (currentWidth > table.getPrefWidth()) {
currentWidth = table.getPrefWidth();
collapsed = false;
actionRunning = false;
}
protected void update (float percent) {
super.update(percent);
currentWidth = getValue();

if (percent == 1) {
actionRunning = false;
collapsed = currentWidth == 0;
}

invalidateHierarchy();
return !actionRunning;
}
}
}