Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [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 | */ |
| 7 | |
| 8 | #ifndef SampleCode_DEFINED |
| 9 | #define SampleCode_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkPoint.h" |
| 13 | #include "include/core/SkRefCnt.h" |
| 14 | #include "include/core/SkString.h" |
| 15 | #include "include/private/SkMacros.h" |
| 16 | #include "src/utils/SkMetaData.h" |
Hal Canary | 3a85ed1 | 2019-07-08 16:07:57 -0400 | [diff] [blame^] | 17 | #include "tools/ModifierKey.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "tools/Registry.h" |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 19 | |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 20 | class AnimTimer; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 21 | class SkCanvas; |
| 22 | class Sample; |
| 23 | |
| 24 | using SampleFactory = Sample* (*)(); |
| 25 | using SampleRegistry = sk_tools::Registry<SampleFactory>; |
| 26 | |
| 27 | #define DEF_SAMPLE(code) \ |
| 28 | static Sample* SK_MACRO_APPEND_LINE(F_)() { code } \ |
| 29 | static SampleRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); |
| 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | class Sample : public SkRefCnt { |
| 34 | public: |
| 35 | Sample() |
| 36 | : fBGColor(SK_ColorWHITE) |
| 37 | , fWidth(0), fHeight(0) |
| 38 | , fHaveCalledOnceBeforeDraw(false) |
| 39 | {} |
| 40 | |
| 41 | SkScalar width() const { return fWidth; } |
| 42 | SkScalar height() const { return fHeight; } |
| 43 | void setSize(SkScalar width, SkScalar height); |
| 44 | void setSize(const SkPoint& size) { this->setSize(size.fX, size.fY); } |
| 45 | void setWidth(SkScalar width) { this->setSize(width, fHeight); } |
| 46 | void setHeight(SkScalar height) { this->setSize(fWidth, height); } |
| 47 | |
| 48 | /** Call this to have the view draw into the specified canvas. */ |
| 49 | virtual void draw(SkCanvas* canvas); |
| 50 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 51 | virtual bool onChar(SkUnichar) { return false; } |
| 52 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 53 | // Click handling |
| 54 | class Click { |
| 55 | public: |
| 56 | Click(Sample* target); |
| 57 | virtual ~Click(); |
| 58 | |
| 59 | enum State { |
| 60 | kDown_State, |
| 61 | kMoved_State, |
| 62 | kUp_State |
| 63 | }; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 64 | SkPoint fOrig, fPrev, fCurr; |
| 65 | SkIPoint fIOrig, fIPrev, fICurr; |
| 66 | State fState; |
Hal Canary | 3a85ed1 | 2019-07-08 16:07:57 -0400 | [diff] [blame^] | 67 | ModifierKey fModifierKeys = ModifierKey::kNone; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 68 | |
| 69 | SkMetaData fMeta; |
| 70 | private: |
| 71 | sk_sp<Sample> fTarget; |
| 72 | |
| 73 | friend class Sample; |
| 74 | }; |
Hal Canary | 3a85ed1 | 2019-07-08 16:07:57 -0400 | [diff] [blame^] | 75 | Click* findClickHandler(SkScalar x, SkScalar y, ModifierKey modifierKeys); |
| 76 | static void DoClickDown(Click*, int x, int y, ModifierKey modi); |
| 77 | static void DoClickMoved(Click*, int x, int y, ModifierKey modi); |
| 78 | static void DoClickUp(Click*, int x, int y, ModifierKey modi); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 79 | |
| 80 | void setBGColor(SkColor color) { fBGColor = color; } |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 81 | bool animate(const AnimTimer& timer) { return this->onAnimate(timer); } |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 82 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 83 | virtual SkString name() = 0; |
| 84 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 85 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 86 | /** Override to be notified of size changes. Overriders must call the super class. */ |
| 87 | virtual void onSizeChange(); |
| 88 | |
| 89 | /** Override this if you might handle the click */ |
Hal Canary | 3a85ed1 | 2019-07-08 16:07:57 -0400 | [diff] [blame^] | 90 | virtual Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 91 | |
| 92 | /** Override to track clicks. Return true as long as you want to track the pen/mouse. */ |
| 93 | virtual bool onClick(Click*); |
| 94 | |
| 95 | virtual void onDrawBackground(SkCanvas*); |
| 96 | virtual void onDrawContent(SkCanvas*) = 0; |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 97 | virtual bool onAnimate(const AnimTimer&) { return false; } |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 98 | virtual void onOnceBeforeDraw() {} |
| 99 | |
| 100 | private: |
| 101 | SkColor fBGColor; |
| 102 | SkScalar fWidth, fHeight; |
| 103 | bool fHaveCalledOnceBeforeDraw; |
| 104 | }; |
| 105 | |
| 106 | #endif |