Search in sources :

Example 1 with PointInDocument

use of doc.PointInDocument in project OpenNotebook by jaltekruse.

the class DocMouseListener method dragMouseToPlaceObj.

private boolean dragMouseToPlaceObj(MouseEvent e) {
    if (!(objPlacementRequiresMouseDrag || selectionRectRequiresMouseDrag)) {
        // document mouse is not is a state in which this method is needed
        return false;
    }
    PointInDocument docPt = docPanel.panelPt2DocPt(e.getX(), e.getY());
    MathObject objToPlace = docPanel.getObjToPlace();
    if (selectionRectRequiresMouseDrag) {
        // swap in the selection rectangle,
        // allows reuse of code for the
        // addition of a
        // rectangle to select objects
        objToPlace = docPanel.getSelectionRect();
        selectionRectBeingResized = true;
        selectionRectRequiresMouseDrag = false;
    }
    if (docPt.isOutSidePage()) {
        objToPlace.getParentContainer().removeObject(objToPlace);
        docPanel.setFocusedObject(null);
    } else {
        boolean isWest = false;
        if (!selectionRectBeingResized) {
            // if the object being re-sized is
            // not the selection rectangle
            draggingDot = true;
        }
        if (docPt.getxPos() < objToPlace.getxPos()) {
            objToPlace.setWidth(objToPlace.getxPos() - docPt.getxPos());
            objToPlace.setxPos(docPt.getxPos());
            isWest = true;
        } else {
            objToPlace.setWidth(docPt.getxPos() - objToPlace.getxPos());
        }
        if (objToPlace.isHorizontallyResizable() && !objToPlace.isVerticallyResizable()) {
            objToPlace.setHeight(15);
            if (isWest) {
                currentDragDot = MathObjectGUI.WEST_DOT;
            } else {
                currentDragDot = MathObjectGUI.EAST_DOT;
            }
        } else if (!objToPlace.isHorizontallyResizable() && objToPlace.isVerticallyResizable()) {
            if (docPt.getyPos() < objToPlace.getyPos()) {
                objToPlace.setyPos(docPt.getyPos());
                currentDragDot = MathObjectGUI.NORTH_DOT;
            } else {
                currentDragDot = MathObjectGUI.SOUTH_DOT;
            }
        } else if (docPt.getyPos() < objToPlace.getyPos()) {
            objToPlace.setHeight(objToPlace.getyPos() - docPt.getyPos());
            objToPlace.setyPos(docPt.getyPos());
            if (isWest) {
                currentDragDot = MathObjectGUI.NORTHWEST_DOT;
            } else {
                currentDragDot = MathObjectGUI.NORTHEAST_DOT;
                objToPlace.flipHorizontally();
            }
        } else {
            objToPlace.setHeight(docPt.getyPos() - objToPlace.getyPos());
            if (isWest) {
                currentDragDot = MathObjectGUI.SOUTHWEST_DOT;
                objToPlace.flipVertically();
            } else {
                currentDragDot = MathObjectGUI.SOUTHEAST_DOT;
            }
        }
        if (selectionRectRequiresMouseDrag) {
            selectionRectRequiresMouseDrag = false;
            selectionRectBeingResized = true;
        }
        setObjPlacementRequiresMouseDrag(false);
        docPanel.repaintDoc();
    }
    return true;
}
Also used : MathObject(doc.mathobjects.MathObject) PointInDocument(doc.PointInDocument)

Example 2 with PointInDocument

use of doc.PointInDocument in project OpenNotebook by jaltekruse.

the class DocMouseListener method mouseClicked.

