Beispiel #1
0
  public SimpleWhiteboard(MultitouchFramework mf, GraphicsWrapper gw) {
    multitouchFramework = mf;
    this.gw = gw;
    multitouchFramework.setPreferredWindowSize(
        Constant.INITIAL_WINDOW_WIDTH, Constant.INITIAL_WINDOW_HEIGHT);

    userContexts = new UserContext[Constant.NUM_USERS];
    for (int j = 0; j < Constant.NUM_USERS; ++j) {
      userContexts[j] = new UserContext(drawing);
    }
    // initialize the positions of the palettes
    if (Constant.NUM_USERS == 1) {
      userContexts[0].setPositionOfCenterOfPalette(
          Constant.INITIAL_WINDOW_WIDTH / 2, Constant.INITIAL_WINDOW_HEIGHT / 2);
    } else {
      // Compute a circular layout of the palettes
      float radius = Math.min(Constant.INITIAL_WINDOW_WIDTH, Constant.INITIAL_WINDOW_HEIGHT) / 4;
      for (int j = 0; j < Constant.NUM_USERS; ++j) {
        float angleInRadians = (float) (2 * Math.PI * j / Constant.NUM_USERS);
        userContexts[j].setPositionOfCenterOfPalette(
            Constant.INITIAL_WINDOW_WIDTH / 2 + (float) (radius * Math.cos(angleInRadians)),
            Constant.INITIAL_WINDOW_HEIGHT / 2 + (float) (radius * Math.sin(angleInRadians)));
      }
    }

    gw.setFontHeight(Constant.TEXT_HEIGHT);

    gw.frame(new AlignedRectangle2D(new Point2D(-100, -100), new Point2D(100, 100)), true);
  }
Beispiel #2
0
 public void draw(GraphicsWrapper gw) {
   gw.setLineWidth(5);
   for (Stroke s : strokes) {
     s.draw(gw);
   }
   gw.setLineWidth(1);
 }
Beispiel #3
0
  public void draw(GraphicsWrapper gw) {
    // draw border
    gw.setColor(0, 0, 0);
    gw.drawRect(x0, y0, width, height);

    for (PaletteButton b : buttons) {
      b.draw(x0, y0, gw);
    }
  }
Beispiel #4
0
  public synchronized void draw() {
    gw.clear(1, 1, 1);
    gw.setColor(0, 0, 0);
    gw.setupForDrawing();

    gw.setCoordinateSystemToWorldSpaceUnits();
    gw.enableAlphaBlending();

    drawing.draw(gw);

    gw.setCoordinateSystemToPixels();

    for (int j = 0; j < Constant.NUM_USERS; ++j) {
      userContexts[j].draw(gw);
    }

    // Draw some text to indicate the number of fingers touching the user interface.
    // This is useful for debugging.
    int totalNumCursors = 0;
    String s = "[";
    for (int j = 0; j < Constant.NUM_USERS; ++j) {
      totalNumCursors += userContexts[j].getNumCursors();
      s += (j == 0 ? "" : "+") + userContexts[j].getNumCursors();
    }
    s += " contacts]";
    if (totalNumCursors > 0) {
      gw.setColor(0, 0, 0);
      gw.setFontHeight(Constant.TEXT_HEIGHT);
      gw.drawString(Constant.TEXT_HEIGHT, 2 * Constant.TEXT_HEIGHT, s);
    }
  }
Beispiel #5
0
 public ArrayList<Point2D> getWorldPositionsOfCursors(GraphicsWrapper gw) {
   ArrayList<Point2D> positions = new ArrayList<Point2D>();
   for (MyCursor cursor : cursors) {
     positions.add(gw.convertPixelsToWorldSpaceUnits(cursor.getCurrentPosition()));
   }
   return positions;
 }
