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 */ 7 8#ifndef SampleCode_DEFINED 9#define SampleCode_DEFINED 10 11#include "SkColor.h" 12#include "SkEvent.h" 13#include "SkView.h" 14 15class SkAnimTimer; 16 17#define DEF_SAMPLE(code) \ 18 static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \ 19 static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); 20 21static const char gCharEvtName[] = "SampleCode_Char_Event"; 22static const char gTitleEvtName[] = "SampleCode_Title_Event"; 23 24class SampleCode { 25public: 26 static bool CharQ(const SkEvent&, SkUnichar* outUni); 27 28 static bool TitleQ(const SkEvent&); 29 static void TitleR(SkEvent*, const char title[]); 30 static bool RequestTitle(SkView* view, SkString* title); 31 32 friend class SampleWindow; 33}; 34 35////////////////////////////////////////////////////////////////////////////// 36 37// interface that constructs SkViews 38class SkViewFactory : public SkRefCnt { 39public: 40 virtual SkView* operator() () const = 0; 41}; 42 43typedef SkView* (*SkViewCreateFunc)(); 44 45// wraps SkViewCreateFunc in SkViewFactory interface 46class SkFuncViewFactory : public SkViewFactory { 47public: 48 SkFuncViewFactory(SkViewCreateFunc func); 49 SkView* operator() () const override; 50 51private: 52 SkViewCreateFunc fCreateFunc; 53}; 54 55class SkViewRegister : public SkRefCnt { 56public: 57 explicit SkViewRegister(SkViewFactory*); 58 explicit SkViewRegister(SkViewCreateFunc); 59 60 ~SkViewRegister() { 61 fFact->unref(); 62 } 63 64 static const SkViewRegister* Head() { return gHead; } 65 66 SkViewRegister* next() const { return fChain; } 67 const SkViewFactory* factory() const { return fFact; } 68 69private: 70 SkViewFactory* fFact; 71 SkViewRegister* fChain; 72 73 static SkViewRegister* gHead; 74}; 75 76/////////////////////////////////////////////////////////////////////////////// 77 78class SampleView : public SkView { 79public: 80 SampleView() 81 : fBGColor(SK_ColorWHITE) 82 , fHaveCalledOnceBeforeDraw(false) 83 {} 84 85 void setBGColor(SkColor color) { fBGColor = color; } 86 bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); } 87 88 static bool IsSampleView(SkView*); 89 90protected: 91 virtual void onDrawBackground(SkCanvas*); 92 virtual void onDrawContent(SkCanvas*) = 0; 93 virtual bool onAnimate(const SkAnimTimer&) { return false; } 94 virtual void onOnceBeforeDraw() {} 95 96 // overrides 97 virtual bool onQuery(SkEvent* evt); 98 virtual void onDraw(SkCanvas*); 99 100 SkColor fBGColor; 101 102private: 103 bool fHaveCalledOnceBeforeDraw; 104 typedef SkView INHERITED; 105}; 106 107#endif 108