blob: 2ae21196e000374ef3fbf08df1dab4753d116579 [file] [log] [blame]
reed@android.com34245c72009-11-03 04:00:48 +00001#include "SampleCode.h"
2#include "SkCanvas.h"
3#include "SkView.h"
4
5static const int N = 8;
6const SkScalar W = SkIntToScalar(640);
7const SkScalar H = SkIntToScalar(480);
8
9class OverView : public SkView {
10public:
11 OverView(int count, const SkViewFactory factories[]);
12 virtual ~OverView();
13
14protected:
15 virtual bool onEvent(const SkEvent&);
16 virtual void onSizeChange();
17
18 virtual void onDraw(SkCanvas* canvas) {
19 canvas->drawColor(SK_ColorLTGRAY);
20 }
21
22 virtual SkCanvas* beforeChildren(SkCanvas*);
23
24 virtual bool onQuery(SkEvent* evt) {
25 if (SampleCode::TitleQ(*evt)) {
26 SampleCode::TitleR(evt, "Overview");
27 return true;
28 }
29 return this->INHERITED::onQuery(evt);
30 }
31
reed@android.come72fee52009-11-16 14:52:01 +000032 virtual bool onSendClickToChildren(SkScalar x, SkScalar y) {
33 return false;
34 }
35
36 virtual Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com34245c72009-11-03 04:00:48 +000037 int ix = (int)(SkScalarDiv(x * N, W));
38 int iy = (int)(SkScalarDiv(y * N, H));
39 if (ix >= 0 && iy >= 0) {
40 SkEvent evt("set-curr-index");
41 evt.setFast32(iy * N + ix);
42 this->sendEventToParents(evt);
43 }
44 return NULL;
45 }
46
47private:
48 int fCount;
49 const SkViewFactory* fFactories;
50
51 typedef SkView INHERITED;
52};
53
54SkView* create_overview(int count, const SkViewFactory factories[]);
55SkView* create_overview(int count, const SkViewFactory factories[]) {
56 return SkNEW_ARGS(OverView, (count, factories));
57};
58
59OverView::OverView(int count, const SkViewFactory factories[]) {
60 fCount = count;
61 fFactories = factories;
62}
63
64OverView::~OverView() {
65}
66
67bool OverView::onEvent(const SkEvent& evt) {
68 return this->INHERITED::onEvent(evt);
69}
70
71void OverView::onSizeChange() {
72 this->detachAllChildren();
73
74 SkScalar locX = 0;
75 SkScalar locY = 0;
76 for (int i = 0; i < fCount; i++) {
77 SkView* view = fFactories[i]();
78 view->setVisibleP(true);
79 this->attachChildToBack(view)->unref();
80 view->setLoc(locX, locY);
81 view->setSize(W, H);
82 locX += W;
83 if ((i % N) == N - 1) {
84 locY += H;
85 locX = 0;
86 }
87 }
88}
89
90SkCanvas* OverView::beforeChildren(SkCanvas* canvas) {
91 canvas->scale(SK_Scalar1 / N, SK_Scalar1 / N);
92 return canvas;
93}
94