예제 #1
0
  /**
   * Get a rectangle representing the bounds of the known colonies. The returned rectangle will have
   * bottom and left sides at zero. The top and right sides will be expanded to fit all the
   * colonies.
   *
   * @return the rectangle.
   */
  private Rect getColonyMapBounds() {
    Set<Colony> colonies = provider.getColonies();

    if (colonies == null) {
      // No valid colonies: Return a rect with everything zero.
      return new Rect();
    } else {
      // Colonies obtained

      // Find the most north colony
      double furthestNorth = 0;
      double furthestSouth = 400; // Initial, high, value
      double furthestEast = 0;
      double furthestWest = 400; // Initial, high, value
      for (Colony colony : colonies) {
        double colonyY = colony.getY();
        if (colonyY > furthestNorth) {
          furthestNorth = colonyY;
        }
        if (colonyY < furthestSouth) {
          furthestSouth = colonyY;
        }

        double colonyX = colony.getX();
        if (colonyX > furthestEast) {
          furthestEast = colonyX;
        }

        if (colonyX < furthestWest) {
          furthestWest = colonyX;
        }
      }

      return new Rect(
          (int) Math.round(furthestWest),
          (int) Math.round(furthestNorth),
          (int) Math.round(furthestEast),
          (int) Math.round(furthestSouth));
    }
  }
