OSDN Git Service

ADT GRE: Move gscripts package.
[android-x86/sdk.git] / eclipse / plugins / com.android.ide.eclipse.adt / src / com / android / ide / eclipse / adt / editors / layout / gscripts / Rect.java
1 /*
2  * Copyright (C) 2009 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.eclipse.adt.editors.layout.gscripts;
18
19
20 /**
21  * Mutable rectangle bounds.
22  * <p/>
23  * To be valid, w >= 1 and h >= 1.
24  * By definition:
25  * - right side = x + w - 1.
26  * - bottom side = y + h - 1.
27  */
28 public class Rect {
29     public int x, y, w, h;
30
31     /** Initialize an invalid rectangle. */
32     public Rect() {
33     }
34
35     /** Initialize rectangle to the given values. They can be invalid. */
36     public Rect(int x, int y, int w, int h) {
37         set(x, y, w, h);
38     }
39
40     /** Initialize rectangle to the given values. They can be invalid. */
41     public void set(int x, int y, int w, int h) {
42         this.x = x;
43         this.y = y;
44         this.w = w;
45         this.h = h;
46     }
47
48     /** Initialize rectangle to match the given one. */
49     public void set(Rect r) {
50         x = r.x;
51         y = r.y;
52         w = r.w;
53         h = r.h;
54     }
55
56     /** Returns a new instance of a rectangle with the same values. */
57     public Rect copy() {
58         return new Rect(x, y, w, h);
59     }
60
61     /** Returns true if the rectangle has valid bounds, i.e. w>0 and h>0. */
62     public boolean isValid() {
63         return w > 0 && h > 0;
64     }
65
66     /** Returns true if the rectangle contains the x,y coordinates, borders included. */
67     public boolean contains(int x, int y) {
68         return isValid() &&
69             x >= this.x &&
70             y >= this.y &&
71             x < (this.x + this.w) &&
72             y < (this.y + this.h);
73     }
74
75     @Override
76     public String toString() {
77         return String.format("Rect [%dx%d - %dx%d]", x, y, w, h);
78     }
79 }