1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.common.api; 18 19 /** A segment type describes the different roles or positions a segment can have in a node */ 20 public enum SegmentType { 21 LEFT, TOP, RIGHT, BOTTOM, BASELINE, CENTER_VERTICAL, CENTER_HORIZONTAL, UNKNOWN; 22 23 public boolean isHorizontal() { 24 return this == TOP || this == BOTTOM || this == BASELINE || this == CENTER_HORIZONTAL; 25 } 26 27 /** 28 * Returns the X coordinate for an edge of this type given its bounds 29 * 30 * @param node the node containing the edge 31 * @param bounds the bounds of the node 32 * @return the X coordinate for an edge of this type given its bounds 33 */ 34 public int getX(INode node, Rect bounds) { 35 // We pass in the bounds rather than look it up via node.getBounds() because 36 // during a resize or move operation, we call this method to look up proposed 37 // bounds rather than actual bounds 38 switch (this) { 39 case RIGHT: 40 return bounds.x + bounds.w; 41 case TOP: 42 case BOTTOM: 43 case CENTER_VERTICAL: 44 return bounds.x + bounds.w / 2; 45 case UNKNOWN: 46 assert false; 47 return bounds.x; 48 case LEFT: 49 case BASELINE: 50 default: 51 return bounds.x; 52 } 53 } 54 55 /** 56 * Returns the Y coordinate for an edge of this type given its bounds 57 * 58 * @param node the node containing the edge 59 * @param bounds the bounds of the node 60 * @return the Y coordinate for an edge of this type given its bounds 61 */ 62 public int getY(INode node, Rect bounds) { 63 switch (this) { 64 case TOP: 65 return bounds.y; 66 case BOTTOM: 67 return bounds.y + bounds.h; 68 case BASELINE: { 69 int baseline = node != null ? node.getBaseline() : -1; 70 if (node == null) { 71 // This happens when you are dragging an element and we don't have 72 // a node (only an IDragElement) such as on a palette drag. 73 // For now just hack it. 74 baseline = (int) (bounds.h * 0.8f); // HACK 75 } 76 return bounds.y + baseline; 77 } 78 case UNKNOWN: 79 assert false; 80 return bounds.y; 81 case RIGHT: 82 case LEFT: 83 case CENTER_HORIZONTAL: 84 default: 85 return bounds.y + bounds.h / 2; 86 } 87 } 88 89 @Override 90 public String toString() { 91 return name(); 92 } 93 } 94