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 | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SampleCode_DEFINED |
| 9 | #define SampleCode_DEFINED |
| 10 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 11 | #include "SkColor.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkEvent.h" |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 13 | #include "SkView.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 14 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 15 | class SkAnimTimer; |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 16 | |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 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 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 21 | static const char gCharEvtName[] = "SampleCode_Char_Event"; |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 22 | static const char gTitleEvtName[] = "SampleCode_Title_Event"; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 23 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | class SampleCode { |
| 25 | public: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 26 | static bool CharQ(const SkEvent&, SkUnichar* outUni); |
| 27 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | static bool TitleQ(const SkEvent&); |
| 29 | static void TitleR(SkEvent*, const char title[]); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 30 | static bool RequestTitle(SkView* view, SkString* title); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 32 | friend class SampleWindow; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | ////////////////////////////////////////////////////////////////////////////// |
| 36 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 37 | // interface that constructs SkViews |
| 38 | class SkViewFactory : public SkRefCnt { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 39 | public: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 40 | virtual SkView* operator() () const = 0; |
| 41 | }; |
| 42 | |
| 43 | typedef SkView* (*SkViewCreateFunc)(); |
| 44 | |
| 45 | // wraps SkViewCreateFunc in SkViewFactory interface |
| 46 | class SkFuncViewFactory : public SkViewFactory { |
| 47 | public: |
| 48 | SkFuncViewFactory(SkViewCreateFunc func); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | SkView* operator() () const override; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 50 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 51 | private: |
| 52 | SkViewCreateFunc fCreateFunc; |
| 53 | }; |
| 54 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 55 | class SkViewRegister : public SkRefCnt { |
| 56 | public: |
| 57 | explicit SkViewRegister(SkViewFactory*); |
| 58 | explicit SkViewRegister(SkViewCreateFunc); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 59 | |
| 60 | ~SkViewRegister() { |
| 61 | fFact->unref(); |
| 62 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 63 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | static const SkViewRegister* Head() { return gHead; } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 65 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | SkViewRegister* next() const { return fChain; } |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 67 | const SkViewFactory* factory() const { return fFact; } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | private: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 70 | SkViewFactory* fFact; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | SkViewRegister* fChain; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | static SkViewRegister* gHead; |
| 74 | }; |
| 75 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 76 | /////////////////////////////////////////////////////////////////////////////// |
| 77 | |
| 78 | class SampleView : public SkView { |
| 79 | public: |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 80 | SampleView() |
Brian Osman | 4f99e58 | 2017-11-22 13:23:35 -0500 | [diff] [blame] | 81 | : fBGColor(SK_ColorWHITE) |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 82 | , fHaveCalledOnceBeforeDraw(false) |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 83 | {} |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 84 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 85 | void setBGColor(SkColor color) { fBGColor = color; } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 86 | bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); } |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 87 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 88 | static bool IsSampleView(SkView*); |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 89 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 90 | protected: |
| 91 | virtual void onDrawBackground(SkCanvas*); |
| 92 | virtual void onDrawContent(SkCanvas*) = 0; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 93 | virtual bool onAnimate(const SkAnimTimer&) { return false; } |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 94 | virtual void onOnceBeforeDraw() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 95 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 96 | // overrides |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 97 | virtual bool onQuery(SkEvent* evt); |
| 98 | virtual void onDraw(SkCanvas*); |
| 99 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 100 | SkColor fBGColor; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 101 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 102 | private: |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 103 | bool fHaveCalledOnceBeforeDraw; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 104 | typedef SkView INHERITED; |
| 105 | }; |
| 106 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | #endif |