Beispiel #6
0
  /**
   * Draws the first person view on the screen during the game
   *
   * @param gw2 graphics handler to manipulate screen
   * @param px x coordinate of current position, only used to get viewx
   * @param py y coordinate of current position, only used to get viewy
   * @param view_dx view direction, x coordinate
   * @param view_dy view direction, y coordinate
   * @param walk_step, only used to get viewx and viewy
   * @param view_offset, only used to get viewx and viewy
   * @param rset
   * @param ang
   */
  public void redrawPlay(
      GraphicsWrapper gw2,
      int px,
      int py,
      int view_dx,
      int view_dy,
      int walk_step,
      int view_offset,
      RangeSet rset,
      int ang) {
    // new adjustment
    this.gw = gw2;
    this.rset = rset;
    this.view_dx = view_dx;
    this.view_dy = view_dy;
    this.ang = ang;

    // calculate view
    viewx =
        (px * map_unit + map_unit / 2)
            + viewd_unscale(view_dx * (step_size * walk_step - view_offset));
    viewy =
        (py * map_unit + map_unit / 2)
            + viewd_unscale(view_dy * (step_size * walk_step - view_offset));
    // update graphics
    // draw black background on lower half
    gw.setColor("black");
    gw.fillRect(0, 0, view_width, view_height / 2);
    // draw dark gray background on upper half
    gw.setColor("darkGray");
    gw.fillRect(0, view_height / 2, view_width, view_height / 2);
    // set color to white and draw what ever can be seen from the current position
    gw.setColor("white");
    rset.set(0, view_width - 1); // reset set of ranges to set with single new element (0,width-1)
    // debug: reset counters
    traverse_node_ct =
        traverse_ssector_ct = drawrect_ct = drawrect_late_ct = drawrect_segment_ct = 0;
    //
    traverse_node(bsp_root);
  }
Beispiel #7
0
 public void actionPerformed(ActionEvent e) {
   Object source = e.getSource();
   if (source == testMenuItem1) {
     System.out.println("testMenuItem1 has been selected");
   } else if (source == testMenuItem2) {
     System.out.println("testMenuItem2 has been selected");
   } else if (source == frameAllButton) {
     gw.frame(drawing.getBoundingRectangle(), true);
     multitouchFramework.requestRedraw();
   } else if (source == testButton1) {
     System.out.println("testButton1 has been selected");
   } else if (source == testButton2) {
     System.out.println("testButton2 has been selected");
   }
 }
Beispiel #8
0
 public void draw(
     int palette_x,
     int palette_y, // upper left corner of the palette that contains us, in pixels
     GraphicsWrapper gw) {
   // draw background
   if (isPressed) {
     gw.setColor(0, 0, 0, Palette.alpha);
     gw.fillRect(palette_x + x0, palette_y + y0, width, height);
     // set the foreground color in preparation for drawing the label
     gw.setColor(1, 1, 1);
   } else {
     gw.setColor(1, 1, 1, Palette.alpha);
     gw.fillRect(palette_x + x0, palette_y + y0, width, height);
     // draw border
     gw.setColor(0, 0, 0);
     gw.drawRect(palette_x + x0, palette_y + y0, width, height);
   }
   // draw text label
   int stringWidth = Math.round(gw.stringWidth(label));
   gw.drawString(
       palette_x + x0 + (width - stringWidth) / 2,
       palette_y + y0 + height / 2 + Constant.TEXT_HEIGHT / 2,
       label);
 }
 /**
  * Set the show map variable.
  *
  * @param b True if the map should be shown
  */
 public void setMapMode(boolean b) {
   mapMode = b;
   wrapper.invalidate();
 }
 /**
  * Set the show solution variable.
  *
  * @param b True if the solution should be shown
  */
 public void setShowSolution(boolean b) {
   showSolution = b;
   wrapper.invalidate();
 }
 /**
  * Set the show maze variable.
  *
  * @param b True if the Maze walls should be shown
  */
 public void setShowMaze(boolean b) {
   showMaze = b;
   wrapper.invalidate();
 }
