reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkView.h" |
| 4 | |
| 5 | static const int N = 8; |
| 6 | const SkScalar W = SkIntToScalar(640); |
| 7 | const SkScalar H = SkIntToScalar(480); |
| 8 | |
| 9 | class OverView : public SkView { |
| 10 | public: |
| 11 | OverView(int count, const SkViewFactory factories[]); |
| 12 | virtual ~OverView(); |
| 13 | |
| 14 | protected: |
| 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.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 32 | virtual bool onSendClickToChildren(SkScalar x, SkScalar y) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | virtual Click* onFindClickHandler(SkScalar x, SkScalar y) { |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 37 | 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 | |
| 47 | private: |
| 48 | int fCount; |
| 49 | const SkViewFactory* fFactories; |
| 50 | |
| 51 | typedef SkView INHERITED; |
| 52 | }; |
| 53 | |
| 54 | SkView* create_overview(int count, const SkViewFactory factories[]); |
| 55 | SkView* create_overview(int count, const SkViewFactory factories[]) { |
| 56 | return SkNEW_ARGS(OverView, (count, factories)); |
| 57 | }; |
| 58 | |
| 59 | OverView::OverView(int count, const SkViewFactory factories[]) { |
| 60 | fCount = count; |
| 61 | fFactories = factories; |
| 62 | } |
| 63 | |
| 64 | OverView::~OverView() { |
| 65 | } |
| 66 | |
| 67 | bool OverView::onEvent(const SkEvent& evt) { |
| 68 | return this->INHERITED::onEvent(evt); |
| 69 | } |
| 70 | |
| 71 | void 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 | |
| 90 | SkCanvas* OverView::beforeChildren(SkCanvas* canvas) { |
| 91 | canvas->scale(SK_Scalar1 / N, SK_Scalar1 / N); |
| 92 | return canvas; |
| 93 | } |
| 94 | |