blob: 26a8207ddcb6d29c9c9ba96550f7af806dcc130b [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@android.com8a1c16f2008-12-17 15:59:43 +000042};
43
44//////////////////////////////////////////////////////////////////////////////
45
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000046// interface that constructs SkViews
47class SkViewFactory : public SkRefCnt {
rmistry@google.comae933ce2012-08-23 18:19:56 +000048public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000049 virtual SkView* operator() () const = 0;
50};
51
52typedef SkView* (*SkViewCreateFunc)();
53
54// wraps SkViewCreateFunc in SkViewFactory interface
55class SkFuncViewFactory : public SkViewFactory {
56public:
57 SkFuncViewFactory(SkViewCreateFunc func);
58 virtual SkView* operator() () const SK_OVERRIDE;
rmistry@google.comae933ce2012-08-23 18:19:56 +000059
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000060private:
61 SkViewCreateFunc fCreateFunc;
62};
63
64namespace skiagm {
65class GM;
66}
67
68// factory function that creates a skiagm::GM
69typedef skiagm::GM* (*GMFactoryFunc)(void*);
70
rmistry@google.comae933ce2012-08-23 18:19:56 +000071// Takes a GM factory function and implements the SkViewFactory interface
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000072// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
73// the SampleView interface to skiagm::GM.
74class SkGMSampleViewFactory : public SkViewFactory {
75public:
76 SkGMSampleViewFactory(GMFactoryFunc func);
77 virtual SkView* operator() () const SK_OVERRIDE;
78private:
79 GMFactoryFunc fFunc;
80};
81
82class SkViewRegister : public SkRefCnt {
83public:
84 explicit SkViewRegister(SkViewFactory*);
85 explicit SkViewRegister(SkViewCreateFunc);
86 explicit SkViewRegister(GMFactoryFunc);
87
88 ~SkViewRegister() {
89 fFact->unref();
90 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 static const SkViewRegister* Head() { return gHead; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 SkViewRegister* next() const { return fChain; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000095 const SkViewFactory* factory() const { return fFact; }
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097private:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000098 SkViewFactory* fFact;
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 SkViewRegister* fChain;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 static SkViewRegister* gHead;
102};
103
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000104///////////////////////////////////////////////////////////////////////////////
105
106class SampleView : public SkView {
107public:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000108 SampleView()
109 : fPipeState(SkOSMenu::kOffState)
110 , fBGColor(SK_ColorWHITE)
111 , fRepeatCount(1)
reed868074b2014-06-03 10:53:59 -0700112 {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000113
reed@google.comf2183392011-04-22 14:10:48 +0000114 void setBGColor(SkColor color) { fBGColor = color; }
115
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;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000135 // overrides
136 virtual bool onEvent(const SkEvent& evt);
137 virtual bool onQuery(SkEvent* evt);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000138 virtual void draw(SkCanvas*);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000139 virtual void onDraw(SkCanvas*);
140
scroggo@google.comb073d922012-06-08 15:35:03 +0000141 SkOSMenu::TriState fPipeState;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000142 SkColor fBGColor;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000144private:
145 int fRepeatCount;
reed@google.com0faac1e2011-05-11 05:58:58 +0000146
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000147 typedef SkView INHERITED;
148};
149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150#endif