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