/* tab:2 * * * "Copyright (c) 2000 and The Regents of the University * of California. All rights reserved. * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose, without fee, and without written * agreement is hereby granted, provided that the above copyright * notice and the following two paragraphs appear in all copies of * this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Authors: Phil Levis, Nelson Lee * Date: December 11, 2002 * Desc: mote object for AdvSim * */ /** * @author Phil Levis * @author Nelson Lee */ package net.tinyos.sim; import net.tinyos.sim.event.*; import java.util.*; public class ResponderSimObject extends SimObject { private int id; public ResponderSimObject(TinyViz tv, double x, double y, int id, SimObjectPopupMenu popup) { super(tv, MOTE_OBJECT_SIZE, popup); Attribute a; a = new MoteCoordinateAttribute(x, y); addAttribute(a); a = new MoteIDAttribute(id); addAttribute(a); this.id = id; } public int getID() { return id; } public MoteCoordinateAttribute getCoordinate() { return (MoteCoordinateAttribute)getAttribute("net.tinyos.sim.MoteCoordinateAttribute"); } public boolean getPower() { MotePowerAttribute attr = (MotePowerAttribute)getAttribute("net.tinyos.sim.MotePowerAttribute"); if (attr == null) return false; return attr.getPower(); } public void setPower(boolean power) { MotePowerAttribute attr = (MotePowerAttribute)getAttribute("net.tinyos.sim.MotePowerAttribute"); if (attr == null) { addAttribute(new MotePowerAttribute(power)); } else { attr.setPower(power); } } public boolean pointInSimObjectSpace(int windowX, int windowY, CoordinateTransformer cT) { synchronized (simState) { MoteCoordinateAttribute coordAttrib = getCoordinate(); double moteWinX = cT.simXToGUIX(coordAttrib.getX()); double moteWinY = cT.simYToGUIY(coordAttrib.getY()); double dx = (double)windowX - moteWinX; double dy = (double)windowY - moteWinY; int distance = (int)Math.sqrt((dx * dx) + (dy * dy)); if (distance <= super.getObjectSize()) { return true; } return false; } } public boolean simObjectInQuad(double x1, double y1, double x2, double y2, CoordinateTransformer cT) { synchronized (simState) { MoteCoordinateAttribute coordAttrib = getCoordinate(); double sGUIMinX = cT.simXToGUIX(coordAttrib.getX()); double sGUIMaxX = sGUIMinX + getObjectSize(); double sGUIMaxY = cT.simYToGUIY(coordAttrib.getY()); double sGUIMinY = sGUIMaxY - getObjectSize(); if ((((x1 <= sGUIMinX) && (sGUIMinX <= x2)) || ((x1 <= sGUIMaxX) && (sGUIMaxX <= x2))) && (((y1 <= sGUIMinY) && (sGUIMinY <= y2)) || ((y1 <= sGUIMaxY) && (sGUIMaxY <= y2)))) { return true; } return false; } } public void moveSimObject(int dx, int dy, CoordinateTransformer cT) { synchronized (simState) { MoteCoordinateAttribute coordinateAttribute = getCoordinate(); coordinateAttribute.setX(cT.guiXToSimX(dx) + coordinateAttribute.getX()); coordinateAttribute.setY(cT.guiYToSimY(dy) + coordinateAttribute.getY()); } } public void addCoordinateChangedEvent() { synchronized (simState) { MoteCoordinateAttribute coordinateAttribute = getCoordinate(); eventBus.addEvent(new AttributeEvent(ATTRIBUTE_CHANGED, this, coordinateAttribute)); } } public String toString() { return "[Responder "+id+"]"; } }