void WisteriaApp::update() { // Iterate all the blossoms, update each one and notice if they are all idle bool isIdle = true; for( vector<Branch>::iterator blossomIt = mBlossoms.begin(); blossomIt != mBlossoms.end(); ++blossomIt ) { blossomIt->update(); if( blossomIt->isAlive() ) isIdle = false; } if( isIdle && ( ! mIsIdle ) ) { mIsIdle = true; // first frame of idleness mIdleFrames = 0; } else if( isIdle ) { // the blossoms have all not changed in a while, let's clear them out and put up some new ones if( mIdleFrames++ > IDLE_RESTART_FRAMES ) { // clear the context mOffscreenContext.setSourceRgb( 0.23f, 0.23f, 0.23f ); mOffscreenContext.paint(); mBlossoms.clear(); mBlossoms.push_back( Branch( Vec2f( Rand::randFloat( WIDTH ), Rand::randFloat( HEIGHT ) ), Branch::randomHue(), 0 ) ); mBlossoms.push_back( Branch( Vec2f( Rand::randFloat( WIDTH ), Rand::randFloat( HEIGHT ) ), Branch::randomHue(), 180 ) ); mBlossoms.push_back( Branch( Vec2f( Rand::randFloat( WIDTH ), Rand::randFloat( HEIGHT ) ), Branch::randomHue(), 320 ) ); mIsIdle = false; } } }
void bsplineApp::drawBSpline( cairo::Context &ctx ) { if( mPoints.size() > (size_t)mDegree ) { ctx.setLineWidth( 2.5f ); ctx.setSourceRgb( 1.0f, 0.5f, 0.25f ); ctx.appendPath( Path2d( BSpline2f( mPoints, mDegree, mLoop, mOpen ) ) ); ctx.stroke(); // ctx.fill(); } }
void CairoBasicApp::renderScene( cairo::Context &ctx ) { // clear the context with our radial gradient cairo::GradientRadial radialGrad( getWindowCenter(), 0, getWindowCenter(), getWindowWidth() ); radialGrad.addColorStop( 0, Color( 1, 1, 1 ) ); radialGrad.addColorStop( 1, Color( 0.6, 0.6, 0.6 ) ); ctx.setSource( radialGrad ); ctx.paint(); for( vector<Flower>::const_iterator flIt = mFlowers.begin(); flIt != mFlowers.end(); ++flIt ) flIt->draw( ctx ); }
void draw( cairo::Context &ctx ) const { // draw the solid petals ctx.setSource( mColor ); makePath( ctx ); ctx.fill(); // draw the petal outlines ctx.setSource( mColor * 0.8f ); makePath( ctx ); ctx.stroke(); };
void makePath( cairo::Context &ctx ) const { for( int petal = 0; petal < mNumPetals; ++petal ) { ctx.newSubPath(); float petalAngle = ( petal / (float)mNumPetals ) * 2 * M_PI; Vec2f outsideCircleCenter = mLoc + Vec2f::xAxis() * cos( petalAngle ) * mRadius + Vec2f::yAxis() * sin( petalAngle ) * mRadius; Vec2f insideCircleCenter = mLoc + Vec2f::xAxis() * cos( petalAngle ) * mPetalInsideRadius + Vec2f::yAxis() * sin( petalAngle ) * mPetalInsideRadius; ctx.arc( outsideCircleCenter, mPetalOutsideRadius, petalAngle + M_PI / 2 + M_PI, petalAngle + M_PI / 2 ); ctx.arc( insideCircleCenter, mPetalInsideRadius, petalAngle + M_PI / 2, petalAngle + M_PI / 2 + M_PI ); ctx.closePath(); } }
void fontSampleApp::drawCharacterVerbose( cairo::Context &ctx, vec2 where ) { cairo::Matrix prevMat; ctx.getMatrix( &prevMat ); // draw it filled ctx.setSourceRgb( 1.0f, 1.0, 0.5f ); ctx.translate( where ); // Uncomment below to render the character filled // ctx.appendPath( mShape ); // ctx.fill(); VerboseCharDraw verb( ctx ); mShape.iterate<VerboseCharDraw>( verb ); ctx.setMatrix( prevMat ); }
void ContinuousPathApp::drawCurves(cairo::Context &ctx, Path2d path, float thickness, Color col) { ctx.setLineWidth(thickness); ctx.setSource(col); ctx.newSubPath(); int pointIndex = 0; for (int i=0; i<path.getNumSegments(); i++) { int segType = path.getSegmentType(i); // change jumpIndex depending on the type of segment switch(segType){ case Path2d::CUBICTO: if(i==0) ctx.moveTo(path.getPoint(pointIndex)); // do a curve to using the next 2 points as the curves and the 3rd as the end point ctx.curveTo(path.getPoint(pointIndex+1), path.getPoint(pointIndex+2), path.getPoint(pointIndex+3)); pointIndex += 3; break; case Path2d::MOVETO: // don't do anything with this point ctx.moveTo(path.getPoint(pointIndex)); pointIndex += 0; break; default: pointIndex += 1; break; } } ctx.stroke(); }
void WisteriaApp::setup() { // allocate our offscreen buffer mOffscreenBuffer = cairo::SurfaceImage( WIDTH, HEIGHT, false ); mOffscreenContext = cairo::Context( mOffscreenBuffer ); // fill the buffer with gray mOffscreenContext.setSourceRgb( 0.23f, 0.23f, 0.23f ); mOffscreenContext.paint(); // Let the Branches know how big our windows is Branch::setWindowSize( WIDTH, HEIGHT ); // Create some starting blossoms mBlossoms.push_back( Branch( Vec2f( WIDTH - 50, 50 ), 0.619444444f, 0 ) ); mBlossoms.push_back( Branch( Vec2f( 60, HEIGHT - 60 ), 0.905f, 180 ) ); mBlossoms.push_back( Branch( Vec2f( WIDTH / 2, HEIGHT / 2 ), 0.105555556f, 320 ) ); mIsIdle = false; mIdleFrames = 0; }
void PathSimplificationApp::drawLine(cairo::Context &ctx, Vec2f const &pt1, Vec2f const &pt2, float thickness, Color col) { ctx.setLineWidth(thickness); ctx.setSource(col); ctx.newSubPath(); ctx.moveTo(pt1.x, pt1.y); ctx.lineTo(pt2.x, pt2.y); ctx.closePath(); ctx.stroke(); }
// Draw each path onto the passed cairo context void PathSimplificationApp::drawPath(cairo::Context &ctx, SmoothPath *smoothPath, int mode) { // initialize the line settings float thickness = 1.0; ctx.setLineWidth( 1.0f ); ctx.setLineCap(cairo::LINE_CAP_ROUND); ctx.setLineJoin(cairo::LINE_JOIN_ROUND); if(smoothPath->inProgress){ // draw lines point to point vector<Vec2f> pathPoints = smoothPath->getPathPoints(); for (int i=1; i<pathPoints.size(); i++) { drawLine(ctx, pathPoints[i-1], pathPoints[i], 1.0, Color(1.0, 0.0, 0.0)); drawCircle(ctx, pathPoints[i], 2, Color(1.0, 0.0, 0.0)); } }else{ // draw smooth lines Path2d path = smoothPath->getCurrentPath(); drawCurves(ctx, path, thickness, Color(255.0, 0.0, 0.0)); int pointIndex = 0; // draw circles at the bezier points for (int i=0; i<path.getNumSegments(); i++) { Vec2f c1 = path.getPoint(pointIndex+1); Vec2f c2 = path.getPoint(pointIndex+2); Vec2f pt1 = path.getPoint(pointIndex); Vec2f pt2 = path.getPoint(pointIndex+3); drawCircle(ctx, c1, 1, Color(1.0f, 1.0f, 0.0f), true); drawCircle(ctx, c2, 1, Color(0.0f, 1.0f, 1.0f)); drawLine(ctx, c1, pt1, 1, Color(1.0, 1.0, 0.0 )); drawLine(ctx, c2, pt2, 1, Color(0.0, 1.0, 1.0 )); pointIndex += 3; } } }
void ContinuousPathApp::drawCircle(cairo::Context &ctx, Vec2f const &pt, double diameter, Color col, bool outline) { ctx.setLineWidth(1); ctx.setSource( col ); ctx.newSubPath(); ctx.circle( pt.x, pt.y, diameter ); ctx.closePath(); if(outline){ ctx.stroke(); }else{ ctx.fill(); } }
void Branch::draw( cairo::Context ctx ) { if ( mLaunchDelay > 0 ) return; if( ( mLifespan > 0 ) && ( ! mIsRoot ) ) { float ageLerp = 1.0 - mLifespan / (float)mTotalLifespan; float radius = lerp( mStartEllipseRadius, mEndEllipseRadius, ageLerp ); ColorA drawColor = lerp( mStartColor, mEndColor, ageLerp ); ctx.setSourceRgba( drawColor.r, drawColor.g, drawColor.b, 0.075f ); ctx.circle( mPos, ( 5.0f * mScale + radius * 1.3f ) / 2 ); ctx.fill(); ctx.setSource( drawColor ); ctx.circle( mPos, radius / 2 ); ctx.fill(); } for( vector<Branch>::iterator blossomIt = mBranches.begin(); blossomIt != mBranches.end(); ++blossomIt ) { blossomIt->draw( ctx ); } }
void ChessPiece::Render(cairo::Context &ctx) { /*if (team = Black) { ctx.setSourceRgb(0.3, 0.3, 0.3); } else { ctx.setSourceRgb(0.7, 0.7, 0.7); } switch (type) { case King: ctx.showText("King"); break; case Queen: ctx.showText("Queen"); break; case Knight: ctx.showText("Knight"); break; case Bishop: ctx.showText("Bishop"); break; case Rook: ctx.showText("Rook"); break; case Pawn: ctx.showText("Pawn"); break; case Invalid: break; default: break; }*/ int verticalOffset; int horizontalOffset; switch (type) { case King: horizontalOffset = 37; verticalOffset = 17; break; case Queen: horizontalOffset = 35; verticalOffset = 17; break; case Knight: horizontalOffset = 26; verticalOffset = 17; break; case Bishop: horizontalOffset = 33; verticalOffset = 17; break; case Rook: horizontalOffset = 27; verticalOffset = 17; break; case Pawn: horizontalOffset = 26; verticalOffset = 17; break; case Invalid: break; default: break; } cinder::Vec2f TLPoint = GetTopLeftBoxPoint(); horizontalOffset += (int)TLPoint.x; verticalOffset += (int)TLPoint.y; ctx.translate(horizontalOffset, verticalOffset); cinder::cairo::SurfaceImage siPiece(surface); cinder::cairo::SurfaceBase* sb = &siPiece; ctx.setSourceSurface(siPiece, 0, 0); ctx.maskSurface(sb, 0, 0); ctx.stroke(); ctx.translate(-horizontalOffset, -verticalOffset); }
void NodeView::draw(cairo::Context &theG) { _size = _calculateSize(); cairo::GradientLinear myGradient(0, 0, 0, _size.y); if(_node->state() == WARNING) { myGradient.addColorStop(0.15, Colorf(0.518, 0.298, 0.298*0.5)); myGradient.addColorStop(0.85, Colorf(0.898, 0.467, 0.0)); } else if(_node->state() == ERROR) { myGradient.addColorStop(0.15, Colorf(0.5, 0, 0)); myGradient.addColorStop(0.85, Colorf(0.8, 0, 0)); } else { myGradient.addColorStop(0.15, Colorf(0.14, 0.12, 0.129)); myGradient.addColorStop(0.85, Colorf(0.227, 0.188, 0.2)); } Rectf myRect; myRect.set(0, 0, _size.x, _size.y); Vec2f myCorrection(0.5, 0.5); theG.translate(myCorrection); theG.translate(_position); theG.setSourceRgb(0.7, 0.7, 0.7); theG.roundedRectangle(myRect, 2); theG.setLineWidth(1); theG.stroke(); theG.translate(Vec2f(-0.5, -0.5)); theG.setSource(myGradient); theG.roundedRectangle(myRect, 2); theG.fill(); // for(int i = 0; i < _node->inputs().size(); i++) { // NodeInputBase *myInput = _node->inputs()[i]; // int myX = _size.x - (i * PIN_WIDTH + (i-1) * PIN_SPACING + PIN_SPACING + PIN_WIDTH); // // theG.setSourceRgb(0.8, 0.8, 0.8); // theG.rectangle(myX - PIN_WIDTH, 0, PIN_WIDTH, PIN_HEIGHT); // theG.fill(); // // // // } // // // // for(int i = 0; i < _node->outputs().size(); i++) { // NodeOutputBase *myOutput = _node->outputs()[i]; // // int myX = _size.x - (i * PIN_WIDTH + (i-1) * PIN_SPACING + PIN_SPACING + PIN_WIDTH); // // theG.setSourceRgb(0.8, 0.8, 0.8); // theG.rectangle(myX - PIN_WIDTH, _size.y - PIN_HEIGHT, PIN_WIDTH, PIN_HEIGHT); // theG.fill(); // } theG.translate(Vec2f(5, _size.y - 5)); theG.setSourceRgb(0.8, 0.8, 0.8); theG.setFont( _font ); theG.setFontSize( 10 ); theG.showText( _node->name() ); theG.fill(); theG.setSourceRgb(1.0, 0, 0); theG.flush(); }
// Draw each path onto the passed cairo context void ContinuousPathApp::drawPath(cairo::Context &ctx, SmoothPath *smoothPath, int mode) { // update the drawing points only if the line hasn't been completed Path2d path = smoothPath->getCurrentPath(); vector<Vec2f> pathPoints = smoothPath->getPathPoints(); vector<int> endPoints = smoothPath->getEndPoints(); if(path.getNumPoints() == 0) return; // initialize the line settings float thickness = 1.0; ctx.setLineWidth( 1.0f ); ctx.setLineCap(cairo::LINE_CAP_ROUND); ctx.setLineJoin(cairo::LINE_JOIN_ROUND); Vec2f pt = path.getPoint(0); // draw bezier line if (mode != 5) { // draw the line based on bezier points drawCurves(ctx, path, thickness, Color(255.0, 0.0, 0.0)); } // draw circles at each of the original path points if(mode != 1 && mode != 5){ for (int i=0; i<pathPoints.size(); i++) { Vec2f pt = pathPoints[i]; drawCircle(ctx, pt, 1, Color( 1.0f, 1.0f, 1.0f )); } } int i; int pointIndex = 0; // draw circles at the bezier points for (i=0; i<path.getNumSegments(); i++) { int segType = path.getSegmentType(i); // change jumpIndex depending on the type of segment switch(segType){ case Path2d::CUBICTO: { Vec2f c1 = path.getPoint(pointIndex+1); Vec2f c2 = path.getPoint(pointIndex+2); Vec2f pt1 = path.getPoint(pointIndex); Vec2f pt2 = path.getPoint(pointIndex+3); if (mode == 2 || mode == 3) { if(mode == 2){ for(int j=0; j<endPoints.size(); j++){ if(endPoints[j] == pointIndex){ drawCircle(ctx, pt2, 8, Color(0.0f, 1.0f, 1.0f), true); } } } if(mode == 3){ // draw the control points and tangent lines drawCircle(ctx, c1, 2, Color(1.0f, 1.0f, 0.0f), true); drawCircle(ctx, c2, 2, Color(0.0f, 1.0f, 1.0f)); drawLine(ctx, c1, pt1, 1, Color(1.0, 1.0, 0.0 )); drawLine(ctx, c2, pt2, 1, Color(0.0, 1.0, 1.0 )); } drawCircle(ctx, pt2, 2, Color(1.0f, 0.0f, 1.0f)); drawCircle(ctx, pt1, 2, Color(1.0f, 0.0f, 0.0f), true); } if (mode == 4) { drawLine(ctx, pt1, pt2, 1, Color(1.0, 1.0, 1.0 )); drawCircle(ctx, pt1, 5, Color(1.0, 0.0, 0.0), true); drawCircle(ctx, pt2, 3, Color(1.0, 0.0, 1.0)); } pointIndex += 3; break; } case Path2d::MOVETO: // don't do anything with this point pointIndex += 0; break; default: pointIndex += 1; break; } } if (mode== 5) { // draw a line between the last bezier point and the current point for (i=1; i<pathPoints.size(); i++) { drawLine(ctx, pathPoints[i-1], pathPoints[i], 1.0, Color(1.0, 0.0, 0.0)); } } }