Search in sources :

Example 1 with LineFaceRenderProps

use of maspack.render.LineFaceRenderProps in project artisynth_core by artisynth.

the class ColorBar method render.

@Override
public void render(Renderer renderer, int flags) {
    if (!isSelectable() && renderer.isSelecting()) {
        return;
    }
    RenderObject robj = myRenderObject;
    VectorNd labelPos = myLabelPos;
    ArrayList<String> labelText = myLabelText;
    if (robj == null || labelPos == null || labelText == null) {
        System.out.println((robj == null) + " " + (labelPos == null) + " " + (labelText == null));
        return;
    }
    int screenWidth = renderer.getScreenWidth();
    int screenHeight = renderer.getScreenHeight();
    // turn off shading for the labels
    Renderer.Shading savedShadeModel = renderer.getShading();
    renderer.setShading(Renderer.Shading.NONE);
    double x0 = myLoc.x;
    double y0 = myLoc.y;
    double w = myLoc.width;
    double h = myLoc.height;
    if (horizontal) {
        h = myLoc.width;
        w = myLoc.height;
        x0 = myLoc.y;
        y0 = myLoc.x;
    }
    // absolute or normalized -> absolute
    if (Math.abs(x0) <= 1) {
        x0 = x0 * screenWidth;
    }
    if (x0 < 0) {
        x0 = screenWidth + x0;
    }
    if (Math.abs(y0) <= 1) {
        y0 = y0 * screenHeight;
    }
    if (y0 < 0) {
        y0 = screenHeight + y0;
    }
    if (w <= 1) {
        w = w * screenWidth;
    }
    if (h <= 1) {
        h = h * screenHeight;
    }
    renderer.pushModelMatrix();
    renderer.setModelMatrix2d(0, screenWidth, 0, screenHeight);
    // transform so that the colorbar occupies correct location
    AffineTransform3d trans = new AffineTransform3d();
    if (horizontal) {
        trans.setRotation(ROT_Z_270);
        trans.applyScaling(h, w, 1);
    } else {
        trans.applyScaling(w, h, 1);
    }
    trans.setTranslation(x0, y0, 0);
    renderer.mulModelMatrix(trans);
    renderer.setVertexColorMixing(ColorMixing.REPLACE);
    renderer.drawTriangles(robj, 0);
    float savedLineWidth = renderer.getLineWidth();
    LineFaceRenderProps props = (LineFaceRenderProps) getRenderProps();
    renderer.setLineWidth(props.getLineWidth());
    renderer.setLineColoring(props, /*highlight=*/
    false);
    renderer.setVertexColorMixing(ColorMixing.NONE);
    renderer.drawLines(robj, 0);
    renderer.popModelMatrix();
    // return line width
    renderer.setLineWidth(savedLineWidth);
    // labels
    int nLabels = Math.min(labelPos.size(), labelText.size());
    if (nLabels > 0) {
        double tx, ty;
        // // for consistency, assume line top as 3/4 font size
        // double t = myTextSize*0.75;
        // double vc = myTextSize* 0.25;
        // double b = myTextSize*0.25;
        renderer.setFaceColoring(props, isSelected());
        float[] loc = new float[3];
        for (int i = 0; i < nLabels; i++) {
            tx = 0;
            ty = 0;
            // text orientation computation
            String label = labelText.get(i);
            Rectangle2D box = renderer.getTextBounds(myFont, label, myTextSize);
            double bw = box.getWidth();
            double bh = box.getHeight();
            double b = box.getY();
            double vc = b + bh / 2;
            double t = bh + b;
            if (horizontal) {
                switch(hAlignment) {
                    case LEFT:
                        tx = x0 + myTextOffset.x;
                        break;
                    case CENTRE:
                        tx = x0 - bw / 2;
                        break;
                    case RIGHT:
                        tx = x0 - myTextOffset.x - bw;
                        break;
                }
                tx += w * labelPos.get(i);
                switch(vAlignment) {
                    case BOTTOM:
                        ty = y0 - myTextOffset.y - t;
                        break;
                    case CENTRE:
                        ty = y0 - vc + h / 2;
                        break;
                    case TOP:
                        ty = y0 + h + myTextOffset.y + b;
                        break;
                }
            } else {
                switch(hAlignment) {
                    case LEFT:
                        tx = x0 - myTextOffset.x - bw;
                        break;
                    case CENTRE:
                        tx = x0 - bw / 2 + w / 2;
                        break;
                    case RIGHT:
                        tx = x0 + myTextOffset.x + w;
                        break;
                }
                switch(vAlignment) {
                    case BOTTOM:
                        ty = y0 + myTextOffset.y;
                        break;
                    case CENTRE:
                        ty = y0 - vc;
                        break;
                    case TOP:
                        ty = y0 - myTextOffset.y - t;
                        break;
                }
                ty += h * labelPos.get(i);
            }
            loc[0] = (float) tx;
            loc[1] = (float) ty;
            renderer.drawText(myFont, label, loc, myTextSize);
        }
    }
    renderer.setShading(savedShadeModel);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) VectorNd(maspack.matrix.VectorNd) Renderer(maspack.render.Renderer) LineFaceRenderProps(maspack.render.LineFaceRenderProps) RenderObject(maspack.render.RenderObject) AffineTransform3d(maspack.matrix.AffineTransform3d)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)1 AffineTransform3d (maspack.matrix.AffineTransform3d)1 VectorNd (maspack.matrix.VectorNd)1 LineFaceRenderProps (maspack.render.LineFaceRenderProps)1 RenderObject (maspack.render.RenderObject)1 Renderer (maspack.render.Renderer)1