2011-08-08 15:37:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkInterpolator.h"
|
|
|
|
|
2011-08-12 14:27:47 +00:00
|
|
|
extern bool is_overview(SkView* view);
|
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
static const char gIsTransitionQuery[] = "is-transition";
|
|
|
|
static const char gReplaceTransitionEvt[] = "replace-transition-view";
|
2011-08-12 14:27:47 +00:00
|
|
|
|
2012-06-06 12:09:51 +00:00
|
|
|
static bool is_transition(SkView* view) {
|
2011-08-08 15:37:23 +00:00
|
|
|
SkEvent isTransition(gIsTransitionQuery);
|
|
|
|
return view->doQuery(&isTransition);
|
|
|
|
}
|
|
|
|
|
|
|
|
class TransitionView : public SampleView {
|
|
|
|
public:
|
|
|
|
TransitionView(SkView* prev, SkView* next, int direction) : fInterp(4, 2){
|
|
|
|
fAnimationDirection = (Direction)(1 << (direction % 8));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
fPrev = prev;
|
|
|
|
fPrev->setClipToBounds(false);
|
|
|
|
fPrev->setVisibleP(true);
|
2012-06-08 15:35:03 +00:00
|
|
|
(void)SampleView::SetUsePipe(fPrev, SkOSMenu::kOffState);
|
2012-08-23 18:19:56 +00:00
|
|
|
//Not calling unref because fPrev is assumed to have been created, so
|
2011-08-08 15:37:23 +00:00
|
|
|
//this will result in a transfer of ownership
|
|
|
|
this->attachChildToBack(fPrev);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
fNext = next;
|
|
|
|
fNext->setClipToBounds(true);
|
|
|
|
fNext->setVisibleP(true);
|
2012-06-08 15:35:03 +00:00
|
|
|
(void)SampleView::SetUsePipe(fNext, SkOSMenu::kOffState);
|
2011-08-08 15:37:23 +00:00
|
|
|
//Calling unref because next is a newly created view and TransitionView
|
|
|
|
//is now the sole owner of fNext
|
|
|
|
this->attachChildToFront(fNext)->unref();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-12 14:27:47 +00:00
|
|
|
fDone = false;
|
|
|
|
//SkDebugf("--created transition\n");
|
2011-08-08 15:37:23 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
~TransitionView(){
|
2011-08-12 14:27:47 +00:00
|
|
|
//SkDebugf("--deleted transition\n");
|
2011-08-08 15:37:23 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
virtual void requestMenu(SkOSMenu* menu) {
|
|
|
|
if (SampleView::IsSampleView(fNext))
|
|
|
|
((SampleView*)fNext)->requestMenu(menu);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
protected:
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SkString title;
|
|
|
|
if (SampleCode::RequestTitle(fNext, &title)) {
|
|
|
|
SampleCode::TitleR(evt, title.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (evt->isType(gIsTransitionQuery)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
virtual bool onEvent(const SkEvent& evt) {
|
|
|
|
if (evt.isType(gReplaceTransitionEvt)) {
|
|
|
|
fPrev->detachFromParent();
|
|
|
|
fPrev = (SkView*)SkEventSink::FindSink(evt.getFast32());
|
2012-06-08 15:35:03 +00:00
|
|
|
(void)SampleView::SetUsePipe(fPrev, SkOSMenu::kOffState);
|
2011-08-08 15:37:23 +00:00
|
|
|
//attach the new fPrev and call unref to balance the ref in onDraw
|
|
|
|
this->attachChildToBack(fPrev)->unref();
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
2011-08-12 14:27:47 +00:00
|
|
|
}
|
|
|
|
if (evt.isType("transition-done")) {
|
|
|
|
fNext->setLoc(0, 0);
|
|
|
|
fNext->setClipToBounds(false);
|
2012-08-23 18:19:56 +00:00
|
|
|
SkEvent* evt = new SkEvent(gReplaceTransitionEvt,
|
2011-08-12 14:27:47 +00:00
|
|
|
this->getParent()->getSinkID());
|
|
|
|
evt->setFast32(fNext->getSinkID());
|
|
|
|
//increate ref count of fNext so it survives detachAllChildren
|
|
|
|
fNext->ref();
|
|
|
|
this->detachAllChildren();
|
|
|
|
evt->post();
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-08 15:37:23 +00:00
|
|
|
return this->INHERITED::onEvent(evt);
|
|
|
|
}
|
|
|
|
virtual void onDrawBackground(SkCanvas* canvas) {}
|
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2011-08-12 14:27:47 +00:00
|
|
|
if (fDone)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (is_overview(fNext) || is_overview(fPrev)) {
|
2012-06-08 15:35:03 +00:00
|
|
|
fPipeState = SkOSMenu::kOffState;
|
2011-08-12 14:27:47 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
SkScalar values[4];
|
|
|
|
SkInterpolator::Result result = fInterp.timeToValues(SkTime::GetMSecs(), values);
|
|
|
|
//SkDebugf("transition %x %d pipe:%d\n", this, result, fUsePipe);
|
|
|
|
//SkDebugf("%f %f %f %f %d\n", values[0], values[1], values[2], values[3], result);
|
|
|
|
if (SkInterpolator::kNormal_Result == result) {
|
|
|
|
fPrev->setLocX(values[kPrevX]);
|
|
|
|
fPrev->setLocY(values[kPrevY]);
|
|
|
|
fNext->setLocX(values[kNextX]);
|
|
|
|
fNext->setLocY(values[kNextY]);
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
else {
|
2011-08-12 14:27:47 +00:00
|
|
|
(new SkEvent("transition-done", this->getSinkID()))->post();
|
|
|
|
fDone = true;
|
2011-08-08 15:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
virtual void onSizeChange() {
|
|
|
|
this->INHERITED::onSizeChange();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
fNext->setSize(this->width(), this->height());
|
|
|
|
fPrev->setSize(this->width(), this->height());
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
SkScalar lr = 0, ud = 0;
|
|
|
|
if (fAnimationDirection & (kLeftDirection|kULDirection|kDLDirection))
|
|
|
|
lr = this->width();
|
|
|
|
if (fAnimationDirection & (kRightDirection|kURDirection|kDRDirection))
|
|
|
|
lr = -this->width();
|
|
|
|
if (fAnimationDirection & (kUpDirection|kULDirection|kURDirection))
|
|
|
|
ud = this->height();
|
|
|
|
if (fAnimationDirection & (kDownDirection|kDLDirection|kDRDirection))
|
|
|
|
ud = -this->height();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
fBegin[kPrevX] = fBegin[kPrevY] = 0;
|
|
|
|
fBegin[kNextX] = lr;
|
|
|
|
fBegin[kNextY] = ud;
|
|
|
|
fNext->setLocX(lr);
|
|
|
|
fNext->setLocY(ud);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-12 14:27:47 +00:00
|
|
|
if (is_transition(fPrev))
|
2011-08-08 15:37:23 +00:00
|
|
|
lr = ud = 0;
|
|
|
|
fEnd[kPrevX] = -lr;
|
|
|
|
fEnd[kPrevY] = -ud;
|
|
|
|
fEnd[kNextX] = fEnd[kNextY] = 0;
|
2012-05-08 13:15:37 +00:00
|
|
|
SkScalar blend[] = { SkFloatToScalar(0.8f), SkFloatToScalar(0.0f),
|
|
|
|
SkFloatToScalar(0.0f), SK_Scalar1 };
|
2011-08-08 15:37:23 +00:00
|
|
|
fInterp.setKeyFrame(0, SkTime::GetMSecs(), fBegin, blend);
|
|
|
|
fInterp.setKeyFrame(1, SkTime::GetMSecs()+500, fEnd, blend);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
kPrevX = 0,
|
|
|
|
kPrevY = 1,
|
|
|
|
kNextX = 2,
|
|
|
|
kNextY = 3
|
|
|
|
};
|
|
|
|
SkView* fPrev;
|
|
|
|
SkView* fNext;
|
2011-08-12 14:27:47 +00:00
|
|
|
bool fDone;
|
2011-08-08 15:37:23 +00:00
|
|
|
SkInterpolator fInterp;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
enum Direction{
|
|
|
|
kUpDirection = 1,
|
|
|
|
kURDirection = 1 << 1,
|
|
|
|
kRightDirection = 1 << 2,
|
|
|
|
kDRDirection = 1 << 3,
|
|
|
|
kDownDirection = 1 << 4,
|
|
|
|
kDLDirection = 1 << 5,
|
|
|
|
kLeftDirection = 1 << 6,
|
|
|
|
kULDirection = 1 << 7
|
|
|
|
};
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
Direction fAnimationDirection;
|
|
|
|
SkScalar fBegin[4];
|
|
|
|
SkScalar fEnd[4];
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-06-06 12:09:51 +00:00
|
|
|
// FIXME: this should go in a header
|
|
|
|
SkView* create_transition(SkView* prev, SkView* next, int direction);
|
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
SkView* create_transition(SkView* prev, SkView* next, int direction) {
|
|
|
|
return SkNEW_ARGS(TransitionView, (prev, next, direction));
|
2012-06-06 12:09:51 +00:00
|
|
|
};
|