blob: e4ccfb2af6c49db3a0aa4af5b1d5ed71c8ad43fe [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
24
reed@android.com8a1c16f2008-12-17 15:59:43 +000025class SampleCode {
26public:
reed@android.comf2b98d62010-12-20 18:26:13 +000027 static bool KeyQ(const SkEvent&, SkKey* outKey);
28 static bool CharQ(const SkEvent&, SkUnichar* outUni);
29
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 static bool TitleQ(const SkEvent&);
31 static void TitleR(SkEvent*, const char title[]);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000032 static bool RequestTitle(SkView* view, SkString* title);
rmistry@google.comae933ce2012-08-23 18:19:56 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 static bool PrefSizeQ(const SkEvent&);
35 static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
reed@android.comf2b98d62010-12-20 18:26:13 +000036
37 static bool FastTextQ(const SkEvent&);
38
reedd9adfe62015-02-01 19:01:04 -080039 friend class SampleWindow;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040};
41
42//////////////////////////////////////////////////////////////////////////////
43
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000044// interface that constructs SkViews
45class SkViewFactory : public SkRefCnt {
rmistry@google.comae933ce2012-08-23 18:19:56 +000046public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000047 virtual SkView* operator() () const = 0;
48};
49
50typedef SkView* (*SkViewCreateFunc)();
51
52// wraps SkViewCreateFunc in SkViewFactory interface
53class SkFuncViewFactory : public SkViewFactory {
54public:
55 SkFuncViewFactory(SkViewCreateFunc func);
mtklein36352bf2015-03-25 18:17:31 -070056 SkView* operator() () const override;
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000058private:
59 SkViewCreateFunc fCreateFunc;
60};
61
62namespace skiagm {
63class GM;
64}
65
66// factory function that creates a skiagm::GM
67typedef skiagm::GM* (*GMFactoryFunc)(void*);
68
rmistry@google.comae933ce2012-08-23 18:19:56 +000069// Takes a GM factory function and implements the SkViewFactory interface
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000070// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
71// the SampleView interface to skiagm::GM.
72class SkGMSampleViewFactory : public SkViewFactory {
73public:
74 SkGMSampleViewFactory(GMFactoryFunc func);
mtklein36352bf2015-03-25 18:17:31 -070075 SkView* operator() () const override;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000076private:
77 GMFactoryFunc fFunc;
78};
79
80class SkViewRegister : public SkRefCnt {
81public:
82 explicit SkViewRegister(SkViewFactory*);
83 explicit SkViewRegister(SkViewCreateFunc);
84 explicit SkViewRegister(GMFactoryFunc);
85
86 ~SkViewRegister() {
87 fFact->unref();
88 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 static const SkViewRegister* Head() { return gHead; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 SkViewRegister* next() const { return fChain; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000093 const SkViewFactory* factory() const { return fFact; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095private:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000096 SkViewFactory* fFact;
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 SkViewRegister* fChain;
rmistry@google.comae933ce2012-08-23 18:19:56 +000098
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 static SkViewRegister* gHead;
100};
101
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000102///////////////////////////////////////////////////////////////////////////////
103
104class SampleView : public SkView {
105public:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000106 SampleView()
107 : fPipeState(SkOSMenu::kOffState)
108 , fBGColor(SK_ColorWHITE)
109 , fRepeatCount(1)
caryclark63c684a2015-02-25 09:04:04 -0800110 , fHaveCalledOnceBeforeDraw(false)
reed868074b2014-06-03 10:53:59 -0700111 {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000112
reed@google.comf2183392011-04-22 14:10:48 +0000113 void setBGColor(SkColor color) { fBGColor = color; }
reed76113a92015-02-02 12:55:02 -0800114 bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); }
reed@google.comf2183392011-04-22 14:10:48 +0000115
reed@google.coma6ff4dc2011-05-12 22:08:24 +0000116 static bool IsSampleView(SkView*);
reed@google.comf2183392011-04-22 14:10:48 +0000117 static bool SetRepeatDraw(SkView*, int count);
scroggo@google.comb073d922012-06-08 15:35:03 +0000118 static bool SetUsePipe(SkView*, SkOSMenu::TriState);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000120 /**
121 * Call this to request menu items from a SampleView.
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122 * Subclassing notes: A subclass of SampleView can overwrite this method
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000123 * to add new items of various types to the menu and change its title.
124 * The events attached to any new menu items must be handled in its onEvent
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125 * method. See SkOSMenu.h for helper functions.
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000126 */
127 virtual void requestMenu(SkOSMenu* menu) {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000128
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000129 virtual void onTileSizeChanged(const SkSize& tileSize) {}
130
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000131protected:
132 virtual void onDrawBackground(SkCanvas*);
133 virtual void onDrawContent(SkCanvas*) = 0;
reed76113a92015-02-02 12:55:02 -0800134 virtual bool onAnimate(const SkAnimTimer&) { return false; }
caryclark63c684a2015-02-25 09:04:04 -0800135 virtual void onOnceBeforeDraw() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000137 // overrides
138 virtual bool onEvent(const SkEvent& evt);
139 virtual bool onQuery(SkEvent* evt);
140 virtual void onDraw(SkCanvas*);
141
scroggo@google.comb073d922012-06-08 15:35:03 +0000142 SkOSMenu::TriState fPipeState;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000143 SkColor fBGColor;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000145private:
146 int fRepeatCount;
caryclark63c684a2015-02-25 09:04:04 -0800147 bool fHaveCalledOnceBeforeDraw;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000148 typedef SkView INHERITED;
149};
150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151#endif