SegmentType.java revision 5d7dc30638953195ed55d32bc9dc37102a613ff8
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    LEFT, TOP, RIGHT, BOTTOM, BASELINE, CENTER_VERTICAL, CENTER_HORIZONTAL, UNKNOWN;
31
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     */
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     */
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
99    public String toString() {
100        return name();
101    }
102}
103