blob: fe72e4b970646d311cfb1946b4bb958641f4f714 [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"
reed@android.comf2b98d62010-12-20 18:26:13 +000013#include "SkKey.h"
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000014#include "SkView.h"
scroggo@google.comb073d922012-06-08 15:35:03 +000015#include "SkOSMenu.h"
reed76113a92015-02-02 12:55:02 -080016
reed@google.com3cec4d72011-07-06 13:59:47 +000017class GrContext;
reed76113a92015-02-02 12:55:02 -080018class SkAnimTimer;
reed@google.com3cec4d72011-07-06 13:59:47 +000019
reeda7a8b102014-12-16 08:07:43 -080020#define DEF_SAMPLE(code) \
21 static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \
22 static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
23
jvanverthc7027ab2016-06-16 09:52:35 -070024#define MAX_ZOOM_LEVEL 8
25#define MIN_ZOOM_LEVEL -8
26
27static const char gCharEvtName[] = "SampleCode_Char_Event";
28static const char gKeyEvtName[] = "SampleCode_Key_Event";
29static const char gTitleEvtName[] = "SampleCode_Title_Event";
30static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
31static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
32static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
reeda7a8b102014-12-16 08:07:43 -080033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034class SampleCode {
35public:
reed@android.comf2b98d62010-12-20 18:26:13 +000036 static bool KeyQ(const SkEvent&, SkKey* outKey);
37 static bool CharQ(const SkEvent&, SkUnichar* outUni);
38
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 static bool TitleQ(const SkEvent&);
40 static void TitleR(SkEvent*, const char title[]);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000041 static bool RequestTitle(SkView* view, SkString* title);
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 static bool PrefSizeQ(const SkEvent&);
44 static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
reed@android.comf2b98d62010-12-20 18:26:13 +000045
46 static bool FastTextQ(const SkEvent&);
47
reedd9adfe62015-02-01 19:01:04 -080048 friend class SampleWindow;
reed@android.com8a1c16f2008-12-17 15:59:43 +000049};
50
51//////////////////////////////////////////////////////////////////////////////
52
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000053// interface that constructs SkViews
54class SkViewFactory : public SkRefCnt {
rmistry@google.comae933ce2012-08-23 18:19:56 +000055public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000056 virtual SkView* operator() () const = 0;
57};
58
59typedef SkView* (*SkViewCreateFunc)();
60
61// wraps SkViewCreateFunc in SkViewFactory interface
62class SkFuncViewFactory : public SkViewFactory {
63public:
64 SkFuncViewFactory(SkViewCreateFunc func);
mtklein36352bf2015-03-25 18:17:31 -070065 SkView* operator() () const override;
rmistry@google.comae933ce2012-08-23 18:19:56 +000066
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000067private:
68 SkViewCreateFunc fCreateFunc;
69};
70
71namespace skiagm {
72class GM;
73}
74
75// factory function that creates a skiagm::GM
76typedef skiagm::GM* (*GMFactoryFunc)(void*);
77
rmistry@google.comae933ce2012-08-23 18:19:56 +000078// Takes a GM factory function and implements the SkViewFactory interface
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000079// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
80// the SampleView interface to skiagm::GM.
81class SkGMSampleViewFactory : public SkViewFactory {
82public:
83 SkGMSampleViewFactory(GMFactoryFunc func);
mtklein36352bf2015-03-25 18:17:31 -070084 SkView* operator() () const override;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000085private:
86 GMFactoryFunc fFunc;
87};
88
89class SkViewRegister : public SkRefCnt {
90public:
91 explicit SkViewRegister(SkViewFactory*);
92 explicit SkViewRegister(SkViewCreateFunc);
93 explicit SkViewRegister(GMFactoryFunc);
94
95 ~SkViewRegister() {
96 fFact->unref();
97 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000098
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 static const SkViewRegister* Head() { return gHead; }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 SkViewRegister* next() const { return fChain; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000102 const SkViewFactory* factory() const { return fFact; }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104private:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000105 SkViewFactory* fFact;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 SkViewRegister* fChain;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 static SkViewRegister* gHead;
109};
110
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000111///////////////////////////////////////////////////////////////////////////////
112
113class SampleView : public SkView {
114public:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000115 SampleView()
116 : fPipeState(SkOSMenu::kOffState)
117 , fBGColor(SK_ColorWHITE)
118 , fRepeatCount(1)
caryclark63c684a2015-02-25 09:04:04 -0800119 , fHaveCalledOnceBeforeDraw(false)
reed868074b2014-06-03 10:53:59 -0700120 {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000121
reed@google.comf2183392011-04-22 14:10:48 +0000122 void setBGColor(SkColor color) { fBGColor = color; }
reed76113a92015-02-02 12:55:02 -0800123 bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); }
reed@google.comf2183392011-04-22 14:10:48 +0000124
reed@google.coma6ff4dc2011-05-12 22:08:24 +0000125 static bool IsSampleView(SkView*);
reed@google.comf2183392011-04-22 14:10:48 +0000126 static bool SetRepeatDraw(SkView*, int count);
scroggo@google.comb073d922012-06-08 15:35:03 +0000127 static bool SetUsePipe(SkView*, SkOSMenu::TriState);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000129 /**
130 * Call this to request menu items from a SampleView.
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131 * Subclassing notes: A subclass of SampleView can overwrite this method
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000132 * to add new items of various types to the menu and change its title.
133 * The events attached to any new menu items must be handled in its onEvent
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134 * method. See SkOSMenu.h for helper functions.
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000135 */
136 virtual void requestMenu(SkOSMenu* menu) {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000137
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000138 virtual void onTileSizeChanged(const SkSize& tileSize) {}
139
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000140protected:
141 virtual void onDrawBackground(SkCanvas*);
142 virtual void onDrawContent(SkCanvas*) = 0;
reed76113a92015-02-02 12:55:02 -0800143 virtual bool onAnimate(const SkAnimTimer&) { return false; }
caryclark63c684a2015-02-25 09:04:04 -0800144 virtual void onOnceBeforeDraw() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000146 // overrides
147 virtual bool onEvent(const SkEvent& evt);
148 virtual bool onQuery(SkEvent* evt);
149 virtual void onDraw(SkCanvas*);
150
scroggo@google.comb073d922012-06-08 15:35:03 +0000151 SkOSMenu::TriState fPipeState;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000152 SkColor fBGColor;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000154private:
155 int fRepeatCount;
caryclark63c684a2015-02-25 09:04:04 -0800156 bool fHaveCalledOnceBeforeDraw;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000157 typedef SkView INHERITED;
158};
159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160#endif