Search in sources :

Example 1 with Point2f

use of com.xenoage.utils.math.geom.Point2f in project Zong by Xenoage.

the class Content method onClick.

/**
 * This method is called when the mouse was clicked on the content.
 * A message with the clicked element is shown to the user.
 */
public void onClick(Point2f positionPx) {
    if (layout.getScoreFrames().size() == 0)
        return;
    // get the layout of first score frame
    ScoreFrame frame = layout.getScoreFrames().get(0);
    ScoreFrameLayout frameLayout = frame.getScoreFrameLayout();
    // convert position from screen space to page space, then from page space
    // to frame space, and them from frame space to score frame space
    Point2f positionMm = positionPx.scale(Units.pxToMm(1, mainWindow.getZoom()));
    Point2f framePositionMm = positionMm.sub(frame.getAbsolutePosition());
    Point2f scorePositionMm = frame.getScoreLayoutPosition(framePositionMm);
    // find elements under this position
    for (Stamping stamping : frameLayout.getAllStampings()) {
        if (stamping.getBoundingShape() != null && stamping.getBoundingShape().contains(scorePositionMm)) {
            MusicElement element = stamping.getMusicElement();
            if (element != null) {
                // music element found
                String message = "An element was clicked: " + element;
                if (element instanceof MPElement) {
                    // music element with a known musical position found
                    MPElement mpElement = (MPElement) element;
                    if (mpElement.getParent() != null)
                        message += " at " + mpElement.getMP();
                }
                mainWindow.showMessageDialog(message);
            }
        }
    }
}
Also used : Stamping(com.xenoage.zong.musiclayout.stampings.Stamping) ScoreFrame(com.xenoage.zong.layout.frames.ScoreFrame) Point2f(com.xenoage.utils.math.geom.Point2f) MusicElement(com.xenoage.zong.core.music.MusicElement) MPElement(com.xenoage.zong.core.position.MPElement) ScoreFrameLayout(com.xenoage.zong.musiclayout.ScoreFrameLayout)

Example 2 with Point2f

use of com.xenoage.utils.math.geom.Point2f in project Zong by Xenoage.

the class PageViewManagerTest method getPageDisplayOffset.

@Test
public void getPageDisplayOffset() {
    float distance = view.pageDisplayDistance;
    float d = Delta.DELTA_FLOAT;
    // page 1
    Point2f p = view.getPageDisplayOffset(0);
    assertEquals(-100 / 2, p.x, d);
    assertEquals(-200 / 2, p.y, d);
    // page 2
    p = view.getPageDisplayOffset(1);
    assertEquals(100 / 2 + distance, p.x, d);
    assertEquals(-100 / 2, p.y, d);
    // page 3
    p = view.getPageDisplayOffset(2);
    assertEquals(100 / 2 + distance + 200 + distance, p.x, d);
    assertEquals(-200 / 2, p.y, d);
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f) Test(org.junit.Test)

Example 3 with Point2f

use of com.xenoage.utils.math.geom.Point2f in project Zong by Xenoage.

the class PageViewManagerTest method computeScreenPosition.

@Test
public void computeScreenPosition() {
    Size2i viewSize = new Size2i(800, 600);
    float viewScaling = 1;
    Point2f viewScroll = new Point2f(0, 0);
    ViewState viewState = new ViewState(viewSize, viewScaling, viewScroll);
    // center point of the page must have screen coordinates 400/300
    Point2i pos = view.computePositionPx(layoutPos(null, 0, p(50, 100)), viewState);
    assertEquals(pos.x, 400);
    assertEquals(pos.y, 300);
    // set zoom to 200%, must still be 400/300
    viewScaling = 2;
    viewState = new ViewState(viewSize, viewScaling, viewScroll);
    pos = view.computePositionPx(layoutPos(null, 0, p(50, 100)), viewState);
    assertEquals(pos.x, 400);
    assertEquals(pos.y, 300);
    // scroll to -5/10
    // page point 45/110 must be 400/300
    viewScroll = new Point2f(-5, 10);
    viewState = new ViewState(viewSize, viewScaling, viewScroll);
    pos = view.computePositionPx(layoutPos(null, 0, p(45, 110)), viewState);
    assertEquals(pos.x, 400);
    assertEquals(pos.y, 300);
    // page center point must be
    // 400-(-5*MmToPx*zoom); 300-(10*MmToPx*zoom)
    pos = view.computePositionPx(layoutPos(null, 0, p(50, 100)), viewState);
    assertEquals(pos.x, Math.round(400 - Units.mmToPx(-5, viewScaling)));
    assertEquals(pos.y, Math.round(300 - Units.mmToPx(10, viewScaling)));
}
Also used : Point2i(com.xenoage.utils.math.geom.Point2i) Point2f(com.xenoage.utils.math.geom.Point2f) Size2i(com.xenoage.utils.math.geom.Size2i) Test(org.junit.Test)

