blob: 3310d28beb34107e438061aa1a2d6cfae05b2853 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SampleCode_DEFINED
9#define SampleCode_DEFINED
10
reed@google.comf2183392011-04-22 14:10:48 +000011#include "SkColor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkEvent.h"
Hal Canary22be4c42018-06-12 12:37:31 -040013#include "SkMacros.h"
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000014#include "SkView.h"
reed76113a92015-02-02 12:55:02 -080015
reed76113a92015-02-02 12:55:02 -080016class SkAnimTimer;
reed@google.com3cec4d72011-07-06 13:59:47 +000017
reeda7a8b102014-12-16 08:07:43 -080018#define DEF_SAMPLE(code) \
19 static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \
20 static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
21
jvanverthc7027ab2016-06-16 09:52:35 -070022static const char gCharEvtName[] = "SampleCode_Char_Event";
jvanverthc7027ab2016-06-16 09:52:35 -070023static const char gTitleEvtName[] = "SampleCode_Title_Event";
reeda7a8b102014-12-16 08:07:43 -080024
reed@android.com8a1c16f2008-12-17 15:59:43 +000025class SampleCode {
26public:
reed@android.comf2b98d62010-12-20 18:26:13 +000027 static bool CharQ(const SkEvent&, SkUnichar* outUni);
28
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 static bool TitleQ(const SkEvent&);
30 static void TitleR(SkEvent*, const char title[]);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000031 static bool RequestTitle(SkView* view, SkString* title);
rmistry@google.comae933ce2012-08-23 18:19:56 +000032
reedd9adfe62015-02-01 19:01:04 -080033 friend class SampleWindow;
reed@android.com8a1c16f2008-12-17 15:59:43 +000034};
35
36//////////////////////////////////////////////////////////////////////////////
37
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000038// interface that constructs SkViews
39class SkViewFactory : public SkRefCnt {
rmistry@google.comae933ce2012-08-23 18:19:56 +000040public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000041 virtual SkView* operator() () const = 0;
42};
43
44typedef SkView* (*SkViewCreateFunc)();
45
46// wraps SkViewCreateFunc in SkViewFactory interface
47class SkFuncViewFactory : public SkViewFactory {
48public:
49 SkFuncViewFactory(SkViewCreateFunc func);
mtklein36352bf2015-03-25 18:17:31 -070050 SkView* operator() () const override;
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000052private:
53 SkViewCreateFunc fCreateFunc;
54};
55
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000056class SkViewRegister : public SkRefCnt {
57public:
58 explicit SkViewRegister(SkViewFactory*);
59 explicit SkViewRegister(SkViewCreateFunc);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000060
61 ~SkViewRegister() {
62 fFact->unref();
63 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 static const SkViewRegister* Head() { return gHead; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000066
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 SkViewRegister* next() const { return fChain; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000068 const SkViewFactory* factory() const { return fFact; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070private:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000071 SkViewFactory* fFact;
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkViewRegister* fChain;
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 static SkViewRegister* gHead;
75};
76
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000077///////////////////////////////////////////////////////////////////////////////
78
79class SampleView : public SkView {
80public:
reed@google.com4d5c26d2013-01-08 16:17:50 +000081 SampleView()
Brian Osman4f99e582017-11-22 13:23:35 -050082 : fBGColor(SK_ColorWHITE)
caryclark63c684a2015-02-25 09:04:04 -080083 , fHaveCalledOnceBeforeDraw(false)
reed868074b2014-06-03 10:53:59 -070084 {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000085
reed@google.comf2183392011-04-22 14:10:48 +000086 void setBGColor(SkColor color) { fBGColor = color; }
reed76113a92015-02-02 12:55:02 -080087 bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); }
reed@google.comf2183392011-04-22 14:10:48 +000088
reed@google.coma6ff4dc2011-05-12 22:08:24 +000089 static bool IsSampleView(SkView*);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000090
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000091protected:
92 virtual void onDrawBackground(SkCanvas*);
93 virtual void onDrawContent(SkCanvas*) = 0;
reed76113a92015-02-02 12:55:02 -080094 virtual bool onAnimate(const SkAnimTimer&) { return false; }
caryclark63c684a2015-02-25 09:04:04 -080095 virtual void onOnceBeforeDraw() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000097 // overrides
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000098 virtual bool onQuery(SkEvent* evt);
99 virtual void onDraw(SkCanvas*);
100
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000101 SkColor fBGColor;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000103private:
caryclark63c684a2015-02-25 09:04:04 -0800104 bool fHaveCalledOnceBeforeDraw;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000105 typedef SkView INHERITED;
106};
107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108#endif