yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +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 | */ |
epoger@google.com | 6a12178 | 2012-09-14 18:42:01 +0000 | [diff] [blame] | 7 | #include "TransitionView.h" |
| 8 | |
tfarina@chromium.org | b1b7f707 | 2012-09-18 01:52:20 +0000 | [diff] [blame] | 9 | #include "OverView.h" |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 10 | #include "SampleCode.h" |
| 11 | #include "SkView.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkTime.h" |
| 14 | #include "SkInterpolator.h" |
| 15 | |
| 16 | static const char gIsTransitionQuery[] = "is-transition"; |
| 17 | static const char gReplaceTransitionEvt[] = "replace-transition-view"; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 18 | |
tfarina@chromium.org | b1b7f707 | 2012-09-18 01:52:20 +0000 | [diff] [blame] | 19 | bool is_transition(SkView* view) { |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 20 | SkEvent isTransition(gIsTransitionQuery); |
| 21 | return view->doQuery(&isTransition); |
| 22 | } |
| 23 | |
| 24 | class TransitionView : public SampleView { |
reed@google.com | 8d88475 | 2012-09-08 20:14:23 +0000 | [diff] [blame] | 25 | enum { |
tfarina@chromium.org | b1b7f707 | 2012-09-18 01:52:20 +0000 | [diff] [blame] | 26 | // kDurationMS = 500 |
reed@google.com | 8d88475 | 2012-09-08 20:14:23 +0000 | [diff] [blame] | 27 | kDurationMS = 1 |
| 28 | }; |
tfarina@chromium.org | b1b7f707 | 2012-09-18 01:52:20 +0000 | [diff] [blame] | 29 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 30 | public: |
| 31 | TransitionView(SkView* prev, SkView* next, int direction) : fInterp(4, 2){ |
| 32 | fAnimationDirection = (Direction)(1 << (direction % 8)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 34 | fPrev = prev; |
| 35 | fPrev->setClipToBounds(false); |
| 36 | fPrev->setVisibleP(true); |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 37 | (void)SampleView::SetUsePipe(fPrev, SkOSMenu::kOffState); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 38 | //Not calling unref because fPrev is assumed to have been created, so |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 39 | //this will result in a transfer of ownership |
| 40 | this->attachChildToBack(fPrev); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 41 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 42 | fNext = next; |
| 43 | fNext->setClipToBounds(true); |
| 44 | fNext->setVisibleP(true); |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 45 | (void)SampleView::SetUsePipe(fNext, SkOSMenu::kOffState); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 46 | //Calling unref because next is a newly created view and TransitionView |
| 47 | //is now the sole owner of fNext |
| 48 | this->attachChildToFront(fNext)->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 49 | |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 50 | fDone = false; |
| 51 | //SkDebugf("--created transition\n"); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 52 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 54 | ~TransitionView(){ |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 55 | //SkDebugf("--deleted transition\n"); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 56 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 58 | virtual void requestMenu(SkOSMenu* menu) { |
| 59 | if (SampleView::IsSampleView(fNext)) |
| 60 | ((SampleView*)fNext)->requestMenu(menu); |
| 61 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 62 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 63 | protected: |
| 64 | virtual bool onQuery(SkEvent* evt) { |
| 65 | if (SampleCode::TitleQ(*evt)) { |
| 66 | SkString title; |
| 67 | if (SampleCode::RequestTitle(fNext, &title)) { |
| 68 | SampleCode::TitleR(evt, title.c_str()); |
| 69 | return true; |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | if (evt->isType(gIsTransitionQuery)) { |
| 74 | return true; |
| 75 | } |
| 76 | return this->INHERITED::onQuery(evt); |
| 77 | } |
| 78 | virtual bool onEvent(const SkEvent& evt) { |
| 79 | if (evt.isType(gReplaceTransitionEvt)) { |
reed@google.com | 079813e | 2013-06-14 17:46:07 +0000 | [diff] [blame] | 80 | SkView* prev = fPrev; |
| 81 | prev->ref(); |
| 82 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 83 | fPrev->detachFromParent(); |
| 84 | fPrev = (SkView*)SkEventSink::FindSink(evt.getFast32()); |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 85 | (void)SampleView::SetUsePipe(fPrev, SkOSMenu::kOffState); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 86 | //attach the new fPrev and call unref to balance the ref in onDraw |
| 87 | this->attachChildToBack(fPrev)->unref(); |
| 88 | this->inval(NULL); |
skia.committer@gmail.com | f85693d | 2013-06-15 07:00:53 +0000 | [diff] [blame] | 89 | |
reed@google.com | 079813e | 2013-06-14 17:46:07 +0000 | [diff] [blame] | 90 | SkASSERT(1 == prev->getRefCnt()); |
| 91 | prev->unref(); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 92 | return true; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 93 | } |
| 94 | if (evt.isType("transition-done")) { |
| 95 | fNext->setLoc(0, 0); |
| 96 | fNext->setClipToBounds(false); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 97 | SkEvent* evt = new SkEvent(gReplaceTransitionEvt, |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 98 | this->getParent()->getSinkID()); |
| 99 | evt->setFast32(fNext->getSinkID()); |
| 100 | //increate ref count of fNext so it survives detachAllChildren |
| 101 | fNext->ref(); |
| 102 | this->detachAllChildren(); |
| 103 | evt->post(); |
| 104 | return true; |
| 105 | } |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 106 | return this->INHERITED::onEvent(evt); |
| 107 | } |
| 108 | virtual void onDrawBackground(SkCanvas* canvas) {} |
| 109 | virtual void onDrawContent(SkCanvas* canvas) { |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 110 | if (fDone) |
| 111 | return; |
| 112 | |
| 113 | if (is_overview(fNext) || is_overview(fPrev)) { |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 114 | fPipeState = SkOSMenu::kOffState; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 115 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 117 | SkScalar values[4]; |
| 118 | SkInterpolator::Result result = fInterp.timeToValues(SkTime::GetMSecs(), values); |
| 119 | //SkDebugf("transition %x %d pipe:%d\n", this, result, fUsePipe); |
| 120 | //SkDebugf("%f %f %f %f %d\n", values[0], values[1], values[2], values[3], result); |
| 121 | if (SkInterpolator::kNormal_Result == result) { |
| 122 | fPrev->setLocX(values[kPrevX]); |
| 123 | fPrev->setLocY(values[kPrevY]); |
| 124 | fNext->setLocX(values[kNextX]); |
| 125 | fNext->setLocY(values[kNextY]); |
| 126 | this->inval(NULL); |
| 127 | } |
| 128 | else { |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 129 | (new SkEvent("transition-done", this->getSinkID()))->post(); |
| 130 | fDone = true; |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 131 | } |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 132 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 133 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 134 | virtual void onSizeChange() { |
| 135 | this->INHERITED::onSizeChange(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 137 | fNext->setSize(this->width(), this->height()); |
| 138 | fPrev->setSize(this->width(), this->height()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 139 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 140 | SkScalar lr = 0, ud = 0; |
| 141 | if (fAnimationDirection & (kLeftDirection|kULDirection|kDLDirection)) |
| 142 | lr = this->width(); |
| 143 | if (fAnimationDirection & (kRightDirection|kURDirection|kDRDirection)) |
| 144 | lr = -this->width(); |
| 145 | if (fAnimationDirection & (kUpDirection|kULDirection|kURDirection)) |
| 146 | ud = this->height(); |
| 147 | if (fAnimationDirection & (kDownDirection|kDLDirection|kDRDirection)) |
| 148 | ud = -this->height(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 149 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 150 | fBegin[kPrevX] = fBegin[kPrevY] = 0; |
| 151 | fBegin[kNextX] = lr; |
| 152 | fBegin[kNextY] = ud; |
| 153 | fNext->setLocX(lr); |
| 154 | fNext->setLocY(ud); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 155 | |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 156 | if (is_transition(fPrev)) |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 157 | lr = ud = 0; |
| 158 | fEnd[kPrevX] = -lr; |
| 159 | fEnd[kPrevY] = -ud; |
| 160 | fEnd[kNextX] = fEnd[kNextY] = 0; |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 161 | SkScalar blend[] = { SkFloatToScalar(0.8f), SkFloatToScalar(0.0f), |
| 162 | SkFloatToScalar(0.0f), SK_Scalar1 }; |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 163 | fInterp.setKeyFrame(0, SkTime::GetMSecs(), fBegin, blend); |
reed@google.com | 8d88475 | 2012-09-08 20:14:23 +0000 | [diff] [blame] | 164 | fInterp.setKeyFrame(1, SkTime::GetMSecs()+kDurationMS, fEnd, blend); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 165 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 166 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 167 | private: |
| 168 | enum { |
| 169 | kPrevX = 0, |
| 170 | kPrevY = 1, |
| 171 | kNextX = 2, |
| 172 | kNextY = 3 |
| 173 | }; |
| 174 | SkView* fPrev; |
| 175 | SkView* fNext; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 176 | bool fDone; |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 177 | SkInterpolator fInterp; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 178 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 179 | enum Direction{ |
| 180 | kUpDirection = 1, |
| 181 | kURDirection = 1 << 1, |
| 182 | kRightDirection = 1 << 2, |
| 183 | kDRDirection = 1 << 3, |
| 184 | kDownDirection = 1 << 4, |
| 185 | kDLDirection = 1 << 5, |
| 186 | kLeftDirection = 1 << 6, |
| 187 | kULDirection = 1 << 7 |
| 188 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 189 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 190 | Direction fAnimationDirection; |
| 191 | SkScalar fBegin[4]; |
| 192 | SkScalar fEnd[4]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 193 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 194 | typedef SampleView INHERITED; |
| 195 | }; |
| 196 | |
| 197 | SkView* create_transition(SkView* prev, SkView* next, int direction) { |
djsollen@google.com | 6293820 | 2012-10-10 18:29:31 +0000 | [diff] [blame] | 198 | #ifdef SK_BUILD_FOR_ANDROID |
skia.committer@gmail.com | fc84359 | 2012-10-11 02:01:14 +0000 | [diff] [blame] | 199 | // Disable transitions for Android |
| 200 | return next; |
djsollen@google.com | 6293820 | 2012-10-10 18:29:31 +0000 | [diff] [blame] | 201 | #else |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 202 | return SkNEW_ARGS(TransitionView, (prev, next, direction)); |
djsollen@google.com | 6293820 | 2012-10-10 18:29:31 +0000 | [diff] [blame] | 203 | #endif |
epoger@google.com | 6a12178 | 2012-09-14 18:42:01 +0000 | [diff] [blame] | 204 | } |