public void mouseClicked(MouseEvent e) {
    docPanel.requestFocus();
    boolean clickHandled = false, requiresRedraw = false;
    if (selectionRectRequiresMouseDrag || selectionRectBeingResized) {
        selectionRectRequiresMouseDrag = false;
        selectionRectBeingResized = false;
        docPanel.setSelectionRect(null);
    }
    PointInDocument clickedPt = docPanel.panelPt2DocPt(e.getX(), e.getY());
    if (!clickedPt.isOutSidePage()) {
        Rectangle objRect;
        // gives extra space around object to allow selection,
        // most useful for when objects are very thin
        int clickBuffer = 3;
        Vector<MathObject> currPageObjects = docPanel.getDoc().getPage(clickedPt.getPage()).getObjects();
        MathObject mObj, oldFocused;
        oldFocused = docPanel.getFocusedObject();
        Page page = docPanel.getDoc().getPage(clickedPt.getPage());
        for (int i = (currPageObjects.size() - 1); i >= 0; i--) {
            // cycle through all of the objects belonging the page that was clicked on
            mObj = currPageObjects.get(i);
            objRect = mObj.getBounds();
            if (objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos())) && mObj != docPanel.getFocusedObject()) {
                // the click occurred within an object, that was already selected
                if (mObj instanceof Grouping && docPanel.isInStudentMode()) {
                    for (MathObject subObj : ((Grouping) mObj).getObjects()) {
                        objRect = subObj.getBounds();
                        if (objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos())) && mObj != docPanel.getFocusedObject()) {
                            docPanel.setFocusedObject(subObj);
                            docPanel.repaintDoc();
                            return;
                        }
                    }
                } else {
                    if (e.isShiftDown()) {
                        docPanel.addObjectToSelection(mObj);
                    } else {
                        docPanel.setFocusedObject(mObj);
                    }
                    docPanel.repaintDoc();
                    docPanel.updateObjectToolFrame();
                    return;
                }
            }
            // without ungrouping
            if (e.getClickCount() == 2 && objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos())) && mObj == docPanel.getFocusedObject() && docPanel.getFocusedObject() instanceof Grouping) {
                for (MathObject subObj : ((Grouping) mObj).getObjects()) {
                    objRect = new Rectangle(subObj.getxPos(), subObj.getyPos(), subObj.getWidth(), subObj.getHeight());
                    if (objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos()))) {
                        docPanel.setFocusedObject(subObj);
                        docPanel.repaintDoc();
                        return;
                    }
                }
            }
        }
        if (docPanel.getFocusedObject() == oldFocused) {
            if (oldFocused != null) {
                objRect = new Rectangle(oldFocused.getxPos() - clickBuffer, oldFocused.getyPos() - clickBuffer, oldFocused.getWidth() + 2 * clickBuffer, oldFocused.getHeight() + 2 * clickBuffer);
                if (!objRect.contains(new Point(clickedPt.getxPos(), clickedPt.getyPos()))) {
                    // send event to object
                    docPanel.setFocusedObject(null);
                    docPanel.repaintDoc();
                    return;
                }
            }
        }
    } else {
        // click was outside of page
        docPanel.setSelectedPage(null);
        docPanel.setFocusedObject(null);
        docPanel.repaintDoc();
        return;
    }
    // objects can be off of the page, so this check must happen out here
    if (docPanel.getFocusedObject() != null && !clickHandled) {
        Point objPos = null;
        objPos = docPanel.getObjectPos(docPanel.getFocusedObject());
        Rectangle focusedRect = new Rectangle(objPos.x, objPos.y, (int) (docPanel.getFocusedObject().getWidth() * docPanel.getZoomLevel()), (int) (docPanel.getFocusedObject().getHeight() * docPanel.getZoomLevel()));
        if (focusedRect.contains(new Point(e.getX(), e.getY()))) {
            // user clicked on the object that already had focus, send an event to the objet so it
            // can handle it for (for drag and drop, moving graphs etc.)
            docPanel.getPageGUI().handleMouseAction(docPanel.getFocusedObject(), (int) (e.getX() - objPos.getX()), (int) (e.getY() - objPos.getY()), PageGUI.MOUSE_LEFT_CLICK);
            clickHandled = true;
            requiresRedraw = true;
        }
    // throw click down to focused object
    }
    if (!clickHandled && !clickedPt.isOutSidePage()) {
        // the click hit a page, but missed all of its objects, select the
        // page
        docPanel.setSelectedPage(clickedPt.getPage());
        clickHandled = true;
        requiresRedraw = true;
    }
    if (!clickHandled) {
        // click occurred on a non-active part of screen,
        // unfocus current object
        docPanel.setFocusedObject(null);
        requiresRedraw = true;
    }
    if (requiresRedraw) {
        docPanel.repaintDoc();
    }
}
Also used : Rectangle(java.awt.Rectangle) Page(doc.Page) Grouping(doc.mathobjects.Grouping) Point(java.awt.Point) MathObject(doc.mathobjects.MathObject) Point(java.awt.Point) PointInDocument(doc.PointInDocument)

