Example #1
0
bool MgGrid::_draw(int mode, GiGraphics& gs, const GiContext& ctx) const
{
    Vector2d cell(m_cell / 2);
    
    if (cell.x < _MGZERO || cell.y < _MGZERO) {
        GiContext ctxedge(ctx);
        ctxedge.setNoFillColor();
        gs.drawRect(&ctxedge, getRect());
        return __super::_draw(mode, gs, ctx);
    }
    
    int nx = (int)(getWidth() / cell.x + _MGZERO);
    int ny = (int)(getHeight() / cell.y + _MGZERO);
    Box2d rect(getPoint(3), getPoint(3)
               + Vector2d((float)(cell.x * nx), (float)(cell.y * ny)));
    
    float w = gs.calcPenWidth(ctx.getLineWidth(), ctx.isAutoScale()) / -2.f;
    GiContext ctxgrid(w, ctx.getLineColor());
    
    bool antiAlias = gs.setAntiAliasMode(false);
    int ret = gs.drawRect(&ctxgrid, rect) ? 1 : 0;
    
    bool switchx = (nx >= 10 && cell.x < gs.xf().displayToModel(20, true));
    bool switchy = (ny >= 10 && cell.y < gs.xf().displayToModel(20, true));
    Point2d pts[2] = { rect.leftTop(), rect.leftBottom() };
    
    for (int i = 1; i < nx; i++) {
        pts[0].x += cell.x;
        pts[1].x += cell.x;
        ctxgrid.setLineWidth(!switchx || i%5 > 0 ? w/2 : w, false);
        ctxgrid.setLineAlpha(-w < 0.9f && (!switchx || i%5 > 0) ?
            ctx.getLineAlpha() / 2 : ctx.getLineAlpha());
        ret += gs.drawLine(&ctxgrid, pts[0], pts[1]) ? 1 : 0;
    }
    
    pts[0] = rect.leftBottom();
    pts[1] = rect.rightBottom();
    for (int j = 1; j < ny; j++) {
        pts[0].y += cell.y;
        pts[1].y += cell.y;
        ctxgrid.setLineWidth(!switchy || j%5 > 0 ? w/2 : w, false);
        ctxgrid.setLineAlpha(-w < 0.9f && (!switchy || j%5 > 0) ?
            ctx.getLineAlpha() / 2 : ctx.getLineAlpha());
        ret += gs.drawLine(&ctxgrid, pts[0], pts[1]) ? 1 : 0;
    }

    gs.setAntiAliasMode(antiAlias);
    
    return __super::_draw(mode, gs, ctx) || ret > 0;
}