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