Beispiel #12
0
  /**
   * Draws segment on screen via graphics attribute gc Helper method for traverse_ssector
   *
   * @param seg whose seen attribute may be set to true
   * @param ox1
   * @param y1
   * @param ox2
   * @param y2
   */
  private void drawrect(Seg seg, int ox1, int y1, int ox2, int y2) {
    // int y11, y12, y21, y22;
    // Log.v("inside drawrect", "hooray");
    int z1 = 0;
    int z2 = 100;

    drawrect_ct++; // debug, counter
    ox1 -= viewx;
    y1 -= viewy;
    z1 -= viewz;
    ox2 -= viewx;
    y2 -= viewy;
    z2 -= viewz;

    int y11, y12, y21, y22;
    y11 = y21 = -z1;
    y12 = y22 = -z2;

    int x1;
    int x2;
    x1 = -viewd_unscale(view_dy * ox1 - view_dx * y1);
    z1 = -viewd_unscale(view_dx * ox1 + view_dy * y1);
    x2 = -viewd_unscale(view_dy * ox2 - view_dx * y2);
    z2 = -viewd_unscale(view_dx * ox2 + view_dy * y2);

    RangePair rp = new RangePair(x1, z1, x2, z2);
    if (!clip3d(rp)) {
      return;
    }

    y11 = y11 * zscale / rp.z1 + (view_height / 2); // constant from here
    y12 = y12 * zscale / rp.z1 + (view_height / 2); // constant from here
    y21 = y21 * zscale / rp.z2 + (view_height / 2); // constant from here
    y22 = y22 * zscale / rp.z2 + (view_height / 2); // constant from here
    x1 = rp.x1 * zscale / rp.z1 + (view_width / 2); // constant from here
    x2 = rp.x2 * zscale / rp.z2 + (view_width / 2); // constant from here
    if (x1 >= x2) {
        /* reject backfaces */
      // Log.v("hatefulthings", "grrr");
      return;
    }
    int x1i = x1;
    int xd = x2 - x1;
    gw.setColor(seg.col);

    boolean drawn = false;
    drawrect_late_ct++; // debug, counter
    // loop variable is x1i, upper limit x2 is fixed
    // Log.v("more things", ""+x1i);
    // Log.v("more more things", ""+x2);
    while (x1i <= x2) {
      // check if there is an intersection,
      // if there is none proceed exit the loop,
      // if there is one, get it as (x1i,x2i)
      int[] p = {x1i, x2};
      if (!rset.intersect(p)) break;
      x1i = p[0];
      int x2i = p[1];
      // let's work on the intersection (x1i,x2i)
      int xps[] = {x1i, x1i, x2i + 1, x2i + 1};
      int yps[] = {
        y11 + (x1i - x1) * (y21 - y11) / xd,
        y12 + (x1i - x1) * (y22 - y12) / xd + 1,
        y22 + (x2i - x2) * (y22 - y12) / xd + 1,
        y21 + (x2i - x2) * (y21 - y11) / xd
      };
      System.out.println("I get to here");
      gw.fillPolygon(xps, yps, 4);
      drawn = true;
      rset.remove(x1i, x2i);
      x1i = x2i + 1;
      drawrect_segment_ct++; // debug, counter
    }
    if (drawn && !seg.seen) {
      udpateSeenCellsForSegment(seg);
    }
  }
Beispiel #13
0
 public void draw(GraphicsWrapper gw) {
   gw.setColor(color_red, color_green, color_blue);
   gw.drawPolyline(points);
 }
