blob: 053d6821a25a88d7139923ccabf731426f1e5f99 [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"
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000013#include "SkView.h"
reed76113a92015-02-02 12:55:02 -080014
reed76113a92015-02-02 12:55:02 -080015class SkAnimTimer;
reed@google.com3cec4d72011-07-06 13:59:47 +000016
reeda7a8b102014-12-16 08:07:43 -080017#define DEF_SAMPLE(code) \
18 static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \
19 static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
20
jvanverthc7027ab2016-06-16 09:52:35 -070021static const char gCharEvtName[] = "SampleCode_Char_Event";
jvanverthc7027ab2016-06-16 09:52:35 -070022static const char gTitleEvtName[] = "SampleCode_Title_Event";
reeda7a8b102014-12-16 08:07:43 -080023
reed@android.com8a1c16f2008-12-17 15:59:43 +000024class SampleCode {
25public:
reed@android.comf2b98d62010-12-20 18:26:13 +000026 static bool CharQ(const SkEvent&, SkUnichar* outUni);
27
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 static bool TitleQ(const SkEvent&);
29 static void TitleR(SkEvent*, const char title[]);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000030 static bool RequestTitle(SkView* view, SkString* title);
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
reedd9adfe62015-02-01 19:01:04 -080032 friend class SampleWindow;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033};
34
35//////////////////////////////////////////////////////////////////////////////
36
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000037// interface that constructs SkViews
38class SkViewFactory : public SkRefCnt {
rmistry@google.comae933ce2012-08-23 18:19:56 +000039public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000040 virtual SkView* operator() () const = 0;
41};
42
43typedef SkView* (*SkViewCreateFunc)();
44
45// wraps SkViewCreateFunc in SkViewFactory interface
46class SkFuncViewFactory : public SkViewFactory {
47public:
48 SkFuncViewFactory(SkViewCreateFunc func);
mtklein36352bf2015-03-25 18:17:31 -070049 SkView* operator() () const override;
rmistry@google.comae933ce2012-08-23 18:19:56 +000050
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000051private:
52 SkViewCreateFunc fCreateFunc;
53};
54
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000055class SkViewRegister : public SkRefCnt {
56public:
57 explicit SkViewRegister(SkViewFactory*);
58 explicit SkViewRegister(SkViewCreateFunc);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000059
60 ~SkViewRegister() {
61 fFact->unref();
62 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 static const SkViewRegister* Head() { return gHead; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 SkViewRegister* next() const { return fChain; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000067 const SkViewFactory* factory() const { return fFact; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069private:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000070 SkViewFactory* fFact;
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 SkViewRegister* fChain;
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 static SkViewRegister* gHead;
74};
75
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000076///////////////////////////////////////////////////////////////////////////////
77
78class SampleView : public SkView {
79public:
reed@google.com4d5c26d2013-01-08 16:17:50 +000080 SampleView()
Brian Osman4f99e582017-11-22 13:23:35 -050081 : fBGColor(SK_ColorWHITE)
caryclark63c684a2015-02-25 09:04:04 -080082 , fHaveCalledOnceBeforeDraw(false)
reed868074b2014-06-03 10:53:59 -070083 {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000084
reed@google.comf2183392011-04-22 14:10:48 +000085 void setBGColor(SkColor color) { fBGColor = color; }
reed76113a92015-02-02 12:55:02 -080086 bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); }
reed@google.comf2183392011-04-22 14:10:48 +000087
reed@google.coma6ff4dc2011-05-12 22:08:24 +000088 static bool IsSampleView(SkView*);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000089
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000090protected:
91 virtual void onDrawBackground(SkCanvas*);
92 virtual void onDrawContent(SkCanvas*) = 0;
reed76113a92015-02-02 12:55:02 -080093 virtual bool onAnimate(const SkAnimTimer&) { return false; }
caryclark63c684a2015-02-25 09:04:04 -080094 virtual void onOnceBeforeDraw() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +000095
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000096 // overrides
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000097 virtual bool onQuery(SkEvent* evt);
98 virtual void onDraw(SkCanvas*);
99
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000100 SkColor fBGColor;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000102private:
caryclark63c684a2015-02-25 09:04:04 -0800103 bool fHaveCalledOnceBeforeDraw;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000104 typedef SkView INHERITED;
105};
106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107#endif