
net.vectorpublish.desktop.vp.kf.FramerAnimationValues Maven / Gradle / Ivy
/*
* Copyright (c) 2016, Peter Rader. All rights reserved.
* ___ ___ __ ______ __ __ __ __
* | | |.-----..----.| |_ .-----..----.| __ \.--.--.| |--.| ||__|.-----.| |--.
* | | || -__|| __|| _|| _ || _|| __/| | || _ || || ||__ --|| |
* \_____/ |_____||____||____||_____||__| |___| |_____||_____||__||__||_____||__|__|
*
* http://www.gnu.org/licenses/gpl-3.0.html
*/
package net.vectorpublish.desktop.vp.kf;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import net.vectorpublish.desktop.vp.api.ContextHolder;
import net.vectorpublish.desktop.vp.api.DrawParticipant;
import net.vectorpublish.desktop.vp.api.Moment;
import net.vectorpublish.desktop.vp.api.ui.kf.AnimableField;
import net.vectorpublish.desktop.vp.api.ui.kf.Keyframe;
import net.vectorpublish.desktop.vp.api.ui.kf.Keyframer;
import net.vectorpublish.desktop.vp.log.Log;
/**
* Values of integer-fields (like x,y,width,height) of one element in the
* document (a {@link DrawParticipant}).
*
* This {@link FramerAnimationValues} is detached from a {@link Moment time}. To
* find a {@link FramerAnimationValues} at a specific {@link Moment time}, see
* the {@link Keyframe}.
*/
public class FramerAnimationValues extends HashMap {
private static Log LOG = ContextHolder.context.getBean(Log.class);
/**
* The keyframer this values have informations for.
*/
private final Keyframer keyframer;
/**
* The keyframer's fields we like to animate. This field is always readonly!
*/
private final Set fields;
/**
* Copyconstructor for a empty instance for the {@link Keyframer}.
*
* @param dirtyValues
* The {@link FramerAnimationValues} to make the clean copy for.
*/
private FramerAnimationValues(FramerAnimationValues dirtyValues) {
super(0);
this.keyframer = dirtyValues.keyframer;
this.fields = dirtyValues.fields;
}
/**
* Constructor for the {@link Keyframer} with predefined fields. Uses all
* fields, if they are set or not.
*
* @param keyframer
* The {@link Keyframer}.
* @param fields
* The fields found for the {@link Keyframer}.
*/
public FramerAnimationValues(Keyframer keyframer, Set fields) {
super();
this.keyframer = keyframer;
this.fields = Collections.unmodifiableSet(fields);
for (final AnimableField name : this.fields) {
put(name, name.getInteger(keyframer));
}
}
/**
* Creates an clean copy of this instance.
*
* @return Clean copy of this instance.
*/
public FramerAnimationValues clearCopy() {
return new FramerAnimationValues(keyframer, fields);
}
/**
* Returns the value of the field.
*
* @param field
* The field.
* @return The value or null
.
*/
public Integer get(AnimableField field) {
return super.get(field);
}
@Override
@Deprecated
public Integer get(Object key) {
return super.get(key);
}
/**
* Returns the {@link Keyframer}.
*
* @return The {@link Keyframer}.
*/
public Keyframer getKeyframer() {
return keyframer;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer();
boolean first = true;
for (final AnimableField field : keySet()) {
if (!first) {
sb.append(",");
}
sb.append(field);
sb.append("=");
sb.append(get(field));
first = false;
}
return sb.toString();
}
/**
* Creates a new, smaller Instance of the same {@link Keyframer}.
*
* The new instance will only contain values who differs from my values.
* Same values will not be added.
*
* @param subtrahent
* The values who are removed and not be added in the result.
* @return The reduced values that may be empty and null
if the
* subtrahent has a different {@link Keyframer}.
*/
public FramerAnimationValues withoutDuplicatesFrom(FramerAnimationValues subtrahent) {
if (subtrahent.keyframer == keyframer) {
final FramerAnimationValues cleanCopy = new FramerAnimationValues(this);
for (final AnimableField key : keySet()) {
final Integer otherValue = subtrahent.get(key);
final Integer myValue = get(key);
final boolean differs;
if (otherValue == null && myValue == null) {
differs = false;
} else if (otherValue != null && myValue != null) {
differs = otherValue.intValue() != myValue.intValue();
} else if (otherValue == null && myValue != null) {
differs = true;
} else if (otherValue != null && myValue == null) {
differs = true;
} else {
throw new RuntimeException("Invalid differences");
}
if (differs) {
LOG.found("Difference in field " + key.getCanoncialName() + " of " + subtrahent.getKeyframer() + " because " + otherValue + " != " + myValue);
cleanCopy.put(key, myValue);
}
}
return cleanCopy;
}
return null;
}
/**
* Writes all stored values to the {@link Keyframer}.
*
* @param target
* The target to write the values for.
*/
public void writeTo(Keyframer target) {
for (final AnimableField p : keySet()) {
p.setInteger(target, get(p));
}
}
}