blob: d09022f35064c877eb3d4ee4fe6d1b95343e037c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00008
9
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SampleCode_DEFINED
11#define SampleCode_DEFINED
12
reed@google.comf2183392011-04-22 14:10:48 +000013#include "SkColor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkEvent.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000015#include "SkKey.h"
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000016#include "SkView.h"
scroggo@google.comb073d922012-06-08 15:35:03 +000017#include "SkOSMenu.h"
reed@google.com3cec4d72011-07-06 13:59:47 +000018class GrContext;
19
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
reed@android.com44177402009-11-23 21:07:51 +000039 static SkMSec GetAnimTime();
reed@android.comf2b98d62010-12-20 18:26:13 +000040 static SkMSec GetAnimTimeDelta();
41 static SkScalar GetAnimSecondsDelta();
reed@android.com44177402009-11-23 21:07:51 +000042 static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
bsalomon@google.com85003222012-03-28 14:44:37 +000043 // gives a sinusoidal value between 0 and amplitude
44 static SkScalar GetAnimSinScalar(SkScalar amplitude,
45 SkScalar periodInSec,
46 SkScalar phaseInSec = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047};
48
49//////////////////////////////////////////////////////////////////////////////
50
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000051// interface that constructs SkViews
52class SkViewFactory : public SkRefCnt {
rmistry@google.comae933ce2012-08-23 18:19:56 +000053public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000054 virtual SkView* operator() () const = 0;
55};
56
57typedef SkView* (*SkViewCreateFunc)();
58
59// wraps SkViewCreateFunc in SkViewFactory interface
60class SkFuncViewFactory : public SkViewFactory {
61public:
62 SkFuncViewFactory(SkViewCreateFunc func);
mtklein72c9faa2015-01-09 10:06:39 -080063 SkView* operator() () const SK_OVERRIDE;
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000065private:
66 SkViewCreateFunc fCreateFunc;
67};
68
69namespace skiagm {
70class GM;
71}
72
73// factory function that creates a skiagm::GM
74typedef skiagm::GM* (*GMFactoryFunc)(void*);
75
rmistry@google.comae933ce2012-08-23 18:19:56 +000076// Takes a GM factory function and implements the SkViewFactory interface
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000077// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
78// the SampleView interface to skiagm::GM.
79class SkGMSampleViewFactory : public SkViewFactory {
80public:
81 SkGMSampleViewFactory(GMFactoryFunc func);
mtklein72c9faa2015-01-09 10:06:39 -080082 SkView* operator() () const SK_OVERRIDE;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000083private:
84 GMFactoryFunc fFunc;
85};
86
87class SkViewRegister : public SkRefCnt {
88public:
89 explicit SkViewRegister(SkViewFactory*);
90 explicit SkViewRegister(SkViewCreateFunc);
91 explicit SkViewRegister(GMFactoryFunc);
92
93 ~SkViewRegister() {
94 fFact->unref();
95 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 static const SkViewRegister* Head() { return gHead; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000098
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 SkViewRegister* next() const { return fChain; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000100 const SkViewFactory* factory() const { return fFact; }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102private:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000103 SkViewFactory* fFact;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 SkViewRegister* fChain;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 static SkViewRegister* gHead;
107};
108
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000109///////////////////////////////////////////////////////////////////////////////
110
111class SampleView : public SkView {
112public:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000113 SampleView()
114 : fPipeState(SkOSMenu::kOffState)
115 , fBGColor(SK_ColorWHITE)
116 , fRepeatCount(1)
reed868074b2014-06-03 10:53:59 -0700117 {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000118
reed@google.comf2183392011-04-22 14:10:48 +0000119 void setBGColor(SkColor color) { fBGColor = color; }
120
reed@google.coma6ff4dc2011-05-12 22:08:24 +0000121 static bool IsSampleView(SkView*);
reed@google.comf2183392011-04-22 14:10:48 +0000122 static bool SetRepeatDraw(SkView*, int count);
scroggo@google.comb073d922012-06-08 15:35:03 +0000123 static bool SetUsePipe(SkView*, SkOSMenu::TriState);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000125 /**
126 * Call this to request menu items from a SampleView.
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127 * Subclassing notes: A subclass of SampleView can overwrite this method
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000128 * to add new items of various types to the menu and change its title.
129 * The events attached to any new menu items must be handled in its onEvent
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130 * method. See SkOSMenu.h for helper functions.
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000131 */
132 virtual void requestMenu(SkOSMenu* menu) {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000133
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000134 virtual void onTileSizeChanged(const SkSize& tileSize) {}
135
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000136protected:
137 virtual void onDrawBackground(SkCanvas*);
138 virtual void onDrawContent(SkCanvas*) = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000140 // overrides
141 virtual bool onEvent(const SkEvent& evt);
142 virtual bool onQuery(SkEvent* evt);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000143 virtual void draw(SkCanvas*);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000144 virtual void onDraw(SkCanvas*);
145
scroggo@google.comb073d922012-06-08 15:35:03 +0000146 SkOSMenu::TriState fPipeState;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000147 SkColor fBGColor;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000149private:
150 int fRepeatCount;
reed@google.com0faac1e2011-05-11 05:58:58 +0000151
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000152 typedef SkView INHERITED;
153};
154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155#endif