Example 4 with Point2f

use of com.xenoage.utils.math.geom.Point2f in project Zong by Xenoage.

the class ScoreDocScoreView method updateScreen.

@Override
public void updateScreen(Size2i screenSizePx, float zoom) {
    this.screenSizePx = screenSizePx;
    this.zoom = zoom;
    // recompute the layout, so that each page fits into the available screen space
    Size2f pageSizeMm = pxToMm(screenSizePx, zoom);
    float marginMm = pxToMm(marginPx, zoom);
    PageFormat pageFormat = new PageFormat(pageSizeMm, new PageMargins(marginMm, marginMm, marginMm, marginMm));
    Size2f frameSizeMm = new Size2f(pageFormat.getUseableWidth(), pageFormat.getUseableHeight());
    // first page needs space for title text
    titleTextHeightPx = screenSizePx.height / 20f;
    float firstFrameOffsetY = pxToMm(titleTextHeightPx, zoom);
    Size2f firstFrameSizeMm = new Size2f(frameSizeMm.width, frameSizeMm.height - firstFrameOffsetY);
    // delete unnecessary layout information, like system distances or system breaks
    Score score = doc.getScore();
    score.setFormat(new ScoreFormat());
    ScoreHeader header = score.getHeader();
    for (int i : range(header.getSystemLayouts())) {
        SystemLayout sl = header.getSystemLayout(i);
        if (sl != null)
            sl.setDistance(SystemLayout.defaultDistance);
    }
    for (int i : range(header.getColumnHeaders())) {
        ColumnHeader ch = header.getColumnHeader(i);
        if (ch.getMeasureBreak() != null)
            ch.setBreak(null);
    }
    // layout the score to find out the needed space
    Context context = new Context(score, App.getSymbolPool(), doc.getLayout().getDefaults().getLayoutSettings());
    Target target = new Target(alist(new ScoreLayoutArea(firstFrameSizeMm)), new ScoreLayoutArea(frameSizeMm), true);
    ScoreLayouter layouter = new ScoreLayouter(context, target);
    ScoreLayout scoreLayout = layouter.createScoreLayout();
    // create and fill at least one page
    Layout layout = new Layout(doc.getLayout().getDefaults());
    ScoreFrameChain chain = null;
    for (int i = 0; i < scoreLayout.frames.size(); i++) {
        Page page = new Page(pageFormat);
        Point2f position;
        Size2f size;
        if (i == 0) {
            // first page
            position = new Point2f(pageSizeMm.width / 2, pageSizeMm.height / 2 + firstFrameOffsetY);
            size = firstFrameSizeMm;
        } else {
            // other pages
            position = new Point2f(pageSizeMm.width / 2, pageSizeMm.height / 2);
            size = frameSizeMm;
        }
        ScoreFrame frame = new ScoreFrame();
        frame.setPosition(position);
        frame.setSize(size);
        page.addFrame(frame);
        layout.addPage(page);
        if (chain == null) {
            chain = new ScoreFrameChain(score);
            chain.setScoreLayout(scoreLayout);
        }
        chain.add(frame);
    }
    this.layout = layout;
}
Also used : Context(com.xenoage.zong.musiclayout.layouter.Context) ScoreFrame(com.xenoage.zong.layout.frames.ScoreFrame) ScoreLayouter(com.xenoage.zong.musiclayout.layouter.ScoreLayouter) ScoreLayout(com.xenoage.zong.musiclayout.ScoreLayout) Page(com.xenoage.zong.layout.Page) Paint(android.graphics.Paint) ScoreFormat(com.xenoage.zong.core.format.ScoreFormat) PageMargins(com.xenoage.zong.core.format.PageMargins) PageFormat(com.xenoage.zong.core.format.PageFormat) Score(com.xenoage.zong.core.Score) ScoreHeader(com.xenoage.zong.core.header.ScoreHeader) ColumnHeader(com.xenoage.zong.core.header.ColumnHeader) Target(com.xenoage.zong.musiclayout.layouter.Target) Point2f(com.xenoage.utils.math.geom.Point2f) ScoreLayout(com.xenoage.zong.musiclayout.ScoreLayout) SystemLayout(com.xenoage.zong.core.format.SystemLayout) Layout(com.xenoage.zong.layout.Layout) SystemLayout(com.xenoage.zong.core.format.SystemLayout) ScoreFrameChain(com.xenoage.zong.layout.frames.ScoreFrameChain) Size2f(com.xenoage.utils.math.geom.Size2f) ScoreLayoutArea(com.xenoage.zong.musiclayout.layouter.ScoreLayoutArea)

