epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 8 | |
| 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SampleCode_DEFINED |
| 11 | #define SampleCode_DEFINED |
| 12 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 13 | #include "SkColor.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkEvent.h" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 15 | #include "SkKey.h" |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 16 | #include "SkView.h" |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 17 | #include "SkOSMenu.h" |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 18 | class GrContext; |
| 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@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 39 | static SkMSec GetAnimTime(); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 40 | static SkMSec GetAnimTimeDelta(); |
| 41 | static SkScalar GetAnimSecondsDelta(); |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 42 | static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0); |
bsalomon@google.com | 8500322 | 2012-03-28 14:44:37 +0000 | [diff] [blame] | 43 | // gives a sinusoidal value between 0 and amplitude |
| 44 | static SkScalar GetAnimSinScalar(SkScalar amplitude, |
| 45 | SkScalar periodInSec, |
| 46 | SkScalar phaseInSec = 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | ////////////////////////////////////////////////////////////////////////////// |
| 50 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 51 | // interface that constructs SkViews |
| 52 | class SkViewFactory : public SkRefCnt { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | public: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 54 | virtual SkView* operator() () const = 0; |
| 55 | }; |
| 56 | |
| 57 | typedef SkView* (*SkViewCreateFunc)(); |
| 58 | |
| 59 | // wraps SkViewCreateFunc in SkViewFactory interface |
| 60 | class SkFuncViewFactory : public SkViewFactory { |
| 61 | public: |
| 62 | SkFuncViewFactory(SkViewCreateFunc func); |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 63 | SkView* operator() () const SK_OVERRIDE; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 65 | private: |
| 66 | SkViewCreateFunc fCreateFunc; |
| 67 | }; |
| 68 | |
| 69 | namespace skiagm { |
| 70 | class GM; |
| 71 | } |
| 72 | |
| 73 | // factory function that creates a skiagm::GM |
| 74 | typedef skiagm::GM* (*GMFactoryFunc)(void*); |
| 75 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 76 | // Takes a GM factory function and implements the SkViewFactory interface |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 77 | // by making the GM and wrapping it in a GMSampleView. GMSampleView bridges |
| 78 | // the SampleView interface to skiagm::GM. |
| 79 | class SkGMSampleViewFactory : public SkViewFactory { |
| 80 | public: |
| 81 | SkGMSampleViewFactory(GMFactoryFunc func); |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 82 | SkView* operator() () const SK_OVERRIDE; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 83 | private: |
| 84 | GMFactoryFunc fFunc; |
| 85 | }; |
| 86 | |
| 87 | class SkViewRegister : public SkRefCnt { |
| 88 | public: |
| 89 | explicit SkViewRegister(SkViewFactory*); |
| 90 | explicit SkViewRegister(SkViewCreateFunc); |
| 91 | explicit SkViewRegister(GMFactoryFunc); |
| 92 | |
| 93 | ~SkViewRegister() { |
| 94 | fFact->unref(); |
| 95 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | static const SkViewRegister* Head() { return gHead; } |
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 | SkViewRegister* next() const { return fChain; } |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 100 | const SkViewFactory* factory() const { return fFact; } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 101 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | private: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 103 | SkViewFactory* fFact; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | SkViewRegister* fChain; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | static SkViewRegister* gHead; |
| 107 | }; |
| 108 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 109 | /////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | class SampleView : public SkView { |
| 112 | public: |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 113 | SampleView() |
| 114 | : fPipeState(SkOSMenu::kOffState) |
| 115 | , fBGColor(SK_ColorWHITE) |
| 116 | , fRepeatCount(1) |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 117 | {} |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 118 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 119 | void setBGColor(SkColor color) { fBGColor = color; } |
| 120 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 121 | static bool IsSampleView(SkView*); |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 122 | static bool SetRepeatDraw(SkView*, int count); |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 123 | static bool SetUsePipe(SkView*, SkOSMenu::TriState); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Call this to request menu items from a SampleView. |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 127 | * Subclassing notes: A subclass of SampleView can overwrite this method |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 128 | * to add new items of various types to the menu and change its title. |
| 129 | * 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] | 130 | * method. See SkOSMenu.h for helper functions. |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 131 | */ |
| 132 | virtual void requestMenu(SkOSMenu* menu) {} |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 133 | |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 134 | virtual void onTileSizeChanged(const SkSize& tileSize) {} |
| 135 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 136 | protected: |
| 137 | virtual void onDrawBackground(SkCanvas*); |
| 138 | virtual void onDrawContent(SkCanvas*) = 0; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 139 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 140 | // overrides |
| 141 | virtual bool onEvent(const SkEvent& evt); |
| 142 | virtual bool onQuery(SkEvent* evt); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 143 | virtual void draw(SkCanvas*); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 144 | virtual void onDraw(SkCanvas*); |
| 145 | |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 146 | SkOSMenu::TriState fPipeState; |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 147 | SkColor fBGColor; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 148 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 149 | private: |
| 150 | int fRepeatCount; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 151 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 152 | typedef SkView INHERITED; |
| 153 | }; |
| 154 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | #endif |