epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 7 | |
tfarina@chromium.org | b1b7f707 | 2012-09-18 01:52:20 +0000 | [diff] [blame] | 8 | #include "OverView.h" |
| 9 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 10 | #include "SampleCode.h" |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 11 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
| 13 | #include "SkView.h" |
| 14 | |
commit-bot@chromium.org | ab13167 | 2013-09-13 12:39:55 +0000 | [diff] [blame] | 15 | static const int N = 8; |
| 16 | static const SkScalar kWidth = SkIntToScalar(640); |
| 17 | static const SkScalar kHeight = SkIntToScalar(480); |
| 18 | static const char gIsOverview[] = "is-overview"; |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 19 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 20 | class OverView : public SkView { |
| 21 | public: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 22 | OverView(int count, const SkViewFactory* factories[]); |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 23 | virtual ~OverView(); |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 24 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 25 | protected: |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 26 | // Overridden from SkEventSink: |
| 27 | virtual bool onEvent(const SkEvent&) SK_OVERRIDE; |
| 28 | virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 29 | if (SampleCode::TitleQ(*evt)) { |
| 30 | SampleCode::TitleR(evt, "Overview"); |
| 31 | return true; |
| 32 | } |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 33 | if (evt->isType(gIsOverview)) { |
| 34 | return true; |
| 35 | } |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 36 | return this->INHERITED::onQuery(evt); |
| 37 | } |
| 38 | |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 39 | |
| 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.com | 72708fa | 2013-01-08 16:22:44 +0000 | [diff] [blame] | 48 | virtual bool onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE { |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
reed@google.com | 72708fa | 2013-01-08 16:22:44 +0000 | [diff] [blame] | 52 | virtual Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE { |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 53 | int ix = (int)(SkScalarDiv(x * N, kWidth)); |
| 54 | int iy = (int)(SkScalarDiv(y * N, kHeight)); |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 55 | 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 | |
| 63 | private: |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 64 | int fCount; |
| 65 | const SkViewFactory** fFactories; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 66 | |
| 67 | typedef SkView INHERITED; |
| 68 | }; |
| 69 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 70 | SkView* create_overview(int count, const SkViewFactory* factories[]) { |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 71 | return SkNEW_ARGS(OverView, (count, factories)); |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | bool is_overview(SkView* view) { |
| 75 | SkEvent isOverview(gIsOverview); |
| 76 | return view->doQuery(&isOverview); |
| 77 | } |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 78 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 79 | OverView::OverView(int count, const SkViewFactory* factories[]) { |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 80 | fCount = count; |
| 81 | fFactories = factories; |
| 82 | } |
| 83 | |
| 84 | OverView::~OverView() { |
| 85 | } |
| 86 | |
| 87 | bool OverView::onEvent(const SkEvent& evt) { |
| 88 | return this->INHERITED::onEvent(evt); |
| 89 | } |
| 90 | |
| 91 | void OverView::onSizeChange() { |
| 92 | this->detachAllChildren(); |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 93 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 94 | SkScalar locX = 0; |
| 95 | SkScalar locY = 0; |
| 96 | for (int i = 0; i < fCount; i++) { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 97 | SkView* view = (*fFactories[i])(); |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 98 | view->setVisibleP(true); |
| 99 | this->attachChildToBack(view)->unref(); |
| 100 | view->setLoc(locX, locY); |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 101 | view->setSize(kWidth, kHeight); |
| 102 | locX += kWidth; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 103 | if ((i % N) == N - 1) { |
bsalomon@google.com | ab9e2c6 | 2012-03-08 16:20:16 +0000 | [diff] [blame] | 104 | locY += kHeight; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 105 | locX = 0; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | SkCanvas* OverView::beforeChildren(SkCanvas* canvas) { |
| 111 | canvas->scale(SK_Scalar1 / N, SK_Scalar1 / N); |
| 112 | return canvas; |
| 113 | } |