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" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 13 | #include "SkKey.h" |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 14 | #include "SkView.h" |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 15 | #include "SkOSMenu.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 16 | |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 17 | class GrContext; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 18 | class SkAnimTimer; |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 19 | |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 20 | #define DEF_SAMPLE(code) \ |
| 21 | static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \ |
| 22 | static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); |
| 23 | |
| 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | class SampleCode { |
| 26 | public: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 27 | static bool KeyQ(const SkEvent&, SkKey* outKey); |
| 28 | static bool CharQ(const SkEvent&, SkUnichar* outUni); |
| 29 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | static bool TitleQ(const SkEvent&); |
| 31 | static void TitleR(SkEvent*, const char title[]); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 32 | static bool RequestTitle(SkView* view, SkString* title); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | static bool PrefSizeQ(const SkEvent&); |
| 35 | static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 36 | |
| 37 | static bool FastTextQ(const SkEvent&); |
| 38 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 39 | friend class SampleWindow; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | ////////////////////////////////////////////////////////////////////////////// |
| 43 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 44 | // interface that constructs SkViews |
| 45 | class SkViewFactory : public SkRefCnt { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | public: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 47 | virtual SkView* operator() () const = 0; |
| 48 | }; |
| 49 | |
| 50 | typedef SkView* (*SkViewCreateFunc)(); |
| 51 | |
| 52 | // wraps SkViewCreateFunc in SkViewFactory interface |
| 53 | class SkFuncViewFactory : public SkViewFactory { |
| 54 | public: |
| 55 | SkFuncViewFactory(SkViewCreateFunc func); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 56 | SkView* operator() () const override; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 58 | private: |
| 59 | SkViewCreateFunc fCreateFunc; |
| 60 | }; |
| 61 | |
| 62 | namespace skiagm { |
| 63 | class GM; |
| 64 | } |
| 65 | |
| 66 | // factory function that creates a skiagm::GM |
| 67 | typedef skiagm::GM* (*GMFactoryFunc)(void*); |
| 68 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | // Takes a GM factory function and implements the SkViewFactory interface |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 70 | // by making the GM and wrapping it in a GMSampleView. GMSampleView bridges |
| 71 | // the SampleView interface to skiagm::GM. |
| 72 | class SkGMSampleViewFactory : public SkViewFactory { |
| 73 | public: |
| 74 | SkGMSampleViewFactory(GMFactoryFunc func); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | SkView* operator() () const override; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 76 | private: |
| 77 | GMFactoryFunc fFunc; |
| 78 | }; |
| 79 | |
| 80 | class SkViewRegister : public SkRefCnt { |
| 81 | public: |
| 82 | explicit SkViewRegister(SkViewFactory*); |
| 83 | explicit SkViewRegister(SkViewCreateFunc); |
| 84 | explicit SkViewRegister(GMFactoryFunc); |
| 85 | |
| 86 | ~SkViewRegister() { |
| 87 | fFact->unref(); |
| 88 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 89 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | static const SkViewRegister* Head() { return gHead; } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 91 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | SkViewRegister* next() const { return fChain; } |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 93 | const SkViewFactory* factory() const { return fFact; } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | private: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 96 | SkViewFactory* fFact; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | SkViewRegister* fChain; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | static SkViewRegister* gHead; |
| 100 | }; |
| 101 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 102 | /////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | class SampleView : public SkView { |
| 105 | public: |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 106 | SampleView() |
| 107 | : fPipeState(SkOSMenu::kOffState) |
| 108 | , fBGColor(SK_ColorWHITE) |
| 109 | , fRepeatCount(1) |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 110 | , fHaveCalledOnceBeforeDraw(false) |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 111 | {} |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 112 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 113 | void setBGColor(SkColor color) { fBGColor = color; } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 114 | bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); } |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 115 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 116 | static bool IsSampleView(SkView*); |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 117 | static bool SetRepeatDraw(SkView*, int count); |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 118 | static bool SetUsePipe(SkView*, SkOSMenu::TriState); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 119 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 120 | /** |
| 121 | * Call this to request menu items from a SampleView. |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | * Subclassing notes: A subclass of SampleView can overwrite this method |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 123 | * to add new items of various types to the menu and change its title. |
| 124 | * The events attached to any new menu items must be handled in its onEvent |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 125 | * method. See SkOSMenu.h for helper functions. |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 126 | */ |
| 127 | virtual void requestMenu(SkOSMenu* menu) {} |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 128 | |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 129 | virtual void onTileSizeChanged(const SkSize& tileSize) {} |
| 130 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 131 | protected: |
| 132 | virtual void onDrawBackground(SkCanvas*); |
| 133 | virtual void onDrawContent(SkCanvas*) = 0; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 134 | virtual bool onAnimate(const SkAnimTimer&) { return false; } |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 135 | virtual void onOnceBeforeDraw() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 137 | // overrides |
| 138 | virtual bool onEvent(const SkEvent& evt); |
| 139 | virtual bool onQuery(SkEvent* evt); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 140 | virtual void draw(SkCanvas*); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 141 | virtual void onDraw(SkCanvas*); |
| 142 | |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 143 | SkOSMenu::TriState fPipeState; |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 144 | SkColor fBGColor; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 145 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 146 | private: |
| 147 | int fRepeatCount; |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 148 | bool fHaveCalledOnceBeforeDraw; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 149 | typedef SkView INHERITED; |
| 150 | }; |
| 151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | #endif |