Beispiel #14
0
  // returns true if a redraw is requested
  public boolean processMultitouchInputEvent(
      int id,
      float x, // in pixels
      float y, // in pixels
      int type,
      GraphicsWrapper gw,
      boolean doOtherUserContextsHaveCursors) {
    // Find the cursor that corresponds to the event id, if such a cursor already exists.
    // If no such cursor exists, the below index will be -1, and the reference to cursor will be
    // null.
    int cursorIndex = cursorContainer.findIndexOfCursorById(id);
    MyCursor cursor = (cursorIndex == -1) ? null : cursorContainer.getCursorByIndex(cursorIndex);

    if (cursor == null) {

      if (type == MultitouchFramework.TOUCH_EVENT_UP) {
        // This should never happen, but if it does, just ignore the event.
        return false;
      }

      // The event does not correspond to any existing cursor.
      // In other words, this is a new finger touching the screen.
      // The event is probably of type TOUCH_EVENT_DOWN.
      // A new cursor will need to be created for the event.

      if (palette.contains(x, y)) {
        // The event occurred inside the palette.

        if (cursorContainer.getNumCursors() == 0) {
          // There are currently no cursors engaged for this user context.
          // In other words, this new finger is the only finger for the user context.
          // So, we allow the event for the new finger to activate a button in the palette.
          // We branch according to the button under the event.
          //
          int indexOfButton = palette.indexOfButtonContainingTheGivenPoint(x, y);
          if (indexOfButton == palette.movePalette_buttonIndex) {
            palette.buttons.get(indexOfButton).isPressed = true;

            // Cause a new cursor to be created to keep track of this event id in the future
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_INTERACTING_WITH_WIDGET, indexOfButton);
          } else if (indexOfButton == palette.ink_buttonIndex
              || indexOfButton == palette.select_buttonIndex
              || indexOfButton == palette.manipulate_buttonIndex
              || indexOfButton == palette.camera_buttonIndex) {
            // We transition to the mode corresponding to the button
            palette.buttons.get(palette.currentlyActiveModalButton).isPressed = false;
            palette.currentlyActiveModalButton = indexOfButton;
            palette.buttons.get(indexOfButton).isPressed = true;

            // Cause a new cursor to be created to keep track of this event id in the future
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_INTERACTING_WITH_WIDGET, indexOfButton);
          } else if (indexOfButton == palette.black_buttonIndex
              || indexOfButton == palette.red_buttonIndex
              || indexOfButton == palette.green_buttonIndex) {
            // We transition to the color corresponding to the button
            palette.buttons.get(palette.currentlyActiveColorButton).isPressed = false;
            palette.currentlyActiveColorButton = indexOfButton;
            palette.buttons.get(indexOfButton).isPressed = true;

            if (indexOfButton == palette.black_buttonIndex) {
              palette.current_red = 0;
              palette.current_green = 0;
              palette.current_blue = 0;
            } else if (indexOfButton == palette.red_buttonIndex) {
              palette.current_red = 1.0f;
              palette.current_green = 0;
              palette.current_blue = 0;
            } else if (indexOfButton == palette.green_buttonIndex) {
              palette.current_red = 0;
              palette.current_green = 1.0f;
              palette.current_blue = 0;
            }

            // Cause a new cursor to be created to keep track of this event id in the future
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_INTERACTING_WITH_WIDGET, indexOfButton);
          } else if (indexOfButton == palette.horizFlip_buttonIndex) {
            palette.buttons.get(indexOfButton).isPressed = true;

            // Cause a new cursor to be created to keep track of this event id in the future
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_INTERACTING_WITH_WIDGET, indexOfButton);

            // Flip the selected strokes horizontally (around a vertical axis)
            for (Stroke s : selectedStrokes) {
              Point2D center = s.getBoundingRectangle().getCenter();
              for (Point2D p : s.getPoints()) {
                p.copy(center.x() - (p.x() - center.x()), p.y());
              }
              s.markBoundingRectangleDirty();
            }
            drawing.markBoundingRectangleDirty();
          } else if (indexOfButton == palette.frameAll_buttonIndex) {
            palette.buttons.get(indexOfButton).isPressed = true;

            // Cause a new cursor to be created to keep track of this event id in the future
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_INTERACTING_WITH_WIDGET, indexOfButton);

            // Frame the entire drawing
            gw.frame(drawing.getBoundingRectangle(), true);
          } else {
            // The event occurred on some part of the palette where there are no buttons.
            // We cause a new cursor to be created to keep track of this event id in the future.
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);

            // Prevent the cursor from doing anything in the future.
            cursor.setType(MyCursor.TYPE_NOTHING);
          }
        } else {
          // There is already at least one cursor.
          // In other words, there is already one or more other fingers being tracked in this user
          // context
          // (possibly on a palette button, and/or over the drawing).
          // To keep things simple, we prevent this new finger from doing anything.

          // We create a new cursor ...
          cursorIndex = cursorContainer.updateCursorById(id, x, y);
          cursor = cursorContainer.getCursorByIndex(cursorIndex);

          // ... and prevent the cursor from doing anything in the future.
          cursor.setType(MyCursor.TYPE_NOTHING);
        }
      } else {
        // The event did not occur inside the palette.
        // This new finger may have been placed down to start
        // drawing a stroke, or start camera manipulation, etc.
        // We branch according to the current mode.
        //
        if (palette.currentlyActiveModalButton == palette.ink_buttonIndex) {
          // start drawing a stroke
          cursorIndex = cursorContainer.updateCursorById(id, x, y);
          cursor = cursorContainer.getCursorByIndex(cursorIndex);
          cursor.setType(MyCursor.TYPE_INKING);
        } else if (palette.currentlyActiveModalButton == palette.select_buttonIndex) {
          // The new finger should only start selecting
          // if there is not already another finger performing selection.
          if (cursorContainer.getNumCursorsOfGivenType(MyCursor.TYPE_SELECTION) == 0) {
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_SELECTION);
          } else {
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_NOTHING);
          }
        } else if (palette.currentlyActiveModalButton == palette.manipulate_buttonIndex) {
          // The new finger should only manipulate the selection
          // if there are not already 2 fingers manipulating the selection.
          if (cursorContainer.getNumCursorsOfGivenType(MyCursor.TYPE_DIRECT_MANIPULATION) < 2) {
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_DIRECT_MANIPULATION);
          } else {
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_NOTHING);
          }
        } else if (palette.currentlyActiveModalButton == palette.camera_buttonIndex) {
          // The new finger should only manipulate the camera
          // if there are not already 2 fingers manipulating the camera.
          if (cursorContainer.getNumCursorsOfGivenType(MyCursor.TYPE_CAMERA_PAN_ZOOM) < 2) {
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_CAMERA_PAN_ZOOM);
          } else {
            cursorIndex = cursorContainer.updateCursorById(id, x, y);
            cursor = cursorContainer.getCursorByIndex(cursorIndex);
            cursor.setType(MyCursor.TYPE_NOTHING);
          }
        }
      }
    } else {
      // The event corresponds to an already existing cursor
      // (and the cursor was probably created during an earlier event of type TOUCH_EVENT_DOWN).
      // The current event is probably of type TOUCH_EVENT_MOVE or TOUCH_EVENT_UP.

      if (type == MultitouchFramework.TOUCH_EVENT_MOVE) {
        // The event is a move event, and corresponds to an existing cursor.
        // Is the location of the event different from the last reported location?
        Point2D newPosition = new Point2D(x, y);
        if (cursor.getCurrentPosition().equals(newPosition)) {
          // The event's location is the same as last time.
          // Don't bother processing the event any further.
          return false; // do not request a redraw
        }
      }

      // We branch according to the type of cursor.
      //
      if (cursor.type == MyCursor.TYPE_NOTHING) {
        // Update the cursor with its new position.
        cursorContainer.updateCursorById(id, x, y);

        if (type == MultitouchFramework.TOUCH_EVENT_UP)
          cursorContainer.removeCursorByIndex(cursorIndex);
      } else if (cursor.type == MyCursor.TYPE_INTERACTING_WITH_WIDGET) {
        if (type == MultitouchFramework.TOUCH_EVENT_UP) {
          // The user lifted their finger off of a palette button.
          cursorContainer.removeCursorByIndex(cursorIndex);

          if (!palette.buttons.get(cursor.indexOfButton).isSticky) {
            palette.buttons.get(cursor.indexOfButton).isPressed = false;
          }
        } else {
          // Earlier, the user pressed down on a button in the palette,
          // and now they are dragging their finger over the button
          // (and possibly onto other buttons).
          // If this is the "move palette" button, we move the palette.
          if (cursor.indexOfButton == palette.movePalette_buttonIndex) {
            movePalette(x - cursor.getCurrentPosition().x(), y - cursor.getCurrentPosition().y());
          }
          cursorIndex = cursorContainer.updateCursorById(id, x, y);
        }
      } else if (cursor.type == MyCursor.TYPE_INKING) {
        if (type == MultitouchFramework.TOUCH_EVENT_UP) {
          // up event
          cursorIndex = cursorContainer.updateCursorById(id, x, y);

          // Add the newly drawn stroke to the drawing
          Stroke newStroke = new Stroke();
          newStroke.setColor(palette.current_red, palette.current_green, palette.current_blue);
          for (Point2D p : cursor.getPositions()) {
            newStroke.addPoint(gw.convertPixelsToWorldSpaceUnits(p));
          }
          drawing.addStroke(newStroke);

          cursorContainer.removeCursorByIndex(cursorIndex);
        } else {
          // drag event; just update the cursor with the new position
          cursorIndex = cursorContainer.updateCursorById(id, x, y);
        }
      } else if (cursor.type == MyCursor.TYPE_CAMERA_PAN_ZOOM) {
        if (type == MultitouchFramework.TOUCH_EVENT_UP) {
          // up event
          cursorContainer.removeCursorByIndex(cursorIndex);
        } else {
          // drag event
          cursorIndex = cursorContainer.updateCursorById(id, x, y);

          if (cursorContainer.getNumCursorsOfGivenType(MyCursor.TYPE_CAMERA_PAN_ZOOM) == 2) {
            MyCursor cursor0 = cursorContainer.getCursorByType(MyCursor.TYPE_CAMERA_PAN_ZOOM, 0);
            MyCursor cursor1 = cursorContainer.getCursorByType(MyCursor.TYPE_CAMERA_PAN_ZOOM, 1);
            gw.panAndZoomBasedOnDisplacementOfTwoPoints(
                id == cursor0.id ? cursor0.getPreviousPosition() : cursor0.getCurrentPosition(),
                id == cursor1.id ? cursor1.getPreviousPosition() : cursor1.getCurrentPosition(),
                cursor0.getCurrentPosition(),
                cursor1.getCurrentPosition());
          } else if (cursorContainer.getNumCursorsOfGivenType(MyCursor.TYPE_CAMERA_PAN_ZOOM) == 1) {
            gw.pan(
                cursor.getCurrentPosition().x() - cursor.getPreviousPosition().x(),
                cursor.getCurrentPosition().y() - cursor.getPreviousPosition().y());
          }
        }
      } else if (cursor.type == MyCursor.TYPE_SELECTION) {
        if (type == MultitouchFramework.TOUCH_EVENT_UP) {
          // up event
          cursorIndex = cursorContainer.updateCursorById(id, x, y);

          // Update the selection
          if (cursor.doesDragLookLikeLassoGesture()) {
            // complete a lasso selection

            // Need to transform the positions of the cursor from pixels to world space coordinates.
            // We will store the world space coordinates in the following data structure.
            ArrayList<Point2D> lassoPolygonPoints = new ArrayList<Point2D>();
            for (Point2D p : cursor.getPositions()) {
              lassoPolygonPoints.add(gw.convertPixelsToWorldSpaceUnits(p));
            }

            selectedStrokes.clear();
            for (Stroke s : drawing.strokes) {
              if (s.isContainedInLassoPolygon(lassoPolygonPoints)) selectedStrokes.add(s);
            }
          } else {
            // complete a rectangle selection

            AlignedRectangle2D selectedRectangle =
                new AlignedRectangle2D(
                    gw.convertPixelsToWorldSpaceUnits(cursor.getFirstPosition()),
                    gw.convertPixelsToWorldSpaceUnits(cursor.getCurrentPosition()));

            selectedStrokes.clear();
            for (Stroke s : drawing.strokes) {
              if (s.isContainedInRectangle(selectedRectangle)) selectedStrokes.add(s);
            }
          }

          cursorContainer.removeCursorByIndex(cursorIndex);
        } else {
          // drag event; just update the cursor with the new position
          cursorIndex = cursorContainer.updateCursorById(id, x, y);
        }
      } else if (cursor.type == MyCursor.TYPE_DIRECT_MANIPULATION) {
        if (type == MultitouchFramework.TOUCH_EVENT_UP) {
          // up event
          cursorContainer.removeCursorByIndex(cursorIndex);
        } else {
          // drag event
          cursorIndex = cursorContainer.updateCursorById(id, x, y);

          if (cursorContainer.getNumCursorsOfGivenType(MyCursor.TYPE_DIRECT_MANIPULATION) == 2) {
            MyCursor cursor0 =
                cursorContainer.getCursorByType(MyCursor.TYPE_DIRECT_MANIPULATION, 0);
            MyCursor cursor1 =
                cursorContainer.getCursorByType(MyCursor.TYPE_DIRECT_MANIPULATION, 1);

            // convert cursor positions to world space
            Point2D cursor0_currentPosition_worldSpace =
                gw.convertPixelsToWorldSpaceUnits(cursor0.getCurrentPosition());
            Point2D cursor1_currentPosition_worldSpace =
                gw.convertPixelsToWorldSpaceUnits(cursor1.getCurrentPosition());
            Point2D cursor0_previousPosition_worldSpace =
                gw.convertPixelsToWorldSpaceUnits(cursor0.getPreviousPosition());
            Point2D cursor1_previousPosition_worldSpace =
                gw.convertPixelsToWorldSpaceUnits(cursor1.getPreviousPosition());

            for (Stroke s : selectedStrokes) {
              Point2DUtil.transformPointsBasedOnDisplacementOfTwoPoints(
                  s.getPoints(),
                  id == cursor0.id
                      ? cursor0_previousPosition_worldSpace
                      : cursor0_currentPosition_worldSpace,
                  id == cursor1.id
                      ? cursor1_previousPosition_worldSpace
                      : cursor1_currentPosition_worldSpace,
                  cursor0_currentPosition_worldSpace,
                  cursor1_currentPosition_worldSpace);
              s.markBoundingRectangleDirty();
            }
            drawing.markBoundingRectangleDirty();
          } else if (cursorContainer.getNumCursorsOfGivenType(MyCursor.TYPE_DIRECT_MANIPULATION)
              == 1) {
            // convert cursor positions to world space
            Point2D cursor_currentPosition_worldSpace =
                gw.convertPixelsToWorldSpaceUnits(cursor.getCurrentPosition());
            Point2D cursor_previousPosition_worldSpace =
                gw.convertPixelsToWorldSpaceUnits(cursor.getPreviousPosition());

            // compute translation vector
            Vector2D translationVector =
                Point2D.diff(cursor_currentPosition_worldSpace, cursor_previousPosition_worldSpace);

            // apply the translation to the selected strokes
            for (Stroke s : selectedStrokes) {
              for (Point2D p : s.getPoints()) {
                p.copy(Point2D.sum(p, translationVector));
              }
              s.markBoundingRectangleDirty();
            }
            drawing.markBoundingRectangleDirty();
          }
        }
      }
    }

    return true; // request a redraw
  }
