• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.android.annotations.NonNull;
20 import com.android.annotations.Nullable;
21 import com.google.common.annotations.Beta;
22 
23 /** A segment type describes the different roles or positions a segment can have in a node
24  * <p>
25  * <b>NOTE: This is not a public or final API; if you rely on this be prepared
26  * to adjust your code for the next tools release.</b>
27  */
28 @Beta
29 public enum SegmentType {
30     LEFT, TOP, RIGHT, BOTTOM, BASELINE, CENTER_VERTICAL, CENTER_HORIZONTAL, UNKNOWN;
31 
isHorizontal()32     public boolean isHorizontal() {
33         return this == TOP || this == BOTTOM || this == BASELINE || this == CENTER_HORIZONTAL;
34     }
35 
36     /**
37      * Returns the X coordinate for an edge of this type given its bounds
38      *
39      * @param node the node containing the edge
40      * @param bounds the bounds of the node
41      * @return the X coordinate for an edge of this type given its bounds
42      */
getX(@ullable INode node, @NonNull Rect bounds)43     public int getX(@Nullable INode node, @NonNull Rect bounds) {
44         // We pass in the bounds rather than look it up via node.getBounds() because
45         // during a resize or move operation, we call this method to look up proposed
46         // bounds rather than actual bounds
47         switch (this) {
48             case RIGHT:
49                 return bounds.x + bounds.w;
50             case TOP:
51             case BOTTOM:
52             case CENTER_VERTICAL:
53                 return bounds.x + bounds.w / 2;
54             case UNKNOWN:
55                 assert false;
56                 return bounds.x;
57             case LEFT:
58             case BASELINE:
59             default:
60                 return bounds.x;
61         }
62     }
63 
64     /**
65      * Returns the Y coordinate for an edge of this type given its bounds
66      *
67      * @param node the node containing the edge
68      * @param bounds the bounds of the node
69      * @return the Y coordinate for an edge of this type given its bounds
70      */
getY(@ullable INode node, @NonNull Rect bounds)71     public int getY(@Nullable INode node, @NonNull Rect bounds) {
72         switch (this) {
73             case TOP:
74                 return bounds.y;
75             case BOTTOM:
76                 return bounds.y + bounds.h;
77             case BASELINE: {
78                 int baseline = node != null ? node.getBaseline() : -1;
79                 if (node == null) {
80                     // This happens when you are dragging an element and we don't have
81                     // a node (only an IDragElement) such as on a palette drag.
82                     // For now just hack it.
83                     baseline = (int) (bounds.h * 0.8f); // HACK
84                 }
85                 return bounds.y + baseline;
86             }
87             case UNKNOWN:
88                 assert false;
89                 return bounds.y;
90             case RIGHT:
91             case LEFT:
92             case CENTER_HORIZONTAL:
93             default:
94                 return bounds.y + bounds.h / 2;
95         }
96     }
97 
98     @Override
toString()99     public String toString() {
100         return name();
101     }
102 }
103