use of com.android.tools.sherpa.interaction.WidgetInteractionTargets in project android by JetBrains.
the class WidgetsScene method updatePositions.
/**
* Make sure the positions of the interaction targets are correctly updated
*
* @param viewTransform the view transform
*/
public void updatePositions(ViewTransform viewTransform) {
for (ConstraintWidget widget : mWidgets.values()) {
widget.updateDrawPosition();
WidgetCompanion companion = (WidgetCompanion) widget.getCompanionWidget();
WidgetInteractionTargets widgetInteraction = companion.getWidgetInteractionTargets();
widgetInteraction.updatePosition(viewTransform);
}
}
use of com.android.tools.sherpa.interaction.WidgetInteractionTargets in project android by JetBrains.
the class WidgetCompanion method create.
public static WidgetCompanion create(ConstraintWidget widget) {
WidgetCompanion companion = new WidgetCompanion();
WidgetDecorator blueprintDecorator = new WidgetDecorator(widget);
blueprintDecorator.setStyle(WidgetDecorator.BLUEPRINT_STYLE);
WidgetDecorator androidDecorator = new WidgetDecorator(widget);
androidDecorator.setStyle(WidgetDecorator.ANDROID_STYLE);
companion.addDecorator(blueprintDecorator);
companion.addDecorator(androidDecorator);
companion.setWidgetInteractionTargets(new WidgetInteractionTargets(widget));
return companion;
}
use of com.android.tools.sherpa.interaction.WidgetInteractionTargets in project android by JetBrains.
the class ConstraintModel method setupConstraintWidget.
/**
* Set up a ConstraintWidget from a component
*
* @param component
* @param widget
* @return true if we need to save the XML
*/
private boolean setupConstraintWidget(@NotNull NlComponent component, ConstraintWidget widget) {
WidgetDecorator blueprintDecorator = createDecorator(component, widget);
WidgetDecorator androidDecorator = createDecorator(component, widget);
blueprintDecorator.setStateModel(this);
androidDecorator.setStateModel(this);
blueprintDecorator.setStyle(WidgetDecorator.BLUEPRINT_STYLE);
androidDecorator.setStyle(WidgetDecorator.ANDROID_STYLE);
WidgetCompanion companion = new WidgetCompanion();
companion.addDecorator(blueprintDecorator);
companion.addDecorator(androidDecorator);
companion.setWidgetInteractionTargets(new WidgetInteractionTargets(widget));
companion.setWidgetModel(component);
companion.setWidgetTag(component);
widget.setCompanionWidget(companion);
widget.setDebugName(component.getId());
return ConstraintUtilities.updateWidgetFromComponent(null, this, widget, component);
}
use of com.android.tools.sherpa.interaction.WidgetInteractionTargets in project android by JetBrains.
the class ConstraintUtilities method setGuideline.
/**
* Update the given guideline with the attributes set in the NlComponent
*
* @param component the component we get the attributes from
* @param guideline the guideline widget we want to update with the values
*/
private static void setGuideline(NlComponent component, Guideline guideline) {
String relativeBegin = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_BEGIN);
String relativeEnd = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_END);
boolean useFloat = useGuidelineFloat(component.getModel());
String percentAttribute = useFloat ? SdkConstants.LAYOUT_CONSTRAINT_GUIDE_PERCENT : SdkConstants.LAYOUT_CONSTRAINT_DEPRECATED_GUIDE_PERCENT;
String relativePercent = component.getAttribute(SdkConstants.SHERPA_URI, percentAttribute);
String oldRelativePercent = component.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_DEPRECATED_GUIDE_PERCENT);
WidgetCompanion companion = (WidgetCompanion) guideline.getCompanionWidget();
if (useFloat && oldRelativePercent != null) {
// we need to upgrade
companion.getWidgetProperties().setGuidelineAttribute(relativePercent);
float value = 0;
try {
value = Integer.parseInt(oldRelativePercent) / 100f;
} catch (NumberFormatException e) {
// ignore
}
guideline.setGuidePercent(value);
} else if (relativePercent != null && relativePercent.length() > 0) {
companion.getWidgetProperties().setGuidelineAttribute(relativePercent);
float value = 0;
try {
if (useFloat) {
value = Float.parseFloat(relativePercent);
} else {
value = Integer.parseInt(relativePercent) / 100f;
}
} catch (NumberFormatException e) {
// ignore
}
guideline.setGuidePercent(value);
} else if (relativeBegin != null && relativeBegin.length() > 0) {
companion.getWidgetProperties().setGuidelineAttribute(relativeBegin);
try {
int value = getDpValue(component, relativeBegin);
guideline.setGuideBegin(value);
} catch (NumberFormatException e) {
// Ignore
}
} else if (relativeEnd != null && relativeEnd.length() > 0) {
companion.getWidgetProperties().setGuidelineAttribute(relativeEnd);
try {
int value = getDpValue(component, relativeEnd);
guideline.setGuideEnd(value);
} catch (NumberFormatException e) {
// Ignore
}
}
String orientation = component.getAttribute(SdkConstants.NS_RESOURCES, SdkConstants.ATTR_ORIENTATION);
if (orientation != null) {
int newOrientation = Guideline.HORIZONTAL;
if (SdkConstants.ATTR_GUIDELINE_ORIENTATION_VERTICAL.equalsIgnoreCase(orientation)) {
newOrientation = Guideline.VERTICAL;
}
if (newOrientation != guideline.getOrientation()) {
guideline.setOrientation(newOrientation);
WidgetInteractionTargets interactionTargets = companion.getWidgetInteractionTargets();
interactionTargets.resetConstraintHandles();
}
}
}
use of com.android.tools.sherpa.interaction.WidgetInteractionTargets in project android by JetBrains.
the class WidgetDecorator method onPaintAnchors.
/**
* Paint the anchors of this object
*
* @param transform the view transform
* @param g the graphics context
*/
public void onPaintAnchors(ViewTransform transform, Graphics2D g) {
if (mColorSet == null) {
return;
}
if (mWidget.getVisibility() == ConstraintWidget.GONE) {
return;
}
ConstraintAnchor leftAnchor = mWidget.getAnchor(ConstraintAnchor.Type.LEFT);
ConstraintAnchor rightAnchor = mWidget.getAnchor(ConstraintAnchor.Type.RIGHT);
ConstraintAnchor topAnchor = mWidget.getAnchor(ConstraintAnchor.Type.TOP);
ConstraintAnchor bottomAnchor = mWidget.getAnchor(ConstraintAnchor.Type.BOTTOM);
boolean leftAnchorIsConnected = leftAnchor != null ? leftAnchor.isConnected() : false;
boolean rightAnchorIsConnected = rightAnchor != null ? rightAnchor.isConnected() : false;
boolean topAnchorIsConnected = topAnchor != null ? topAnchor.isConnected() : false;
boolean bottomAnchorIsConnected = bottomAnchor != null ? bottomAnchor.isConnected() : false;
boolean displayAllAnchors = mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.ALL);
boolean showLeftAnchor = displayAllAnchors || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.LEFT) || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.HORIZONTAL);
boolean showRightAnchor = displayAllAnchors || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.RIGHT) || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.HORIZONTAL);
boolean showTopAnchor = displayAllAnchors || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.TOP) || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.VERTICAL);
boolean showBottomAnchor = displayAllAnchors || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.BOTTOM) || mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.VERTICAL);
if (!mDisplayAnchorsPolicy.contains(WidgetDraw.ANCHORS_DISPLAY.NONE)) {
showLeftAnchor |= leftAnchorIsConnected;
showRightAnchor |= rightAnchorIsConnected;
showTopAnchor |= topAnchorIsConnected;
showBottomAnchor |= bottomAnchorIsConnected;
}
WidgetCompanion widgetCompanion = (WidgetCompanion) mWidget.getCompanionWidget();
WidgetInteractionTargets interactionTargets = widgetCompanion.getWidgetInteractionTargets();
// Let's draw all the anchors
g.setColor(mConstraintsColor.getColor());
// Draw the baseline first, if needed
if (mWidget.hasBaseline()) {
ConstraintAnchor baseline = mWidget.getAnchor(ConstraintAnchor.Type.BASELINE);
if (baseline.isConnected() || (mIsSelected && mShowResizeHandles)) {
Color c = g.getColor();
float progress = 1;
if (!baseline.isConnected()) {
progress = mShowBaseline.getProgress();
if (progress > 0) {
int alpha = (int) (255 * progress);
g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha));
}
}
if (progress > 0) {
ConstraintHandle handle = interactionTargets.getConstraintHandle(baseline);
handle.draw(transform, g, mColorSet, mIsSelected);
}
g.setColor(c);
}
}
if (mIsSelected) {
g.setColor(mColorSet.getSelectedConstraints());
}
if (showLeftAnchor) {
ConstraintHandle handle = interactionTargets.getConstraintHandle(leftAnchor);
if (handle != null) {
handle.draw(transform, g, mColorSet, mIsSelected);
}
}
if (showRightAnchor) {
ConstraintHandle handle = interactionTargets.getConstraintHandle(rightAnchor);
if (handle != null) {
handle.draw(transform, g, mColorSet, mIsSelected);
}
}
if (showTopAnchor) {
ConstraintHandle handle = interactionTargets.getConstraintHandle(topAnchor);
if (handle != null) {
handle.draw(transform, g, mColorSet, mIsSelected);
}
}
if (showBottomAnchor) {
ConstraintHandle handle = interactionTargets.getConstraintHandle(bottomAnchor);
if (handle != null) {
handle.draw(transform, g, mColorSet, mIsSelected);
}
}
}
Aggregations