Beispiel #15
0
  public void draw(GraphicsWrapper gw) {

    palette.draw(gw);

    // draw filled rectangles over the selected strokes
    gw.setCoordinateSystemToWorldSpaceUnits();
    for (Stroke s : selectedStrokes) {
      AlignedRectangle2D r = s.getBoundingRectangle();
      gw.setColor(1.0f, 0.5f, 0, 0.2f); // transparent orange
      Vector2D diagonal = r.getDiagonal();
      gw.fillRect(r.getMin().x(), r.getMin().y(), diagonal.x(), diagonal.y());
    }

    gw.setCoordinateSystemToPixels();

    // draw cursors
    for (int i = 0; i < cursorContainer.getNumCursors(); ++i) {
      MyCursor cursor = cursorContainer.getCursorByIndex(i);
      if (cursor.type == MyCursor.TYPE_NOTHING)
        gw.setColor(0.5f, 0, 0, 0.65f); // red (because this cursor is being ignored)
      else gw.setColor(0, 0.5f, 0.5f, 0.65f); // cyan
      gw.fillCircle(cursor.getCurrentPosition().x() - 10, cursor.getCurrentPosition().y() - 10, 10);

      if (cursor.type == MyCursor.TYPE_INKING) {
        // draw ink trail
        gw.setColor(0, 0, 0);
        gw.drawPolyline(cursor.getPositions());
      } else if (cursor.type == MyCursor.TYPE_SELECTION) {
        if (cursor.doesDragLookLikeLassoGesture()) {
          // draw filled polygon
          gw.setColor(0, 0, 0, 0.2f);
          gw.fillPolygon(cursor.getPositions());
        } else {
          // draw polyline to indicate that a lasso could be started
          gw.setColor(0, 0, 0);
          gw.drawPolyline(cursor.getPositions());

          // also draw selection rectangle
          gw.setColor(0, 0, 0, 0.2f);
          Vector2D diagonal = Point2D.diff(cursor.getCurrentPosition(), cursor.getFirstPosition());
          gw.fillRect(
              cursor.getFirstPosition().x(),
              cursor.getFirstPosition().y(),
              diagonal.x(),
              diagonal.y());
        }
      }
    }
  }