blob: 2dacc1fc7d7ecb5487695ecd3240eeea42d4eed3 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000020class SampleCode {
21public:
reed@android.comf2b98d62010-12-20 18:26:13 +000022 static bool KeyQ(const SkEvent&, SkKey* outKey);
23 static bool CharQ(const SkEvent&, SkUnichar* outUni);
24
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 static bool TitleQ(const SkEvent&);
26 static void TitleR(SkEvent*, const char title[]);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000027 static bool RequestTitle(SkView* view, SkString* title);
rmistry@google.comae933ce2012-08-23 18:19:56 +000028
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 static bool PrefSizeQ(const SkEvent&);
30 static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
reed@android.comf2b98d62010-12-20 18:26:13 +000031
32 static bool FastTextQ(const SkEvent&);
33
reed@android.com44177402009-11-23 21:07:51 +000034 static SkMSec GetAnimTime();
reed@android.comf2b98d62010-12-20 18:26:13 +000035 static SkMSec GetAnimTimeDelta();
36 static SkScalar GetAnimSecondsDelta();
reed@android.com44177402009-11-23 21:07:51 +000037 static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
bsalomon@google.com85003222012-03-28 14:44:37 +000038 // gives a sinusoidal value between 0 and amplitude
39 static SkScalar GetAnimSinScalar(SkScalar amplitude,
40 SkScalar periodInSec,
41 SkScalar phaseInSec = 0);
reed@google.com3cec4d72011-07-06 13:59:47 +000042
43 static GrContext* GetGr();
reed@android.com8a1c16f2008-12-17 15:59:43 +000044};
45
46//////////////////////////////////////////////////////////////////////////////
47
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000048// interface that constructs SkViews
49class SkViewFactory : public SkRefCnt {
rmistry@google.comae933ce2012-08-23 18:19:56 +000050public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000051 virtual SkView* operator() () const = 0;
52};
53
54typedef SkView* (*SkViewCreateFunc)();
55
56// wraps SkViewCreateFunc in SkViewFactory interface
57class SkFuncViewFactory : public SkViewFactory {
58public:
59 SkFuncViewFactory(SkViewCreateFunc func);
60 virtual SkView* operator() () const SK_OVERRIDE;
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000062private:
63 SkViewCreateFunc fCreateFunc;
64};
65
66namespace skiagm {
67class GM;
68}
69
70// factory function that creates a skiagm::GM
71typedef skiagm::GM* (*GMFactoryFunc)(void*);
72
rmistry@google.comae933ce2012-08-23 18:19:56 +000073// Takes a GM factory function and implements the SkViewFactory interface
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000074// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
75// the SampleView interface to skiagm::GM.
76class SkGMSampleViewFactory : public SkViewFactory {
77public:
78 SkGMSampleViewFactory(GMFactoryFunc func);
79 virtual SkView* operator() () const SK_OVERRIDE;
80private:
81 GMFactoryFunc fFunc;
82};
83
84class SkViewRegister : public SkRefCnt {
85public:
86 explicit SkViewRegister(SkViewFactory*);
87 explicit SkViewRegister(SkViewCreateFunc);
88 explicit SkViewRegister(GMFactoryFunc);
89
90 ~SkViewRegister() {
91 fFact->unref();
92 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 static const SkViewRegister* Head() { return gHead; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 SkViewRegister* next() const { return fChain; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000097 const SkViewFactory* factory() const { return fFact; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000098
reed@android.com8a1c16f2008-12-17 15:59:43 +000099private:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000100 SkViewFactory* fFact;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 SkViewRegister* fChain;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 static SkViewRegister* gHead;
104};
105
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000106///////////////////////////////////////////////////////////////////////////////
107
108class SampleView : public SkView {
109public:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000110 SampleView()
111 : fPipeState(SkOSMenu::kOffState)
112 , fBGColor(SK_ColorWHITE)
113 , fRepeatCount(1)
114 , fDebugHitTest(false) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000115 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000116
reed@google.comf2183392011-04-22 14:10:48 +0000117 void setBGColor(SkColor color) { fBGColor = color; }
118
reed@google.coma6ff4dc2011-05-12 22:08:24 +0000119 static bool IsSampleView(SkView*);
reed@google.comf2183392011-04-22 14:10:48 +0000120 static bool SetRepeatDraw(SkView*, int count);
scroggo@google.comb073d922012-06-08 15:35:03 +0000121 static bool SetUsePipe(SkView*, SkOSMenu::TriState);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000123 /**
124 * Call this to request menu items from a SampleView.
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125 * Subclassing notes: A subclass of SampleView can overwrite this method
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000126 * to add new items of various types to the menu and change its title.
127 * The events attached to any new menu items must be handled in its onEvent
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128 * method. See SkOSMenu.h for helper functions.
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000129 */
130 virtual void requestMenu(SkOSMenu* menu) {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000131
132protected:
133 virtual void onDrawBackground(SkCanvas*);
134 virtual void onDrawContent(SkCanvas*) = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000135
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000136 // overrides
137 virtual bool onEvent(const SkEvent& evt);
138 virtual bool onQuery(SkEvent* evt);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000139 virtual void draw(SkCanvas*);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000140 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;
reed@google.com0faac1e2011-05-11 05:58:58 +0000147
reed@google.com4d5c26d2013-01-08 16:17:50 +0000148 bool fDebugHitTest;
149 SkIPoint fDebugHitTestLoc;
150
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000151 typedef SkView INHERITED;
152};
153
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154#endif