Example 5 with Point2f

use of com.xenoage.utils.math.geom.Point2f in project Zong by Xenoage.

the class Frame method getFP.

/**
 * Transforms the given coordinates in page space to a {@link FP}.
 * If the given coordinates are not within this frame and force is false,
 * null is returned. Otherwise the computed coordinates are returned, even if
 * they are outside the frame.
 */
public FP getFP(Point2f p, boolean force) {
    Point2f pos = getAbsolutePosition();
    float rot = getAbsoluteRotation();
    float hw = size.width / 2;
    float hh = size.height / 2;
    // two cases: no rotation or rotation
    if (rot == 0f) {
        // no rotation. this is easy to compute.
        if (force || (p.x >= pos.x - hw && p.x <= pos.x + hw && p.y >= pos.y - hh && p.y <= pos.y + hh)) {
            return new FP(this, new Point2f(p.x - pos.x, p.y - pos.y));
        }
    } else {
        // rotated frame. this is more complicated.
        // first fast check: point within circle around center point?
        float radius = max(size.width, size.height);
        Point2f pRel = new Point2f(p.x - pos.x, p.y - pos.y);
        float distanceSq = pRel.x * pRel.x + pRel.y * pRel.y;
        if (force || (distanceSq <= radius * radius)) {
            // the given point could be within the frame. rotate the
            // point and check again.
            pRel = MathUtils.INSTANCE.rotate(pRel, -rot);
            if (force || (pRel.x >= -hw && pRel.x <= +hw && pRel.y >= -hh && pRel.y <= +hh)) {
                return new FP(this, new Point2f(pRel.x, pRel.y));
            }
        }
    }
    return null;
}
Also used : Point2f(com.xenoage.utils.math.geom.Point2f)

Aggregations

Point2f (com.xenoage.utils.math.geom.Point2f)51 BitmapStaff (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapStaff)11 StaffStamping (com.xenoage.zong.musiclayout.stampings.StaffStamping)10 Test (org.junit.Test)10 Color (com.xenoage.utils.color.Color)7 Page (com.xenoage.zong.layout.Page)6 Size2f (com.xenoage.utils.math.geom.Size2f)5 Layout (com.xenoage.zong.layout.Layout)5 ScoreFrame (com.xenoage.zong.layout.frames.ScoreFrame)5 BitmapLine (com.xenoage.zong.musiclayout.stampings.bitmap.BitmapLine)5 Rectangle2f (com.xenoage.utils.math.geom.Rectangle2f)4 FormattedText (com.xenoage.zong.core.text.FormattedText)4 TextMetrics (com.xenoage.utils.font.TextMetrics)3 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)3 ScoreFrameChain (com.xenoage.zong.layout.frames.ScoreFrameChain)3 ScoreLayout (com.xenoage.zong.musiclayout.ScoreLayout)3 Paint (android.graphics.Paint)2 Point2i (com.xenoage.utils.math.geom.Point2i)2 Size2i (com.xenoage.utils.math.geom.Size2i)2 Score (com.xenoage.zong.core.Score)2