blob: 197e0f1d5a0122ae906537cb3d8df4f1c046630a [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"
yangsu@google.com921091f2011-08-02 13:39:12 +000017class SkOSMenu;
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);
reed@android.com8a1c16f2008-12-17 15:59:43 +000028
29 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);
reed@google.com3cec4d72011-07-06 13:59:47 +000038
39 static GrContext* GetGr();
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 {
reed@android.com8a1c16f2008-12-17 15:59:43 +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);
56 virtual SkView* operator() () const SK_OVERRIDE;
57
58private:
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
69// Takes a GM factory function and implements the SkViewFactory interface
70// 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);
75 virtual SkView* operator() () const SK_OVERRIDE;
76private:
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 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
90 static const SkViewRegister* Head() { return gHead; }
91
92 SkViewRegister* next() const { return fChain; }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000093 const SkViewFactory* factory() const { return fFact; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
95private:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000096 SkViewFactory* fFact;
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 SkViewRegister* fChain;
98
99 static SkViewRegister* gHead;
100};
101
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000102///////////////////////////////////////////////////////////////////////////////
103
104class SampleView : public SkView {
105public:
epoger@google.com17b78942011-08-26 14:40:38 +0000106 SampleView() : fBGColor(SK_ColorWHITE), fRepeatCount(1) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000107 fUsePipe = false;
108 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000109
reed@google.comf2183392011-04-22 14:10:48 +0000110 void setBGColor(SkColor color) { fBGColor = color; }
111
reed@google.coma6ff4dc2011-05-12 22:08:24 +0000112 static bool IsSampleView(SkView*);
reed@google.comf2183392011-04-22 14:10:48 +0000113 static bool SetRepeatDraw(SkView*, int count);
reed@google.com0faac1e2011-05-11 05:58:58 +0000114 static bool SetUsePipe(SkView*, bool);
yangsu@google.com921091f2011-08-02 13:39:12 +0000115
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000116 /**
117 * Call this to request menu items from a SampleView.
118 * Subclassing notes: A subclass of SampleView can overwrite this method
119 * to add new items of various types to the menu and change its title.
120 * The events attached to any new menu items must be handled in its onEvent
121 * method. See SkOSMenu.h for helper functions.
122 */
123 virtual void requestMenu(SkOSMenu* menu) {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000124
125protected:
126 virtual void onDrawBackground(SkCanvas*);
127 virtual void onDrawContent(SkCanvas*) = 0;
yangsu@google.com921091f2011-08-02 13:39:12 +0000128
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000129 // overrides
130 virtual bool onEvent(const SkEvent& evt);
131 virtual bool onQuery(SkEvent* evt);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000132 virtual void draw(SkCanvas*);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000133 virtual void onDraw(SkCanvas*);
134
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000135 bool fUsePipe;
136 SkColor fBGColor;
137
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000138private:
139 int fRepeatCount;
reed@google.com0faac1e2011-05-11 05:58:58 +0000140
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000141 typedef SkView INHERITED;
142};
143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144#endif
145