1429ae88878cf781753d8261d350ad89fe5864169Tor Norbye/* 2429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Copyright (C) 2011 The Android Open Source Project 3429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * 4429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * you may not use this file except in compliance with the License. 6429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * You may obtain a copy of the License at 7429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * 8429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * http://www.eclipse.org/org/documents/epl-v10.php 9429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * 10429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Unless required by applicable law or agreed to in writing, software 11429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * distributed under the License is distributed on an "AS IS" BASIS, 12429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * See the License for the specific language governing permissions and 14429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * limitations under the License. 15429ae88878cf781753d8261d350ad89fe5864169Tor Norbye */ 16429ae88878cf781753d8261d350ad89fe5864169Tor Norbyepackage com.android.ide.eclipse.adt.internal.editors.layout.gle2; 17429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 18429ae88878cf781753d8261d350ad89fe5864169Tor Norbyeimport org.eclipse.swt.SWT; 19429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 20429ae88878cf781753d8261d350ad89fe5864169Tor Norbye/** 21429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * A selection handle is a small rectangle on the border of a selected view which lets you 22429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * change the size of the view by dragging it. 23429ae88878cf781753d8261d350ad89fe5864169Tor Norbye */ 24429ae88878cf781753d8261d350ad89fe5864169Tor Norbyepublic class SelectionHandle { 25429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** 26429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Size of the selection handle radius, in control coordinates. Note that this isn't 27429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * necessarily a <b>circular</b> radius; in the case of a rectangular handle, the 28429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * width and the height are both equal to this radius. 29429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Note also that this radius is in <b>control</b> coordinates, whereas the rest 30429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * of the class operates in layout coordinates. This is because we do not want the 31429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * selection handles to grow or shrink along with the screen zoom; they are always 32429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * at the given pixel size in the control. 33429ae88878cf781753d8261d350ad89fe5864169Tor Norbye */ 34429ae88878cf781753d8261d350ad89fe5864169Tor Norbye public final static int PIXEL_RADIUS = 3; 35429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 36429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** 37429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Extra number of pixels to look beyond the actual radius of the selection handle 38429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * when matching mouse positions to handles 39429ae88878cf781753d8261d350ad89fe5864169Tor Norbye */ 40429ae88878cf781753d8261d350ad89fe5864169Tor Norbye public final static int PIXEL_MARGIN = 2; 41429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 42429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** The position of the handle in the selection rectangle */ 43429ae88878cf781753d8261d350ad89fe5864169Tor Norbye enum Position { 44429ae88878cf781753d8261d350ad89fe5864169Tor Norbye TOP_MIDDLE(SWT.CURSOR_SIZEN), 45429ae88878cf781753d8261d350ad89fe5864169Tor Norbye TOP_RIGHT(SWT.CURSOR_SIZENE), 46429ae88878cf781753d8261d350ad89fe5864169Tor Norbye RIGHT_MIDDLE(SWT.CURSOR_SIZEE), 47429ae88878cf781753d8261d350ad89fe5864169Tor Norbye BOTTOM_RIGHT(SWT.CURSOR_SIZESE), 48429ae88878cf781753d8261d350ad89fe5864169Tor Norbye BOTTOM_MIDDLE(SWT.CURSOR_SIZES), 49429ae88878cf781753d8261d350ad89fe5864169Tor Norbye BOTTOM_LEFT(SWT.CURSOR_SIZESW), 50429ae88878cf781753d8261d350ad89fe5864169Tor Norbye LEFT_MIDDLE(SWT.CURSOR_SIZEW), 51429ae88878cf781753d8261d350ad89fe5864169Tor Norbye TOP_LEFT(SWT.CURSOR_SIZENW); 52429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 53429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** Corresponding SWT cursor value */ 54429ae88878cf781753d8261d350ad89fe5864169Tor Norbye private int mSwtCursor; 55429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 56429ae88878cf781753d8261d350ad89fe5864169Tor Norbye private Position(int swtCursor) { 57429ae88878cf781753d8261d350ad89fe5864169Tor Norbye mSwtCursor = swtCursor; 58429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 59429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 60429ae88878cf781753d8261d350ad89fe5864169Tor Norbye private int getCursorType() { 61429ae88878cf781753d8261d350ad89fe5864169Tor Norbye return mSwtCursor; 62429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 63429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 64429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** Is the {@link SelectionHandle} somewhere on the left edge? */ 65429ae88878cf781753d8261d350ad89fe5864169Tor Norbye boolean isLeft() { 66429ae88878cf781753d8261d350ad89fe5864169Tor Norbye return this == TOP_LEFT || this == LEFT_MIDDLE || this == BOTTOM_LEFT; 67429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 68429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 69429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** Is the {@link SelectionHandle} somewhere on the right edge? */ 70429ae88878cf781753d8261d350ad89fe5864169Tor Norbye boolean isRight() { 71429ae88878cf781753d8261d350ad89fe5864169Tor Norbye return this == TOP_RIGHT || this == RIGHT_MIDDLE || this == BOTTOM_RIGHT; 72429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 73429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 74429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** Is the {@link SelectionHandle} somewhere on the top edge? */ 75429ae88878cf781753d8261d350ad89fe5864169Tor Norbye boolean isTop() { 76429ae88878cf781753d8261d350ad89fe5864169Tor Norbye return this == TOP_LEFT || this == TOP_MIDDLE || this == TOP_RIGHT; 77429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 78429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 79429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** Is the {@link SelectionHandle} somewhere on the bottom edge? */ 80429ae88878cf781753d8261d350ad89fe5864169Tor Norbye boolean isBottom() { 81429ae88878cf781753d8261d350ad89fe5864169Tor Norbye return this == BOTTOM_LEFT || this == BOTTOM_MIDDLE || this == BOTTOM_RIGHT; 82429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 83429ae88878cf781753d8261d350ad89fe5864169Tor Norbye }; 84429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 85429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** The x coordinate of the center of the selection handle */ 86429ae88878cf781753d8261d350ad89fe5864169Tor Norbye public final int centerX; 87429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** The y coordinate of the center of the selection handle */ 88429ae88878cf781753d8261d350ad89fe5864169Tor Norbye public final int centerY; 89429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** The position of the handle in the selection rectangle */ 90429ae88878cf781753d8261d350ad89fe5864169Tor Norbye private final Position mPosition; 91429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 92429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** 93429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Constructs a new {@link SelectionHandle} at the given layout coordinate 94429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * corresponding to a handle at the given {@link Position}. 95429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * 96429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * @param centerX the x coordinate of the center of the selection handle 97429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * @param centerY y coordinate of the center of the selection handle 98429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * @param position the position of the handle in the selection rectangle 99429ae88878cf781753d8261d350ad89fe5864169Tor Norbye */ 100429ae88878cf781753d8261d350ad89fe5864169Tor Norbye public SelectionHandle(int centerX, int centerY, Position position) { 101429ae88878cf781753d8261d350ad89fe5864169Tor Norbye mPosition = position; 102429ae88878cf781753d8261d350ad89fe5864169Tor Norbye this.centerX = centerX; 103429ae88878cf781753d8261d350ad89fe5864169Tor Norbye this.centerY = centerY; 104429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 105429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 106429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** 107429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Determines whether the given {@link LayoutPoint} is within the given distance in 108429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * layout coordinates. The distance should incorporate at least the equivalent 109429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * distance to the control coordinate space {@link #PIXEL_RADIUS}, but usually with a 110429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * few extra pixels added in to make the corners easier to target. 111429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * 112429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * @param point the mouse position in layout coordinates 113429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * @param distance the distance from the center of the handle to check whether the 114429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * point fits within 115429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * @return true if the given point is within the given distance of this handle 116429ae88878cf781753d8261d350ad89fe5864169Tor Norbye */ 117429ae88878cf781753d8261d350ad89fe5864169Tor Norbye public boolean contains(LayoutPoint point, int distance) { 118429ae88878cf781753d8261d350ad89fe5864169Tor Norbye return (point.x >= centerX - distance 119429ae88878cf781753d8261d350ad89fe5864169Tor Norbye && point.x <= centerX + distance 120429ae88878cf781753d8261d350ad89fe5864169Tor Norbye && point.y >= centerY - distance 121429ae88878cf781753d8261d350ad89fe5864169Tor Norbye && point.y <= centerY + distance); 122429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 123429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 124429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** 125429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Returns the position of the handle in the selection rectangle 126429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * 127429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * @return the position of the handle in the selection rectangle 128429ae88878cf781753d8261d350ad89fe5864169Tor Norbye */ 129429ae88878cf781753d8261d350ad89fe5864169Tor Norbye public Position getPosition() { 130429ae88878cf781753d8261d350ad89fe5864169Tor Norbye return mPosition; 131429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 132429ae88878cf781753d8261d350ad89fe5864169Tor Norbye 133429ae88878cf781753d8261d350ad89fe5864169Tor Norbye /** 134429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * Returns the SWT cursor type to use for this selection handle 135429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * 136429ae88878cf781753d8261d350ad89fe5864169Tor Norbye * @return the position of the handle in the selection rectangle 137429ae88878cf781753d8261d350ad89fe5864169Tor Norbye */ 138429ae88878cf781753d8261d350ad89fe5864169Tor Norbye public int getSwtCursorType() { 139429ae88878cf781753d8261d350ad89fe5864169Tor Norbye return mPosition.getCursorType(); 140429ae88878cf781753d8261d350ad89fe5864169Tor Norbye } 141429ae88878cf781753d8261d350ad89fe5864169Tor Norbye} 142