blob: f62a58cf57a3719b24237087b3f19f0d18404df1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkNinePatch.h"
11#include "SkPaint.h"
reed@google.com40a37722011-09-06 18:54:54 +000012#include "SkGpuDevice.h"
13
14static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) {
15 SkDevice* dev;
16 SkCanvas canvas;
17
18 const int kFixed = 28;
19 const int kStretchy = 8;
20 const int kSize = 2*kFixed + kStretchy;
21
22 if (ctx) {
23 dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize);
24 *bitmap = dev->accessBitmap(false);
25 } else {
26 bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize);
27 bitmap->allocPixels();
28 dev = new SkDevice(*bitmap);
29 }
30
31 canvas.setDevice(dev)->unref();
32 canvas.clear(0);
33
34 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
35 const SkScalar strokeWidth = SkIntToScalar(6);
36 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
37
38 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
39
40 SkPaint paint;
41 paint.setAntiAlias(true);
42
reed@google.com40a37722011-09-06 18:54:54 +000043 paint.setColor(0xFFFF0000);
44 canvas.drawRoundRect(r, radius, radius, paint);
45 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
46 paint.setColor(0x8800FF00);
47 canvas.drawRect(r, paint);
48 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
49 paint.setColor(0x880000FF);
50 canvas.drawRect(r, paint);
reed@google.com40a37722011-09-06 18:54:54 +000051}
52
reed@android.com8a1c16f2008-12-17 15:59:43 +000053
reed@google.com81e3d7f2011-06-01 12:42:36 +000054class NinePatchView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055public:
reed@google.com40a37722011-09-06 18:54:54 +000056 NinePatchView() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000057
58protected:
59 // overrides from SkEventSink
60 virtual bool onQuery(SkEvent* evt) {
61 if (SampleCode::TitleQ(*evt)) {
62 SampleCode::TitleR(evt, "NinePatch");
63 return true;
64 }
65 return this->INHERITED::onQuery(evt);
66 }
reed@android.com7d970c72010-04-22 16:07:49 +000067
reed@google.com40a37722011-09-06 18:54:54 +000068 static void drawNine(SkCanvas* canvas, const SkRect& dst, const SkBitmap& bm,
69 const SkIRect& center, const SkPaint* paint) {
70 SkIRect margin;
71 margin.set(center.fLeft, center.fTop, bm.width() - center.fRight,
72 bm.height() - center.fBottom);
73 SkNinePatch::DrawNine(canvas, dst, bm, margin, paint);
reed@android.com7d970c72010-04-22 16:07:49 +000074 }
75
reed@google.com81e3d7f2011-06-01 12:42:36 +000076 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com40a37722011-09-06 18:54:54 +000077 SkBitmap bm;
78 SkIRect center;
79 make_bitmap(&bm, SampleCode::GetGr(), &center);
reed@android.com7d970c72010-04-22 16:07:49 +000080
reed@google.com40a37722011-09-06 18:54:54 +000081 // amount of bm that should not be stretched (unless we have to)
82 const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
reed@android.com7d970c72010-04-22 16:07:49 +000083
reed@google.com40a37722011-09-06 18:54:54 +000084 const SkTSize<SkScalar> size[] = {
85 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
86 { fixed * 4 / 5, fixed * 4 }, // shrink in X
87 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
88 { fixed * 4, fixed * 4 }
89 };
90
91 canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
92
93 SkScalar x = SkIntToScalar(100);
94 SkScalar y = SkIntToScalar(100);
95
reed@android.com7d970c72010-04-22 16:07:49 +000096 SkPaint paint;
reed@google.com40a37722011-09-06 18:54:54 +000097 paint.setFilterBitmap(true);
98
99 for (int iy = 0; iy < 2; ++iy) {
100 for (int ix = 0; ix < 2; ++ix) {
101 int i = ix * 2 + iy;
102 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
103 size[i].width(), size[i].height());
104 drawNine(canvas, r, bm, center, &paint);
105 }
106 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 }
108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109private:
reed@android.comc4cae852009-09-23 15:06:10 +0000110 SkScalar fX, fY;
reed@google.com81e3d7f2011-06-01 12:42:36 +0000111 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112};
113
114//////////////////////////////////////////////////////////////////////////////
115
116static SkView* MyFactory() { return new NinePatchView; }
117static SkViewRegister reg(MyFactory);
118