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