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 | */ |
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" |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 15 | class SkOSMenu; |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 16 | class GrContext; |
| 17 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | class SampleCode { |
| 19 | public: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 20 | static bool KeyQ(const SkEvent&, SkKey* outKey); |
| 21 | static bool CharQ(const SkEvent&, SkUnichar* outUni); |
| 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | static bool TitleQ(const SkEvent&); |
| 24 | static void TitleR(SkEvent*, const char title[]); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame^] | 25 | static bool RequestTitle(SkView* view, SkString* title); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | |
| 27 | static bool PrefSizeQ(const SkEvent&); |
| 28 | static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 29 | |
| 30 | static bool FastTextQ(const SkEvent&); |
| 31 | |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 32 | static SkMSec GetAnimTime(); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 33 | static SkMSec GetAnimTimeDelta(); |
| 34 | static SkScalar GetAnimSecondsDelta(); |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 35 | static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 36 | |
| 37 | static GrContext* GetGr(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | ////////////////////////////////////////////////////////////////////////////// |
| 41 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | typedef SkView* (*SkViewFactory)(); |
| 43 | |
| 44 | class SkViewRegister : SkNoncopyable { |
| 45 | public: |
deanm@chromium.org | 1220e1c | 2009-06-11 12:26:47 +0000 | [diff] [blame] | 46 | explicit SkViewRegister(SkViewFactory); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | |
| 48 | static const SkViewRegister* Head() { return gHead; } |
| 49 | |
| 50 | SkViewRegister* next() const { return fChain; } |
| 51 | SkViewFactory factory() const { return fFact; } |
| 52 | |
| 53 | private: |
| 54 | SkViewFactory fFact; |
| 55 | SkViewRegister* fChain; |
| 56 | |
| 57 | static SkViewRegister* gHead; |
| 58 | }; |
| 59 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | |
| 62 | class SampleView : public SkView { |
| 63 | public: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 64 | SampleView() : fRepeatCount(1), fBGColor(SK_ColorWHITE) { |
| 65 | fUsePipe = false; |
| 66 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 67 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 68 | void setBGColor(SkColor color) { fBGColor = color; } |
| 69 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 70 | static bool IsSampleView(SkView*); |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 71 | static bool SetRepeatDraw(SkView*, int count); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 72 | static bool SetUsePipe(SkView*, bool); |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 73 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame^] | 74 | /** |
| 75 | * Call this to request menu items from a SampleView. |
| 76 | * Subclassing notes: A subclass of SampleView can overwrite this method |
| 77 | * to add new items of various types to the menu and change its title. |
| 78 | * The events attached to any new menu items must be handled in its onEvent |
| 79 | * method. See SkOSMenu.h for helper functions. |
| 80 | */ |
| 81 | virtual void requestMenu(SkOSMenu* menu) {} |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 82 | |
| 83 | protected: |
| 84 | virtual void onDrawBackground(SkCanvas*); |
| 85 | virtual void onDrawContent(SkCanvas*) = 0; |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 86 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 87 | // overrides |
| 88 | virtual bool onEvent(const SkEvent& evt); |
| 89 | virtual bool onQuery(SkEvent* evt); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame^] | 90 | virtual void draw(SkCanvas*); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 91 | virtual void onDraw(SkCanvas*); |
| 92 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame^] | 93 | bool fUsePipe; |
| 94 | SkColor fBGColor; |
| 95 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 96 | private: |
| 97 | int fRepeatCount; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 98 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 99 | typedef SkView INHERITED; |
| 100 | }; |
| 101 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | #endif |
| 103 | |