Example 3 with PointInDocument

use of doc.PointInDocument in project OpenNotebook by jaltekruse.

the class DocMouseListener method mousePressed.

@Override
public void mousePressed(MouseEvent e) {
    docPanel.requestFocus();
    draggingDot = false;
    PointInDocument clickedPt = docPanel.panelPt2DocPt(e.getX(), e.getY());
    MathObject objToPlace = docPanel.getObjToPlace();
    if (docPanel.isPlacingObject()) {
        objToPlace.setxPos(clickedPt.getxPos());
        objToPlace.setyPos(clickedPt.getyPos());
        objToPlace.setWidth(1);
        objToPlace.setHeight(1);
        objToPlace.setParentContainer(docPanel.getDoc().getPage(clickedPt.getPage()));
        objToPlace.getParentContainer().addObject(objToPlace);
        docPanel.repaintDoc();
        docPanel.updateObjectToolFrame();
        objPlacementRequiresMouseDrag = true;
        docPanel.setFocusedObject(objToPlace);
        return;
    } else if (detectResizeOrMove(e)) {
        // resize dot or the selected object
        return;
    } else if (clickedPt.isOutSidePage()) {
        // click was outside of page
        if (docPanel.isPlacingObject()) {
            docPanel.getNotebookPanel().objHasBeenPlaced();
            docPanel.ungroupTempGroup();
        }
    } else {
        // create a box to select multiple objects
        if (!docPanel.isInStudentMode()) {
            docPanel.ungroupTempGroup();
            RectangleObject rect = new RectangleObject(docPanel.getDoc().getPage(clickedPt.getPage()));
            rect.setxPos(clickedPt.getxPos());
            rect.setyPos(clickedPt.getyPos());
            rect.setWidth(1);
            rect.setHeight(1);
            docPanel.setSelectionRect(rect);
            selectionRectRequiresMouseDrag = true;
        }
    }
}
Also used : RectangleObject(doc.mathobjects.RectangleObject) MathObject(doc.mathobjects.MathObject) PointInDocument(doc.PointInDocument)

Example 4 with PointInDocument

use of doc.PointInDocument in project OpenNotebook by jaltekruse.

the class DocViewerPanel method panelPt2DocPt.

public PointInDocument panelPt2DocPt(int x, int y) {
    //pixels needed to render a page, and the minimum border space around it
    DocSizeMetrics docSizeMetrics = new DocSizeMetrics();
    PointInDocument ptInDoc = new PointInDocument();
    ptInDoc.setOutSidePage(true);
    ptInDoc.setPage(doc.getNumPages() - 1);
    //go through all of the pages to look for collisions
    for (int i = 0; i < doc.getNumPages(); i++) {
        if (y > docSizeMetrics.pageYOrigin && y < docSizeMetrics.pageYOrigin + docSizeMetrics.pageYSize) {
            ptInDoc.setyPos((int) ((y - docSizeMetrics.pageYOrigin) / zoomLevel));
            ptInDoc.setPage(i);
            ptInDoc.setOutSidePage(false);
            break;
        } else if (y <= docSizeMetrics.pageYOrigin) {
            //the click was above this page, but did not hit any previous pages, it must be in the buffer
            //space between pages
            ptInDoc.setPage(i);
            break;
        }
        docSizeMetrics.pageYOrigin += docSizeMetrics.pageYSize + docSizeMetrics.adjustedBufferSpace;
    }
    if (y > docSizeMetrics.pageYOrigin + docSizeMetrics.pageYSize) {
        // the click was below the last page
        ptInDoc.setBelowPage(true);
        ptInDoc.setPage(doc.lastPageIndex());
    }
    if (x < docSizeMetrics.pageXOrigin || x > docSizeMetrics.pageXOrigin + docSizeMetrics.pageXSize) {
        ptInDoc.setOutSidePage(true);
    } else {
        ptInDoc.setxPos((int) ((x - docSizeMetrics.pageXOrigin) / zoomLevel));
    }
    return ptInDoc;
}
Also used : Point(java.awt.Point) PointInDocument(doc.PointInDocument)

