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