[ 
https://issues.apache.org/jira/browse/PIVOT-748?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Edgar Merino updated PIVOT-748:
-------------------------------

    Description: This is a simple, yet useful, Transition which is able to 
scale the component(s) attached to it for a given period of time, providing a 
"zoom" effect. The code is attached.  (was: This is a simple, yet useful, 
Transition which is able to scale the component(s) attached to it for a given 
period of time, providing a "zoom" effect.

ScaleTransition.java

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.effects.Decorator;
import org.apache.pivot.wtk.effects.Transition;
import org.apache.pivot.wtk.effects.ScaleDecorator;

/**
 * This transition provides a "zoom" effect for components that use it.
 * More than one component can be attached to it.
 * 
 * @author emerino
 */
public class ScaleTransition extends Transition {

    private class CustomScaleDecorator extends ScaleDecorator {

        private float initialScaleX = 1.0f;
        private float initialScaleY = 1.0f;

        public void setInitialScales(ScaleDecorator decorator) {
            initialScaleX = decorator.getScaleX();
            initialScaleY = decorator.getScaleY();
        }

        public float getInitialScaleX() {
            return initialScaleX;
        }

        public float getInitialScaleY() {
            return initialScaleY;
        }
    }

    private static final int DEFAULT_DURATION = 200;
    private static final int DEFAULT_RATE = 28;

    private Map<Component, CustomScaleDecorator> componentDecoratorMap =
            Collections.synchronizedMap(new HashMap<Component, 
CustomScaleDecorator>());

    private boolean reversed;
    private float scaleFactor = 0.3f;

    public ScaleTransition() {
        this(DEFAULT_DURATION, DEFAULT_RATE);
    }

    public ScaleTransition(int duration, int rate) {
        super(duration, rate);
    }

    public Set<Component> getComponents() {
        return componentDecoratorMap.keySet();
    }

    public void addComponent(Component component) {
        if (component == null) {
            throw new IllegalArgumentException("Component cannot be null");
        }

        if (!componentDecoratorMap.containsKey(component)) {
            CustomScaleDecorator scaleDecorator = new CustomScaleDecorator();

            componentDecoratorMap.put(component, scaleDecorator);
            component.getDecorators().add(scaleDecorator);
        }
    }

    public void addComponents(Collection<Component> components) {
        for (Component c : components) {
            addComponent(c);
        }
    }

    public void setScaleFactor(float zoom) {
        if (isRunning()) {
            throw new IllegalStateException("Transition in progress");
        }

        this.scaleFactor = zoom;
    }

    public void setReversed(boolean reversed) {
        if (isRunning()) {
            throw new IllegalStateException("Transition in progress");
        }

        this.reversed = reversed;
    }

    @Override
    protected void update() {
        float scaleX = 0;
        float scaleY = 0;
        CustomScaleDecorator scaleDecorator = null;

        for (Component component : componentDecoratorMap.keySet()) {
            scaleDecorator = componentDecoratorMap.get(component);

            if (!reversed) {
                scaleX = scaleDecorator.getInitialScaleX() + (scaleFactor * 
getPercentComplete());
                scaleY = scaleDecorator.getInitialScaleY() + (scaleFactor * 
getPercentComplete());
            } else {
                scaleX = (scaleDecorator.getInitialScaleX() + scaleFactor) - 
(scaleFactor * getPercentComplete());
                scaleY = (scaleDecorator.getInitialScaleY() + scaleFactor) - 
(scaleFactor * getPercentComplete());
            }

            scaleDecorator.setScaleX(scaleX);
            scaleDecorator.setScaleY(scaleY);

            component.repaint();
        }
    }

    public void removeComponent(Component component) {
        if (componentDecoratorMap.containsKey(component)) {
            
component.getDecorators().remove(componentDecoratorMap.remove(component));
            componentDecoratorMap.remove(component);
        }
    }

    public void clearComponents() {
        for (Component c : componentDecoratorMap.keySet()) {
            removeComponent(c);
        }
    }
}
)

> ScaleTransition
> ---------------
>
>                 Key: PIVOT-748
>                 URL: https://issues.apache.org/jira/browse/PIVOT-748
>             Project: Pivot
>          Issue Type: New Feature
>          Components: wtk-effects
>            Reporter: Edgar Merino
>
> This is a simple, yet useful, Transition which is able to scale the 
> component(s) attached to it for a given period of time, providing a "zoom" 
> effect. The code is attached.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to