blob: fec7fb39fbad0db5513133e04dcf17ec47160f53 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bsalomon@google.comab9e2c62012-03-08 16:20:16 +00007
tfarina@chromium.orgb1b7f7072012-09-18 01:52:20 +00008#include "OverView.h"
9
reed@android.com34245c72009-11-03 04:00:48 +000010#include "SampleCode.h"
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000011
reed@android.com34245c72009-11-03 04:00:48 +000012#include "SkCanvas.h"
13#include "SkView.h"
14
commit-bot@chromium.orgab131672013-09-13 12:39:55 +000015static const int N = 8;
16static const SkScalar kWidth = SkIntToScalar(640);
17static const SkScalar kHeight = SkIntToScalar(480);
18static const char gIsOverview[] = "is-overview";
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000019
reed@android.com34245c72009-11-03 04:00:48 +000020class OverView : public SkView {
21public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000022 OverView(int count, const SkViewFactory* factories[]);
reed@android.com34245c72009-11-03 04:00:48 +000023 virtual ~OverView();
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000024
reed@android.com34245c72009-11-03 04:00:48 +000025protected:
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000026 // Overridden from SkEventSink:
27 virtual bool onEvent(const SkEvent&) SK_OVERRIDE;
28 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
reed@android.com34245c72009-11-03 04:00:48 +000029 if (SampleCode::TitleQ(*evt)) {
30 SampleCode::TitleR(evt, "Overview");
31 return true;
32 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000033 if (evt->isType(gIsOverview)) {
34 return true;
35 }
reed@android.com34245c72009-11-03 04:00:48 +000036 return this->INHERITED::onQuery(evt);
37 }
38
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000039
40 // Overridden from SkView:
41 virtual void onSizeChange() SK_OVERRIDE;
42 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
43 canvas->drawColor(SK_ColorLTGRAY);
44 }
45
46 virtual SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE;
47
reed@google.com72708fa2013-01-08 16:22:44 +000048 virtual bool onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
reed@android.come72fee52009-11-16 14:52:01 +000049 return false;
50 }
51
reed@google.com72708fa2013-01-08 16:22:44 +000052 virtual Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000053 int ix = (int)(SkScalarDiv(x * N, kWidth));
54 int iy = (int)(SkScalarDiv(y * N, kHeight));
reed@android.com34245c72009-11-03 04:00:48 +000055 if (ix >= 0 && iy >= 0) {
56 SkEvent evt("set-curr-index");
57 evt.setFast32(iy * N + ix);
58 this->sendEventToParents(evt);
59 }
60 return NULL;
61 }
62
63private:
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000064 int fCount;
65 const SkViewFactory** fFactories;
reed@android.com34245c72009-11-03 04:00:48 +000066
67 typedef SkView INHERITED;
68};
69
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000070SkView* create_overview(int count, const SkViewFactory* factories[]) {
reed@android.com34245c72009-11-03 04:00:48 +000071 return SkNEW_ARGS(OverView, (count, factories));
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000072}
73
74bool is_overview(SkView* view) {
75 SkEvent isOverview(gIsOverview);
76 return view->doQuery(&isOverview);
77}
reed@android.com34245c72009-11-03 04:00:48 +000078
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000079OverView::OverView(int count, const SkViewFactory* factories[]) {
reed@android.com34245c72009-11-03 04:00:48 +000080 fCount = count;
81 fFactories = factories;
82}
83
84OverView::~OverView() {
85}
86
87bool OverView::onEvent(const SkEvent& evt) {
88 return this->INHERITED::onEvent(evt);
89}
90
91void OverView::onSizeChange() {
92 this->detachAllChildren();
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000093
reed@android.com34245c72009-11-03 04:00:48 +000094 SkScalar locX = 0;
95 SkScalar locY = 0;
96 for (int i = 0; i < fCount; i++) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000097 SkView* view = (*fFactories[i])();
reed@android.com34245c72009-11-03 04:00:48 +000098 view->setVisibleP(true);
99 this->attachChildToBack(view)->unref();
100 view->setLoc(locX, locY);
bsalomon@google.comab9e2c62012-03-08 16:20:16 +0000101 view->setSize(kWidth, kHeight);
102 locX += kWidth;
reed@android.com34245c72009-11-03 04:00:48 +0000103 if ((i % N) == N - 1) {
bsalomon@google.comab9e2c62012-03-08 16:20:16 +0000104 locY += kHeight;
reed@android.com34245c72009-11-03 04:00:48 +0000105 locX = 0;
106 }
107 }
108}
109
110SkCanvas* OverView::beforeChildren(SkCanvas* canvas) {
111 canvas->scale(SK_Scalar1 / N, SK_Scalar1 / N);
112 return canvas;
113}