• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 "tools/InputState.h"
17 #include "tools/ModifierKey.h"
18 #include "tools/Registry.h"
19 #include "tools/SkMetaData.h"
20 
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 {
34 public:
Sample()35     Sample()
36         : fBGColor(SK_ColorWHITE)
37         , fWidth(0), fHeight(0)
38         , fHaveCalledOnceBeforeDraw(false)
39     {}
40 
41     virtual ~Sample() = default;
42 
width()43     SkScalar    width() const { return fWidth; }
height()44     SkScalar    height() const { return fHeight; }
45     void        setSize(SkScalar width, SkScalar height);
setSize(const SkPoint & size)46     void        setSize(const SkPoint& size) { this->setSize(size.fX, size.fY); }
setWidth(SkScalar width)47     void        setWidth(SkScalar width) { this->setSize(width, fHeight); }
setHeight(SkScalar height)48     void        setHeight(SkScalar height) { this->setSize(fWidth, height); }
49 
50     /** Call this to have the view draw into the specified canvas. */
51     virtual void draw(SkCanvas* canvas);
52 
onChar(SkUnichar)53     virtual bool onChar(SkUnichar) { return false; }
54 
55     // Click handling
56     class Click {
57     public:
58         virtual ~Click() = default;
59         SkPoint     fOrig = {0, 0};
60         SkPoint     fPrev = {0, 0};
61         SkPoint     fCurr = {0, 0};
62         InputState  fState = InputState::kDown;
63         ModifierKey fModifierKeys = ModifierKey::kNone;
64         SkMetaData  fMeta;
65     };
66     bool mouse(SkPoint point, InputState clickState, ModifierKey modifierKeys);
67 
setBGColor(SkColor color)68     void setBGColor(SkColor color) { fBGColor = color; }
animate(double nanos)69     bool animate(double nanos) { return this->onAnimate(nanos); }
70 
71     virtual SkString name() = 0;
72 
73 protected:
74     /** Override to be notified of size changes. Overriders must call the super class. */
75     virtual void onSizeChange();
76 
77     /** Override this if you might handle the click */
78     virtual Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi);
79 
80     /** Override to track clicks. Return true as long as you want to track the pen/mouse. */
81     virtual bool onClick(Click*);
82 
83     virtual void onDrawBackground(SkCanvas*);
84     virtual void onDrawContent(SkCanvas*) = 0;
onAnimate(double)85     virtual bool onAnimate(double /*nanos*/) { return false; }
onOnceBeforeDraw()86     virtual void onOnceBeforeDraw() {}
87 
88 private:
89     std::unique_ptr<Click> fClick;
90     SkColor fBGColor;
91     SkScalar fWidth, fHeight;
92     bool fHaveCalledOnceBeforeDraw;
93 
94     Sample(const Sample&) = delete;
95     Sample& operator=(const Sample&) = delete;
96 };
97 
98 #endif
99