SegmentType.java revision 7e4b8e9d595e45baa9d87cdb8282f02759e73abc
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
17package com.android.ide.common.api;
18
19import com.android.annotations.NonNull;
20import com.android.annotations.Nullable;
21import 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
29public enum SegmentType {
30    /** Segment is on the left edge */
31    @NonNull LEFT,
32    /** Segment is on the top edge */
33    @NonNull TOP,
34    /** Segment is on the right edge */
35    @NonNull RIGHT,
36    /** Segment is on the bottom edge */
37    @NonNull BOTTOM,
38    /** Segment is along the baseline */
39    @NonNull BASELINE,
40    /** Segment is along the center vertically */
41    @NonNull CENTER_VERTICAL,
42    /** Segment is along the center horizontally */
43    @NonNull CENTER_HORIZONTAL,
44    /** Segment is on an unknown edge */
45    @NonNull UNKNOWN;
46
47    public boolean isHorizontal() {
48        return this == TOP || this == BOTTOM || this == BASELINE || this == CENTER_HORIZONTAL;
49    }
50
51    /**
52     * Returns the X coordinate for an edge of this type given its bounds
53     *
54     * @param node the node containing the edge
55     * @param bounds the bounds of the node
56     * @return the X coordinate for an edge of this type given its bounds
57     */
58    public int getX(@Nullable INode node, @NonNull Rect bounds) {
59        // We pass in the bounds rather than look it up via node.getBounds() because
60        // during a resize or move operation, we call this method to look up proposed
61        // bounds rather than actual bounds
62        switch (this) {
63            case RIGHT:
64                return bounds.x + bounds.w;
65            case TOP:
66            case BOTTOM:
67            case CENTER_VERTICAL:
68                return bounds.x + bounds.w / 2;
69            case UNKNOWN:
70                assert false;
71                return bounds.x;
72            case LEFT:
73            case BASELINE:
74            default:
75                return bounds.x;
76        }
77    }
78
79    /**
80     * Returns the Y coordinate for an edge of this type given its bounds
81     *
82     * @param node the node containing the edge
83     * @param bounds the bounds of the node
84     * @return the Y coordinate for an edge of this type given its bounds
85     */
86    public int getY(@Nullable INode node, @NonNull Rect bounds) {
87        switch (this) {
88            case TOP:
89                return bounds.y;
90            case BOTTOM:
91                return bounds.y + bounds.h;
92            case BASELINE: {
93                int baseline = node != null ? node.getBaseline() : -1;
94                if (node == null) {
95                    // This happens when you are dragging an element and we don't have
96                    // a node (only an IDragElement) such as on a palette drag.
97                    // For now just hack it.
98                    baseline = (int) (bounds.h * 0.8f); // HACK
99                }
100                return bounds.y + baseline;
101            }
102            case UNKNOWN:
103                assert false;
104                return bounds.y;
105            case RIGHT:
106            case LEFT:
107            case CENTER_HORIZONTAL:
108            default:
109                return bounds.y + bounds.h / 2;
110        }
111    }
112
113    @Override
114    public String toString() {
115        return name();
116    }
117}
118