/** * Performs a pick at the tap location. */ public void pick(MotionEvent event) { final int PICK_REGION_SIZE = 40; // pixels // Forget our last picked objects togglePickedObjectHighlights(); this.pickedObjects.clear(); // Perform a new pick at the screen x, y PickedObjectList pickList = getWorldWindow().pickShapesInRect( event.getX() - PICK_REGION_SIZE / 2, event.getY() - PICK_REGION_SIZE / 2, PICK_REGION_SIZE, PICK_REGION_SIZE); // pickShapesInRect can return multiple objects, i.e., they're may be more that one 'top object' // So we iterate through the list instead of calling pickList.topPickedObject which returns the // arbitrary 'first' top object. for (int i = 0; i < pickList.count(); i++) { if (pickList.pickedObjectAt(i).isOnTop()) { this.pickedObjects.add(pickList.pickedObjectAt(i).getUserObject()); } } togglePickedObjectHighlights(); }
protected void resolvePickRect(DrawContext dc) { if (dc.pickedObjects.count() == 0) { return; // no eligible objects; avoid expensive calls to glReadPixels } // Read the unique fragment colors in the pick rectangle. Set<Color> pickColors = dc.readPixelColors(dc.pickViewport.x, dc.pickViewport.y, dc.pickViewport.width, dc.pickViewport.height); for (Color pickColor : pickColors) { // Convert the fragment color to a picked object ID. This returns zero if the color cannot indicate a picked // object ID. int topObjectId = PickedObject.uniqueColorToIdentifier(pickColor); if (topObjectId != 0) { PickedObject topObject = dc.pickedObjects.pickedObjectWithId(topObjectId); if (topObject != null) { topObject.markOnTop(); } } } // Remove all picked objects not marked as on top. dc.pickedObjects.keepTopObjects(); } }
protected void resolvePick(DrawContext dc) { if (dc.pickedObjects.count() == 0) { return; // no eligible objects; avoid expensive calls to glReadPixels } // Read the fragment color at the pick point. this.pickColor = dc.readPixelColor((int) Math.round(dc.pickPoint.x), (int) Math.round(dc.pickPoint.y), this.pickColor); // Convert the fragment color to a picked object ID. This returns zero if the color cannot indicate a picked // object ID, in which case no objects have been drawn at the pick point. int topObjectId = PickedObject.uniqueColorToIdentifier(this.pickColor); if (topObjectId != 0) { PickedObject terrainObject = dc.pickedObjects.terrainPickedObject(); PickedObject topObject = dc.pickedObjects.pickedObjectWithId(topObjectId); if (topObject != null) { topObject.markOnTop(); dc.pickedObjects.clearPickedObjects(); dc.pickedObjects.offerPickedObject(topObject); dc.pickedObjects.offerPickedObject(terrainObject); // handles null objects and duplicate objects } else { dc.pickedObjects.clearPickedObjects(); // no eligible objects drawn at the pick point } } else { dc.pickedObjects.clearPickedObjects(); // no objects drawn at the pick point } }