1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 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 package com.android.monkeyrunner; 17 18 import com.android.chimpchat.core.ChimpRect; 19 20 import com.android.monkeyrunner.doc.MonkeyRunnerExported; 21 22 import org.python.core.ArgParser; 23 import org.python.core.ClassDictInit; 24 import org.python.core.PyInteger; 25 import org.python.core.PyList; 26 import org.python.core.PyObject; 27 28 import java.util.List; 29 import java.util.LinkedList; 30 import java.util.logging.Logger; 31 32 /* 33 * A Jython wrap for the ChimpRect class that stores coordinate information for views 34 */ 35 @MonkeyRunnerExported(doc = "Represents the coordinates of a rectangular object") 36 public class MonkeyRect extends PyObject implements ClassDictInit { 37 private static final Logger LOG = Logger.getLogger(MonkeyRect.class.getName()); 38 39 private ChimpRect rect; 40 41 @MonkeyRunnerExported(doc = "The x coordinate of the left side of the rectangle") 42 public int left; 43 @MonkeyRunnerExported(doc = "The y coordinate of the top side of the rectangle") 44 public int top; 45 @MonkeyRunnerExported(doc = "The x coordinate of the right side of the rectangle") 46 public int right; 47 @MonkeyRunnerExported(doc = "The y coordinate of the bottom side of the rectangle") 48 public int bottom; 49 50 public static void classDictInit(PyObject dict) { 51 JythonUtils.convertDocAnnotationsForClass(MonkeyRect.class, dict); 52 } 53 54 public MonkeyRect(ChimpRect rect) { 55 this.rect = rect; 56 this.left = rect.left; 57 this.right = rect.right; 58 this.top = rect.top; 59 this.bottom = rect.bottom; 60 } 61 62 @MonkeyRunnerExported(doc = "Returns the width of the rectangle", 63 returns = "The width of the rectangle as an integer") 64 public PyInteger getWidth() { 65 return new PyInteger(right-left); 66 } 67 68 @MonkeyRunnerExported(doc = "Returns the height of the rectangle", 69 returns = "The height of the rectangle as an integer") 70 public PyInteger getHeight() { 71 return new PyInteger(bottom-top); 72 } 73 74 @MonkeyRunnerExported(doc = "Returns a two item list that contains the x and y value of " + 75 "the center of the rectangle", 76 returns = "The center coordinates as a two item list of integers") 77 public PyList getCenter(){ 78 List<PyInteger> center = new LinkedList<PyInteger>(); 79 /* Center x coordinate */ 80 center.add(new PyInteger(left+(right-left)/2)); 81 /* Center y coordinate */ 82 center.add(new PyInteger(top+(bottom-top)/2)); 83 return new PyList(center); 84 } 85 } 86