blob: 0d538d261602e4367dda45bec32956e244df73cf [file] [log] [blame]
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#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"
17#include "tools/Registry.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040018
Mike Kleincd5104e2019-03-20 11:55:08 -050019class AnimTimer;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040020class SkCanvas;
21class Sample;
22
23using SampleFactory = Sample* (*)();
24using 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
32class Sample : public SkRefCnt {
33public:
34 Sample()
35 : fBGColor(SK_ColorWHITE)
36 , fWidth(0), fHeight(0)
37 , fHaveCalledOnceBeforeDraw(false)
38 {}
39
40 SkScalar width() const { return fWidth; }
41 SkScalar height() const { return fHeight; }
42 void setSize(SkScalar width, SkScalar height);
43 void setSize(const SkPoint& size) { this->setSize(size.fX, size.fY); }
44 void setWidth(SkScalar width) { this->setSize(width, fHeight); }
45 void setHeight(SkScalar height) { this->setSize(fWidth, height); }
46
47 /** Call this to have the view draw into the specified canvas. */
48 virtual void draw(SkCanvas* canvas);
49
Hal Canary6cc65e12019-07-03 15:53:04 -040050 virtual bool onChar(SkUnichar) { return false; }
51
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040052 // Click handling
53 class Click {
54 public:
55 Click(Sample* target);
56 virtual ~Click();
57
58 enum State {
59 kDown_State,
60 kMoved_State,
61 kUp_State
62 };
63 enum ModifierKeys {
64 kShift_ModifierKey = 1 << 0,
65 kControl_ModifierKey = 1 << 1,
66 kOption_ModifierKey = 1 << 2, // same as ALT
67 kCommand_ModifierKey = 1 << 3,
68 };
69 SkPoint fOrig, fPrev, fCurr;
70 SkIPoint fIOrig, fIPrev, fICurr;
71 State fState;
72 unsigned fModifierKeys;
73
74 SkMetaData fMeta;
75 private:
76 sk_sp<Sample> fTarget;
77
78 friend class Sample;
79 };
80 Click* findClickHandler(SkScalar x, SkScalar y, unsigned modifierKeys);
81 static void DoClickDown(Click*, int x, int y, unsigned modi);
82 static void DoClickMoved(Click*, int x, int y, unsigned modi);
83 static void DoClickUp(Click*, int x, int y, unsigned modi);
84
85 void setBGColor(SkColor color) { fBGColor = color; }
Mike Kleincd5104e2019-03-20 11:55:08 -050086 bool animate(const AnimTimer& timer) { return this->onAnimate(timer); }
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040087
Hal Canary8a027312019-07-03 10:55:44 -040088 virtual SkString name() = 0;
89
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040090protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040091 /** Override to be notified of size changes. Overriders must call the super class. */
92 virtual void onSizeChange();
93
94 /** Override this if you might handle the click */
95 virtual Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi);
96
97 /** Override to track clicks. Return true as long as you want to track the pen/mouse. */
98 virtual bool onClick(Click*);
99
100 virtual void onDrawBackground(SkCanvas*);
101 virtual void onDrawContent(SkCanvas*) = 0;
Mike Kleincd5104e2019-03-20 11:55:08 -0500102 virtual bool onAnimate(const AnimTimer&) { return false; }
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400103 virtual void onOnceBeforeDraw() {}
104
105private:
106 SkColor fBGColor;
107 SkScalar fWidth, fHeight;
108 bool fHaveCalledOnceBeforeDraw;
109};
110
111#endif