1
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 */
8 #include "SampleCode.h"
9 #include "SkCanvas.h"
10 #include "SkView.h"
11
12 static const int N = 8;
13 const SkScalar W = SkIntToScalar(640);
14 const SkScalar H = SkIntToScalar(480);
15
16 static const char gIsOverview[] = "is-overview";
is_overview(SkView * view)17 bool is_overview(SkView* view) {
18 SkEvent isOverview(gIsOverview);
19 return view->doQuery(&isOverview);
20 }
21 class OverView : public SkView {
22 public:
23 OverView(int count, const SkViewFactory* factories[]);
24 virtual ~OverView();
25
26 protected:
27 virtual bool onEvent(const SkEvent&);
28 virtual void onSizeChange();
29
onDraw(SkCanvas * canvas)30 virtual void onDraw(SkCanvas* canvas) {
31 canvas->drawColor(SK_ColorLTGRAY);
32 }
33
34 virtual SkCanvas* beforeChildren(SkCanvas*);
35
onQuery(SkEvent * evt)36 virtual bool onQuery(SkEvent* evt) {
37 if (SampleCode::TitleQ(*evt)) {
38 SampleCode::TitleR(evt, "Overview");
39 return true;
40 }
41 if (evt->isType(gIsOverview)) {
42 return true;
43 }
44 return this->INHERITED::onQuery(evt);
45 }
46
onSendClickToChildren(SkScalar x,SkScalar y)47 virtual bool onSendClickToChildren(SkScalar x, SkScalar y) {
48 return false;
49 }
50
onFindClickHandler(SkScalar x,SkScalar y)51 virtual Click* onFindClickHandler(SkScalar x, SkScalar y) {
52 int ix = (int)(SkScalarDiv(x * N, W));
53 int iy = (int)(SkScalarDiv(y * N, H));
54 if (ix >= 0 && iy >= 0) {
55 SkEvent evt("set-curr-index");
56 evt.setFast32(iy * N + ix);
57 this->sendEventToParents(evt);
58 }
59 return NULL;
60 }
61
62 private:
63 int fCount;
64 const SkViewFactory** fFactories;
65
66 typedef SkView INHERITED;
67 };
68
create_overview(int count,const SkViewFactory * factories[])69 SkView* create_overview(int count, const SkViewFactory* factories[]) {
70 return SkNEW_ARGS(OverView, (count, factories));
71 };
72
OverView(int count,const SkViewFactory * factories[])73 OverView::OverView(int count, const SkViewFactory* factories[]) {
74 fCount = count;
75 fFactories = factories;
76 }
77
~OverView()78 OverView::~OverView() {
79 }
80
onEvent(const SkEvent & evt)81 bool OverView::onEvent(const SkEvent& evt) {
82 return this->INHERITED::onEvent(evt);
83 }
84
onSizeChange()85 void OverView::onSizeChange() {
86 this->detachAllChildren();
87
88 SkScalar locX = 0;
89 SkScalar locY = 0;
90 for (int i = 0; i < fCount; i++) {
91 SkView* view = (*fFactories[i])();
92 view->setVisibleP(true);
93 this->attachChildToBack(view)->unref();
94 view->setLoc(locX, locY);
95 view->setSize(W, H);
96 locX += W;
97 if ((i % N) == N - 1) {
98 locY += H;
99 locX = 0;
100 }
101 }
102 }
103
beforeChildren(SkCanvas * canvas)104 SkCanvas* OverView::beforeChildren(SkCanvas* canvas) {
105 canvas->scale(SK_Scalar1 / N, SK_Scalar1 / N);
106 return canvas;
107 }
108
109