예제 #2
0
  /*
   * (non-Javadoc)
   *
   * @see android.view.View#onDraw(android.graphics.Canvas)
   */
  @Override
  protected void onDraw(Canvas canvas) {

    displayTransform.reset();
    // Rotate to convert to true north
    displayTransform.postRotate(-10);
    // Fix to make the map appear in the center initially
    displayTransform.postTranslate(0, colonyBounds.height());
    displayTransform.postScale(scale, -scale);

    /*
     * When relativeX and relativeY are zero, the origin is centered
     * on the bottom left corner of the screen ((0, getHeight()) ins screen coordinates)
     * Map shifts left (viewport right), relativeX goes to negative infinity
     * Map shifts down (viewport up), relativeY goes to positive infinity
     */
    displayTransform.postTranslate(relativeX, relativeY);

    // Clear the screen
    canvas.drawColor(Color.WHITE);

    Set<Colony> colonies = provider.getColonies();

    Location location = NavigatorLocationListener.getLocation();
    if (colonies != null) {

      synchronized (colonies) {
        for (Colony colony : colonies) {

          float[] points = new float[] {(float) colony.getX(), (float) colony.getY()};

          displayTransform.mapPoints(points);

          if (inWindow(points)) {

            if (colony == selectedColony) {
              // Draw the colony in red with a larger circle
              paint.setColor(Color.RED);
              canvas.drawCircle(points[0], points[1], 4, paint);

              // Draw some extra accoutrements around it

              // Pixels offset from the center of the circle to the tip of the triangle
              final short triOffset = 8;
              // Width of the triangle
              final short triWidth = 10;
              // Length of the triangle
              final short triLength = 20;

              triangle.reset();
              triangle.moveTo(points[0], points[1] - triOffset * scale);
              triangle.lineTo(
                  (float) (points[0] + (triWidth / 2.0) * scale),
                  points[1] - (triOffset + triLength) * scale);
              triangle.lineTo(
                  (float) (points[0] - (triWidth / 2.0) * scale),
                  points[1] - (triOffset + triLength) * scale);
              triangle.close();

              canvas.drawPath(triangle, paint);

              // Bottom triangle
              triangle.reset();
              triangle.moveTo(points[0], points[1] + triOffset * scale);
              triangle.lineTo(
                  (float) (points[0] + (triWidth / 2.0) * scale),
                  points[1] + (triOffset + triLength) * scale);
              triangle.lineTo(
                  (float) (points[0] - (triWidth / 2.0) * scale),
                  points[1] + (triOffset + triLength) * scale);
              triangle.close();

              canvas.drawPath(triangle, paint);

              // Left triangle
              triangle.reset();
              triangle.moveTo(points[0] - triOffset * scale, points[1]);
              triangle.lineTo(
                  points[0] - (triOffset + triLength) * scale,
                  (float) (points[1] + (triWidth / 2.0) * scale));
              triangle.lineTo(
                  points[0] - (triOffset + triLength) * scale,
                  (float) (points[1] - (triWidth / 2.0) * scale));
              triangle.close();

              canvas.drawPath(triangle, paint);

            } else { // Not selected, draw it as usual

              // Colony draw information:
              // Focus & not visited: blue
              // Focus & visited: blue & green
              // Normal & not visited: gray
              // Normal & visited: green

              // Draw the colony in a different color if it has not been visited
              if (!colony.isVisited()) {
                if (colony.isFocusColony()) {
                  // Draw a semitransparent circle
                  paint.setColor(BG_FOCUS_COLOR);
                  canvas.drawCircle(points[0], points[1], BG_RADIUS, paint);
                } else {
                  // Draw a semitransparent circle
                  paint.setColor(BG_NORMAL_COLOR);
                  canvas.drawCircle(points[0], points[1], BG_RADIUS, paint);
                }

                paint.setColor(Color.BLACK);
              } else {
                if (colony.isFocusColony()) {
                  // Focus and visited. Split the circle into two parts
                  // Left side blue
                  paint.setColor(BG_FOCUS_COLOR);
                  RectF bgRect =
                      new RectF(
                          (float) (points[0] - BG_RADIUS),
                          (float) (points[1] - BG_RADIUS),
                          (float) (points[0] + BG_RADIUS),
                          (float) (points[1] + BG_RADIUS));
                  canvas.drawArc(bgRect, 90, 180, true, paint);
                  // Right side green
                  paint.setColor(BG_VISITED_COLOR);
                  canvas.drawArc(bgRect, -90, 180, true, paint);
                } else {
                  // Draw a semitransparent circle
                  paint.setColor(BG_VISITED_COLOR);
                  canvas.drawCircle(points[0], points[1], BG_RADIUS, paint);
                }

                // Draw the colony in a different color
                paint.setColor(Color.GREEN);
              }
              canvas.drawCircle(points[0], points[1], 2, paint);
            }

            paint.setColor(colonyLabelColor);
            paint.setTextSize(10 * scale);
            canvas.drawText(
                String.valueOf(colony.getId()),
                points[0] + 2 * scale,
                points[1] + 3 * scale,
                paint);
          }
        }
      }
    }
    if (location != null) {
      // Get the location in colony coordinates
      PointF pointColonyCoords = transform.toLocal(location.getLongitude(), location.getLatitude());

      float[] locationPoint = new float[] {pointColonyCoords.x, pointColonyCoords.y};

      displayTransform.mapPoints(locationPoint);

      if (inWindow(locationPoint)) {
        // Draw a circle at the user's current location
        canvas.drawCircle(locationPoint[0], locationPoint[1], 5 * scale, paint);
      }

      if (selectedColony != null) {
        // Have location and selected colony
        // Draw a line between them
        int oldColor = paint.getColor();
        paint.setColor(Color.GREEN);
        paint.setColor(oldColor);

        float[] selectedPoint =
            new float[] {(float) selectedColony.getX(), (float) selectedColony.getY()};
        displayTransform.mapPoints(selectedPoint);

        canvas.drawLine(
            locationPoint[0], locationPoint[1], selectedPoint[0], selectedPoint[1], paint);
      }
    }

    // Static map elements
    paint.setColor(Color.BLUE);
    paint.setStrokeWidth(10);

    // Portal road
    float[] portalRoadA = new float[] {-100, -25};
    float[] portalRoadB = new float[] {1500, 265};
    displayTransform.mapPoints(portalRoadA);
    displayTransform.mapPoints(portalRoadB);
    canvas.drawLine(portalRoadA[0], portalRoadA[1], portalRoadB[0], portalRoadB[1], paint);

    paint.setStrokeWidth(5);

    //
    //		canvas.drawLine(1350, transformY(250), 1230, transformY(1000),
    //				kAntiAliasPaint); // Wrangler
    //		// Road
    //
    //		kAntiAliasPaint.setColor(Color.BLACK);
    //		canvas.drawLine(0, transformY(5), -150, transformY(900),
    //				kAntiAliasPaint);// West boundary
  }