Skip to content

Instantly share code, notes, and snippets.

@danesfeder
Created September 11, 2018 17:13
Show Gist options
  • Save danesfeder/7c39153af9b3cde8b4c5ebcbe01ead74 to your computer and use it in GitHub Desktop.
Save danesfeder/7c39153af9b3cde8b4c5ebcbe01ead74 to your computer and use it in GitHub Desktop.
PaintCode default roundabout snippet
private static class CacheForDefaultRoundabout {
private static Paint paint = new Paint();
private static RectF originalFrame = new RectF(0f, 0f, 32f, 32f);
private static RectF resizedFrame = new RectF();
private static RectF frame = new RectF();
private static RectF bezier3Rect = new RectF();
private static Path bezier3Path = new Path();
private static PaintCodeDashPathEffect bezier3PathDashEffect = new PaintCodeDashPathEffect();
private static RectF bezier4Rect = new RectF();
private static Path bezier4Path = new Path();
private static PaintCodeDashPathEffect bezier4PathDashEffect = new PaintCodeDashPathEffect();
private static RectF bezier2Rect = new RectF();
private static Path bezier2Path = new Path();
}
public static void drawDefaultRoundabout(Canvas canvas, int primaryColor, int secondaryColor, PointF size) {
ManeuversStyleKit.drawDefaultRoundabout(canvas, new RectF(0f, 0f, 32f, 32f), ResizingBehavior.AspectFit, primaryColor, secondaryColor, size, 90f);
}
public static void drawDefaultRoundabout(Canvas canvas, RectF targetFrame, ResizingBehavior resizing, int primaryColor, int secondaryColor, PointF size, float roundabout_angle) {
// General Declarations
Stack<Matrix> currentTransformation = new Stack<Matrix>();
currentTransformation.push(new Matrix());
Paint paint = CacheForDefaultRoundabout.paint;
// Local Variables
float roundabout_percentage = roundabout_angle / 360f * 2f * (float) Math.PI * 6.5f;
float scale = Math.min(size.x / 32f, size.y / 32f);
float roundabout_arrow_height = scale * (float) Math.cos((roundabout_angle - 180f) * (float) Math.PI / 180f) * 20f;
float roundabout_y = size.y - scale * (6.5f * 2f + 4f) + 1f + roundabout_arrow_height / 4f;
float roundabout_arrow_width = scale * 0.75f * (float) Math.sin((roundabout_angle - 180f) * (float) Math.PI / 180f) * 16f;
float roundabout_x = size.x / 2f + roundabout_arrow_width / 2f;
// Resize to Target Frame
canvas.save();
RectF resizedFrame = CacheForDefaultRoundabout.resizedFrame;
ManeuversStyleKit.resizingBehaviorApply(resizing, CacheForDefaultRoundabout.originalFrame, targetFrame, resizedFrame);
canvas.translate(resizedFrame.left, resizedFrame.top);
canvas.scale(resizedFrame.width() / 32f, resizedFrame.height() / 32f);
// Frame
RectF frame = CacheForDefaultRoundabout.frame;
frame.set(0f, 0f, size.x, size.y);
// Group 3
{
canvas.save();
canvas.translate(roundabout_x + 4f, roundabout_y + 14f);
currentTransformation.peek().postTranslate(roundabout_x + 4f, roundabout_y + 14f);
canvas.rotate(-90f);
currentTransformation.peek().postRotate(-90f);
canvas.scale(1.4f, 1.4f);
currentTransformation.peek().postScale(1.4f, 1.4f);
// Bezier 3
canvas.save();
canvas.translate(1f, 2f);
currentTransformation.peek().postTranslate(1f, 2f);
canvas.rotate(-90f);
currentTransformation.peek().postRotate(-90f);
canvas.scale(-1f, 1f);
currentTransformation.peek().postScale(-1f, 1f);
RectF bezier3Rect = CacheForDefaultRoundabout.bezier3Rect;
bezier3Rect.set(-7.5f, -7.5f, 5.5f, 5.5f);
Path bezier3Path = CacheForDefaultRoundabout.bezier3Path;
bezier3Path.reset();
bezier3Path.moveTo(5.5f, -1f);
bezier3Path.cubicTo(5.5f, 0.79f, 4.78f, 2.41f, 3.6f, 3.59f);
bezier3Path.cubicTo(2.43f, 4.77f, 0.8f, 5.5f, -1f, 5.5f);
bezier3Path.cubicTo(-2.79f, 5.5f, -4.41f, 4.78f, -5.59f, 3.61f);
bezier3Path.cubicTo(-6.77f, 2.43f, -7.5f, 0.8f, -7.5f, -1f);
bezier3Path.cubicTo(-7.5f, -2.79f, -6.77f, -4.42f, -5.6f, -5.6f);
bezier3Path.cubicTo(-4.42f, -6.77f, -2.79f, -7.5f, -1f, -7.5f);
bezier3Path.cubicTo(0.79f, -7.5f, 2.42f, -6.77f, 3.6f, -5.6f);
bezier3Path.cubicTo(4.77f, -4.42f, 5.5f, -2.79f, 5.5f, -1f);
bezier3Path.close();
paint.reset();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(4f);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeMiter(10f);
paint.setPathEffect(CacheForDefaultRoundabout.bezier3PathDashEffect.get(roundabout_percentage, 1000f, 0f));
canvas.save();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(primaryColor);
canvas.drawPath(bezier3Path, paint);
canvas.restore();
canvas.restore();
// Bezier 4
canvas.save();
canvas.translate(1f, 1.99f);
currentTransformation.peek().postTranslate(1f, 1.99f);
canvas.rotate(-90f);
currentTransformation.peek().postRotate(-90f);
canvas.scale(1f, -1f);
currentTransformation.peek().postScale(1f, -1f);
RectF bezier4Rect = CacheForDefaultRoundabout.bezier4Rect;
bezier4Rect.set(-5.5f, -5.5f, 7.5f, 7.5f);
Path bezier4Path = CacheForDefaultRoundabout.bezier4Path;
bezier4Path.reset();
bezier4Path.moveTo(7.5f, 1f);
bezier4Path.cubicTo(7.5f, -0.79f, 6.78f, -2.41f, 5.6f, -3.59f);
bezier4Path.cubicTo(4.43f, -4.77f, 2.8f, -5.5f, 1f, -5.5f);
bezier4Path.cubicTo(-0.79f, -5.5f, -2.41f, -4.78f, -3.59f, -3.61f);
bezier4Path.cubicTo(-4.77f, -2.43f, -5.5f, -0.8f, -5.5f, 1f);
bezier4Path.cubicTo(-5.5f, 2.79f, -4.77f, 4.42f, -3.6f, 5.6f);
bezier4Path.cubicTo(-2.42f, 6.77f, -0.79f, 7.5f, 1f, 7.5f);
bezier4Path.cubicTo(2.79f, 7.5f, 4.42f, 6.77f, 5.6f, 5.6f);
bezier4Path.cubicTo(6.77f, 4.42f, 7.5f, 2.79f, 7.5f, 1f);
bezier4Path.close();
paint.reset();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(4f);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeMiter(10f);
paint.setPathEffect(CacheForDefaultRoundabout.bezier4PathDashEffect.get(roundabout_percentage, 1000f, 0f));
canvas.save();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(secondaryColor);
canvas.drawPath(bezier4Path, paint);
canvas.restore();
canvas.restore();
// Group
{
// Bezier 2
canvas.save();
canvas.translate(0f, 1f);
currentTransformation.peek().postTranslate(0f, 1f);
canvas.rotate(-(roundabout_angle + 90f));
currentTransformation.peek().postRotate(-(roundabout_angle + 90f));
RectF bezier2Rect = CacheForDefaultRoundabout.bezier2Rect;
bezier2Rect.set(-20.5f, -7.99f, -4.5f, 8.01f);
Path bezier2Path = CacheForDefaultRoundabout.bezier2Path;
bezier2Path.reset();
bezier2Path.moveTo(-9.47f, -7.49f);
bezier2Path.cubicTo(-9.47f, -7.45f, -9.49f, -7.38f, -9.49f, -7.38f);
bezier2Path.cubicTo(-9.57f, -6.97f, -10.45f, -2.64f, -10.45f, -2.64f);
bezier2Path.cubicTo(-10.47f, -2.59f, -10.47f, -2.54f, -10.47f, -2.49f);
bezier2Path.cubicTo(-10.47f, -2.25f, -10.09f, -2.03f, -9.82f, -2f);
bezier2Path.lineTo(-5.5f, -2f);
bezier2Path.cubicTo(-4.95f, -2f, -4.5f, -1.55f, -4.5f, -1f);
bezier2Path.lineTo(-4.5f, 1f);
bezier2Path.cubicTo(-4.5f, 1.55f, -4.95f, 2f, -5.5f, 2f);
bezier2Path.cubicTo(-5.5f, 2f, -9.38f, 2.01f, -9.75f, 2.01f);
bezier2Path.cubicTo(-10.01f, 2.01f, -10.35f, 2.24f, -10.35f, 2.51f);
bezier2Path.cubicTo(-10.35f, 2.56f, -10.41f, 2.6f, -10.39f, 2.65f);
bezier2Path.lineTo(-9.46f, 7.39f);
bezier2Path.cubicTo(-9.46f, 7.39f, -9.46f, 7.47f, -9.46f, 7.51f);
bezier2Path.cubicTo(-9.46f, 7.78f, -9.69f, 8.01f, -9.97f, 8.01f);
bezier2Path.cubicTo(-10.1f, 8.01f, -10.22f, 7.95f, -10.31f, 7.87f);
bezier2Path.lineTo(-20.5f, 0.01f);
bezier2Path.lineTo(-10.31f, -7.86f);
bezier2Path.cubicTo(-10.22f, -7.94f, -10.11f, -7.99f, -9.97f, -7.99f);
bezier2Path.cubicTo(-9.7f, -7.99f, -9.47f, -7.77f, -9.47f, -7.49f);
bezier2Path.close();
paint.reset();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(primaryColor);
canvas.drawPath(bezier2Path, paint);
canvas.restore();
}
canvas.restore();
}
canvas.restore();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment