Options
These globals are now defined:
double duration; // The requested duration of the animation. double frame; // A value in [0, 1] of where we are in the animation.
This global is now defined:
GrBackendRenderTarget backEndRenderTarget;
GrBackendTexture backEndTextureRenderTarget;
Optional source image
These globals are now defined:
SkBitmap source; sk_sp<SkImage> image; GrBackendTexture backEndTexture; // GPU Only.
Note:
Adding comments with SK_FOLD_START and SK_FOLD_END creates foldable code
blocks.
These blocks will be folded by default and are useful for highlighting specific lines of code.
You can also use the keyboard shortcuts Ctrl+S and Ctrl+E in the code editor to set them.
These blocks will be folded by default and are useful for highlighting specific lines of code.
You can also use the keyboard shortcuts Ctrl+S and Ctrl+E in the code editor to set them.
xxxxxxxxxx
126
enum Part {
kScrollbarDownArrow,
kScrollbarLeftArrow,
kScrollbarRightArrow,
kScrollbarUpArrow,
};
// Parameters
SkColor backgroundColor = SK_ColorWHITE;
SkColor borderColor = SK_ColorLTGRAY;
SkColor foregroundColor = SK_ColorBLACK;
int scrollbarSize = 15;
// Implementation (set only one)
void DrawArrow(SkCanvas* canvas, Part direction, SkIRect rect) {
int width_middle, length_middle;
if (direction == kScrollbarUpArrow || direction == kScrollbarDownArrow) {
width_middle = rect.width() / 2 + 1;
length_middle = rect.height() / 2 + 1;
} else {
length_middle = rect.width() / 2 + 1;
width_middle = rect.height() / 2 + 1;
}
SkPaint paint;
paint.setColor(foregroundColor);
paint.setAntiAlias(false);
paint.setStyle(SkPaint::kFill_Style);
SkPath path;
// The constants in this block of code are hand-tailored to produce good
// looking arrows without anti-aliasing.
switch (direction) {
case kScrollbarUpArrow:
path.moveTo(rect.x() + width_middle - 4, rect.y() + length_middle + 2);
path.rLineTo(7, 0);
path.rLineTo(-4, -4);
break;
case kScrollbarDownArrow:
path.moveTo(rect.x() + width_middle - 4, rect.y() + length_middle - 3);
path.rLineTo(7, 0);
path.rLineTo(-4, 4);
break;
case kScrollbarRightArrow:
path.moveTo(rect.x() + length_middle - 3, rect.y() + width_middle - 4);
path.rLineTo(0, 7);
path.rLineTo(4, -4);
break;
case kScrollbarLeftArrow:
path.moveTo(rect.x() + length_middle + 1, rect.y() + width_middle - 5);
path.rLineTo(0, 9);
path.rLineTo(-4, -4);
break;
default:
break;
}
path.close();
canvas->drawPath(path, paint);
}
void DrawArrow(SkCanvas* canvas, Part direction, SkIRect rect) {
SkPaint paint;
paint.setColor(foregroundColor);
paint.setAntiAlias(false);
SkIRect bounding_rect(rect);
const int padding_width = ceil(rect.width() / 4.f);
const int padding_height = ceil(rect.height() / 4.f);
bounding_rect.inset(padding_width, padding_height);
const SkIPoint center = SkIPoint::Make(bounding_rect.centerX(), bounding_rect.centerY());
SkPath path;
SkMatrix transform;
transform.setIdentity();
if (direction == kScrollbarUpArrow || direction == kScrollbarDownArrow) {
int arrow_altitude = bounding_rect.height() / 2 + 1;
path.moveTo(bounding_rect.x(), bounding_rect.y() + bounding_rect.height());
path.rLineTo(bounding_rect.width(), 0);
path.rLineTo(-bounding_rect.width() / 2.0f, -arrow_altitude);
path.close();
path.offset(0, -arrow_altitude / 2 + 1);
if (direction == kScrollbarDownArrow) {
path.offset(0, -1);
transform.setScale(1, -1, center.x(), center.y());
}
} else {
int arrow_altitude = bounding_rect.width() / 2 + 1;
path.moveTo(bounding_rect.x(), bounding_rect.y());
path.rLineTo(0, bounding_rect.height());
path.rLineTo(arrow_altitude, -bounding_rect.height() / 2.0f);
path.close();
path.offset(arrow_altitude / 2, 0);
if (direction == kScrollbarLeftArrow) {
path.offset(-1, 0);
transform.setScale(-1, 1, center.x(), center.y());
}
}
path.transform(transform);
canvas->drawPath(path, paint);
}
void DrawArrowButton(SkCanvas* canvas, Part direction, SkIRect rect) {
SkPaint borderPaint;
borderPaint.setStyle(SkPaint::kStroke_Style);
borderPaint.setColor(borderColor);
canvas->drawRect(SkRect::Make(rect), borderPaint);
DrawArrow(canvas, direction, rect);
}
void draw(SkCanvas* canvas) {
canvas->clear(backgroundColor);
DrawArrowButton(canvas, kScrollbarUpArrow, SkIRect::MakeXYWH(0, 0, scrollbarSize, scrollbarSize));
DrawArrowButton(canvas, kScrollbarDownArrow, SkIRect::MakeXYWH(0, scrollbarSize, scrollbarSize, scrollbarSize));
DrawArrowButton(canvas, kScrollbarLeftArrow, SkIRect::MakeXYWH(scrollbarSize, 0, scrollbarSize, scrollbarSize));
DrawArrowButton(canvas, kScrollbarRightArrow, SkIRect::MakeXYWH(2 * scrollbarSize, 0, scrollbarSize, scrollbarSize));
}