Skip to content

Commit

Permalink
Improve CollapseAction to support custom duration and interpolation (
Browse files Browse the repository at this point in the history
  • Loading branch information
fgnm authored Jan 23, 2021
1 parent 8b997f2 commit c7146e3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
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;
}
}
}

0 comments on commit c7146e3

Please sign in to comment.