Example 5 with PointInDocument

use of doc.PointInDocument in project OpenNotebook by jaltekruse.

the class DocMouseListener method dragMouseToResizeObj.

private boolean dragMouseToResizeObj(MouseEvent e) {
    if (!(draggingDot || selectionRectBeingResized)) {
        // method is needed
        return false;
    }
    // one of the dots of an object is being moved to make it larger or
    // smaller
    // or the temporary selection rectangle is being resized
    PointInDocument docPt = docPanel.panelPt2DocPt(e.getX(), e.getY());
    MathObject objToResize = null;
    if (draggingDot) {
        // an object is focused and one of its resizing dots
        // has been contacted
        // resize the focues object
        objToResize = docPanel.getFocusedObject();
    } else if (selectionRectBeingResized) {
        // the user drew a selection
        // rectangle on the screen,
        // resize the selection
        // rectangle
        objToResize = docPanel.getSelectionRect();
    }
    Page pageOfResizeObject = objToResize.getParentPage();
    Page contactedPage = docPanel.getDoc().getPage(docPt.getPage());
    if (!docPt.isOutSidePage() && pageOfResizeObject == contactedPage) {
        if (selectionRectBeingResized) {
            // a special object that does note
            // exist in the document was
            // resized, it is used
            // to select multiple objects at
            // once and create groups
            MathObjectGUI.moveResizeDot(docPanel.getSelectionRect(), currentDragDot, docPt, this);
            adjustTempGroupSelection();
        } else {
            // a regular object was resized
            MathObjectGUI.moveResizeDot(objToResize, currentDragDot, docPt, this);
        }
        docPanel.repaintDoc();
        return true;
    } else {
        // the mouse moved outside of the page where the object to
        // resize resides
        Point pageOrigin = null;
        pageOrigin = docPanel.getPageOrigin(objToResize.getParentPage());
        // flags to indicate that a position has not been assigned to the
        // click
        int xMouseRequest = Integer.MAX_VALUE;
        int yMouseRequest = Integer.MAX_VALUE;
        if (e.getX() <= pageOrigin.getX()) {
            // event was to the left of the
            // document, send an resize
            // request with the edge of page
            // and the events given y
            // position
            xMouseRequest = 0;
        } else if (e.getX() >= pageOrigin.getX() + (int) (pageOfResizeObject.getWidth() * docPanel.getZoomLevel())) {
            xMouseRequest = pageOfResizeObject.getWidth();
        }
        if (e.getY() <= pageOrigin.getY()) {
            yMouseRequest = 0;
        } else if (e.getY() >= pageOrigin.getY() + (int) (pageOfResizeObject.getHeight() * docPanel.getZoomLevel())) {
            yMouseRequest = pageOfResizeObject.getHeight();
        }
        if (yMouseRequest == Integer.MAX_VALUE) {
            yMouseRequest = docPt.getyPos();
        }
        if (xMouseRequest == Integer.MAX_VALUE) {
            xMouseRequest = docPt.getxPos();
        }
        MathObjectGUI.moveResizeDot(objToResize, currentDragDot, new PointInDocument(1, xMouseRequest, yMouseRequest), this);
        if (objToResize.getParentContainer() instanceof Grouping) {
            ((Grouping) objToResize.getParentContainer()).adjustSizeToFitChildren();
        }
        if (selectionRectBeingResized) {
            // a special object that does note
            // exist in the document was
            // resized, it is used
            // to select multiple objects at
            // once and create groups
            adjustTempGroupSelection();
        }
        docPanel.repaintDoc();
    }
    return true;
}
Also used : Page(doc.Page) Grouping(doc.mathobjects.Grouping) Point(java.awt.Point) MathObject(doc.mathobjects.MathObject) Point(java.awt.Point) PointInDocument(doc.PointInDocument)

Aggregations

PointInDocument (doc.PointInDocument)5 MathObject (doc.mathobjects.MathObject)4 Point (java.awt.Point)3 Page (doc.Page)2 Grouping (doc.mathobjects.Grouping)2 RectangleObject (doc.mathobjects.RectangleObject)1 Rectangle (java.awt.Rectangle)1