001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.anim.values;
020:
021: import org.apache.batik.dom.anim.AnimationTarget;
022:
023: /**
024: * A number-or-percentage value in the animation system.
025: *
026: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
027: * @version $Id: AnimatableNumberOrPercentageValue.java 475477 2006-11-15 22:44:28Z cam $
028: */
029: public class AnimatableNumberOrPercentageValue extends
030: AnimatableNumberValue {
031:
032: /**
033: * Whether the number is a percentage.
034: */
035: protected boolean isPercentage;
036:
037: /**
038: * Creates a new, uninitialized AnimatableNumberOrPercentageValue.
039: */
040: protected AnimatableNumberOrPercentageValue(AnimationTarget target) {
041: super (target);
042: }
043:
044: /**
045: * Creates a new AnimatableNumberOrPercentageValue with a number.
046: */
047: public AnimatableNumberOrPercentageValue(AnimationTarget target,
048: float n) {
049: super (target, n);
050: }
051:
052: /**
053: * Creates a new AnimatableNumberOrPercentageValue with either a number
054: * or a percentage.
055: */
056: public AnimatableNumberOrPercentageValue(AnimationTarget target,
057: float n, boolean isPercentage) {
058: super (target, n);
059: this .isPercentage = isPercentage;
060: }
061:
062: /**
063: * Performs interpolation to the given value.
064: */
065: public AnimatableValue interpolate(AnimatableValue result,
066: AnimatableValue to, float interpolation,
067: AnimatableValue accumulation, int multiplier) {
068: AnimatableNumberOrPercentageValue res;
069: if (result == null) {
070: res = new AnimatableNumberOrPercentageValue(target);
071: } else {
072: res = (AnimatableNumberOrPercentageValue) result;
073: }
074:
075: float newValue;
076: boolean newIsPercentage;
077:
078: AnimatableNumberOrPercentageValue toValue = (AnimatableNumberOrPercentageValue) to;
079: AnimatableNumberOrPercentageValue accValue = (AnimatableNumberOrPercentageValue) accumulation;
080:
081: if (to != null) {
082: if (toValue.isPercentage == isPercentage) {
083: newValue = value + interpolation
084: * (toValue.value - value);
085: newIsPercentage = isPercentage;
086: } else {
087: if (interpolation >= 0.5) {
088: newValue = toValue.value;
089: newIsPercentage = toValue.isPercentage;
090: } else {
091: newValue = value;
092: newIsPercentage = isPercentage;
093: }
094: }
095: } else {
096: newValue = value;
097: newIsPercentage = isPercentage;
098: }
099:
100: if (accumulation != null
101: && accValue.isPercentage == newIsPercentage) {
102: newValue += multiplier * accValue.value;
103: }
104:
105: if (res.value != newValue
106: || res.isPercentage != newIsPercentage) {
107: res.value = newValue;
108: res.isPercentage = newIsPercentage;
109: res.hasChanged = true;
110: }
111: return res;
112: }
113:
114: /**
115: * Returns whether the value is a percentage.
116: */
117: public boolean isPercentage() {
118: return isPercentage;
119: }
120:
121: /**
122: * Returns whether two values of this type can have their distance
123: * computed, as needed by paced animation.
124: */
125: public boolean canPace() {
126: return false;
127: }
128:
129: /**
130: * Returns the absolute distance between this value and the specified other
131: * value.
132: */
133: public float distanceTo(AnimatableValue other) {
134: return 0f;
135: }
136:
137: /**
138: * Returns a zero value of this AnimatableValue's type.
139: */
140: public AnimatableValue getZeroValue() {
141: return new AnimatableNumberOrPercentageValue(target, 0,
142: isPercentage);
143: }
144:
145: /**
146: * Returns the CSS text representation of the value.
147: */
148: public String getCssText() {
149: StringBuffer sb = new StringBuffer();
150: sb.append(formatNumber(value));
151: if (isPercentage) {
152: sb.append('%');
153: }
154: return sb.toString();
155: }
156: }
|