• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
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  */
8 
9 
10 #ifndef SampleCode_DEFINED
11 #define SampleCode_DEFINED
12 
13 #include "SkColor.h"
14 #include "SkEvent.h"
15 #include "SkKey.h"
16 #include "SkView.h"
17 class SkOSMenu;
18 class GrContext;
19 
20 class SampleCode {
21 public:
22     static bool KeyQ(const SkEvent&, SkKey* outKey);
23     static bool CharQ(const SkEvent&, SkUnichar* outUni);
24 
25     static bool TitleQ(const SkEvent&);
26     static void TitleR(SkEvent*, const char title[]);
27     static bool RequestTitle(SkView* view, SkString* title);
28 
29     static bool PrefSizeQ(const SkEvent&);
30     static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
31 
32     static bool FastTextQ(const SkEvent&);
33 
34     static SkMSec GetAnimTime();
35     static SkMSec GetAnimTimeDelta();
36     static SkScalar GetAnimSecondsDelta();
37     static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
38 
39     static GrContext* GetGr();
40 };
41 
42 //////////////////////////////////////////////////////////////////////////////
43 
44 // interface that constructs SkViews
45 class SkViewFactory : public SkRefCnt {
46 public:
47     virtual SkView* operator() () const = 0;
48 };
49 
50 typedef SkView* (*SkViewCreateFunc)();
51 
52 // wraps SkViewCreateFunc in SkViewFactory interface
53 class SkFuncViewFactory : public SkViewFactory {
54 public:
55     SkFuncViewFactory(SkViewCreateFunc func);
56     virtual SkView* operator() () const SK_OVERRIDE;
57 
58 private:
59     SkViewCreateFunc fCreateFunc;
60 };
61 
62 namespace skiagm {
63 class GM;
64 }
65 
66 // factory function that creates a skiagm::GM
67 typedef 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.
72 class SkGMSampleViewFactory : public SkViewFactory {
73 public:
74     SkGMSampleViewFactory(GMFactoryFunc func);
75     virtual SkView* operator() () const SK_OVERRIDE;
76 private:
77     GMFactoryFunc fFunc;
78 };
79 
80 class SkViewRegister : public SkRefCnt {
81 public:
82     explicit SkViewRegister(SkViewFactory*);
83     explicit SkViewRegister(SkViewCreateFunc);
84     explicit SkViewRegister(GMFactoryFunc);
85 
~SkViewRegister()86     ~SkViewRegister() {
87         fFact->unref();
88     }
89 
Head()90     static const SkViewRegister* Head() { return gHead; }
91 
next()92     SkViewRegister* next() const { return fChain; }
factory()93     const SkViewFactory*   factory() const { return fFact; }
94 
95 private:
96     SkViewFactory*  fFact;
97     SkViewRegister* fChain;
98 
99     static SkViewRegister* gHead;
100 };
101 
102 ///////////////////////////////////////////////////////////////////////////////
103 
104 class SampleView : public SkView {
105 public:
SampleView()106     SampleView() : fBGColor(SK_ColorWHITE), fRepeatCount(1) {
107         fUsePipe = false;
108     }
109 
setBGColor(SkColor color)110     void setBGColor(SkColor color) { fBGColor = color; }
111 
112     static bool IsSampleView(SkView*);
113     static bool SetRepeatDraw(SkView*, int count);
114     static bool SetUsePipe(SkView*, bool);
115 
116     /**
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      */
requestMenu(SkOSMenu * menu)123     virtual void requestMenu(SkOSMenu* menu) {}
124 
125 protected:
126     virtual void onDrawBackground(SkCanvas*);
127     virtual void onDrawContent(SkCanvas*) = 0;
128 
129     // overrides
130     virtual bool onEvent(const SkEvent& evt);
131     virtual bool onQuery(SkEvent* evt);
132     virtual void draw(SkCanvas*);
133     virtual void onDraw(SkCanvas*);
134 
135     bool fUsePipe;
136     SkColor fBGColor;
137 
138 private:
139     int fRepeatCount;
140 
141     typedef SkView INHERITED;
142 };
143 
144 #endif
145 
146