use of com.android.tools.sherpa.interaction.ConstraintHandle in project android by JetBrains.
the class SnapDraw method drawSnapHorizontalIndicator.
/**
* Draw a snap indicator for horizontal anchors
*
* @param transform view transform
* @param g Graphics context
* @param candidate the snap candidate
*/
private static void drawSnapHorizontalIndicator(ViewTransform transform, Graphics2D g, SnapCandidate candidate) {
ConstraintAnchor source = candidate.source;
ConstraintAnchor target = candidate.target;
ConstraintHandle sourceHandle = WidgetInteractionTargets.constraintHandle(source);
ConstraintHandle targetHandle = WidgetInteractionTargets.constraintHandle(target);
int x = transform.getSwingX(candidate.x);
if (targetHandle != null) {
x = transform.getSwingX(targetHandle.getDrawX());
}
int y1 = transform.getSwingY(source.getOwner().getDrawY());
int y2 = transform.getSwingY(source.getOwner().getDrawY() + source.getOwner().getHeight());
int y3 = transform.getSwingY(target.getOwner().getDrawY());
int y4 = transform.getSwingY(target.getOwner().getDrawY() + target.getOwner().getHeight());
int minY = Math.min(y1, y3);
int maxY = Math.max(y2, y4);
if (candidate.margin != 0) {
int x2 = transform.getSwingX(sourceHandle.getDrawX());
String textMargin = String.valueOf(Math.abs(candidate.margin));
int yS = y2;
int yT = y4 + OVER_MARGIN / 2;
int mY = yS + OVER_MARGIN;
boolean textOver = false;
if (y1 < y3) {
yS = y1;
yT = y3 - OVER_MARGIN / 2;
mY = yS - OVER_MARGIN;
textOver = true;
}
drawSnapHorizontalMargin(transform, g, x, x2, mY, textMargin, textOver);
Graphics2D g2 = (Graphics2D) g.create();
g2.setStroke(sDashedStroke);
g2.drawLine(x, yS, x, yT);
g2.dispose();
} else {
Graphics2D g2 = (Graphics2D) g.create();
boolean insideIndicator = (source.getOwner().getParent() == candidate.target.getOwner()) || (candidate.target.getOwner() instanceof Guideline);
if (insideIndicator) {
g2.setStroke(sLongDashedStroke);
g2.drawLine(x, minY, x, maxY);
} else {
g2.setStroke(sDashedStroke);
g2.drawLine(x, minY - OVER, x, maxY + OVER);
}
g2.dispose();
}
}
use of com.android.tools.sherpa.interaction.ConstraintHandle in project android by JetBrains.
the class SceneDraw method setCurrentUnderneathAnchor.
/**
* Start an animation for the current anchor (under the mouse)
*
* @param underneathAnchor
*/
public void setCurrentUnderneathAnchor(ConstraintAnchor underneathAnchor) {
if (mCurrentUnderneathAnchor != underneathAnchor) {
mCurrentUnderneathAnchor = underneathAnchor;
mChoreographer.removeAnimation(mAnimationCurrentAnchor);
if (mCurrentUnderneathAnchor != null) {
ConstraintHandle constraintHandle = WidgetInteractionTargets.constraintHandle(mCurrentUnderneathAnchor);
mAnimationCurrentAnchor = new AnimatedHoverAnchor(mColorSet, constraintHandle);
mChoreographer.addAnimation(mAnimationCurrentAnchor);
} else {
mAnimationCurrentAnchor = null;
}
}
}
use of com.android.tools.sherpa.interaction.ConstraintHandle in project android by JetBrains.
the class SceneDraw method paintWidgets.
/**
* Main painting function
*
*
* @param rootDrawComponent the component we want to draw (with its children)
* @param width width of the canvas we paint on
* @param height height of the canvas we paint on
* @param transform
* @param g
* @param showAllConstraints
* @param mouseInteraction
* @return true if need to be called again (animation...)
*/
public boolean paintWidgets(ConstraintWidget rootDrawComponent, int width, int height, ViewTransform transform, Graphics2D g, boolean showAllConstraints, MouseInteraction mouseInteraction) {
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
WidgetContainer root = mWidgetsScene.getRoot();
if (root == null) {
return false;
}
mViewWidth = width;
mViewHeight = height;
if (mApplyConstraints) {
root.layout();
}
// Adapt the anchor size
ConnectionDraw.CONNECTION_ANCHOR_SIZE = (int) getAnchorSize(transform.getScale());
ConstraintAnchor selectedAnchor = mSelection.getSelectedAnchor();
ResizeHandle selectedResizeHandle = mSelection.getSelectedResizeHandle();
// Let's draw the widgets and their constraints.
boolean needsRepaint = false;
WidgetDecorator.setShowAllConstraints(showAllConstraints);
// First, mark which widgets is selected.
for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
WidgetDecorator decorator = widgetCompanion.getWidgetDecorator(getCurrentStyle());
WidgetInteractionTargets widgetInteraction = widgetCompanion.getWidgetInteractionTargets();
widgetInteraction.updatePosition(transform);
decorator.setColorSet(mColorSet);
if (mSelection.contains(widget)) {
decorator.setIsSelected(true);
} else {
decorator.setIsSelected(false);
}
}
// Then, mark highlighted widgets
animateInCandidateAnchors(selectedAnchor);
for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
WidgetDecorator decorator = widgetCompanion.getWidgetDecorator(getCurrentStyle());
decorator.applyLook();
}
// Draw the constraints
for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
WidgetDecorator decorator = widgetCompanion.getWidgetDecorator(getCurrentStyle());
if (widget.getVisibility() == ConstraintWidget.GONE || (widget.getParent() != null && widget.getParent().getVisibility() == ConstraintWidget.GONE)) {
continue;
}
if (DRAW_ENTIRE_TREE || widget == rootDrawComponent || widget.getParent() == rootDrawComponent) {
if (decorator.isVisible() && !decorator.isSelected() && decorator.getLook() != ColorTheme.Look.HIGHLIGHTED) {
decorator.onPaintConstraints(transform, g);
}
}
}
// Draw all the widgets
ConstraintWidget selectedWidget = null;
if (mSelection.hasSingleElement()) {
selectedWidget = mSelection.getFirstElement().widget;
}
needsRepaint |= paintWidgets(transform, g, rootDrawComponent, mWidgetsScene.getRoot(), selectedWidget, selectedAnchor, selectedResizeHandle);
// Draw the selected constraints
for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
WidgetCompanion widgetCompanion = (WidgetCompanion) widget.getCompanionWidget();
WidgetDecorator decorator = widgetCompanion.getWidgetDecorator(getCurrentStyle());
ConstraintWidget parent = widget.getParent();
if (DRAW_ENTIRE_TREE || widget == rootDrawComponent || parent == rootDrawComponent) {
if (decorator.isVisible() && (decorator.isSelected() || decorator.getLook() == ColorTheme.Look.HIGHLIGHTED)) {
decorator.onPaintConstraints(transform, g);
decorator.onPaintAnchors(transform, g);
decorator.onPaintActions(transform, g);
}
}
}
// Draw snap candidates
g.setColor(mColorSet.getHighlightedSnapGuides());
for (SnapCandidate candidate : mWidgetMotion.getSimilarMargins()) {
SnapDraw.drawSnapIndicator(transform, g, candidate);
}
g.setColor(mColorSet.getSnapGuides());
for (SnapCandidate candidate : mWidgetMotion.getSnapCandidates()) {
SnapDraw.drawSnapIndicator(transform, g, candidate);
}
for (SnapCandidate candidate : mWidgetResize.getSnapCandidates()) {
SnapDraw.drawSnapIndicator(transform, g, candidate);
}
if (mSelection.hasSingleElement() && selectedAnchor != null) {
ConstraintAnchor anchor = mSelection.getConnectionCandidateAnchor();
ConstraintHandle selectedHandle = WidgetInteractionTargets.constraintHandle(selectedAnchor);
g.setColor(mColorSet.getHighlightedConstraints());
if (!selectedHandle.getAnchor().isConnected() || selectedHandle.getAnchor().getTarget() != anchor) {
Point lastPoint = mouseInteraction.getLastPoint();
if (lastPoint.x != 0 && lastPoint.y != 0) {
selectedHandle.drawConnection(transform, g, mColorSet, true, mouseInteraction.getLastPoint());
}
}
}
if (selectedResizeHandle != null) {
g.setColor(mColorSet.getSelectionColor());
WidgetDraw.drawResizeHandleSelection(transform, g, selectedResizeHandle);
}
if (mSelection.isEmpty() && mouseInteraction.isMouseDown()) {
Point startPoint = mouseInteraction.getStartPoint();
Point lastMousePosition = mouseInteraction.getLastPoint();
// draw a selection rect
int x1 = Math.min(startPoint.x, lastMousePosition.x);
int x2 = Math.max(startPoint.x, lastMousePosition.x);
int y1 = Math.min(startPoint.y, lastMousePosition.y);
int y2 = Math.max(startPoint.y, lastMousePosition.y);
int ax1 = transform.getSwingX(x1);
int ax2 = transform.getSwingX(x2);
int ay1 = transform.getSwingY(y1);
int ay2 = transform.getSwingY(y2);
int w = x2 - x1;
int h = y2 - y1;
if (w > 0 || h > 0) {
g.setColor(mColorSet.getSelectionColor());
g.setStroke(SnapDraw.sDashedStroke);
if (w >= 8 && h >= 8) {
g.drawRect(ax1, ay1, ax2 - ax1, ay2 - ay1);
} else if (w >= 8 && h < 8) {
g.drawLine(ax1, ay1, ax2, ay1);
} else {
g.drawLine(ax1, ay1, ax1, ay2);
}
g.setStroke(SnapDraw.sNormalStroke);
if (w >= 8) {
ConnectionDraw.drawHorizontalMarginIndicator(g, String.valueOf(w), ax1, ax2, ay1 - 20);
}
if (h >= 8) {
ConnectionDraw.drawVerticalMarginIndicator(g, String.valueOf(h), ax1 - 20, ay1, ay2);
}
}
}
if (mSelection.getSelectionBounds() != null) {
Selection.Element bounds = mSelection.getSelectionBounds();
g.setColor(mColorSet.getSelectionColor());
g.setStroke(SnapDraw.sDashedStroke);
int x = transform.getSwingX(bounds.widget.getDrawX());
int y = transform.getSwingY(bounds.widget.getDrawY());
int w = transform.getSwingDimension(bounds.widget.getDrawWidth());
int h = transform.getSwingDimension(bounds.widget.getDrawHeight());
g.drawRect(x, y, w, h);
}
needsRepaint |= mChoreographer.onPaint(transform, g);
if (!needsRepaint && !mSelection.isEmpty()) {
for (Selection.Element element : mSelection.getElements()) {
needsRepaint |= element.widget.isAnimating();
}
}
if (!needsRepaint) {
for (ConstraintWidget widget : mWidgetsScene.getWidgets()) {
needsRepaint |= widget.isAnimating();
}
}
if (needsRepaint) {
repaint();
}
return needsRepaint;
}
Aggregations