blob: fc4a9ef1831c0831232274ae01184bceecc22d25 [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.com34245c72009-11-03 04:00:48 +00008#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkView.h"
11
12static const int N = 8;
13const SkScalar W = SkIntToScalar(640);
14const SkScalar H = SkIntToScalar(480);
15
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000016static const char gIsOverview[] = "is-overview";
17bool is_overview(SkView* view) {
18 SkEvent isOverview(gIsOverview);
19 return view->doQuery(&isOverview);
20}
reed@android.com34245c72009-11-03 04:00:48 +000021class OverView : public SkView {
22public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000023 OverView(int count, const SkViewFactory* factories[]);
reed@android.com34245c72009-11-03 04:00:48 +000024 virtual ~OverView();
25
26protected:
27 virtual bool onEvent(const SkEvent&);
28 virtual void onSizeChange();
29
30 virtual void onDraw(SkCanvas* canvas) {
31 canvas->drawColor(SK_ColorLTGRAY);
32 }
33
34 virtual SkCanvas* beforeChildren(SkCanvas*);
35
36 virtual bool onQuery(SkEvent* evt) {
37 if (SampleCode::TitleQ(*evt)) {
38 SampleCode::TitleR(evt, "Overview");
39 return true;
40 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000041 if (evt->isType(gIsOverview)) {
42 return true;
43 }
reed@android.com34245c72009-11-03 04:00:48 +000044 return this->INHERITED::onQuery(evt);
45 }
46
reed@android.come72fee52009-11-16 14:52:01 +000047 virtual bool onSendClickToChildren(SkScalar x, SkScalar y) {
48 return false;
49 }
50
51 virtual Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com34245c72009-11-03 04:00:48 +000052 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
62private:
63 int fCount;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000064 const SkViewFactory** fFactories;
reed@android.com34245c72009-11-03 04:00:48 +000065
66 typedef SkView INHERITED;
67};
68
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000069SkView* create_overview(int count, const SkViewFactory* factories[]) {
reed@android.com34245c72009-11-03 04:00:48 +000070 return SkNEW_ARGS(OverView, (count, factories));
71};
72
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000073OverView::OverView(int count, const SkViewFactory* factories[]) {
reed@android.com34245c72009-11-03 04:00:48 +000074 fCount = count;
75 fFactories = factories;
76}
77
78OverView::~OverView() {
79}
80
81bool OverView::onEvent(const SkEvent& evt) {
82 return this->INHERITED::onEvent(evt);
83}
84
85void OverView::onSizeChange() {
86 this->detachAllChildren();
87
88 SkScalar locX = 0;
89 SkScalar locY = 0;
90 for (int i = 0; i < fCount; i++) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000091 SkView* view = (*fFactories[i])();
reed@android.com34245c72009-11-03 04:00:48 +000092 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
104SkCanvas* OverView::beforeChildren(SkCanvas* canvas) {
105 canvas->scale(SK_Scalar1 / N, SK_Scalar1 / N);
106 return canvas;
107}
108