2015-01-19 12:37:30 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
package com.facebook.csslayout;
|
|
|
|
|
2015-05-15 14:13:25 +01:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2015-01-19 12:37:30 +00:00
|
|
|
/**
|
2015-05-14 13:59:13 +01:00
|
|
|
* Class representing CSS spacing (padding, margin, and borders). This is mostly necessary to
|
|
|
|
* properly implement interactions and updates for properties like margin, marginLeft, and
|
|
|
|
* marginHorizontal.
|
2015-01-19 12:37:30 +00:00
|
|
|
*/
|
|
|
|
public class Spacing {
|
|
|
|
|
2015-05-14 13:59:13 +01:00
|
|
|
/**
|
|
|
|
* Spacing type that represents the left direction. E.g. {@code marginLeft}.
|
|
|
|
*/
|
2015-01-19 12:37:30 +00:00
|
|
|
public static final int LEFT = 0;
|
2015-05-14 13:59:13 +01:00
|
|
|
/**
|
|
|
|
* Spacing type that represents the top direction. E.g. {@code marginTop}.
|
|
|
|
*/
|
2015-01-19 12:37:30 +00:00
|
|
|
public static final int TOP = 1;
|
2015-05-14 13:59:13 +01:00
|
|
|
/**
|
|
|
|
* Spacing type that represents the right direction. E.g. {@code marginRight}.
|
|
|
|
*/
|
2015-01-19 12:37:30 +00:00
|
|
|
public static final int RIGHT = 2;
|
2015-05-14 13:59:13 +01:00
|
|
|
/**
|
|
|
|
* Spacing type that represents the bottom direction. E.g. {@code marginBottom}.
|
|
|
|
*/
|
2015-01-19 12:37:30 +00:00
|
|
|
public static final int BOTTOM = 3;
|
2015-05-14 13:59:13 +01:00
|
|
|
/**
|
|
|
|
* Spacing type that represents vertical direction (top and bottom). E.g. {@code marginVertical}.
|
|
|
|
*/
|
2015-01-19 12:37:30 +00:00
|
|
|
public static final int VERTICAL = 4;
|
2015-05-14 13:59:13 +01:00
|
|
|
/**
|
|
|
|
* Spacing type that represents horizontal direction (left and right). E.g.
|
|
|
|
* {@code marginHorizontal}.
|
|
|
|
*/
|
2015-01-19 12:37:30 +00:00
|
|
|
public static final int HORIZONTAL = 5;
|
2015-05-11 15:39:02 +01:00
|
|
|
/**
|
|
|
|
* Spacing type that represents start direction e.g. left in left-to-right, right in right-to-left.
|
|
|
|
*/
|
|
|
|
public static final int START = 6;
|
|
|
|
/**
|
|
|
|
* Spacing type that represents end direction e.g. right in left-to-right, left in right-to-left.
|
|
|
|
*/
|
|
|
|
public static final int END = 7;
|
2015-05-14 13:59:13 +01:00
|
|
|
/**
|
|
|
|
* Spacing type that represents all directions (left, top, right, bottom). E.g. {@code margin}.
|
|
|
|
*/
|
2015-05-11 15:39:02 +01:00
|
|
|
public static final int ALL = 8;
|
2015-01-19 12:37:30 +00:00
|
|
|
|
2015-05-14 13:59:13 +01:00
|
|
|
private final float[] mSpacing = newFullSpacingArray();
|
2015-05-15 14:13:25 +01:00
|
|
|
@Nullable private float[] mDefaultSpacing = null;
|
2015-05-14 13:59:13 +01:00
|
|
|
|
2015-01-19 12:37:30 +00:00
|
|
|
/**
|
2015-05-14 13:59:13 +01:00
|
|
|
* Set a spacing value.
|
|
|
|
*
|
|
|
|
* @param spacingType one of {@link #LEFT}, {@link #TOP}, {@link #RIGHT}, {@link #BOTTOM},
|
|
|
|
* {@link #VERTICAL}, {@link #HORIZONTAL}, {@link #ALL}
|
|
|
|
* @param value the value for this direction
|
|
|
|
* @return {@code true} if the spacing has changed, or {@code false} if the same value was already
|
|
|
|
* set
|
2015-01-19 12:37:30 +00:00
|
|
|
*/
|
2015-05-14 13:59:13 +01:00
|
|
|
public boolean set(int spacingType, float value) {
|
|
|
|
if (!FloatUtil.floatsEqual(mSpacing[spacingType], value)) {
|
|
|
|
mSpacing[spacingType] = value;
|
2015-05-15 14:13:25 +01:00
|
|
|
return true;
|
2015-05-14 13:59:13 +01:00
|
|
|
}
|
2015-05-15 14:13:25 +01:00
|
|
|
return false;
|
2015-05-14 13:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a default spacing value. This is used as a fallback when no spacing has been set for a
|
|
|
|
* particular direction.
|
|
|
|
*
|
|
|
|
* @param spacingType one of {@link #LEFT}, {@link #TOP}, {@link #RIGHT}, {@link #BOTTOM}
|
|
|
|
* @param value the default value for this direction
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean setDefault(int spacingType, float value) {
|
2015-05-15 14:13:25 +01:00
|
|
|
if (mDefaultSpacing == null) {
|
|
|
|
mDefaultSpacing = newSpacingResultArray();
|
|
|
|
}
|
2015-05-14 13:59:13 +01:00
|
|
|
if (!FloatUtil.floatsEqual(mDefaultSpacing[spacingType], value)) {
|
|
|
|
mDefaultSpacing[spacingType] = value;
|
2015-05-15 14:13:25 +01:00
|
|
|
return true;
|
2015-05-14 13:59:13 +01:00
|
|
|
}
|
2015-05-15 14:13:25 +01:00
|
|
|
return false;
|
2015-05-14 13:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the spacing for a direction. This takes into account any default values that have been set.
|
|
|
|
*
|
|
|
|
* @param spacingType one of {@link #LEFT}, {@link #TOP}, {@link #RIGHT}, {@link #BOTTOM}
|
|
|
|
*/
|
|
|
|
public float get(int spacingType) {
|
2015-05-15 14:13:25 +01:00
|
|
|
int secondType = spacingType == TOP || spacingType == BOTTOM ? VERTICAL : HORIZONTAL;
|
|
|
|
float defaultValue = spacingType == START || spacingType == END ? CSSConstants.UNDEFINED : 0;
|
|
|
|
return
|
|
|
|
!CSSConstants.isUndefined(mSpacing[spacingType])
|
|
|
|
? mSpacing[spacingType]
|
|
|
|
: !CSSConstants.isUndefined(mSpacing[secondType])
|
|
|
|
? mSpacing[secondType]
|
|
|
|
: !CSSConstants.isUndefined(mSpacing[ALL])
|
|
|
|
? mSpacing[ALL]
|
|
|
|
: mDefaultSpacing != null
|
|
|
|
? mDefaultSpacing[spacingType]
|
|
|
|
: defaultValue;
|
2015-05-14 13:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the raw value (that was set using {@link #set(int, float)}), without taking into account
|
|
|
|
* any default values.
|
|
|
|
*
|
|
|
|
* @param spacingType one of {@link #LEFT}, {@link #TOP}, {@link #RIGHT}, {@link #BOTTOM},
|
|
|
|
* {@link #VERTICAL}, {@link #HORIZONTAL}, {@link #ALL}
|
|
|
|
*/
|
|
|
|
public float getRaw(int spacingType) {
|
|
|
|
return mSpacing[spacingType];
|
|
|
|
}
|
|
|
|
|
2015-09-12 16:51:51 +01:00
|
|
|
/**
|
|
|
|
* Try to get start value and fallback to given type if not defined. This is used privately
|
|
|
|
* by the layout engine as a more efficient way to fetch direction-aware values by
|
|
|
|
* avoid extra method invocations.
|
|
|
|
*/
|
|
|
|
float getWithFallback(int spacingType, int fallbackType) {
|
|
|
|
return
|
|
|
|
!CSSConstants.isUndefined(mSpacing[spacingType])
|
|
|
|
? mSpacing[spacingType]
|
|
|
|
: get(fallbackType);
|
|
|
|
}
|
|
|
|
|
2015-05-14 13:59:13 +01:00
|
|
|
private static float[] newFullSpacingArray() {
|
2015-01-19 12:37:30 +00:00
|
|
|
return new float[] {
|
|
|
|
CSSConstants.UNDEFINED,
|
|
|
|
CSSConstants.UNDEFINED,
|
|
|
|
CSSConstants.UNDEFINED,
|
|
|
|
CSSConstants.UNDEFINED,
|
|
|
|
CSSConstants.UNDEFINED,
|
|
|
|
CSSConstants.UNDEFINED,
|
|
|
|
CSSConstants.UNDEFINED,
|
|
|
|
CSSConstants.UNDEFINED,
|
2015-05-11 15:39:02 +01:00
|
|
|
CSSConstants.UNDEFINED,
|
2015-01-19 12:37:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-14 13:59:13 +01:00
|
|
|
private static float[] newSpacingResultArray() {
|
2015-01-19 12:37:30 +00:00
|
|
|
return newSpacingResultArray(0);
|
|
|
|
}
|
|
|
|
|
2015-05-14 13:59:13 +01:00
|
|
|
private static float[] newSpacingResultArray(float defaultValue) {
|
2015-01-19 12:37:30 +00:00
|
|
|
return new float[] {
|
|
|
|
defaultValue,
|
|
|
|
defaultValue,
|
|
|
|
defaultValue,
|
|
|
|
defaultValue,
|
2015-05-11 15:39:02 +01:00
|
|
|
defaultValue,
|
|
|
|
defaultValue,
|
|
|
|
CSSConstants.UNDEFINED,
|
|
|
|
CSSConstants.UNDEFINED,
|
|
|
|
defaultValue,
|
2015-01-19 12:37:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|