blob: f1026bddcb00ec4cff7f2d074ea879e5fc6612f4 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "tools/Registry.h"
Hal Canarye574f1e2019-07-15 14:01:37 -040017#include "tools/SkMetaData.h"
Hal Canaryb1f411a2019-08-29 10:39:22 -040018#include "tools/skui/InputState.h"
19#include "tools/skui/ModifierKey.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040020
Mike Reed0ce3e882020-04-22 11:09:30 -040021#include <functional>
22
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040023class SkCanvas;
24class Sample;
25
26using SampleFactory = Sample* (*)();
27using 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 Canaryfcf63592019-07-12 11:32:43 -040035class Sample {
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040036public:
37 Sample()
38 : fBGColor(SK_ColorWHITE)
39 , fWidth(0), fHeight(0)
40 , fHaveCalledOnceBeforeDraw(false)
41 {}
42
Hal Canaryfcf63592019-07-12 11:32:43 -040043 virtual ~Sample() = default;
44
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040045 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 Canary6cc65e12019-07-03 15:53:04 -040055 virtual bool onChar(SkUnichar) { return false; }
56
Hal Canaryfcf63592019-07-12 11:32:43 -040057 // Click handling
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040058 class Click {
59 public:
Mike Reed0ce3e882020-04-22 11:09:30 -040060 Click() {}
61 Click(std::function<bool(Click*)> f) : fFunc(f), fHasFunc(true) {}
Hal Canaryfcf63592019-07-12 11:32:43 -040062 virtual ~Click() = default;
Mike Reed0ce3e882020-04-22 11:09:30 -040063
Hal Canaryfcf63592019-07-12 11:32:43 -040064 SkPoint fOrig = {0, 0};
65 SkPoint fPrev = {0, 0};
66 SkPoint fCurr = {0, 0};
Hal Canaryb1f411a2019-08-29 10:39:22 -040067 skui::InputState fState = skui::InputState::kDown;
68 skui::ModifierKey fModifierKeys = skui::ModifierKey::kNone;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040069 SkMetaData fMeta;
Mike Reed0ce3e882020-04-22 11:09:30 -040070
71 std::function<bool(Click*)> fFunc;
72 bool fHasFunc = false;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040073 };
Hal Canaryb1f411a2019-08-29 10:39:22 -040074 bool mouse(SkPoint point, skui::InputState clickState, skui::ModifierKey modifierKeys);
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040075
76 void setBGColor(SkColor color) { fBGColor = color; }
Hal Canary41248072019-07-11 16:32:53 -040077 bool animate(double nanos) { return this->onAnimate(nanos); }
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040078
Hal Canary8a027312019-07-03 10:55:44 -040079 virtual SkString name() = 0;
80
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040081protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040082 /** 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 Canaryb1f411a2019-08-29 10:39:22 -040086 virtual Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi);
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040087
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 Canary41248072019-07-11 16:32:53 -040093 virtual bool onAnimate(double /*nanos*/) { return false; }
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040094 virtual void onOnceBeforeDraw() {}
95
96private:
Hal Canaryfcf63592019-07-12 11:32:43 -040097 std::unique_ptr<Click> fClick;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040098 SkColor fBGColor;
99 SkScalar fWidth, fHeight;
100 bool fHaveCalledOnceBeforeDraw;
Hal Canaryfcf63592019-07-12 11:32:43 -0400101
102 Sample(const Sample&) = delete;
103 Sample& operator=(const Sample&) = delete;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400104};
105
106#endif