• 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/Registry.h"
17 #include "tools/SkMetaData.h"
18 #include "tools/skui/InputState.h"
19 #include "tools/skui/ModifierKey.h"
20 
21 #include <functional>
22 
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 
35 class Sample {
36 public:
Sample()37     Sample()
38         : fBGColor(SK_ColorWHITE)
39         , fWidth(0), fHeight(0)
40         , fHaveCalledOnceBeforeDraw(false)
41     {}
42 
43     virtual ~Sample() = default;
44 
width()45     SkScalar    width() const { return fWidth; }
height()46     SkScalar    height() const { return fHeight; }
47     void        setSize(SkScalar width, SkScalar height);
setSize(const SkPoint & size)48     void        setSize(const SkPoint& size) { this->setSize(size.fX, size.fY); }
setWidth(SkScalar width)49     void        setWidth(SkScalar width) { this->setSize(width, fHeight); }
setHeight(SkScalar height)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 
onChar(SkUnichar)55     virtual bool onChar(SkUnichar) { return false; }
56 
57     // Click handling
58     class Click {
59     public:
Click()60         Click() {}
Click(std::function<bool (Click *)> f)61         Click(std::function<bool(Click*)> f) : fFunc(f), fHasFunc(true) {}
62         virtual ~Click() = default;
63 
64         SkPoint     fOrig = {0, 0};
65         SkPoint     fPrev = {0, 0};
66         SkPoint     fCurr = {0, 0};
67         skui::InputState  fState = skui::InputState::kDown;
68         skui::ModifierKey fModifierKeys = skui::ModifierKey::kNone;
69         SkMetaData  fMeta;
70 
71         std::function<bool(Click*)> fFunc;
72         bool fHasFunc = false;
73     };
74     bool mouse(SkPoint point, skui::InputState clickState, skui::ModifierKey modifierKeys);
75 
setBGColor(SkColor color)76     void setBGColor(SkColor color) { fBGColor = color; }
animate(double nanos)77     bool animate(double nanos) { return this->onAnimate(nanos); }
78 
79     virtual SkString name() = 0;
80 
81 protected:
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 */
86     virtual Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi);
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;
onAnimate(double)93     virtual bool onAnimate(double /*nanos*/) { return false; }
onOnceBeforeDraw()94     virtual void onOnceBeforeDraw() {}
95 
96 private:
97     std::unique_ptr<Click> fClick;
98     SkColor fBGColor;
99     SkScalar fWidth, fHeight;
100     bool fHaveCalledOnceBeforeDraw;
101 
102     Sample(const Sample&) = delete;
103     Sample& operator=(const Sample&) = delete;
104 };
105 
106 #endif
107